Skip to content

Instantly share code, notes, and snippets.

@jakubkulhan
Created August 20, 2010 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakubkulhan/540892 to your computer and use it in GitHub Desktop.
Save jakubkulhan/540892 to your computer and use it in GitHub Desktop.
-namespace Nette\Templates
-name LatteParser
-init {
$self = (object) array(
"latte_pair" => array(
"if" => TRUE,
"for" => TRUE,
"foreach" => TRUE,
"while" => TRUE,
"block" => TRUE,
),
);
}
latte
= node+ !.
node
= text_node
/ html_node
/ latte_comment_node
/ latte_node
text_node
= (!("<" ![!?] / "{" [a-z$!/*]) .)+
html_node
= "<" __ tag:[a-zA-Z0-9-]+
attrs:( _ attr:[a-zA-Z0-9:-]+ "=" '"' value:( latte_node
/ latte_comment_node
/ (!'"' !("{" [a-z$!/*]) .)+
)* '"'
-> array($attr, $value)
)*
__
children:( "/>" -> NULL
/ ">" nodes:node* "</" endtag:[a-zA-Z0-9-]+ ">" ?-> $endtag === $tag
-> $nodes
)
-> array("html", $tag, $attrs, $children)
latte_comment_node
= "{*" content:(!"*}" .)* "*}"
-> array("comment", $content)
latte_node
= "{" bang:"!"? "$" var:([a-zA-Z_] [a-zA-Z0-9_]*) filters:(!"}" .)* "}"
-> array("echo", $var, $bang, $filters)
/ "{" name:[a-zA-Z]+ _ content:(!"}" .)* "}"
children:( ?-> !isset($self->latte_pair[$name])
-> NULL
/ ?-> isset($self->latte_pair[$name])
nodes:node*
"{/" endname:[a-zA-Z]+ "}" ?-> $endname === $name
-> $nodes
)
-> array("latte", $name, $content, $children)
_
= [ \t\r\n]+
__
= [ \t\r\n]*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment