Dashboard sipadu mbip
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

rebuildParsers.php 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. $grammarFileToName = [
  3. __DIR__ . '/php5.y' => 'Php5',
  4. __DIR__ . '/php7.y' => 'Php7',
  5. ];
  6. $tokensFile = __DIR__ . '/tokens.y';
  7. $tokensTemplate = __DIR__ . '/tokens.template';
  8. $skeletonFile = __DIR__ . '/parser.template';
  9. $tmpGrammarFile = __DIR__ . '/tmp_parser.phpy';
  10. $tmpResultFile = __DIR__ . '/tmp_parser.php';
  11. $resultDir = __DIR__ . '/../lib/PhpParser/Parser';
  12. $tokensResultsFile = $resultDir . '/Tokens.php';
  13. // check for kmyacc binary in this directory, otherwise fall back to global name
  14. if (file_exists(__DIR__ . '/kmyacc.exe')) {
  15. $kmyacc = __DIR__ . '/kmyacc.exe';
  16. } else if (file_exists(__DIR__ . '/kmyacc')) {
  17. $kmyacc = __DIR__ . '/kmyacc';
  18. } else {
  19. $kmyacc = 'kmyacc';
  20. }
  21. $options = array_flip($argv);
  22. $optionDebug = isset($options['--debug']);
  23. $optionKeepTmpGrammar = isset($options['--keep-tmp-grammar']);
  24. ///////////////////////////////
  25. /// Utility regex constants ///
  26. ///////////////////////////////
  27. const LIB = '(?(DEFINE)
  28. (?<singleQuotedString>\'[^\\\\\']*+(?:\\\\.[^\\\\\']*+)*+\')
  29. (?<doubleQuotedString>"[^\\\\"]*+(?:\\\\.[^\\\\"]*+)*+")
  30. (?<string>(?&singleQuotedString)|(?&doubleQuotedString))
  31. (?<comment>/\*[^*]*+(?:\*(?!/)[^*]*+)*+\*/)
  32. (?<code>\{[^\'"/{}]*+(?:(?:(?&string)|(?&comment)|(?&code)|/)[^\'"/{}]*+)*+})
  33. )';
  34. const PARAMS = '\[(?<params>[^[\]]*+(?:\[(?&params)\][^[\]]*+)*+)\]';
  35. const ARGS = '\((?<args>[^()]*+(?:\((?&args)\)[^()]*+)*+)\)';
  36. ///////////////////
  37. /// Main script ///
  38. ///////////////////
  39. $tokens = file_get_contents($tokensFile);
  40. foreach ($grammarFileToName as $grammarFile => $name) {
  41. echo "Building temporary $name grammar file.\n";
  42. $grammarCode = file_get_contents($grammarFile);
  43. $grammarCode = str_replace('%tokens', $tokens, $grammarCode);
  44. $grammarCode = resolveNodes($grammarCode);
  45. $grammarCode = resolveMacros($grammarCode);
  46. $grammarCode = resolveStackAccess($grammarCode);
  47. file_put_contents($tmpGrammarFile, $grammarCode);
  48. $additionalArgs = $optionDebug ? '-t -v' : '';
  49. echo "Building $name parser.\n";
  50. $output = trim(shell_exec("$kmyacc $additionalArgs -l -m $skeletonFile -p $name $tmpGrammarFile 2>&1"));
  51. echo "Output: \"$output\"\n";
  52. $resultCode = file_get_contents($tmpResultFile);
  53. $resultCode = removeTrailingWhitespace($resultCode);
  54. ensureDirExists($resultDir);
  55. file_put_contents("$resultDir/$name.php", $resultCode);
  56. unlink($tmpResultFile);
  57. echo "Building token definition.\n";
  58. $output = trim(shell_exec("$kmyacc -l -m $tokensTemplate $tmpGrammarFile 2>&1"));
  59. assert($output === '');
  60. rename($tmpResultFile, $tokensResultsFile);
  61. if (!$optionKeepTmpGrammar) {
  62. unlink($tmpGrammarFile);
  63. }
  64. }
  65. ///////////////////////////////
  66. /// Preprocessing functions ///
  67. ///////////////////////////////
  68. function resolveNodes($code) {
  69. return preg_replace_callback(
  70. '~\b(?<name>[A-Z][a-zA-Z_\\\\]++)\s*' . PARAMS . '~',
  71. function($matches) {
  72. // recurse
  73. $matches['params'] = resolveNodes($matches['params']);
  74. $params = magicSplit(
  75. '(?:' . PARAMS . '|' . ARGS . ')(*SKIP)(*FAIL)|,',
  76. $matches['params']
  77. );
  78. $paramCode = '';
  79. foreach ($params as $param) {
  80. $paramCode .= $param . ', ';
  81. }
  82. return 'new ' . $matches['name'] . '(' . $paramCode . 'attributes())';
  83. },
  84. $code
  85. );
  86. }
  87. function resolveMacros($code) {
  88. return preg_replace_callback(
  89. '~\b(?<!::|->)(?!array\()(?<name>[a-z][A-Za-z]++)' . ARGS . '~',
  90. function($matches) {
  91. // recurse
  92. $matches['args'] = resolveMacros($matches['args']);
  93. $name = $matches['name'];
  94. $args = magicSplit(
  95. '(?:' . PARAMS . '|' . ARGS . ')(*SKIP)(*FAIL)|,',
  96. $matches['args']
  97. );
  98. if ('attributes' == $name) {
  99. assertArgs(0, $args, $name);
  100. return '$this->startAttributeStack[#1] + $this->endAttributes';
  101. }
  102. if ('stackAttributes' == $name) {
  103. assertArgs(1, $args, $name);
  104. return '$this->startAttributeStack[' . $args[0] . ']'
  105. . ' + $this->endAttributeStack[' . $args[0] . ']';
  106. }
  107. if ('init' == $name) {
  108. return '$$ = array(' . implode(', ', $args) . ')';
  109. }
  110. if ('push' == $name) {
  111. assertArgs(2, $args, $name);
  112. return $args[0] . '[] = ' . $args[1] . '; $$ = ' . $args[0];
  113. }
  114. if ('pushNormalizing' == $name) {
  115. assertArgs(2, $args, $name);
  116. return 'if (is_array(' . $args[1] . ')) { $$ = array_merge(' . $args[0] . ', ' . $args[1] . '); }'
  117. . ' else { ' . $args[0] . '[] = ' . $args[1] . '; $$ = ' . $args[0] . '; }';
  118. }
  119. if ('toArray' == $name) {
  120. assertArgs(1, $args, $name);
  121. return 'is_array(' . $args[0] . ') ? ' . $args[0] . ' : array(' . $args[0] . ')';
  122. }
  123. if ('parseVar' == $name) {
  124. assertArgs(1, $args, $name);
  125. return 'substr(' . $args[0] . ', 1)';
  126. }
  127. if ('parseEncapsed' == $name) {
  128. assertArgs(3, $args, $name);
  129. return 'foreach (' . $args[0] . ' as $s) { if ($s instanceof Node\Scalar\EncapsedStringPart) {'
  130. . ' $s->value = Node\Scalar\String_::parseEscapeSequences($s->value, ' . $args[1] . ', ' . $args[2] . '); } }';
  131. }
  132. if ('makeNop' == $name) {
  133. assertArgs(3, $args, $name);
  134. return '$startAttributes = ' . $args[1] . ';'
  135. . ' if (isset($startAttributes[\'comments\']))'
  136. . ' { ' . $args[0] . ' = new Stmt\Nop($startAttributes + ' . $args[2] . '); }'
  137. . ' else { ' . $args[0] . ' = null; }';
  138. }
  139. if ('makeZeroLengthNop' == $name) {
  140. assertArgs(2, $args, $name);
  141. return '$startAttributes = ' . $args[1] . ';'
  142. . ' if (isset($startAttributes[\'comments\']))'
  143. . ' { ' . $args[0] . ' = new Stmt\Nop($this->createZeroLengthAttributes($startAttributes)); }'
  144. . ' else { ' . $args[0] . ' = null; }';
  145. }
  146. if ('strKind' == $name) {
  147. assertArgs(1, $args, $name);
  148. return '(' . $args[0] . '[0] === "\'" || (' . $args[0] . '[1] === "\'" && '
  149. . '(' . $args[0] . '[0] === \'b\' || ' . $args[0] . '[0] === \'B\')) '
  150. . '? Scalar\String_::KIND_SINGLE_QUOTED : Scalar\String_::KIND_DOUBLE_QUOTED)';
  151. }
  152. if ('prependLeadingComments' == $name) {
  153. assertArgs(1, $args, $name);
  154. return '$attrs = $this->startAttributeStack[#1]; $stmts = ' . $args[0] . '; '
  155. . 'if (!empty($attrs[\'comments\'])) {'
  156. . '$stmts[0]->setAttribute(\'comments\', '
  157. . 'array_merge($attrs[\'comments\'], $stmts[0]->getAttribute(\'comments\', []))); }';
  158. }
  159. return $matches[0];
  160. },
  161. $code
  162. );
  163. }
  164. function assertArgs($num, $args, $name) {
  165. if ($num != count($args)) {
  166. die('Wrong argument count for ' . $name . '().');
  167. }
  168. }
  169. function resolveStackAccess($code) {
  170. $code = preg_replace('/\$\d+/', '$this->semStack[$0]', $code);
  171. $code = preg_replace('/#(\d+)/', '$$1', $code);
  172. return $code;
  173. }
  174. function removeTrailingWhitespace($code) {
  175. $lines = explode("\n", $code);
  176. $lines = array_map('rtrim', $lines);
  177. return implode("\n", $lines);
  178. }
  179. function ensureDirExists($dir) {
  180. if (!is_dir($dir)) {
  181. mkdir($dir, 0777, true);
  182. }
  183. }
  184. //////////////////////////////
  185. /// Regex helper functions ///
  186. //////////////////////////////
  187. function regex($regex) {
  188. return '~' . LIB . '(?:' . str_replace('~', '\~', $regex) . ')~';
  189. }
  190. function magicSplit($regex, $string) {
  191. $pieces = preg_split(regex('(?:(?&string)|(?&comment)|(?&code))(*SKIP)(*FAIL)|' . $regex), $string);
  192. foreach ($pieces as &$piece) {
  193. $piece = trim($piece);
  194. }
  195. if ($pieces === ['']) {
  196. return [];
  197. }
  198. return $pieces;
  199. }