Last active
January 9, 2016 04:52
-
-
Save TimothyGu/a4fcc39c1b1f2fc4ac34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = function (options) { | |
| return { | |
| lex: { | |
| advance: function(lexer) { | |
| return this.php(lexer); | |
| }, | |
| php: function(lexer) { | |
| var tok = lexer.scanEndOfLine(/^@([^\n]+)/, 'php-code'); | |
| if (tok) { | |
| lexer.tokens.push(tok); | |
| return true; | |
| } | |
| } | |
| }, | |
| parse: { | |
| expressionTokens: { | |
| 'php-code': function(parser) { | |
| var tok = parser.expect('php-code'); | |
| var spaceIndex = tok.val.indexOf(' '); | |
| var splitted; | |
| if (spaceIndex === -1) { | |
| splitted = [tok.val, '']; | |
| } else { | |
| splitted = [ | |
| tok.val.slice(0, spaceIndex), | |
| tok.val.slice(spaceIndex).trim() | |
| ]; | |
| } | |
| var value = ''; | |
| switch (splitted[0]) { | |
| case 'if': | |
| case 'for': | |
| case 'elseif': | |
| value = splitted[0] + ' (' + splitted[1] + ')'; | |
| break; | |
| case 'else': | |
| value = splitted[0]; | |
| if (splitted[1]) { | |
| parser.error('PHP:INVALID_EXPRESSION', 'unexpected code', { | |
| line: tok.line, | |
| col: tok.col + splitted[0].length + 2 | |
| }); | |
| } | |
| break; | |
| case '': | |
| value = splitted[1]; | |
| break; | |
| default: | |
| value = splitted.join(' '); | |
| } | |
| var block = parser.peek().type === 'indent' ? parser.block() : null; | |
| var tail = ''; | |
| if (block) { | |
| value += ' {'; | |
| tail = '}'; | |
| } | |
| // we don't have code generator plugins yet, so we have to output | |
| // AST nodes that pug-code-gen can digest | |
| var node = { | |
| type: 'Text', | |
| val: '<?php ' + value + ' ?>', | |
| line: tok.line, | |
| filename: parser.filename, | |
| isHtml: true | |
| }; | |
| if (block) { | |
| block.nodes.unshift(node); | |
| if (tail) { | |
| block.nodes.push({ | |
| type: 'Text', | |
| val: '<?php ' + tail + ' ?>', | |
| line: tok.line, | |
| filename: parser.filename, | |
| isHtml: true | |
| }); | |
| } | |
| return block; | |
| } else { | |
| return node; | |
| } | |
| } | |
| } | |
| } | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment