\PhugLexer

Performs lexical analysis and provides a token generator.

Tokens are defined as single units of code (e.g. tag, class, id, attributeStart, attribute, attributeEnd)

These will run through the parser and be converted to an AST

The lexer works sequentially, lex() will return a generator and you can read that generator in any manner you like. The generator will produce valid tokens until the end of the passed input.

Usage example:

use Phug\Lexer;

$lexer = new Lexer();

foreach ($lexer->lex($pugInput) as $token)
     var_dump($token);

Summary

Methods
Properties
Constants
hasModule()
getModule()
getModules()
getStaticModules()
addModule()
addModules()
removeModule()
getModuleBaseClassName()
hasState()
getState()
__construct()
getPreviousToken()
updateOptions()
getScanners()
prependScanner()
addScanner()
lex()
getLastToken()
dump()
No public properties found
INDENT_SPACE
INDENT_TAB
DEFAULT_TAB_WIDTH
No protected methods found
No protected properties found
N/A
getTokenName()
dumpAttributeToken()
dumpTextToken()
dumpExpressionToken()
getTokenSymbol()
dumpToken()
setLastToken()
getRegExpOption()
proceedTokenEvent()
handleToken()
handleTokens()
filterScanner()
$modules
$state
$lastToken
$previousToken
N/A

Constants

INDENT_SPACE

INDENT_SPACE

INDENT_TAB

INDENT_TAB

DEFAULT_TAB_WIDTH

DEFAULT_TAB_WIDTH

Properties

$modules

$modules : \Phug\Util\Partial\array<ModuleInterface>

Type

\Phug\Util\Partial\array

$state

$state : \Phug\Lexer\State

The state of the current lexing process.

Type

\Phug\Lexer\State

Methods

hasModule()

hasModule(string|\Phug\Util\ModuleInterface  $module) : boolean

Parameters

string|\Phug\Util\ModuleInterface $module

Returns

boolean

getModules()

getModules() : \Phug\Util\Partial\array<ModuleInterface>

Returns

\Phug\Util\Partial\array

getStaticModules()

getStaticModules() : \Phug\Util\Partial\array<string>

Returns

\Phug\Util\Partial\array

addModule()

addModule(string|\Phug\Util\ModuleInterface  $module) : $this

Parameters

string|\Phug\Util\ModuleInterface $module

Returns

$this

addModules()

addModules(\Phug\Util\Partial\array<string|\Phug\Util\Partial\ModuleInterface>  $modules) : $this

Parameters

\Phug\Util\Partial\array $modules

Returns

$this

removeModule()

removeModule(string|\Phug\Util\ModuleInterface  $module) : $this

Parameters

string|\Phug\Util\ModuleInterface $module

Returns

$this

getModuleBaseClassName()

getModuleBaseClassName() 

hasState()

hasState() : boolean

Returns true if a lexing process is active and a state exists, false if not.

Returns

boolean

getState()

getState() : \Phug\Lexer\State

Returns the state object of the current lexing process.

Returns

\Phug\Lexer\State

__construct()

__construct(array|null  $options = null) 

Creates a new lexer instance.

The options should be an associative array

Valid options are:

lexer_state_class_name: The class of the lexer state to use level: The internal indentation level to start on indent_style: The indentation character (auto-detected) indentwidth: How often to repeat indentStyle (auto-detected) encoding: The encoding when working with mb*-functions (auto-detected) scanners: An array of scans that will be performed

Add a new scan to 'scans' to extend the lexer. Notice that the parser needs to be able to handle newly introduced tokens provided by scanners.

Parameters

array|null $options

the options passed to the lexer instance

Throws

\Exception

updateOptions()

updateOptions() 

Synchronize the lexer to the new options values.

getScanners()

getScanners() : array<mixed,\Phug\Lexer\ScannerInterface>

Returns the current scanners registered for the lexing process.

Returns

array<mixed,\Phug\Lexer\ScannerInterface>

prependScanner()

prependScanner(string  $name, \Phug\Lexer\ScannerInterface|string  $scanner) : $this

Adds a new scanner class to use in the lexing process at the top of scanning order.

The scanner class needs to extend Phug\Lexer\ScannerInterface. It can be the class name itself or an instance of it.

Parameters

string $name
\Phug\Lexer\ScannerInterface|string $scanner

Returns

$this

addScanner()

addScanner(string  $name, \Phug\Lexer\ScannerInterface|string  $scanner) : $this

Adds a new scanner class to use in the lexing process.

The scanner class needs to extend Phug\Lexer\ScannerInterface. It can be the class name itself or an instance of it.

Parameters

string $name
\Phug\Lexer\ScannerInterface|string $scanner

Returns

$this

lex()

lex(string  $input, null  $path = null) : \Phug\iterable

Returns a generator that will lex the passed input sequentially.

If you don't move the generator, the lexer does nothing. Only as soon as you iterate the generator or call next()/current() on it the lexer will start its work and spit out tokens sequentially. This approach requires less memory during the lexing process.

The returned tokens are required to be Phug\Lexer\TokenInterface instances.

Parameters

string $input

the pug-string to lex into tokens.

null $path

Returns

\Phug\iterable —

a generator that can be iterated sequentially

dump()

dump(  $input) 

Parameters

$input

getTokenName()

getTokenName(  $token) 

Parameters

$token

getRegExpOption()

getRegExpOption(  $name) 

Parameters

$name

proceedTokenEvent()

proceedTokenEvent(  $token) 

Parameters

$token

handleToken()

handleToken(  $token) 

Parameters

$token

handleTokens()

handleTokens(\Phug\iterable  $tokens) : \Phug\iterable

Parameters

\Phug\iterable $tokens

Returns

\Phug\iterable

filterScanner()

filterScanner(  $scanner) 

Parameters

$scanner