use Tale\Jade\Parser;
$parser = new Parser();
echo $parser->parse($jadeInput);
Prints a human-readable dump of the parsed nodes
Mostra file
Open project: talesoft/tale-jade
Class Usage Examples
Method | Description | |
---|---|---|
__construct ( array $options = null, |
Creates a new parser instance. | |
getLexer ( ) : |
Returns the currently used Lexer instance. | |
parse ( string $input ) : |
Parses the provided input-string to an AST. |
Method | Description | |
---|---|---|
createElement ( array $token = null ) : |
Creates an element-node with the properties it should have consistently. | |
createNode ( string $type, array $token = null ) : |
Creates a new node instance with the given type. | |
expect ( array $types ) : |
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 ) : |
Moves the generator on by one and does ->expect(). | |
getToken ( ) : array | Returns the current token in the lexer generator. | |
handleAssignment ( array $token ) |
Parses an |
|
handleAttribute ( array $token ) |
Parses an |
|
handleAttributeEnd ( array $token ) |
Handles an |
|
handleAttributeStart ( array $token ) |
Handles an |
|
handleBlock ( array $token ) |
Handles a |
|
handleCase ( array $token ) |
Handles a |
|
handleClass ( array $token ) |
Handles a |
|
handleCode ( array $token ) |
Handles an -token into an code-node. |
|
handleComment ( array $token ) |
Handles a |
|
handleConditional ( array $token ) |
Handles a |
|
handleDo ( array $token ) |
Handles a |
|
handleDoctype ( array $token ) |
Handles a |
|
handleEach ( array $token ) |
Handles an |
|
handleExpansion ( array $token ) |
Handles an |
|
handleExpression ( array $token ) |
Handles an |
|
handleFilter ( array $token ) |
Handles a |
|
handleFor ( array $token ) |
Handles a |
|
handleId ( array $token ) |
Handles an |
|
handleImport ( array $token ) |
Handles an |
|
handleIndent ( array $token = null ) |
Handles an |
|
handleMixin ( array $token ) |
Handles a |
|
handleMixinCall ( array $token ) |
Handles a |
|
handleNewLine ( ) |
Handles a |
|
handleOutdent ( ) |
Handles an |
|
handleTag ( array $token ) |
Handles a |
|
handleText ( array $token ) |
Handles a |
|
handleToken ( array $token = null ) | Handles any kind of token returned by the lexer dynamically. | |
handleVariable ( array $token ) |
Handles a |
|
handleWhen ( array $token ) |
Handles a |
|
handleWhile ( array $token ) |
Handles a |
|
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. |
public __construct ( array $options = null, |
||
$options | array | the options array |
$lexer | an existing lexer instance (lexer-option will be ignored) |
protected createElement ( array $token = null ) : |
||
$token | array | the token to relate this element to |
return | the newly created element-node |
protected createNode ( string $type, array $token = null ) : |
||
$type | string | the type the node should have |
$token | array | the token to relate this node to |
return | The newly created node |
protected expectNext ( array $types ) : |
||
$types | array | the types that are expected |
return |
public getLexer ( ) : |
||
return |
protected handleAssignment ( array $token ) | ||
$token | array | the |
protected handleAttribute ( array $token ) | ||
$token | array | the |
protected handleAttributeEnd ( array $token ) | ||
$token | array | the |
protected handleAttributeStart ( array $token ) | ||
$token | array | the |
protected handleBlock ( array $token ) | ||
$token | array | the |
protected handleCase ( array $token ) | ||
$token | array | the |
protected handleClass ( array $token ) | ||
$token | array | the |
-token into an code-node.
protected handleCode ( array $token )
$token
array
the -token
The comment node is set as the $_current element
protected handleComment ( array $token )
$token
array
the -token
Handles a -token and parses it into a conditional-node.
protected handleConditional ( array $token )
$token
array
the -token
Handles a -token and parses it into a do-node.
Handles a -token and parses it into a doctype-node.
protected handleDoctype ( array $token )
$token
array
the -token
Handles an -token and parses it into an each-node.
protected handleEach ( array $token )
$token
array
the -token
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
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
Handles a -token and parses it into a filter-node.
protected handleFilter ( array $token )
$token
array
the -token
Handles a -token and parses it into a for-node.
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
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
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
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
Handles a -token and parses it into a mixinCall-node.
protected handleMixinCall ( array $token )
$token
array
the -token
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 ( )
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 ( )
If no $_current element exists, a new one is created
A tag can only exist once on an element
Only elements can have tags
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
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
Handles a -token and parses it into a variable assignment.
protected handleVariable ( array $token )
$token
array
the -token
Handles a -token and parses it into a when-node.
protected handleWhen ( array $token )
$token
array
the -token
Handles a -token and parses it into a while-node.
protected handleWhile ( array $token )
$token
array
the -token
If the lexer-generator still has tokens to generate,
this returns true and false, if it doesn't
Yields tokens of the given types until
a token is encountered, that is not given
in the types-array
Moves on the token generator by one and does ->lookUp().
protected lookUpNext ( array $types ) : Generator
$types
array
the types that are allowed
return
Generator
(It calls ->next() on the generator, look at the PHP doc)
protected nextToken ( )
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
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