4 __DIR__ . '/php5.y' => 'Php5',
5 __DIR__ . '/php7.y' => 'Php7',
8 $tokensFile = __DIR__ . '/tokens.y';
9 $tokensTemplate = __DIR__ . '/tokens.template';
10 $skeletonFile = __DIR__ . '/parser.template';
11 $tmpGrammarFile = __DIR__ . '/tmp_parser.phpy';
12 $tmpResultFile = __DIR__ . '/tmp_parser.php';
13 $resultDir = __DIR__ . '/../lib/PhpParser/Parser';
14 $tokensResultsFile = $resultDir . '/Tokens.php';
16 // check for kmyacc.exe binary in this directory, otherwise fall back to global name
17 $kmyacc = __DIR__ . '/kmyacc.exe';
18 if (!file_exists($kmyacc)) {
22 $options = array_flip($argv);
23 $optionDebug = isset($options['--debug']);
24 $optionKeepTmpGrammar = isset($options['--keep-tmp-grammar']);
26 ///////////////////////////////
27 /// Utility regex constants ///
28 ///////////////////////////////
30 const LIB = '(?(DEFINE)
31 (?<singleQuotedString>\'[^\\\\\']*+(?:\\\\.[^\\\\\']*+)*+\')
32 (?<doubleQuotedString>"[^\\\\"]*+(?:\\\\.[^\\\\"]*+)*+")
33 (?<string>(?&singleQuotedString)|(?&doubleQuotedString))
34 (?<comment>/\*[^*]*+(?:\*(?!/)[^*]*+)*+\*/)
35 (?<code>\{[^\'"/{}]*+(?:(?:(?&string)|(?&comment)|(?&code)|/)[^\'"/{}]*+)*+})
38 const PARAMS = '\[(?<params>[^[\]]*+(?:\[(?¶ms)\][^[\]]*+)*+)\]';
39 const ARGS = '\((?<args>[^()]*+(?:\((?&args)\)[^()]*+)*+)\)';
45 $tokens = file_get_contents($tokensFile);
47 foreach ($grammarFileToName as $grammarFile => $name) {
48 echo "Building temporary $name grammar file.\n";
50 $grammarCode = file_get_contents($grammarFile);
51 $grammarCode = str_replace('%tokens', $tokens, $grammarCode);
53 $grammarCode = resolveNodes($grammarCode);
54 $grammarCode = resolveMacros($grammarCode);
55 $grammarCode = resolveStackAccess($grammarCode);
57 file_put_contents($tmpGrammarFile, $grammarCode);
59 $additionalArgs = $optionDebug ? '-t -v' : '';
61 echo "Building $name parser.\n";
62 $output = trim(shell_exec("$kmyacc $additionalArgs -l -m $skeletonFile -p $name $tmpGrammarFile 2>&1"));
63 echo "Output: \"$output\"\n";
65 $resultCode = file_get_contents($tmpResultFile);
66 $resultCode = removeTrailingWhitespace($resultCode);
68 ensureDirExists($resultDir);
69 file_put_contents("$resultDir/$name.php", $resultCode);
70 unlink($tmpResultFile);
72 echo "Building token definition.\n";
73 $output = trim(shell_exec("$kmyacc -l -m $tokensTemplate $tmpGrammarFile 2>&1"));
74 assert($output === '');
75 rename($tmpResultFile, $tokensResultsFile);
77 if (!$optionKeepTmpGrammar) {
78 unlink($tmpGrammarFile);
82 ///////////////////////////////
83 /// Preprocessing functions ///
84 ///////////////////////////////
86 function resolveNodes($code) {
87 return preg_replace_callback(
88 '~\b(?<name>[A-Z][a-zA-Z_\\\\]++)\s*' . PARAMS . '~',
91 $matches['params'] = resolveNodes($matches['params']);
94 '(?:' . PARAMS . '|' . ARGS . ')(*SKIP)(*FAIL)|,',
99 foreach ($params as $param) {
100 $paramCode .= $param . ', ';
103 return 'new ' . $matches['name'] . '(' . $paramCode . 'attributes())';
109 function resolveMacros($code) {
110 return preg_replace_callback(
111 '~\b(?<!::|->)(?!array\()(?<name>[a-z][A-Za-z]++)' . ARGS . '~',
114 $matches['args'] = resolveMacros($matches['args']);
116 $name = $matches['name'];
118 '(?:' . PARAMS . '|' . ARGS . ')(*SKIP)(*FAIL)|,',
122 if ('attributes' == $name) {
123 assertArgs(0, $args, $name);
124 return '$this->startAttributeStack[#1] + $this->endAttributes';
127 if ('stackAttributes' == $name) {
128 assertArgs(1, $args, $name);
129 return '$this->startAttributeStack[' . $args[0] . ']'
130 . ' + $this->endAttributeStack[' . $args[0] . ']';
133 if ('init' == $name) {
134 return '$$ = array(' . implode(', ', $args) . ')';
137 if ('push' == $name) {
138 assertArgs(2, $args, $name);
140 return $args[0] . '[] = ' . $args[1] . '; $$ = ' . $args[0];
143 if ('pushNormalizing' == $name) {
144 assertArgs(2, $args, $name);
146 return 'if (is_array(' . $args[1] . ')) { $$ = array_merge(' . $args[0] . ', ' . $args[1] . '); }'
147 . ' else { ' . $args[0] . '[] = ' . $args[1] . '; $$ = ' . $args[0] . '; }';
150 if ('toArray' == $name) {
151 assertArgs(1, $args, $name);
153 return 'is_array(' . $args[0] . ') ? ' . $args[0] . ' : array(' . $args[0] . ')';
156 if ('parseVar' == $name) {
157 assertArgs(1, $args, $name);
159 return 'substr(' . $args[0] . ', 1)';
162 if ('parseEncapsed' == $name) {
163 assertArgs(3, $args, $name);
165 return 'foreach (' . $args[0] . ' as $s) { if ($s instanceof Node\Scalar\EncapsedStringPart) {'
166 . ' $s->value = Node\Scalar\String_::parseEscapeSequences($s->value, ' . $args[1] . ', ' . $args[2] . '); } }';
169 if ('parseEncapsedDoc' == $name) {
170 assertArgs(2, $args, $name);
172 return 'foreach (' . $args[0] . ' as $s) { if ($s instanceof Node\Scalar\EncapsedStringPart) {'
173 . ' $s->value = Node\Scalar\String_::parseEscapeSequences($s->value, null, ' . $args[1] . '); } }'
174 . ' $s->value = preg_replace(\'~(\r\n|\n|\r)\z~\', \'\', $s->value);'
175 . ' if (\'\' === $s->value) array_pop(' . $args[0] . ');';
178 if ('makeNop' == $name) {
179 assertArgs(2, $args, $name);
181 return '$startAttributes = ' . $args[1] . ';'
182 . ' if (isset($startAttributes[\'comments\']))'
183 . ' { ' . $args[0] . ' = new Stmt\Nop([\'comments\' => $startAttributes[\'comments\']]); }'
184 . ' else { ' . $args[0] . ' = null; }';
187 if ('strKind' == $name) {
188 assertArgs(1, $args, $name);
190 return '(' . $args[0] . '[0] === "\'" || (' . $args[0] . '[1] === "\'" && '
191 . '(' . $args[0] . '[0] === \'b\' || ' . $args[0] . '[0] === \'B\')) '
192 . '? Scalar\String_::KIND_SINGLE_QUOTED : Scalar\String_::KIND_DOUBLE_QUOTED)';
195 if ('setDocStringAttrs' == $name) {
196 assertArgs(2, $args, $name);
198 return $args[0] . '[\'kind\'] = strpos(' . $args[1] . ', "\'") === false '
199 . '? Scalar\String_::KIND_HEREDOC : Scalar\String_::KIND_NOWDOC; '
200 . 'preg_match(\'/\A[bB]?<<<[ \t]*[\\\'"]?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)[\\\'"]?(?:\r\n|\n|\r)\z/\', ' . $args[1] . ', $matches); '
201 . $args[0] . '[\'docLabel\'] = $matches[1];';
204 if ('prependLeadingComments' == $name) {
205 assertArgs(1, $args, $name);
207 return '$attrs = $this->startAttributeStack[#1]; $stmts = ' . $args[0] . '; '
208 . 'if (!empty($attrs[\'comments\'])) {'
209 . '$stmts[0]->setAttribute(\'comments\', '
210 . 'array_merge($attrs[\'comments\'], $stmts[0]->getAttribute(\'comments\', []))); }';
219 function assertArgs($num, $args, $name) {
220 if ($num != count($args)) {
221 die('Wrong argument count for ' . $name . '().');
225 function resolveStackAccess($code) {
226 $code = preg_replace('/\$\d+/', '$this->semStack[$0]', $code);
227 $code = preg_replace('/#(\d+)/', '$$1', $code);
231 function removeTrailingWhitespace($code) {
232 $lines = explode("\n", $code);
233 $lines = array_map('rtrim', $lines);
234 return implode("\n", $lines);
237 function ensureDirExists($dir) {
239 mkdir($dir, 0777, true);
243 //////////////////////////////
244 /// Regex helper functions ///
245 //////////////////////////////
247 function regex($regex) {
248 return '~' . LIB . '(?:' . str_replace('~', '\~', $regex) . ')~';
251 function magicSplit($regex, $string) {
252 $pieces = preg_split(regex('(?:(?&string)|(?&comment)|(?&code))(*SKIP)(*FAIL)|' . $regex), $string);
254 foreach ($pieces as &$piece) {
255 $piece = trim($piece);
258 if ($pieces === ['']) {