PHP Class Tale\Jade\Parser

This class takes generated tokens from the Lexer sequentially and produces an Abstract Syntax Tree (AST) out of it The AST is an object-tree containing Node-instances with parent/child relations This AST is passed to the compiler to generate PHTML out of it Usage example: use Tale\Jade\Parser; $parser = new Parser(); echo $parser->parse($jadeInput); Prints a human-readable dump of the parsed nodes
Inheritance: use trait Tale\ConfigurableTrait
Mostra file Open project: talesoft/tale-jade Class Usage Examples

Public Methods

Method Description
__construct ( array $options = null, Lexer $lexer = null ) Creates a new parser instance.
getLexer ( ) : Lexer Returns the currently used Lexer instance.
parse ( string $input ) : Node Parses the provided input-string to an AST.

Protected Methods

Method Description
createElement ( array $token = null ) : Node Creates an element-node with the properties it should have consistently.
createNode ( string $type, array $token = null ) : Node Creates a new node instance with the given type.
expect ( array $types ) : Node | null Returns the token of the given type if it is in the token queue.
expectEnd ( array $relatedToken = null ) Throws an exception if the next token is not a newLine token.
expectNext ( array $types ) : Node | null Moves the generator on by one and does ->expect().
getToken ( ) : array Returns the current token in the lexer generator.
handleAssignment ( array $token ) Parses an -token into element assignments.
handleAttribute ( array $token ) Parses an -token into an attribute-node.
handleAttributeEnd ( array $token ) Handles an -token.
handleAttributeStart ( array $token ) Handles an -token.
handleBlock ( array $token ) Handles a -token and parses it into a block-node.
handleCase ( array $token ) Handles a -token and parses it into a case-node.
handleClass ( array $token ) Handles a -token and parses it into an element.
handleCode ( array $token ) Handles an -token into an code-node.
handleComment ( array $token ) Handles a -token and parses it into a comment-node.
handleConditional ( array $token ) Handles a -token and parses it into a conditional-node.
handleDo ( array $token ) Handles a -token and parses it into a do-node.
handleDoctype ( array $token ) Handles a -token and parses it into a doctype-node.
handleEach ( array $token ) Handles an -token and parses it into an each-node.
handleExpansion ( array $token ) Handles an -token.
handleExpression ( array $token ) Handles an -token into an expression-node.
handleFilter ( array $token ) Handles a -token and parses it into a filter-node.
handleFor ( array $token ) Handles a -token and parses it into a for-node.
handleId ( array $token ) Handles an -token and parses it into an element.
handleImport ( array $token ) Handles an -token and parses it into an import-node.
handleIndent ( array $token = null ) Handles an -token.
handleMixin ( array $token ) Handles a -token and parses it into a mixin-node.
handleMixinCall ( array $token ) Handles a -token and parses it into a mixinCall-node.
handleNewLine ( ) Handles a -token.
handleOutdent ( ) Handles an -token.
handleTag ( array $token ) Handles a -token and parses it into a tag-node.
handleText ( array $token ) Handles a -token and parses it into a text-node.
handleToken ( array $token = null ) Handles any kind of token returned by the lexer dynamically.
handleVariable ( array $token ) Handles a -token and parses it into a variable assignment.
handleWhen ( array $token ) Handles a -token and parses it into a when-node.
handleWhile ( array $token ) Handles a -token and parses it into a while-node.
hasTokens ( ) : boolean Returns true, if there are still tokens left to be generated.
lookUp ( array $types ) : Generator Yields tokens as long as the given types match.
lookUpNext ( array $types ) : Generator Moves on the token generator by one and does ->lookUp().
nextToken ( ) Moves the generator on by one token.
throwException ( string $message, array $relatedToken = null ) Throws a Parser-Exception.

Method Details

__construct() public method

The parser will run the provided input through the lexer and generate an AST out of it. The AST will be an object-tree consisting of \Tale\Jade\Parser\Node instances You can take the AST and either compile it with the Compiler or handle it yourself Possible options are: lexerOptions: The options for the lexer
public __construct ( array $options = null, Lexer $lexer = null )
$options array the options array
$lexer Lexer an existing lexer instance (lexer-option will be ignored)

createElement() protected method

This will create the following properties on the Node instance:
protected createElement ( array $token = null ) : Node
$token array the token to relate this element to
return Tale\Jade\Parser\Node the newly created element-node

createNode() protected method

If a token is given, the location in the code of that token is also passed to the Node instance If no token is passed, a dummy-token with the current lexer's offset and line is created Notice that nodes are expando-objects, you can add properties on-the-fly and retrieve them as an array later
protected createNode ( string $type, array $token = null ) : Node
$type string the type the node should have
$token array the token to relate this node to
return Tale\Jade\Parser\Node The newly created node

expect() protected method

If the given token in the queue is not of the given type, this method returns null
protected expect ( array $types ) : Node | null
$types array the types that are expected
return Tale\Jade\Parser\Node | null

expectEnd() protected method

This states that "a line of instructions should end here" Notice that if the next token is _not_ a newLine, it gets handled through handleToken automatically
protected expectEnd ( array $relatedToken = null )
$relatedToken array the token to relate the exception to

expectNext() protected method

Moves the generator on by one and does ->expect().
protected expectNext ( array $types ) : Node | null
$types array the types that are expected
return Tale\Jade\Parser\Node | null

getLexer() public method

Returns the currently used Lexer instance.
public getLexer ( ) : Lexer
return Lexer

getToken() protected method

Returns the current token in the lexer generator.
protected getToken ( ) : array
return array the token array (always _one_ token, as an array)

handleAssignment() protected method

If no there is no $_current element, a new one is created Assignments are possible on elements and mixinCalls only After an assignment, an attribute block is required
protected handleAssignment ( array $token )
$token array the -token

handleAttribute() protected method

That node is appended to the $_current element. If no $_current element exists, a new one is created Attributes in elements and mixins always need a valid name
protected handleAttribute ( array $token )
$token array the -token

handleAttributeEnd() protected method

It does nothing (right now?)
protected handleAttributeEnd ( array $token )
$token array the -token

handleAttributeStart() protected method

Attributes can only start on elements, assignments, imports, mixins and mixinCalls After that, all following -tokens are handled. After that, an -token is expected (When I think about it, the Lexer kind of does that already)
protected handleAttributeStart ( array $token )
$token array the -token

handleBlock() protected method

