|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DevTheorem\Handlebars; |
| 4 | + |
| 5 | +use DevTheorem\Handlebars\Phlexer\Phlexer; |
| 6 | +use DevTheorem\Handlebars\Phlexer\Rule; |
| 7 | + |
| 8 | +/** |
| 9 | + * Implements the same lexical tokenization from |
| 10 | + * https://github.com/handlebars-lang/handlebars-parser/blob/master/src/handlebars.l |
| 11 | + * (as of 2025-01-07). |
| 12 | + */ |
| 13 | +final class Lexer extends Phlexer |
| 14 | +{ |
| 15 | + public function __construct() |
| 16 | + { |
| 17 | + $LEFT_STRIP = $RIGHT_STRIP = '~'; |
| 18 | + $LOOKAHEAD = '[=~}\\s\\/.)\\]|]'; |
| 19 | + $LITERAL_LOOKAHEAD = '[~}\\s)\\]]'; |
| 20 | + |
| 21 | + /* |
| 22 | + * ID is the inverse of control characters. |
| 23 | + * Control characters ranges: |
| 24 | + * [\s] Whitespace |
| 25 | + * [!"#%-,\./] !, ", #, %, &, ', (, ), *, +, ,, ., /, Exceptions in range: $, - |
| 26 | + * [;->@] ;, <, =, >, @, Exceptions in range: :, ? |
| 27 | + * [\[-\^`] [, \, ], ^, `, Exceptions in range: _ |
| 28 | + * [\{-~] {, |, }, ~ |
| 29 | + */ |
| 30 | + $CTRL_INVERSE = '[^\\s!"#%-,\\.\\/;->@\\[-\\^`\\{-~]+'; |
| 31 | + $ID = $CTRL_INVERSE . '(?=' . $LOOKAHEAD . ')'; |
| 32 | + |
| 33 | + parent::__construct([ |
| 34 | + new Rule('[^\\x00]*?(?={{)', function () { |
| 35 | + if (str_ends_with($this->yytext, "\\\\")) { |
| 36 | + $this->strip(0, 1); |
| 37 | + $this->pushState('mu'); |
| 38 | + } elseif (str_ends_with($this->yytext, "\\")) { |
| 39 | + $this->strip(0, 1); |
| 40 | + $this->pushState('emu'); |
| 41 | + } else { |
| 42 | + $this->pushState('mu'); |
| 43 | + } |
| 44 | + |
| 45 | + return $this->yytext !== '' ? 'CONTENT' : null; |
| 46 | + }), |
| 47 | + |
| 48 | + new Rule('[^\\x00]+', fn() => 'CONTENT'), |
| 49 | + |
| 50 | + // marks CONTENT up to the next mustache or escaped mustache |
| 51 | + new Rule('<emu>[^\\x00]{2,}?(?={{|\\\\{{|\\\\\\\\{{|\\Z)', function () { |
| 52 | + $this->popState(); |
| 53 | + return 'CONTENT'; |
| 54 | + }), |
| 55 | + |
| 56 | + // nested raw block will create stacked 'raw' condition |
| 57 | + new Rule('<raw>{{{{(?=[^\\/])', function () { |
| 58 | + $this->pushState('raw'); |
| 59 | + return 'CONTENT'; |
| 60 | + }), |
| 61 | + |
| 62 | + new Rule('<raw>{{{{\\/' . $CTRL_INVERSE . '(?=[=}\\s\\/.])}}}}', function () { |
| 63 | + $this->popState(); |
| 64 | + |
| 65 | + if ($this->topState() === 'raw') { |
| 66 | + return 'CONTENT'; |
| 67 | + } else { |
| 68 | + $this->strip(5, 9); |
| 69 | + return 'END_RAW_BLOCK'; |
| 70 | + } |
| 71 | + }), |
| 72 | + new Rule('<raw>[^\\x00]+?(?={{{{)', fn() => 'CONTENT'), |
| 73 | + |
| 74 | + new Rule('<com>[\\s\\S]*?--' . $RIGHT_STRIP . '?}}', function () { |
| 75 | + $this->popState(); |
| 76 | + return 'COMMENT'; |
| 77 | + }), |
| 78 | + |
| 79 | + new Rule('<mu>\\(', fn() => 'OPEN_SEXPR'), |
| 80 | + new Rule('<mu>\\)', fn() => 'CLOSE_SEXPR'), |
| 81 | + |
| 82 | + new Rule('<mu>\\[', function () { |
| 83 | + // Assuming yy.syntax.square === 'string'. OPEN_ARRAY option not handled |
| 84 | + $this->unput($this->yytext); |
| 85 | + // escaped literal |
| 86 | + $this->pushState('escl'); |
| 87 | + return null; |
| 88 | + }), |
| 89 | + new Rule('<mu>]', fn() => 'CLOSE_ARRAY'), |
| 90 | + |
| 91 | + new Rule('<mu>{{{{', fn() => 'OPEN_RAW_BLOCK'), |
| 92 | + new Rule('<mu>}}}}', function () { |
| 93 | + $this->popState(); |
| 94 | + $this->pushState('raw'); |
| 95 | + return 'CLOSE_RAW_BLOCK'; |
| 96 | + }), |
| 97 | + new Rule('<mu>{{' . $LEFT_STRIP . '?>', fn() => 'OPEN_PARTIAL'), |
| 98 | + new Rule('<mu>{{' . $LEFT_STRIP . '?#>', fn() => 'OPEN_PARTIAL_BLOCK'), |
| 99 | + new Rule('<mu>{{' . $LEFT_STRIP . '?#\\*?', fn() => 'OPEN_BLOCK'), |
| 100 | + new Rule('<mu>{{' . $LEFT_STRIP . '?\\/', fn() => 'OPEN_ENDBLOCK'), |
| 101 | + new Rule('<mu>{{' . $LEFT_STRIP . '?\\^\\s*' . $RIGHT_STRIP . '?}}', function () { |
| 102 | + $this->popState(); |
| 103 | + return 'INVERSE'; |
| 104 | + }), |
| 105 | + new Rule('<mu>{{' . $LEFT_STRIP . '?\\s*else\\s*' . $RIGHT_STRIP . '?}}', function () { |
| 106 | + $this->popState(); |
| 107 | + return 'INVERSE'; |
| 108 | + }), |
| 109 | + new Rule('<mu>{{' . $LEFT_STRIP . '?\\^', fn() => 'OPEN_INVERSE'), |
| 110 | + new Rule('<mu>{{' . $LEFT_STRIP . '?\\s*else', fn() => 'OPEN_INVERSE_CHAIN'), |
| 111 | + new Rule('<mu>{{' . $LEFT_STRIP . '?{', fn() => 'OPEN_UNESCAPED'), |
| 112 | + new Rule('<mu>{{' . $LEFT_STRIP . '?&', fn() => 'OPEN'), |
| 113 | + new Rule('<mu>{{' . $LEFT_STRIP . '?!--', function () { |
| 114 | + $this->unput($this->yytext); |
| 115 | + $this->popState(); |
| 116 | + $this->pushState('com'); |
| 117 | + return null; |
| 118 | + }), |
| 119 | + new Rule('<mu>{{' . $LEFT_STRIP . '?![\\s\\S]*?}}', function () { |
| 120 | + $this->popState(); |
| 121 | + return 'COMMENT'; |
| 122 | + }), |
| 123 | + new Rule('<mu>{{' . $LEFT_STRIP . '?\\*?', fn() => 'OPEN'), |
| 124 | + |
| 125 | + new Rule('<mu>=', fn() => 'EQUALS'), |
| 126 | + new Rule('<mu>\\.\\.', fn() => 'ID'), |
| 127 | + new Rule('<mu>\\.(?=' . $LOOKAHEAD . ')', fn() => 'ID'), |
| 128 | + new Rule('<mu>\\.#', fn() => 'PRIVATE_SEP'), |
| 129 | + new Rule('<mu>[\\/.]', fn() => 'SEP'), |
| 130 | + new Rule('<mu>\\s+', fn() => null), // ignore whitespace |
| 131 | + new Rule('<mu>}' . $RIGHT_STRIP . '?}}', function () { |
| 132 | + $this->popState(); |
| 133 | + return 'CLOSE_UNESCAPED'; |
| 134 | + }), |
| 135 | + new Rule('<mu>' . $RIGHT_STRIP . '?}}', function () { |
| 136 | + $this->popState(); |
| 137 | + return 'CLOSE'; |
| 138 | + }), |
| 139 | + // double-quoted string |
| 140 | + new Rule('<mu>"(\\\\["]|[^"])*"', function () { |
| 141 | + $this->strip(1, 2); |
| 142 | + $this->replace('/\\\\"/', '"'); |
| 143 | + return 'STRING'; |
| 144 | + }), |
| 145 | + // single quoted string |
| 146 | + new Rule("<mu>'(\\\\[']|[^'])*'", function () { |
| 147 | + $this->strip(1, 2); |
| 148 | + $this->replace("/\\\\'/", "'"); |
| 149 | + return 'STRING'; |
| 150 | + }), |
| 151 | + new Rule('<mu>@', fn() => 'DATA'), |
| 152 | + new Rule('<mu>true(?=' . $LITERAL_LOOKAHEAD . ')', fn() => 'BOOLEAN'), |
| 153 | + new Rule('<mu>false(?=' . $LITERAL_LOOKAHEAD . ')', fn() => 'BOOLEAN'), |
| 154 | + new Rule('<mu>undefined(?=' . $LITERAL_LOOKAHEAD . ')', fn() => 'UNDEFINED'), |
| 155 | + new Rule('<mu>null(?=' . $LITERAL_LOOKAHEAD . ')', fn() => 'NULL'), |
| 156 | + new Rule('<mu>\\-?[0-9]+(?:\\.[0-9]+)?(?=' . $LITERAL_LOOKAHEAD . ')', fn() => 'NUMBER'), |
| 157 | + new Rule('<mu>as\\s+\\|', fn() => 'OPEN_BLOCK_PARAMS'), |
| 158 | + new Rule('<mu>\\|', fn() => 'CLOSE_BLOCK_PARAMS'), |
| 159 | + |
| 160 | + new Rule('<mu>' . $ID, fn() => 'ID'), |
| 161 | + |
| 162 | + new Rule('<escl>\\[(\\\\\\]|[^\\]])*\\]', function () { |
| 163 | + $this->replace('/\\\\([\\\\\\]])/', '$1'); |
| 164 | + $this->popState(); |
| 165 | + return 'ID'; |
| 166 | + }), |
| 167 | + |
| 168 | + new Rule('<mu>.', fn() => 'INVALID'), |
| 169 | + |
| 170 | + new Rule('<INITIAL,mu>\\Z', fn() => 'EOF'), |
| 171 | + ]); |
| 172 | + } |
| 173 | + |
| 174 | + private function strip(int $start, int $end): void |
| 175 | + { |
| 176 | + $this->yytext = substr($this->yytext, $start, strlen($this->yytext) - $end); |
| 177 | + } |
| 178 | + |
| 179 | + private function replace(string $pattern, string $replacement): void |
| 180 | + { |
| 181 | + $result = preg_replace($pattern, $replacement, $this->yytext); |
| 182 | + |
| 183 | + if ($result === null) { |
| 184 | + throw new \Exception('Failed to replace string: ' . preg_last_error_msg()); |
| 185 | + } |
| 186 | + |
| 187 | + $this->yytext = $result; |
| 188 | + } |
| 189 | +} |
0 commit comments