Geen omschrijving

phpyLang.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. ///////////////////////////////
  3. /// Utility regex constants ///
  4. ///////////////////////////////
  5. const LIB = '(?(DEFINE)
  6. (?<singleQuotedString>\'[^\\\\\']*+(?:\\\\.[^\\\\\']*+)*+\')
  7. (?<doubleQuotedString>"[^\\\\"]*+(?:\\\\.[^\\\\"]*+)*+")
  8. (?<string>(?&singleQuotedString)|(?&doubleQuotedString))
  9. (?<comment>/\*[^*]*+(?:\*(?!/)[^*]*+)*+\*/)
  10. (?<code>\{[^\'"/{}]*+(?:(?:(?&string)|(?&comment)|(?&code)|/)[^\'"/{}]*+)*+})
  11. )';
  12. const PARAMS = '\[(?<params>[^[\]]*+(?:\[(?&params)\][^[\]]*+)*+)\]';
  13. const ARGS = '\((?<args>[^()]*+(?:\((?&args)\)[^()]*+)*+)\)';
  14. ///////////////////////////////
  15. /// Preprocessing functions ///
  16. ///////////////////////////////
  17. function preprocessGrammar($code) {
  18. $code = resolveNodes($code);
  19. $code = resolveMacros($code);
  20. $code = resolveStackAccess($code);
  21. return $code;
  22. }
  23. function resolveNodes($code) {
  24. return preg_replace_callback(
  25. '~\b(?<name>[A-Z][a-zA-Z_\\\\]++)\s*' . PARAMS . '~',
  26. function($matches) {
  27. // recurse
  28. $matches['params'] = resolveNodes($matches['params']);
  29. $params = magicSplit(
  30. '(?:' . PARAMS . '|' . ARGS . ')(*SKIP)(*FAIL)|,',
  31. $matches['params']
  32. );
  33. $paramCode = '';
  34. foreach ($params as $param) {
  35. $paramCode .= $param . ', ';
  36. }
  37. return 'new ' . $matches['name'] . '(' . $paramCode . 'attributes())';
  38. },
  39. $code
  40. );
  41. }
  42. function resolveMacros($code) {
  43. return preg_replace_callback(
  44. '~\b(?<!::|->)(?!array\()(?<name>[a-z][A-Za-z]++)' . ARGS . '~',
  45. function($matches) {
  46. // recurse
  47. $matches['args'] = resolveMacros($matches['args']);
  48. $name = $matches['name'];
  49. $args = magicSplit(
  50. '(?:' . PARAMS . '|' . ARGS . ')(*SKIP)(*FAIL)|,',
  51. $matches['args']
  52. );
  53. if ('attributes' === $name) {
  54. assertArgs(0, $args, $name);
  55. return '$this->startAttributeStack[#1] + $this->endAttributes';
  56. }
  57. if ('stackAttributes' === $name) {
  58. assertArgs(1, $args, $name);
  59. return '$this->startAttributeStack[' . $args[0] . ']'
  60. . ' + $this->endAttributeStack[' . $args[0] . ']';
  61. }
  62. if ('init' === $name) {
  63. return '$$ = array(' . implode(', ', $args) . ')';
  64. }
  65. if ('push' === $name) {
  66. assertArgs(2, $args, $name);
  67. return $args[0] . '[] = ' . $args[1] . '; $$ = ' . $args[0];
  68. }
  69. if ('pushNormalizing' === $name) {
  70. assertArgs(2, $args, $name);
  71. return 'if (is_array(' . $args[1] . ')) { $$ = array_merge(' . $args[0] . ', ' . $args[1] . '); }'
  72. . ' else { ' . $args[0] . '[] = ' . $args[1] . '; $$ = ' . $args[0] . '; }';
  73. }
  74. if ('toArray' == $name) {
  75. assertArgs(1, $args, $name);
  76. return 'is_array(' . $args[0] . ') ? ' . $args[0] . ' : array(' . $args[0] . ')';
  77. }
  78. if ('parseVar' === $name) {
  79. assertArgs(1, $args, $name);
  80. return 'substr(' . $args[0] . ', 1)';
  81. }
  82. if ('parseEncapsed' === $name) {
  83. assertArgs(3, $args, $name);
  84. return 'foreach (' . $args[0] . ' as $s) { if ($s instanceof Node\Scalar\EncapsedStringPart) {'
  85. . ' $s->value = Node\Scalar\String_::parseEscapeSequences($s->value, ' . $args[1] . ', ' . $args[2] . '); } }';
  86. }
  87. if ('makeNop' === $name) {
  88. assertArgs(3, $args, $name);
  89. return '$startAttributes = ' . $args[1] . ';'
  90. . ' if (isset($startAttributes[\'comments\']))'
  91. . ' { ' . $args[0] . ' = new Stmt\Nop($startAttributes + ' . $args[2] . '); }'
  92. . ' else { ' . $args[0] . ' = null; }';
  93. }
  94. if ('makeZeroLengthNop' == $name) {
  95. assertArgs(2, $args, $name);
  96. return '$startAttributes = ' . $args[1] . ';'
  97. . ' if (isset($startAttributes[\'comments\']))'
  98. . ' { ' . $args[0] . ' = new Stmt\Nop($this->createCommentNopAttributes($startAttributes[\'comments\'])); }'
  99. . ' else { ' . $args[0] . ' = null; }';
  100. }
  101. if ('strKind' === $name) {
  102. assertArgs(1, $args, $name);
  103. return '(' . $args[0] . '[0] === "\'" || (' . $args[0] . '[1] === "\'" && '
  104. . '(' . $args[0] . '[0] === \'b\' || ' . $args[0] . '[0] === \'B\')) '
  105. . '? Scalar\String_::KIND_SINGLE_QUOTED : Scalar\String_::KIND_DOUBLE_QUOTED)';
  106. }
  107. if ('prependLeadingComments' === $name) {
  108. assertArgs(1, $args, $name);
  109. return '$attrs = $this->startAttributeStack[#1]; $stmts = ' . $args[0] . '; '
  110. . 'if (!empty($attrs[\'comments\'])) {'
  111. . '$stmts[0]->setAttribute(\'comments\', '
  112. . 'array_merge($attrs[\'comments\'], $stmts[0]->getAttribute(\'comments\', []))); }';
  113. }
  114. return $matches[0];
  115. },
  116. $code
  117. );
  118. }
  119. function assertArgs($num, $args, $name) {
  120. if ($num != count($args)) {
  121. die('Wrong argument count for ' . $name . '().');
  122. }
  123. }
  124. function resolveStackAccess($code) {
  125. $code = preg_replace('/\$\d+/', '$this->semStack[$0]', $code);
  126. $code = preg_replace('/#(\d+)/', '$$1', $code);
  127. return $code;
  128. }
  129. function removeTrailingWhitespace($code) {
  130. $lines = explode("\n", $code);
  131. $lines = array_map('rtrim', $lines);
  132. return implode("\n", $lines);
  133. }
  134. //////////////////////////////
  135. /// Regex helper functions ///
  136. //////////////////////////////
  137. function regex($regex) {
  138. return '~' . LIB . '(?:' . str_replace('~', '\~', $regex) . ')~';
  139. }
  140. function magicSplit($regex, $string) {
  141. $pieces = preg_split(regex('(?:(?&string)|(?&comment)|(?&code))(*SKIP)(*FAIL)|' . $regex), $string);
  142. foreach ($pieces as &$piece) {
  143. $piece = trim($piece);
  144. }
  145. if ($pieces === ['']) {
  146. return [];
  147. }
  148. return $pieces;
  149. }