Blocks outside a mixin always need a name! (That's what $_inMixin is for)
protected handleBlock ( array $token )
$token array the -token

handleCase() protected method

Handles a -token and parses it into a case-node.
protected handleCase ( array $token )
$token array the -token

handleClass() protected method

If there's no $_current-node, a new one is created It will be converted to a regular -node on the element (There is no class-node) Classes can only exist on elements and mixinCalls
protected handleClass ( array $token )
$token array the -token

handleCode() protected method

Handles an -token into an code-node.
protected handleCode ( array $token )
$token array the -token

handleComment() protected method

The comment node is set as the $_current element
protected handleComment ( array $token )
$token array the -token

handleConditional() protected method

Handles a -token and parses it into a conditional-node.
protected handleConditional ( array $token )
$token array the -token

handleDo() protected method

Handles a -token and parses it into a do-node.
protected handleDo ( array $token )
$token array the -token

handleDoctype() protected method

Handles a -token and parses it into a doctype-node.
protected handleDoctype ( array $token )
$token array the -token

handleEach() protected method

Handles an -token and parses it into an each-node.
protected handleEach ( array $token )
$token array the -token

handleExpansion() protected method

If there's no current element, we don't expand anything and throw an exception If there's no space behind the : and the next token is a -token, we don't treat this as an expansion, but rather as a tag-extension (a:b === , a: b === ) This is important for XML and XML-namespaces Notice that, right now, any element that can also land in $_current can be expanded (so almost all elements there are) It just makes no sense for some elements ("extends", "include") $_current is reset after the expansion so that we can collect the expanding element and handle it on a newLine or in an indent
protected handleExpansion ( array $token )
$token array the -token

handleExpression() protected method

If there's a $_current-element, the expression gets appended to the $_current-element. If not, the expression itself becomes the $_current element
protected handleExpression ( array $token )
$token array the -token

handleFilter() protected method

Handles a -token and parses it into a filter-node.
protected handleFilter ( array $token )
$token array the -token

handleFor() protected method

Handles a -token and parses it into a for-node.
protected handleFor ( array $token )
$token array the -token

handleId() protected method

If no $_current element exists, a new one is created IDs can only exist on elements an mixin calls They will get converted to attribute-nodes and appended to the current element
protected handleId ( array $token )
$token array the -token

handleImport() protected method

Notice that "extends" and "include" are basically the same thing. The only difference is that "extends" can only exist at the very beginning of a jade-block Only "include" can have filters, though. This gets checked in the Compiler, not here
protected handleImport ( array $token )
$token array the -token

handleIndent() protected method

The $_level will be increased by 1 for each If there's no $_last element (which is set on a newLine), we do nothing (because there's nothing to indent into) The $_last node is set as the $_currentParent node and acts as a parent-node for further created nodes (They get appended in handleNewLine) import-nodes can't be indented into, because they can't have children (poor imports :'( ) The opposite of this is, obviously, handleOutdent with -tokens
protected handleIndent ( array $token = null )
$token array the -token

handleMixin() protected method

Mixins can't be inside other mixins. We use $_inMixin and $_mixinLevel for that $_mixinLevel gets reset in handleOutdent
protected handleMixin ( array $token )
$token array the -token

handleMixinCall() protected method

Handles a -token and parses it into a mixinCall-node.
protected handleMixinCall ( array $token )
$token array the -token

handleNewLine() protected method

If there's no $_current element, it does nothing If there is one it: 1. Checks if we have an $_expansion. If we do, append it to $_current and reset $_expansion 2. Appends the $_current element to the $_currentParent 3. Set's the $_last element to the $_current element 4. Resets $_current to null
protected handleNewLine ( )

handleOutdent() protected method

Decreases the current $_level by 1 Sets the $_currentParent to the ->parent of $_currentParent (Walking up the tree by 1) If we're in a mixin and we're at or below our mixin-level again, we're not in a mixin anymore
protected handleOutdent ( )

handleTag() protected method

If no $_current element exists, a new one is created A tag can only exist once on an element Only elements can have tags
protected handleTag ( array $token )
$token array the -token

handleText() protected method

If there's a $_current element, we append it to that element, if not, it becomes the $_current element
protected handleText ( array $token )
$token array the -token

handleToken() protected method

The token-type will be translated into a method name e.g. newLine => handleNewLine attribute => handleAttribute tag => handleTag First argument of that method will always be the token array If no token is passed, it will take the current token in the lexer's token generator
protected handleToken ( array $token = null )
$token array a token or the current lexer's generator token

handleVariable() protected method

Handles a -token and parses it into a variable assignment.
protected handleVariable ( array $token )
$token array the -token

handleWhen() protected method

Handles a -token and parses it into a when-node.
protected handleWhen ( array $token )
$token array the -token

handleWhile() protected method

Handles a -token and parses it into a while-node.
protected handleWhile ( array $token )
$token array the -token

hasTokens() protected method

If the lexer-generator still has tokens to generate, this returns true and false, if it doesn't
protected hasTokens ( ) : boolean
return boolean

lookUp() protected method

Yields tokens of the given types until a token is encountered, that is not given in the types-array
protected lookUp ( array $types ) : Generator
$types array the token types that are allowed
return Generator

lookUpNext() protected method

Moves on the token generator by one and does ->lookUp().
protected lookUpNext ( array $types ) : Generator
$types array the types that are allowed
return Generator

nextToken() protected method

(It calls ->next() on the generator, look at the PHP doc)
protected nextToken ( )

parse() public method

The Abstract Syntax Tree (AST) will be an object-tree consisting of \Tale\Jade\Parser\Node instances. You can either let the compiler compile it or compile it yourself The root-node will always be of type 'document', from there on it can contain several kinds of nodes
public parse ( string $input ) : Node
$input string the input jade string that is to be parsed
return Tale\Jade\Parser\Node the root-node of the parsed AST

throwException() protected method

If a related token is passed, it will also append the location in the input of that token
protected throwException ( string $message, array $relatedToken = null )
$message string a meaningful error-message
$relatedToken array the token related to this error