diff options
author | kpdecker <kpdecker@gmail.com> | 2014-08-23 16:29:22 -0500 |
---|---|---|
committer | kpdecker <kpdecker@gmail.com> | 2014-08-23 16:29:22 -0500 |
commit | 529e2b67960dba2e41122fc4d56b5c2af5ada9a5 (patch) | |
tree | 9b4af5981298bccc8e90d57b66773da7b7a1543f /src | |
parent | 3531e041174509c6c3c69417b1714dcda6bccb3e (diff) | |
parent | eee2c4d4f29e233280907bc89a32556de66fe783 (diff) | |
download | handlebars.js-529e2b67960dba2e41122fc4d56b5c2af5ada9a5.zip handlebars.js-529e2b67960dba2e41122fc4d56b5c2af5ada9a5.tar.gz handlebars.js-529e2b67960dba2e41122fc4d56b5c2af5ada9a5.tar.bz2 |
Merge branch 'refactor-parser' of github.com:mmun/handlebars.js into mmun-refactor-parser
Conflicts:
lib/handlebars/compiler/ast.js
spec/ast.js
src/handlebars.yy
Diffstat (limited to 'src')
-rw-r--r-- | src/handlebars.l | 2 | ||||
-rw-r--r-- | src/handlebars.yy | 64 |
2 files changed, 27 insertions, 39 deletions
diff --git a/src/handlebars.l b/src/handlebars.l index 57ba12a..f775cc4 100644 --- a/src/handlebars.l +++ b/src/handlebars.l @@ -70,6 +70,8 @@ ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/{LOOKAHEAD} <mu>"{{"{LEFT_STRIP}?">" return 'OPEN_PARTIAL'; <mu>"{{"{LEFT_STRIP}?"#" return 'OPEN_BLOCK'; <mu>"{{"{LEFT_STRIP}?"/" return 'OPEN_ENDBLOCK'; +<mu>"{{"{LEFT_STRIP}?"^"\s*{RIGHT_STRIP}?"}}" this.popState(); return 'INVERSE'; +<mu>"{{"{LEFT_STRIP}?\s*"else"\s*{RIGHT_STRIP}?"}}" this.popState(); return 'INVERSE'; <mu>"{{"{LEFT_STRIP}?"^" return 'OPEN_INVERSE'; <mu>"{{"{LEFT_STRIP}?\s*"else" return 'OPEN_INVERSE'; <mu>"{{"{LEFT_STRIP}?"{" return 'OPEN_UNESCAPED'; diff --git a/src/handlebars.yy b/src/handlebars.yy index fa69f73..a8d288f 100644 --- a/src/handlebars.yy +++ b/src/handlebars.yy @@ -2,78 +2,64 @@ %ebnf -%{ - -function stripFlags(open, close) { - return { - left: open.charAt(2) === '~', - right: close.charAt(0) === '~' || close.charAt(1) === '~' - }; -} - -%} - %% root - : statements EOF { return new yy.ProgramNode(true, $1, @$); } - | EOF { return new yy.ProgramNode(true, [], @$); } + : program EOF { yy.prepareProgram($1.statements, true); return $1; } ; program - : simpleInverse statements -> new yy.ProgramNode(false, [], $1, $2, @$) - | statements simpleInverse statements -> new yy.ProgramNode(false, $1, $2, $3, @$) - | statements simpleInverse -> new yy.ProgramNode(false, $1, $2, [], @$) - | statements -> new yy.ProgramNode(false, $1, @$) - | simpleInverse -> new yy.ProgramNode(false, [], @$) - | "" -> new yy.ProgramNode(false, [], @$) - ; - -statements - : statement -> [$1] - | statements statement { $1.push($2); $$ = $1; } + : statement* -> new yy.ProgramNode(yy.prepareProgram($1), {}, @$) ; statement - : openRawBlock CONTENT END_RAW_BLOCK -> new yy.RawBlockNode($1, $2, $3, @$) - | openInverse program closeBlock -> new yy.BlockNode($1, $2.inverse, $2, $3, @$) - | openBlock program closeBlock -> new yy.BlockNode($1, $2, $2.inverse, $3, @$) - | mustache -> $1 + : mustache -> $1 + | block -> $1 + | rawBlock -> $1 | partial -> $1 | CONTENT -> new yy.ContentNode($1, @$) | COMMENT -> new yy.CommentNode($1, @$) ; +rawBlock + : openRawBlock CONTENT END_RAW_BLOCK -> new yy.RawBlockNode($1, $2, $3, @$) + ; + openRawBlock : OPEN_RAW_BLOCK sexpr CLOSE_RAW_BLOCK -> new yy.MustacheNode($2, null, '', '', @$) ; +block + : openBlock program inverseAndProgram? closeBlock -> yy.prepareBlock($1, $2, $3, $4, false, @$) + | openInverse program inverseAndProgram? closeBlock -> yy.prepareBlock($1, $2, $3, $4, true, @$) + ; + openBlock - : OPEN_BLOCK sexpr CLOSE -> new yy.MustacheNode($2, null, $1, stripFlags($1, $3), @$) + : OPEN_BLOCK sexpr CLOSE -> new yy.MustacheNode($2, null, $1, yy.stripFlags($1, $3), @$) ; openInverse - : OPEN_INVERSE sexpr CLOSE -> new yy.MustacheNode($2, null, $1, stripFlags($1, $3), @$) + : OPEN_INVERSE sexpr CLOSE -> new yy.MustacheNode($2, null, $1, yy.stripFlags($1, $3), @$) + ; + +inverseAndProgram + : INVERSE program -> { strip: yy.stripFlags($1, $1), program: $2 } ; closeBlock - : OPEN_ENDBLOCK path CLOSE -> {path: $2, strip: stripFlags($1, $3)} + : OPEN_ENDBLOCK path CLOSE -> {path: $2, strip: yy.stripFlags($1, $3)} ; mustache // Parsing out the '&' escape token at AST level saves ~500 bytes after min due to the removal of one parser node. // This also allows for handler unification as all mustache node instances can utilize the same handler - : OPEN sexpr CLOSE -> new yy.MustacheNode($2, null, $1, stripFlags($1, $3), @$) - | OPEN_UNESCAPED sexpr CLOSE_UNESCAPED -> new yy.MustacheNode($2, null, $1, stripFlags($1, $3), @$) + : OPEN sexpr CLOSE -> new yy.MustacheNode($2, null, $1, yy.stripFlags($1, $3), @$) + | OPEN_UNESCAPED sexpr CLOSE_UNESCAPED -> new yy.MustacheNode($2, null, $1, yy.stripFlags($1, $3), @$) ; partial - : OPEN_PARTIAL partialName param hash? CLOSE -> new yy.PartialNode($2, $3, $4, stripFlags($1, $5), @$) - | OPEN_PARTIAL partialName hash? CLOSE -> new yy.PartialNode($2, undefined, $3, stripFlags($1, $4), @$) - ; - -simpleInverse - : OPEN_INVERSE CLOSE -> stripFlags($1, $2) + : OPEN_PARTIAL partialName param hash? CLOSE -> new yy.PartialNode($2, $3, $4, yy.stripFlags($1, $5), @$) + | OPEN_PARTIAL partialName hash? CLOSE -> new yy.PartialNode($2, undefined, $3, yy.stripFlags($1, $4), @$) ; sexpr |