phpdbg_parser.y 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. %{
  2. /*
  3. * phpdbg_parser.y
  4. * (from php-src root)
  5. * flex sapi/phpdbg/dev/phpdbg_lexer.l
  6. * bison sapi/phpdbg/dev/phpdbg_parser.y
  7. */
  8. #include "phpdbg.h"
  9. #include "phpdbg_cmd.h"
  10. #include "phpdbg_utils.h"
  11. #include "phpdbg_cmd.h"
  12. #include "phpdbg_prompt.h"
  13. #define YYSTYPE phpdbg_param_t
  14. #include "phpdbg_parser.h"
  15. #include "phpdbg_lexer.h"
  16. #undef yyerror
  17. static int yyerror(void ***tsrm_ls, const char *msg);
  18. ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
  19. %}
  20. %pure-parser
  21. %error-verbose
  22. %code requires {
  23. #include "phpdbg.h"
  24. #ifndef YY_TYPEDEF_YY_SCANNER_T
  25. #define YY_TYPEDEF_YY_SCANNER_T
  26. typedef void* yyscan_t;
  27. #endif
  28. }
  29. %parse-param { void *tsrm_ls }
  30. %output "sapi/phpdbg/phpdbg_parser.c"
  31. %defines "sapi/phpdbg/phpdbg_parser.h"
  32. %token T_EVAL "eval"
  33. %token T_RUN "run"
  34. %token T_SHELL "shell"
  35. %token T_IF "if (condition)"
  36. %token T_TRUTHY "truthy (true, on, yes or enabled)"
  37. %token T_FALSY "falsy (false, off, no or disabled)"
  38. %token T_STRING "string (some input, perhaps)"
  39. %token T_COLON ": (colon)"
  40. %token T_DCOLON ":: (double colon)"
  41. %token T_POUND "# (pound sign)"
  42. %token T_PROTO "protocol (file://)"
  43. %token T_DIGITS "digits (numbers)"
  44. %token T_LITERAL "literal (string)"
  45. %token T_ADDR "address"
  46. %token T_OPCODE "opcode"
  47. %token T_ID "identifier (command or function name)"
  48. %token T_INPUT "input (input string or data)"
  49. %token T_UNEXPECTED "input"
  50. %% /* Rules */
  51. input
  52. : parameters
  53. | full_expression { phpdbg_stack_push(PHPDBG_G(parser_stack), &$1); }
  54. | /* nothing */
  55. ;
  56. parameters
  57. : parameter { phpdbg_stack_push(PHPDBG_G(parser_stack), &$1); }
  58. | parameters parameter { phpdbg_stack_push(PHPDBG_G(parser_stack), &$2); }
  59. ;
  60. parameter
  61. : T_ID T_COLON T_DIGITS {
  62. $$.type = FILE_PARAM;
  63. $$.file.name = $2.str;
  64. $$.file.line = $3.num;
  65. }
  66. | T_ID T_COLON T_POUND T_DIGITS {
  67. $$.type = NUMERIC_FILE_PARAM;
  68. $$.file.name = $1.str;
  69. $$.file.line = $4.num;
  70. }
  71. | T_PROTO T_ID T_COLON T_DIGITS {
  72. $$.type = FILE_PARAM;
  73. $$.file.name = malloc($1.len + $2.len + 1);
  74. if ($$.file.name) {
  75. memcpy(&$$.file.name[0], $1.str, $1.len);
  76. memcpy(&$$.file.name[$1.len], $2.str, $2.len);
  77. $$.file.name[$1.len + $2.len] = '\0';
  78. }
  79. $$.file.line = $4.num;
  80. }
  81. | T_PROTO T_ID T_COLON T_POUND T_DIGITS {
  82. $$.type = NUMERIC_FILE_PARAM;
  83. $$.file.name = malloc($1.len + $2.len + 1);
  84. if ($$.file.name) {
  85. memcpy(&$$.file.name[0], $1.str, $1.len);
  86. memcpy(&$$.file.name[$1.len], $2.str, $2.len);
  87. $$.file.name[$1.len + $2.len] = '\0';
  88. }
  89. $$.file.line = $5.num;
  90. }
  91. | T_ID T_DCOLON T_ID {
  92. $$.type = METHOD_PARAM;
  93. $$.method.class = $1.str;
  94. $$.method.name = $3.str;
  95. }
  96. | T_ID T_DCOLON T_ID T_POUND T_DIGITS {
  97. $$.type = NUMERIC_METHOD_PARAM;
  98. $$.method.class = $1.str;
  99. $$.method.name = $3.str;
  100. $$.num = $5.num;
  101. }
  102. | T_ID T_POUND T_DIGITS {
  103. $$.type = NUMERIC_FUNCTION_PARAM;
  104. $$.str = $1.str;
  105. $$.len = $1.len;
  106. $$.num = $3.num;
  107. }
  108. | T_IF T_INPUT {
  109. $$.type = COND_PARAM;
  110. $$.str = $2.str;
  111. $$.len = $2.len;
  112. }
  113. | T_OPCODE { $$ = $1; }
  114. | T_ADDR { $$ = $1; }
  115. | T_LITERAL { $$ = $1; }
  116. | T_TRUTHY { $$ = $1; }
  117. | T_FALSY { $$ = $1; }
  118. | T_DIGITS { $$ = $1; }
  119. | T_ID { $$ = $1; }
  120. ;
  121. full_expression
  122. : T_EVAL T_INPUT {
  123. $$.type = EVAL_PARAM;
  124. $$.str = $2.str;
  125. $$.len = $2.len;
  126. }
  127. | T_SHELL T_INPUT {
  128. $$.type = SHELL_PARAM;
  129. $$.str = $2.str;
  130. $$.len = $2.len;
  131. }
  132. | T_RUN {
  133. $$.type = RUN_PARAM;
  134. $$.len = 0;
  135. }
  136. | T_RUN T_INPUT {
  137. $$.type = RUN_PARAM;
  138. $$.str = $2.str;
  139. $$.len = $2.len;
  140. }
  141. ;
  142. %%
  143. static int yyerror(void ***tsrm_ls, const char *msg) {
  144. phpdbg_error("Parse Error: %s", msg);
  145. {
  146. const phpdbg_param_t *top = PHPDBG_G(parser_stack);
  147. while (top) {
  148. phpdbg_param_debug(top, "--> ");
  149. top = top->next;
  150. }
  151. }
  152. return 0;
  153. }
  154. int phpdbg_do_parse(phpdbg_param_t *stack, char *input TSRMLS_DC) {
  155. phpdbg_init_lexer(stack, input TSRMLS_CC);
  156. #ifdef ZTS
  157. return yyparse(TSRMLS_C);
  158. #else
  159. return yyparse(NULL);
  160. #endif
  161. }