zend_language_parser.phpy 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. %pure_parser
  2. %expect 2
  3. %left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
  4. %left ','
  5. %left T_LOGICAL_OR
  6. %left T_LOGICAL_XOR
  7. %left T_LOGICAL_AND
  8. %right T_PRINT
  9. %right T_YIELD
  10. %left '=' T_PLUS_EQUAL T_MINUS_EQUAL T_MUL_EQUAL T_DIV_EQUAL T_CONCAT_EQUAL T_MOD_EQUAL T_AND_EQUAL T_OR_EQUAL T_XOR_EQUAL T_SL_EQUAL T_SR_EQUAL
  11. %left '?' ':'
  12. %left T_BOOLEAN_OR
  13. %left T_BOOLEAN_AND
  14. %left '|'
  15. %left '^'
  16. %left '&'
  17. %nonassoc T_IS_EQUAL T_IS_NOT_EQUAL T_IS_IDENTICAL T_IS_NOT_IDENTICAL
  18. %nonassoc '<' T_IS_SMALLER_OR_EQUAL '>' T_IS_GREATER_OR_EQUAL
  19. %left T_SL T_SR
  20. %left '+' '-' '.'
  21. %left '*' '/' '%'
  22. %right '!'
  23. %nonassoc T_INSTANCEOF
  24. %right '~' T_INC T_DEC T_INT_CAST T_DOUBLE_CAST T_STRING_CAST T_ARRAY_CAST T_OBJECT_CAST T_BOOL_CAST T_UNSET_CAST '@'
  25. %right '['
  26. %nonassoc T_NEW T_CLONE
  27. %token T_EXIT
  28. %token T_IF
  29. %left T_ELSEIF
  30. %left T_ELSE
  31. %left T_ENDIF
  32. %token T_LNUMBER
  33. %token T_DNUMBER
  34. %token T_STRING
  35. %token T_STRING_VARNAME
  36. %token T_VARIABLE
  37. %token T_NUM_STRING
  38. %token T_INLINE_HTML
  39. %token T_CHARACTER
  40. %token T_BAD_CHARACTER
  41. %token T_ENCAPSED_AND_WHITESPACE
  42. %token T_CONSTANT_ENCAPSED_STRING
  43. %token T_ECHO
  44. %token T_DO
  45. %token T_WHILE
  46. %token T_ENDWHILE
  47. %token T_FOR
  48. %token T_ENDFOR
  49. %token T_FOREACH
  50. %token T_ENDFOREACH
  51. %token T_DECLARE
  52. %token T_ENDDECLARE
  53. %token T_AS
  54. %token T_SWITCH
  55. %token T_ENDSWITCH
  56. %token T_CASE
  57. %token T_DEFAULT
  58. %token T_BREAK
  59. %token T_CONTINUE
  60. %token T_GOTO
  61. %token T_FUNCTION
  62. %token T_CONST
  63. %token T_RETURN
  64. %token T_TRY
  65. %token T_CATCH
  66. %token T_FINALLY
  67. %token T_THROW
  68. %token T_USE
  69. %token T_INSTEADOF
  70. %token T_GLOBAL
  71. %right T_STATIC T_ABSTRACT T_FINAL T_PRIVATE T_PROTECTED T_PUBLIC
  72. %token T_VAR
  73. %token T_UNSET
  74. %token T_ISSET
  75. %token T_EMPTY
  76. %token T_HALT_COMPILER
  77. %token T_CLASS
  78. %token T_TRAIT
  79. %token T_INTERFACE
  80. %token T_EXTENDS
  81. %token T_IMPLEMENTS
  82. %token T_OBJECT_OPERATOR
  83. %token T_DOUBLE_ARROW
  84. %token T_LIST
  85. %token T_ARRAY
  86. %token T_CALLABLE
  87. %token T_CLASS_C
  88. %token T_TRAIT_C
  89. %token T_METHOD_C
  90. %token T_FUNC_C
  91. %token T_LINE
  92. %token T_FILE
  93. %token T_COMMENT
  94. %token T_DOC_COMMENT
  95. %token T_OPEN_TAG
  96. %token T_OPEN_TAG_WITH_ECHO
  97. %token T_CLOSE_TAG
  98. %token T_WHITESPACE
  99. %token T_START_HEREDOC
  100. %token T_END_HEREDOC
  101. %token T_DOLLAR_OPEN_CURLY_BRACES
  102. %token T_CURLY_OPEN
  103. %token T_PAAMAYIM_NEKUDOTAYIM
  104. %token T_NAMESPACE
  105. %token T_NS_C
  106. %token T_DIR
  107. %token T_NS_SEPARATOR
  108. %%
  109. start:
  110. top_statement_list { $$ = Stmt_Namespace::postprocess($1); }
  111. ;
  112. top_statement_list:
  113. top_statement_list top_statement { pushNormalizing($1, $2); }
  114. | /* empty */ { init(); }
  115. ;
  116. namespace_name:
  117. T_STRING { init($1); }
  118. | namespace_name T_NS_SEPARATOR T_STRING { push($1, $3); }
  119. ;
  120. top_statement:
  121. statement { $$ = $1; }
  122. | function_declaration_statement { $$ = $1; }
  123. | class_declaration_statement { $$ = $1; }
  124. | T_HALT_COMPILER
  125. { $$ = Stmt_HaltCompiler[$this->lexer->handleHaltCompiler()]; }
  126. | T_NAMESPACE namespace_name ';' { $$ = Stmt_Namespace[Name[$2], null]; }
  127. | T_NAMESPACE namespace_name '{' top_statement_list '}' { $$ = Stmt_Namespace[Name[$2], $4]; }
  128. | T_NAMESPACE '{' top_statement_list '}' { $$ = Stmt_Namespace[null, $3]; }
  129. | T_USE use_declarations ';' { $$ = Stmt_Use[$2]; }
  130. | T_CONST constant_declaration_list ';' { $$ = Stmt_Const[$2]; }
  131. ;
  132. use_declarations:
  133. use_declarations ',' use_declaration { push($1, $3); }
  134. | use_declaration { init($1); }
  135. ;
  136. use_declaration:
  137. namespace_name { $$ = Stmt_UseUse[Name[$1], null]; }
  138. | namespace_name T_AS T_STRING { $$ = Stmt_UseUse[Name[$1], $3]; }
  139. | T_NS_SEPARATOR namespace_name { $$ = Stmt_UseUse[Name[$2], null]; }
  140. | T_NS_SEPARATOR namespace_name T_AS T_STRING { $$ = Stmt_UseUse[Name[$2], $4]; }
  141. ;
  142. constant_declaration_list:
  143. constant_declaration_list ',' constant_declaration { push($1, $3); }
  144. | constant_declaration { init($1); }
  145. ;
  146. constant_declaration:
  147. T_STRING '=' static_scalar { $$ = Const[$1, $3]; }
  148. ;
  149. inner_statement_list:
  150. inner_statement_list inner_statement { pushNormalizing($1, $2); }
  151. | /* empty */ { init(); }
  152. ;
  153. inner_statement:
  154. statement { $$ = $1; }
  155. | function_declaration_statement { $$ = $1; }
  156. | class_declaration_statement { $$ = $1; }
  157. | T_HALT_COMPILER { error('__halt_compiler() can only be used from the outermost scope'); }
  158. ;
  159. statement:
  160. '{' inner_statement_list '}' { $$ = $2; }
  161. | T_IF parentheses_expr statement elseif_list else_single
  162. { $$ = Stmt_If[$2, [stmts: toArray($3), elseifs: $4, else: $5]]; }
  163. | T_IF parentheses_expr ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';'
  164. { $$ = Stmt_If[$2, [stmts: $4, elseifs: $5, else: $6]]; }
  165. | T_WHILE parentheses_expr while_statement { $$ = Stmt_While[$2, $3]; }
  166. | T_DO statement T_WHILE parentheses_expr ';' { $$ = Stmt_Do [$4, toArray($2)]; }
  167. | T_FOR '(' for_expr ';' for_expr ';' for_expr ')' for_statement
  168. { $$ = Stmt_For[[init: $3, cond: $5, loop: $7, stmts: $9]]; }
  169. | T_SWITCH parentheses_expr switch_case_list { $$ = Stmt_Switch[$2, $3]; }
  170. | T_BREAK ';' { $$ = Stmt_Break[null]; }
  171. | T_BREAK expr ';' { $$ = Stmt_Break[$2]; }
  172. | T_CONTINUE ';' { $$ = Stmt_Continue[null]; }
  173. | T_CONTINUE expr ';' { $$ = Stmt_Continue[$2]; }
  174. | T_RETURN ';' { $$ = Stmt_Return[null]; }
  175. | T_RETURN expr ';' { $$ = Stmt_Return[$2]; }
  176. | yield_expr ';' { $$ = $1; }
  177. | T_GLOBAL global_var_list ';' { $$ = Stmt_Global[$2]; }
  178. | T_STATIC static_var_list ';' { $$ = Stmt_Static[$2]; }
  179. | T_ECHO expr_list ';' { $$ = Stmt_Echo[$2]; }
  180. | T_INLINE_HTML { $$ = Stmt_InlineHTML[$1]; }
  181. | expr ';' { $$ = $1; }
  182. | T_UNSET '(' variables_list ')' ';' { $$ = Stmt_Unset[$3]; }
  183. | T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement
  184. { $$ = Stmt_Foreach[$3, $5[0], [keyVar: null, byRef: $5[1], stmts: $7]]; }
  185. | T_FOREACH '(' expr T_AS variable T_DOUBLE_ARROW foreach_variable ')' foreach_statement
  186. { $$ = Stmt_Foreach[$3, $7[0], [keyVar: $5, byRef: $7[1], stmts: $9]]; }
  187. | T_DECLARE '(' declare_list ')' declare_statement { $$ = Stmt_Declare[$3, $5]; }
  188. | ';' { $$ = array(); /* means: no statement */ }
  189. | T_TRY '{' inner_statement_list '}' catches optional_finally
  190. { $$ = Stmt_TryCatch[$3, $5, $6]; }
  191. | T_THROW expr ';' { $$ = Stmt_Throw[$2]; }
  192. | T_GOTO T_STRING ';' { $$ = Stmt_Goto[$2]; }
  193. | T_STRING ':' { $$ = Stmt_Label[$1]; }
  194. ;
  195. catches:
  196. /* empty */ { init(); }
  197. | catches catch { push($1, $2); }
  198. ;
  199. catch:
  200. T_CATCH '(' name T_VARIABLE ')' '{' inner_statement_list '}'
  201. { $$ = Stmt_Catch[$3, parseVar($4), $7]; }
  202. ;
  203. optional_finally:
  204. /* empty */ { $$ = null; }
  205. | T_FINALLY '{' inner_statement_list '}' { $$ = $3; }
  206. ;
  207. variables_list:
  208. variable { init($1); }
  209. | variables_list ',' variable { push($1, $3); }
  210. ;
  211. optional_ref:
  212. /* empty */ { $$ = false; }
  213. | '&' { $$ = true; }
  214. ;
  215. function_declaration_statement:
  216. T_FUNCTION optional_ref T_STRING '(' parameter_list ')' '{' inner_statement_list '}'
  217. { $$ = Stmt_Function[$3, [byRef: $2, params: $5, stmts: $8]]; }
  218. ;
  219. class_declaration_statement:
  220. class_entry_type T_STRING extends_from implements_list '{' class_statement_list '}'
  221. { $$ = Stmt_Class[$2, [type: $1, extends: $3, implements: $4, stmts: $6]]; }
  222. | T_INTERFACE T_STRING interface_extends_list '{' class_statement_list '}'
  223. { $$ = Stmt_Interface[$2, [extends: $3, stmts: $5]]; }
  224. | T_TRAIT T_STRING '{' class_statement_list '}'
  225. { $$ = Stmt_Trait[$2, $4]; }
  226. ;
  227. class_entry_type:
  228. T_CLASS { $$ = 0; }
  229. | T_ABSTRACT T_CLASS { $$ = Stmt_Class::MODIFIER_ABSTRACT; }
  230. | T_FINAL T_CLASS { $$ = Stmt_Class::MODIFIER_FINAL; }
  231. ;
  232. extends_from:
  233. /* empty */ { $$ = null; }
  234. | T_EXTENDS name { $$ = $2; }
  235. ;
  236. interface_extends_list:
  237. /* empty */ { $$ = array(); }
  238. | T_EXTENDS name_list { $$ = $2; }
  239. ;
  240. implements_list:
  241. /* empty */ { $$ = array(); }
  242. | T_IMPLEMENTS name_list { $$ = $2; }
  243. ;
  244. name_list:
  245. name { init($1); }
  246. | name_list ',' name { push($1, $3); }
  247. ;
  248. for_statement:
  249. statement { $$ = toArray($1); }
  250. | ':' inner_statement_list T_ENDFOR ';' { $$ = $2; }
  251. ;
  252. foreach_statement:
  253. statement { $$ = toArray($1); }
  254. | ':' inner_statement_list T_ENDFOREACH ';' { $$ = $2; }
  255. ;
  256. declare_statement:
  257. statement { $$ = toArray($1); }
  258. | ':' inner_statement_list T_ENDDECLARE ';' { $$ = $2; }
  259. ;
  260. declare_list:
  261. declare_list_element { init($1); }
  262. | declare_list ',' declare_list_element { push($1, $3); }
  263. ;
  264. declare_list_element:
  265. T_STRING '=' static_scalar { $$ = Stmt_DeclareDeclare[$1, $3]; }
  266. ;
  267. switch_case_list:
  268. '{' case_list '}' { $$ = $2; }
  269. | '{' ';' case_list '}' { $$ = $3; }
  270. | ':' case_list T_ENDSWITCH ';' { $$ = $2; }
  271. | ':' ';' case_list T_ENDSWITCH ';' { $$ = $3; }
  272. ;
  273. case_list:
  274. /* empty */ { init(); }
  275. | case_list case { push($1, $2); }
  276. ;
  277. case:
  278. T_CASE expr case_separator inner_statement_list { $$ = Stmt_Case[$2, $4]; }
  279. | T_DEFAULT case_separator inner_statement_list { $$ = Stmt_Case[null, $3]; }
  280. ;
  281. case_separator:
  282. ':'
  283. | ';'
  284. ;
  285. while_statement:
  286. statement { $$ = toArray($1); }
  287. | ':' inner_statement_list T_ENDWHILE ';' { $$ = $2; }
  288. ;
  289. elseif_list:
  290. /* empty */ { init(); }
  291. | elseif_list elseif { push($1, $2); }
  292. ;
  293. elseif:
  294. T_ELSEIF parentheses_expr statement { $$ = Stmt_ElseIf[$2, toArray($3)]; }
  295. ;
  296. new_elseif_list:
  297. /* empty */ { init(); }
  298. | new_elseif_list new_elseif { push($1, $2); }
  299. ;
  300. new_elseif:
  301. T_ELSEIF parentheses_expr ':' inner_statement_list { $$ = Stmt_ElseIf[$2, $4]; }
  302. ;
  303. else_single:
  304. /* empty */ { $$ = null; }
  305. | T_ELSE statement { $$ = Stmt_Else[toArray($2)]; }
  306. ;
  307. new_else_single:
  308. /* empty */ { $$ = null; }
  309. | T_ELSE ':' inner_statement_list { $$ = Stmt_Else[$3]; }
  310. ;
  311. foreach_variable:
  312. variable { $$ = array($1, false); }
  313. | '&' variable { $$ = array($2, true); }
  314. | list_expr { $$ = array($1, false); }
  315. ;
  316. parameter_list:
  317. non_empty_parameter_list { $$ = $1; }
  318. | /* empty */ { $$ = array(); }
  319. ;
  320. non_empty_parameter_list:
  321. parameter { init($1); }
  322. | non_empty_parameter_list ',' parameter { push($1, $3); }
  323. ;
  324. parameter:
  325. optional_class_type optional_ref T_VARIABLE
  326. { $$ = Param[parseVar($3), null, $1, $2]; }
  327. | optional_class_type optional_ref T_VARIABLE '=' static_scalar
  328. { $$ = Param[parseVar($3), $5, $1, $2]; }
  329. ;
  330. optional_class_type:
  331. /* empty */ { $$ = null; }
  332. | name { $$ = $1; }
  333. | T_ARRAY { $$ = 'array'; }
  334. | T_CALLABLE { $$ = 'callable'; }
  335. ;
  336. argument_list:
  337. '(' ')' { $$ = array(); }
  338. | '(' non_empty_argument_list ')' { $$ = $2; }
  339. | '(' yield_expr ')' { $$ = array(Arg[$2, false]); }
  340. ;
  341. non_empty_argument_list:
  342. argument { init($1); }
  343. | non_empty_argument_list ',' argument { push($1, $3); }
  344. ;
  345. argument:
  346. expr { $$ = Arg[$1, false]; }
  347. | '&' variable { $$ = Arg[$2, true]; }
  348. ;
  349. global_var_list:
  350. global_var_list ',' global_var { push($1, $3); }
  351. | global_var { init($1); }
  352. ;
  353. global_var:
  354. T_VARIABLE { $$ = Expr_Variable[parseVar($1)]; }
  355. | '$' variable { $$ = Expr_Variable[$2]; }
  356. | '$' '{' expr '}' { $$ = Expr_Variable[$3]; }
  357. ;
  358. static_var_list:
  359. static_var_list ',' static_var { push($1, $3); }
  360. | static_var { init($1); }
  361. ;
  362. static_var:
  363. T_VARIABLE { $$ = Stmt_StaticVar[parseVar($1), null]; }
  364. | T_VARIABLE '=' static_scalar { $$ = Stmt_StaticVar[parseVar($1), $3]; }
  365. ;
  366. class_statement_list:
  367. class_statement_list class_statement { push($1, $2); }
  368. | /* empty */ { init(); }
  369. ;
  370. class_statement:
  371. variable_modifiers property_declaration_list ';' { $$ = Stmt_Property[$1, $2]; }
  372. | T_CONST constant_declaration_list ';' { $$ = Stmt_ClassConst[$2]; }
  373. | method_modifiers T_FUNCTION optional_ref T_STRING '(' parameter_list ')' method_body
  374. { $$ = Stmt_ClassMethod[$4, [type: $1, byRef: $3, params: $6, stmts: $8]]; }
  375. | T_USE name_list trait_adaptations { $$ = Stmt_TraitUse[$2, $3]; }
  376. ;
  377. trait_adaptations:
  378. ';' { $$ = array(); }
  379. | '{' trait_adaptation_list '}' { $$ = $2; }
  380. ;
  381. trait_adaptation_list:
  382. /* empty */ { init(); }
  383. | trait_adaptation_list trait_adaptation { push($1, $2); }
  384. ;
  385. trait_adaptation:
  386. trait_method_reference_fully_qualified T_INSTEADOF name_list ';'
  387. { $$ = Stmt_TraitUseAdaptation_Precedence[$1[0], $1[1], $3]; }
  388. | trait_method_reference T_AS member_modifier T_STRING ';'
  389. { $$ = Stmt_TraitUseAdaptation_Alias[$1[0], $1[1], $3, $4]; }
  390. | trait_method_reference T_AS member_modifier ';'
  391. { $$ = Stmt_TraitUseAdaptation_Alias[$1[0], $1[1], $3, null]; }
  392. | trait_method_reference T_AS T_STRING ';'
  393. { $$ = Stmt_TraitUseAdaptation_Alias[$1[0], $1[1], null, $3]; }
  394. ;
  395. trait_method_reference_fully_qualified:
  396. name T_PAAMAYIM_NEKUDOTAYIM T_STRING { $$ = array($1, $3); }
  397. ;
  398. trait_method_reference:
  399. trait_method_reference_fully_qualified { $$ = $1; }
  400. | T_STRING { $$ = array(null, $1); }
  401. ;
  402. method_body:
  403. ';' /* abstract method */ { $$ = null; }
  404. | '{' inner_statement_list '}' { $$ = $2; }
  405. ;
  406. variable_modifiers:
  407. non_empty_member_modifiers { $$ = $1; }
  408. | T_VAR { $$ = Stmt_Class::MODIFIER_PUBLIC; }
  409. ;
  410. method_modifiers:
  411. /* empty */ { $$ = Stmt_Class::MODIFIER_PUBLIC; }
  412. | non_empty_member_modifiers { $$ = $1; }
  413. ;
  414. non_empty_member_modifiers:
  415. member_modifier { $$ = $1; }
  416. | non_empty_member_modifiers member_modifier { Stmt_Class::verifyModifier($1, $2); $$ = $1 | $2; }
  417. ;
  418. member_modifier:
  419. T_PUBLIC { $$ = Stmt_Class::MODIFIER_PUBLIC; }
  420. | T_PROTECTED { $$ = Stmt_Class::MODIFIER_PROTECTED; }
  421. | T_PRIVATE { $$ = Stmt_Class::MODIFIER_PRIVATE; }
  422. | T_STATIC { $$ = Stmt_Class::MODIFIER_STATIC; }
  423. | T_ABSTRACT { $$ = Stmt_Class::MODIFIER_ABSTRACT; }
  424. | T_FINAL { $$ = Stmt_Class::MODIFIER_FINAL; }
  425. ;
  426. property_declaration_list:
  427. property_declaration { init($1); }
  428. | property_declaration_list ',' property_declaration { push($1, $3); }
  429. ;
  430. property_declaration:
  431. T_VARIABLE { $$ = Stmt_PropertyProperty[parseVar($1), null]; }
  432. | T_VARIABLE '=' static_scalar { $$ = Stmt_PropertyProperty[parseVar($1), $3]; }
  433. ;
  434. expr_list:
  435. expr_list ',' expr { push($1, $3); }
  436. | expr { init($1); }
  437. ;
  438. for_expr:
  439. /* empty */ { $$ = array(); }
  440. | expr_list { $$ = $1; }
  441. ;
  442. expr:
  443. variable { $$ = $1; }
  444. | list_expr '=' expr { $$ = Expr_Assign[$1, $3]; }
  445. | variable '=' expr { $$ = Expr_Assign[$1, $3]; }
  446. | variable '=' '&' variable { $$ = Expr_AssignRef[$1, $4]; }
  447. | variable '=' '&' new_expr { $$ = Expr_AssignRef[$1, $4]; }
  448. | new_expr { $$ = $1; }
  449. | T_CLONE expr { $$ = Expr_Clone[$2]; }
  450. | variable T_PLUS_EQUAL expr { $$ = Expr_AssignPlus [$1, $3]; }
  451. | variable T_MINUS_EQUAL expr { $$ = Expr_AssignMinus [$1, $3]; }
  452. | variable T_MUL_EQUAL expr { $$ = Expr_AssignMul [$1, $3]; }
  453. | variable T_DIV_EQUAL expr { $$ = Expr_AssignDiv [$1, $3]; }
  454. | variable T_CONCAT_EQUAL expr { $$ = Expr_AssignConcat [$1, $3]; }
  455. | variable T_MOD_EQUAL expr { $$ = Expr_AssignMod [$1, $3]; }
  456. | variable T_AND_EQUAL expr { $$ = Expr_AssignBitwiseAnd[$1, $3]; }
  457. | variable T_OR_EQUAL expr { $$ = Expr_AssignBitwiseOr [$1, $3]; }
  458. | variable T_XOR_EQUAL expr { $$ = Expr_AssignBitwiseXor[$1, $3]; }
  459. | variable T_SL_EQUAL expr { $$ = Expr_AssignShiftLeft [$1, $3]; }
  460. | variable T_SR_EQUAL expr { $$ = Expr_AssignShiftRight[$1, $3]; }
  461. | variable T_INC { $$ = Expr_PostInc[$1]; }
  462. | T_INC variable { $$ = Expr_PreInc [$2]; }
  463. | variable T_DEC { $$ = Expr_PostDec[$1]; }
  464. | T_DEC variable { $$ = Expr_PreDec [$2]; }
  465. | expr T_BOOLEAN_OR expr { $$ = Expr_BooleanOr [$1, $3]; }
  466. | expr T_BOOLEAN_AND expr { $$ = Expr_BooleanAnd[$1, $3]; }
  467. | expr T_LOGICAL_OR expr { $$ = Expr_LogicalOr [$1, $3]; }
  468. | expr T_LOGICAL_AND expr { $$ = Expr_LogicalAnd[$1, $3]; }
  469. | expr T_LOGICAL_XOR expr { $$ = Expr_LogicalXor[$1, $3]; }
  470. | expr '|' expr { $$ = Expr_BitwiseOr [$1, $3]; }
  471. | expr '&' expr { $$ = Expr_BitwiseAnd[$1, $3]; }
  472. | expr '^' expr { $$ = Expr_BitwiseXor[$1, $3]; }
  473. | expr '.' expr { $$ = Expr_Concat [$1, $3]; }
  474. | expr '+' expr { $$ = Expr_Plus [$1, $3]; }
  475. | expr '-' expr { $$ = Expr_Minus [$1, $3]; }
  476. | expr '*' expr { $$ = Expr_Mul [$1, $3]; }
  477. | expr '/' expr { $$ = Expr_Div [$1, $3]; }
  478. | expr '%' expr { $$ = Expr_Mod [$1, $3]; }
  479. | expr T_SL expr { $$ = Expr_ShiftLeft [$1, $3]; }
  480. | expr T_SR expr { $$ = Expr_ShiftRight[$1, $3]; }
  481. | '+' expr %prec T_INC { $$ = Expr_UnaryPlus [$2]; }
  482. | '-' expr %prec T_INC { $$ = Expr_UnaryMinus[$2]; }
  483. | '!' expr { $$ = Expr_BooleanNot[$2]; }
  484. | '~' expr { $$ = Expr_BitwiseNot[$2]; }
  485. | expr T_IS_IDENTICAL expr { $$ = Expr_Identical [$1, $3]; }
  486. | expr T_IS_NOT_IDENTICAL expr { $$ = Expr_NotIdentical [$1, $3]; }
  487. | expr T_IS_EQUAL expr { $$ = Expr_Equal [$1, $3]; }
  488. | expr T_IS_NOT_EQUAL expr { $$ = Expr_NotEqual [$1, $3]; }
  489. | expr '<' expr { $$ = Expr_Smaller [$1, $3]; }
  490. | expr T_IS_SMALLER_OR_EQUAL expr { $$ = Expr_SmallerOrEqual[$1, $3]; }
  491. | expr '>' expr { $$ = Expr_Greater [$1, $3]; }
  492. | expr T_IS_GREATER_OR_EQUAL expr { $$ = Expr_GreaterOrEqual[$1, $3]; }
  493. | expr T_INSTANCEOF class_name_reference { $$ = Expr_Instanceof [$1, $3]; }
  494. | parentheses_expr { $$ = $1; }
  495. /* we need a separate '(' new_expr ')' rule to avoid problems caused by a s/r conflict */
  496. | '(' new_expr ')' { $$ = $2; }
  497. | expr '?' expr ':' expr { $$ = Expr_Ternary[$1, $3, $5]; }
  498. | expr '?' ':' expr { $$ = Expr_Ternary[$1, null, $4]; }
  499. | T_ISSET '(' variables_list ')' { $$ = Expr_Isset[$3]; }
  500. | T_EMPTY '(' expr ')' { $$ = Expr_Empty[$3]; }
  501. | T_INCLUDE expr { $$ = Expr_Include[$2, Expr_Include::TYPE_INCLUDE]; }
  502. | T_INCLUDE_ONCE expr { $$ = Expr_Include[$2, Expr_Include::TYPE_INCLUDE_ONCE]; }
  503. | T_EVAL parentheses_expr { $$ = Expr_Eval[$2]; }
  504. | T_REQUIRE expr { $$ = Expr_Include[$2, Expr_Include::TYPE_REQUIRE]; }
  505. | T_REQUIRE_ONCE expr { $$ = Expr_Include[$2, Expr_Include::TYPE_REQUIRE_ONCE]; }
  506. | T_INT_CAST expr { $$ = Expr_Cast_Int [$2]; }
  507. | T_DOUBLE_CAST expr { $$ = Expr_Cast_Double [$2]; }
  508. | T_STRING_CAST expr { $$ = Expr_Cast_String [$2]; }
  509. | T_ARRAY_CAST expr { $$ = Expr_Cast_Array [$2]; }
  510. | T_OBJECT_CAST expr { $$ = Expr_Cast_Object [$2]; }
  511. | T_BOOL_CAST expr { $$ = Expr_Cast_Bool [$2]; }
  512. | T_UNSET_CAST expr { $$ = Expr_Cast_Unset [$2]; }
  513. | T_EXIT exit_expr { $$ = Expr_Exit [$2]; }
  514. | '@' expr { $$ = Expr_ErrorSuppress[$2]; }
  515. | scalar { $$ = $1; }
  516. | array_expr { $$ = $1; }
  517. | scalar_dereference { $$ = $1; }
  518. | '`' backticks_expr '`' { $$ = Expr_ShellExec[$2]; }
  519. | T_PRINT expr { $$ = Expr_Print[$2]; }
  520. | T_YIELD { $$ = Expr_Yield[null, null]; }
  521. | T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars '{' inner_statement_list '}'
  522. { $$ = Expr_Closure[[static: false, byRef: $2, params: $4, uses: $6, stmts: $8]]; }
  523. | T_STATIC T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars '{' inner_statement_list '}'
  524. { $$ = Expr_Closure[[static: true, byRef: $3, params: $5, uses: $7, stmts: $9]]; }
  525. ;
  526. parentheses_expr:
  527. '(' expr ')' { $$ = $2; }
  528. | '(' yield_expr ')' { $$ = $2; }
  529. ;
  530. yield_expr:
  531. T_YIELD expr { $$ = Expr_Yield[$2, null]; }
  532. | T_YIELD expr T_DOUBLE_ARROW expr { $$ = Expr_Yield[$4, $2]; }
  533. ;
  534. array_expr:
  535. T_ARRAY '(' array_pair_list ')' { $$ = Expr_Array[$3]; }
  536. | '[' array_pair_list ']' { $$ = Expr_Array[$2]; }
  537. ;
  538. scalar_dereference:
  539. array_expr '[' dim_offset ']' { $$ = Expr_ArrayDimFetch[$1, $3]; }
  540. | T_CONSTANT_ENCAPSED_STRING '[' dim_offset ']'
  541. { $$ = Expr_ArrayDimFetch[Scalar_String[Scalar_String::parse($1)], $3]; }
  542. | scalar_dereference '[' dim_offset ']' { $$ = Expr_ArrayDimFetch[$1, $3]; }
  543. /* alternative array syntax missing intentionally */
  544. ;
  545. new_expr:
  546. T_NEW class_name_reference ctor_arguments { $$ = Expr_New[$2, $3]; }
  547. ;
  548. lexical_vars:
  549. /* empty */ { $$ = array(); }
  550. | T_USE '(' lexical_var_list ')' { $$ = $3; }
  551. ;
  552. lexical_var_list:
  553. lexical_var { init($1); }
  554. | lexical_var_list ',' lexical_var { push($1, $3); }
  555. ;
  556. lexical_var:
  557. optional_ref T_VARIABLE { $$ = Expr_ClosureUse[parseVar($2), $1]; }
  558. ;
  559. function_call:
  560. name argument_list { $$ = Expr_FuncCall[$1, $2]; }
  561. | class_name_or_var T_PAAMAYIM_NEKUDOTAYIM T_STRING argument_list
  562. { $$ = Expr_StaticCall[$1, $3, $4]; }
  563. | class_name_or_var T_PAAMAYIM_NEKUDOTAYIM '{' expr '}' argument_list
  564. { $$ = Expr_StaticCall[$1, $4, $6]; }
  565. | static_property argument_list {
  566. if ($1 instanceof PHPParser_Node_Expr_StaticPropertyFetch) {
  567. $$ = Expr_StaticCall[$1->class, Expr_Variable[$1->name], $2];
  568. } elseif ($1 instanceof PHPParser_Node_Expr_ArrayDimFetch) {
  569. $tmp = $1;
  570. while ($tmp->var instanceof PHPParser_Node_Expr_ArrayDimFetch) {
  571. $tmp = $tmp->var;
  572. }
  573. $$ = Expr_StaticCall[$tmp->var->class, $1, $2];
  574. $tmp->var = Expr_Variable[$tmp->var->name];
  575. } else {
  576. throw new Exception;
  577. }
  578. }
  579. | variable_without_objects argument_list
  580. { $$ = Expr_FuncCall[$1, $2]; }
  581. | function_call '[' dim_offset ']' { $$ = Expr_ArrayDimFetch[$1, $3]; }
  582. /* alternative array syntax missing intentionally */
  583. ;
  584. class_name:
  585. T_STATIC { $$ = Name['static']; }
  586. | name { $$ = $1; }
  587. ;
  588. name:
  589. namespace_name { $$ = Name[$1]; }
  590. | T_NS_SEPARATOR namespace_name { $$ = Name_FullyQualified[$2]; }
  591. | T_NAMESPACE T_NS_SEPARATOR namespace_name { $$ = Name_Relative[$3]; }
  592. ;
  593. class_name_reference:
  594. class_name { $$ = $1; }
  595. | dynamic_class_name_reference { $$ = $1; }
  596. ;
  597. dynamic_class_name_reference:
  598. object_access_for_dcnr { $$ = $1; }
  599. | base_variable { $$ = $1; }
  600. ;
  601. class_name_or_var:
  602. class_name { $$ = $1; }
  603. | reference_variable { $$ = $1; }
  604. ;
  605. object_access_for_dcnr:
  606. | base_variable T_OBJECT_OPERATOR object_property
  607. { $$ = Expr_PropertyFetch[$1, $3]; }
  608. | object_access_for_dcnr T_OBJECT_OPERATOR object_property
  609. { $$ = Expr_PropertyFetch[$1, $3]; }
  610. | object_access_for_dcnr '[' dim_offset ']' { $$ = Expr_ArrayDimFetch[$1, $3]; }
  611. | object_access_for_dcnr '{' expr '}' { $$ = Expr_ArrayDimFetch[$1, $3]; }
  612. ;
  613. exit_expr:
  614. /* empty */ { $$ = null; }
  615. | '(' ')' { $$ = null; }
  616. | parentheses_expr { $$ = $1; }
  617. ;
  618. backticks_expr:
  619. /* empty */ { $$ = array(); }
  620. | T_ENCAPSED_AND_WHITESPACE { $$ = array(Scalar_String::parseEscapeSequences($1, '`')); }
  621. | encaps_list { parseEncapsed($1, '`'); $$ = $1; }
  622. ;
  623. ctor_arguments:
  624. /* empty */ { $$ = array(); }
  625. | argument_list { $$ = $1; }
  626. ;
  627. common_scalar:
  628. T_LNUMBER { $$ = Scalar_LNumber[Scalar_LNumber::parse($1)]; }
  629. | T_DNUMBER { $$ = Scalar_DNumber[Scalar_DNumber::parse($1)]; }
  630. | T_CONSTANT_ENCAPSED_STRING { $$ = Scalar_String[Scalar_String::parse($1)]; }
  631. | T_LINE { $$ = Scalar_LineConst[]; }
  632. | T_FILE { $$ = Scalar_FileConst[]; }
  633. | T_DIR { $$ = Scalar_DirConst[]; }
  634. | T_CLASS_C { $$ = Scalar_ClassConst[]; }
  635. | T_TRAIT_C { $$ = Scalar_TraitConst[]; }
  636. | T_METHOD_C { $$ = Scalar_MethodConst[]; }
  637. | T_FUNC_C { $$ = Scalar_FuncConst[]; }
  638. | T_NS_C { $$ = Scalar_NSConst[]; }
  639. | T_START_HEREDOC T_ENCAPSED_AND_WHITESPACE T_END_HEREDOC
  640. { $$ = Scalar_String[Scalar_String::parseDocString($1, $2)]; }
  641. | T_START_HEREDOC T_END_HEREDOC
  642. { $$ = Scalar_String['']; }
  643. | name { $$ = Expr_ConstFetch[$1]; }
  644. ;
  645. static_scalar: /* compile-time evaluated scalars */
  646. common_scalar { $$ = $1; }
  647. | class_name T_PAAMAYIM_NEKUDOTAYIM class_const_name { $$ = Expr_ClassConstFetch[$1, $3]; }
  648. | '+' static_scalar { $$ = Expr_UnaryPlus[$2]; }
  649. | '-' static_scalar { $$ = Expr_UnaryMinus[$2]; }
  650. | T_ARRAY '(' static_array_pair_list ')' { $$ = Expr_Array[$3]; }
  651. | '[' static_array_pair_list ']' { $$ = Expr_Array[$2]; }
  652. ;
  653. scalar:
  654. common_scalar { $$ = $1; }
  655. | class_name_or_var T_PAAMAYIM_NEKUDOTAYIM class_const_name
  656. { $$ = Expr_ClassConstFetch[$1, $3]; }
  657. | '"' encaps_list '"'
  658. { parseEncapsed($2, '"'); $$ = Scalar_Encapsed[$2]; }
  659. | T_START_HEREDOC encaps_list T_END_HEREDOC
  660. { parseEncapsedDoc($2); $$ = Scalar_Encapsed[$2]; }
  661. ;
  662. class_const_name:
  663. T_STRING { $$ = $1; }
  664. | T_CLASS { $$ = 'class'; }
  665. ;
  666. static_array_pair_list:
  667. /* empty */ { $$ = array(); }
  668. | non_empty_static_array_pair_list optional_comma { $$ = $1; }
  669. ;
  670. optional_comma:
  671. /* empty */
  672. | ','
  673. ;
  674. non_empty_static_array_pair_list:
  675. non_empty_static_array_pair_list ',' static_array_pair { push($1, $3); }
  676. | static_array_pair { init($1); }
  677. ;
  678. static_array_pair:
  679. static_scalar T_DOUBLE_ARROW static_scalar { $$ = Expr_ArrayItem[$3, $1, false]; }
  680. | static_scalar { $$ = Expr_ArrayItem[$1, null, false]; }
  681. ;
  682. variable:
  683. object_access { $$ = $1; }
  684. | base_variable { $$ = $1; }
  685. | function_call { $$ = $1; }
  686. | new_expr_array_deref { $$ = $1; }
  687. ;
  688. new_expr_array_deref:
  689. '(' new_expr ')' '[' dim_offset ']' { $$ = Expr_ArrayDimFetch[$2, $5]; }
  690. | new_expr_array_deref '[' dim_offset ']' { $$ = Expr_ArrayDimFetch[$1, $3]; }
  691. /* alternative array syntax missing intentionally */
  692. ;
  693. object_access:
  694. variable_or_new_expr T_OBJECT_OPERATOR object_property
  695. { $$ = Expr_PropertyFetch[$1, $3]; }
  696. | variable_or_new_expr T_OBJECT_OPERATOR object_property argument_list
  697. { $$ = Expr_MethodCall[$1, $3, $4]; }
  698. | object_access argument_list { $$ = Expr_FuncCall[$1, $2]; }
  699. | object_access '[' dim_offset ']' { $$ = Expr_ArrayDimFetch[$1, $3]; }
  700. | object_access '{' expr '}' { $$ = Expr_ArrayDimFetch[$1, $3]; }
  701. ;
  702. variable_or_new_expr:
  703. variable { $$ = $1; }
  704. | '(' new_expr ')' { $$ = $2; }
  705. ;
  706. variable_without_objects:
  707. reference_variable { $$ = $1; }
  708. | '$' variable_without_objects { $$ = Expr_Variable[$2]; }
  709. ;
  710. base_variable:
  711. variable_without_objects { $$ = $1; }
  712. | static_property { $$ = $1; }
  713. ;
  714. static_property:
  715. class_name_or_var T_PAAMAYIM_NEKUDOTAYIM '$' reference_variable
  716. { $$ = Expr_StaticPropertyFetch[$1, $4]; }
  717. | static_property_with_arrays { $$ = $1; }
  718. ;
  719. static_property_with_arrays:
  720. class_name_or_var T_PAAMAYIM_NEKUDOTAYIM T_VARIABLE
  721. { $$ = Expr_StaticPropertyFetch[$1, parseVar($3)]; }
  722. | class_name_or_var T_PAAMAYIM_NEKUDOTAYIM '$' '{' expr '}'
  723. { $$ = Expr_StaticPropertyFetch[$1, $5]; }
  724. | static_property_with_arrays '[' dim_offset ']' { $$ = Expr_ArrayDimFetch[$1, $3]; }
  725. | static_property_with_arrays '{' expr '}' { $$ = Expr_ArrayDimFetch[$1, $3]; }
  726. ;
  727. reference_variable:
  728. reference_variable '[' dim_offset ']' { $$ = Expr_ArrayDimFetch[$1, $3]; }
  729. | reference_variable '{' expr '}' { $$ = Expr_ArrayDimFetch[$1, $3]; }
  730. | T_VARIABLE { $$ = Expr_Variable[parseVar($1)]; }
  731. | '$' '{' expr '}' { $$ = Expr_Variable[$3]; }
  732. ;
  733. dim_offset:
  734. /* empty */ { $$ = null; }
  735. | expr { $$ = $1; }
  736. ;
  737. object_property:
  738. T_STRING { $$ = $1; }
  739. | '{' expr '}' { $$ = $2; }
  740. | variable_without_objects { $$ = $1; }
  741. ;
  742. list_expr:
  743. T_LIST '(' list_expr_elements ')' { $$ = Expr_List[$3]; }
  744. ;
  745. list_expr_elements:
  746. list_expr_elements ',' list_expr_element { push($1, $3); }
  747. | list_expr_element { init($1); }
  748. ;
  749. list_expr_element:
  750. variable { $$ = $1; }
  751. | list_expr { $$ = $1; }
  752. | /* empty */ { $$ = null; }
  753. ;
  754. array_pair_list:
  755. /* empty */ { $$ = array(); }
  756. | non_empty_array_pair_list optional_comma { $$ = $1; }
  757. ;
  758. non_empty_array_pair_list:
  759. non_empty_array_pair_list ',' array_pair { push($1, $3); }
  760. | array_pair { init($1); }
  761. ;
  762. array_pair:
  763. expr T_DOUBLE_ARROW expr { $$ = Expr_ArrayItem[$3, $1, false]; }
  764. | expr { $$ = Expr_ArrayItem[$1, null, false]; }
  765. | expr T_DOUBLE_ARROW '&' variable { $$ = Expr_ArrayItem[$4, $1, true]; }
  766. | '&' variable { $$ = Expr_ArrayItem[$2, null, true]; }
  767. ;
  768. encaps_list:
  769. encaps_list encaps_var { push($1, $2); }
  770. | encaps_list T_ENCAPSED_AND_WHITESPACE { push($1, $2); }
  771. | encaps_var { init($1); }
  772. | T_ENCAPSED_AND_WHITESPACE encaps_var { init($1, $2); }
  773. ;
  774. encaps_var:
  775. T_VARIABLE { $$ = Expr_Variable[parseVar($1)]; }
  776. | T_VARIABLE '[' encaps_var_offset ']' { $$ = Expr_ArrayDimFetch[Expr_Variable[parseVar($1)], $3]; }
  777. | T_VARIABLE T_OBJECT_OPERATOR T_STRING { $$ = Expr_PropertyFetch[Expr_Variable[parseVar($1)], $3]; }
  778. | T_DOLLAR_OPEN_CURLY_BRACES expr '}' { $$ = Expr_Variable[$2]; }
  779. | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '}' { $$ = Expr_Variable[$2]; }
  780. | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}'
  781. { $$ = Expr_ArrayDimFetch[Expr_Variable[$2], $4]; }
  782. | T_CURLY_OPEN variable '}' { $$ = $2; }
  783. ;
  784. encaps_var_offset:
  785. T_STRING { $$ = Scalar_String[$1]; }
  786. | T_NUM_STRING { $$ = Scalar_String[$1]; }
  787. | T_VARIABLE { $$ = Expr_Variable[parseVar($1)]; }
  788. ;
  789. %%