parse37.py 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780
  1. # Copyright (c) 2017-2020, 2022-2024 Rocky Bernstein
  2. #
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. """
  16. Python 3.7 grammar for the spark Earley-algorithm parser.
  17. """
  18. from __future__ import print_function
  19. from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
  20. from uncompyle6.parser import PythonParserSingle, nop_func
  21. from uncompyle6.parsers.parse37base import Python37BaseParser
  22. from uncompyle6.scanners.tok import Token
  23. class Python37Parser(Python37BaseParser):
  24. def __init__(self, debug_parser=PARSER_DEFAULT_DEBUG):
  25. super(Python37Parser, self).__init__(debug_parser)
  26. self.customized = {}
  27. ###############################################
  28. # Python 3.7 grammar rules
  29. ###############################################
  30. def p_start(self, args):
  31. """
  32. # The start or goal symbol
  33. stmts ::= sstmt+
  34. """
  35. def p_call_stmt(self, args):
  36. """
  37. # eval-mode compilation. Single-mode interactive compilation
  38. # adds another rule.
  39. call_stmt ::= expr POP_TOP
  40. """
  41. def p_eval_mode(self, args):
  42. """
  43. # eval-mode compilation. Single-mode interactive compilation
  44. # adds another rule.
  45. expr_stmt ::= expr POP_TOP
  46. """
  47. def p_stmt(self, args):
  48. """
  49. pass ::=
  50. _stmts ::= stmt+
  51. # statements with continue and break
  52. c_stmts ::= _stmts
  53. c_stmts ::= _stmts lastc_stmt
  54. c_stmts ::= lastc_stmt
  55. c_stmts ::= continues
  56. ending_return ::= RETURN_VALUE RETURN_LAST
  57. ending_return ::= RETURN_VALUE_LAMBDA LAMBDA_MARKER
  58. lastc_stmt ::= iflaststmt
  59. lastc_stmt ::= forelselaststmt
  60. lastc_stmt ::= ifelsestmtc
  61. # Statements in a loop
  62. lstmt ::= stmt
  63. l_stmts ::= lstmt+
  64. c_stmts_opt ::= c_stmts
  65. c_stmts_opt ::= pass
  66. # statements inside a loop
  67. l_stmts ::= _stmts
  68. l_stmts ::= returns
  69. l_stmts ::= continues
  70. l_stmts ::= _stmts lastl_stmt
  71. l_stmts ::= lastl_stmt
  72. lastl_stmt ::= iflaststmtl
  73. lastl_stmt ::= ifelsestmtl
  74. lastl_stmt ::= forelselaststmtl
  75. lastl_stmt ::= tryelsestmtl
  76. l_stmts_opt ::= l_stmts
  77. l_stmts_opt ::= pass
  78. suite_stmts ::= _stmts
  79. suite_stmts ::= returns
  80. suite_stmts ::= continues
  81. suite_stmts_opt ::= suite_stmts
  82. # passtmt is needed for semantic actions to add "pass"
  83. suite_stmts_opt ::= pass
  84. else_suite ::= suite_stmts
  85. else_suitel ::= l_stmts
  86. else_suitec ::= c_stmts
  87. else_suitec ::= returns
  88. else_suite_opt ::= else_suite
  89. else_suite_opt ::= pass
  90. stmt ::= classdef
  91. stmt ::= expr_stmt
  92. stmt ::= call_stmt
  93. stmt ::= ifstmt
  94. stmt ::= ifelsestmt
  95. stmt ::= whilestmt
  96. stmt ::= while1stmt
  97. stmt ::= whileelsestmt
  98. stmt ::= while1elsestmt
  99. stmt ::= for
  100. stmt ::= forelsestmt
  101. stmt ::= try_except
  102. stmt ::= tryelsestmt
  103. stmt ::= tryfinallystmt
  104. stmt ::= delete
  105. delete ::= DELETE_FAST
  106. delete ::= DELETE_NAME
  107. delete ::= DELETE_GLOBAL
  108. stmt ::= return
  109. return ::= return_expr RETURN_VALUE
  110. # "returns" nonterminal is a sequence of statements that ends in a
  111. # RETURN statement.
  112. # In later Python versions with jump optimization, this can cause JUMPs
  113. # that would normally appear to be omitted.
  114. returns ::= return
  115. returns ::= _stmts return
  116. stmt ::= genexpr_func
  117. genexpr_func ::= LOAD_ARG _come_froms FOR_ITER store comp_iter
  118. _come_froms JUMP_BACK _come_froms
  119. """
  120. pass
  121. def p_expr(self, args):
  122. """
  123. expr ::= LOAD_CODE
  124. expr ::= LOAD_CONST
  125. expr ::= LOAD_DEREF
  126. expr ::= LOAD_FAST
  127. expr ::= LOAD_GLOBAL
  128. expr ::= LOAD_NAME
  129. expr ::= LOAD_STR
  130. expr ::= _lambda_body
  131. expr ::= and
  132. expr ::= attribute37
  133. expr ::= bin_op
  134. expr ::= call
  135. expr ::= compare
  136. expr ::= dict
  137. expr ::= generator_exp
  138. expr ::= list
  139. expr ::= or
  140. expr ::= subscript
  141. expr ::= subscript2
  142. expr ::= unary_not
  143. expr ::= unary_op
  144. expr ::= yield
  145. # bin_op (formerly "binary_expr") is the Python AST BinOp
  146. bin_op ::= expr expr binary_operator
  147. binary_operator ::= BINARY_ADD
  148. binary_operator ::= BINARY_MULTIPLY
  149. binary_operator ::= BINARY_AND
  150. binary_operator ::= BINARY_OR
  151. binary_operator ::= BINARY_XOR
  152. binary_operator ::= BINARY_SUBTRACT
  153. binary_operator ::= BINARY_TRUE_DIVIDE
  154. binary_operator ::= BINARY_FLOOR_DIVIDE
  155. binary_operator ::= BINARY_MODULO
  156. binary_operator ::= BINARY_LSHIFT
  157. binary_operator ::= BINARY_RSHIFT
  158. binary_operator ::= BINARY_POWER
  159. # unary_op (formerly "unary_expr") is the Python AST UnaryOp
  160. unary_op ::= expr unary_operator
  161. unary_operator ::= UNARY_POSITIVE
  162. unary_operator ::= UNARY_NEGATIVE
  163. unary_operator ::= UNARY_INVERT
  164. unary_not ::= expr UNARY_NOT
  165. subscript ::= expr expr BINARY_SUBSCR
  166. get_iter ::= expr GET_ITER
  167. yield ::= expr YIELD_VALUE
  168. _lambda_body ::= lambda_body
  169. expr ::= if_exp
  170. return_expr ::= expr
  171. return_expr ::= ret_and
  172. return_expr ::= ret_or
  173. return_expr_or_cond ::= return_expr
  174. return_expr_or_cond ::= if_exp_ret
  175. stmt ::= return_expr_lambda
  176. return_expr_lambda ::= return_expr RETURN_VALUE_LAMBDA LAMBDA_MARKER
  177. return_expr_lambda ::= return_expr RETURN_VALUE_LAMBDA
  178. compare ::= compare_chained
  179. compare ::= compare_single
  180. compare_single ::= expr expr COMPARE_OP
  181. # A compare_chained is two comparisons like x <= y <= z
  182. compare_chained ::= expr compared_chained_middle ROT_TWO POP_TOP _come_froms
  183. compare_chained_right ::= expr COMPARE_OP JUMP_FORWARD
  184. # Non-null kvlist items are broken out in the individual grammars
  185. kvlist ::=
  186. # Positional arguments in make_function
  187. pos_arg ::= expr
  188. """
  189. def p_function_def(self, args):
  190. """
  191. stmt ::= function_def
  192. function_def ::= mkfunc store
  193. stmt ::= function_def_deco
  194. function_def_deco ::= mkfuncdeco store
  195. mkfuncdeco ::= expr mkfuncdeco CALL_FUNCTION_1
  196. mkfuncdeco ::= expr mkfuncdeco0 CALL_FUNCTION_1
  197. mkfuncdeco0 ::= mkfunc
  198. load_closure ::= load_closure LOAD_CLOSURE
  199. load_closure ::= LOAD_CLOSURE
  200. """
  201. def p_generator_exp(self, args):
  202. """ """
  203. def p_jump(self, args):
  204. """
  205. _jump ::= JUMP_ABSOLUTE
  206. _jump ::= JUMP_FORWARD
  207. _jump ::= JUMP_BACK
  208. # Zero or more COME_FROMs - loops can have this
  209. _come_froms ::= COME_FROM*
  210. _come_froms ::= _come_froms COME_FROM_LOOP
  211. # One or more COME_FROMs - joins of tryelse's have this
  212. come_froms ::= COME_FROM+
  213. # Zero or one COME_FROM
  214. # And/or expressions have this
  215. come_from_opt ::= COME_FROM?
  216. """
  217. def p_augmented_assign(self, args):
  218. """
  219. stmt ::= aug_assign1
  220. stmt ::= aug_assign2
  221. # This is odd in that other aug_assign1's have only 3 slots
  222. # The store isn't used as that's supposed to be also
  223. # indicated in the first expr
  224. aug_assign1 ::= expr expr
  225. inplace_op store
  226. aug_assign1 ::= expr expr
  227. inplace_op ROT_THREE STORE_SUBSCR
  228. aug_assign2 ::= expr DUP_TOP LOAD_ATTR expr
  229. inplace_op ROT_TWO STORE_ATTR
  230. inplace_op ::= INPLACE_ADD
  231. inplace_op ::= INPLACE_SUBTRACT
  232. inplace_op ::= INPLACE_MULTIPLY
  233. inplace_op ::= INPLACE_TRUE_DIVIDE
  234. inplace_op ::= INPLACE_FLOOR_DIVIDE
  235. inplace_op ::= INPLACE_MODULO
  236. inplace_op ::= INPLACE_POWER
  237. inplace_op ::= INPLACE_LSHIFT
  238. inplace_op ::= INPLACE_RSHIFT
  239. inplace_op ::= INPLACE_AND
  240. inplace_op ::= INPLACE_XOR
  241. inplace_op ::= INPLACE_OR
  242. """
  243. def p_assign(self, args):
  244. """
  245. stmt ::= assign
  246. assign ::= expr DUP_TOP designList
  247. assign ::= expr store
  248. stmt ::= assign2
  249. stmt ::= assign3
  250. assign2 ::= expr expr ROT_TWO store store
  251. assign3 ::= expr expr expr ROT_THREE ROT_TWO store store store
  252. """
  253. def p_forstmt(self, args):
  254. """
  255. get_for_iter ::= GET_ITER _come_froms FOR_ITER
  256. for_block ::= l_stmts_opt _come_froms JUMP_BACK
  257. forelsestmt ::= SETUP_LOOP expr get_for_iter store
  258. for_block POP_BLOCK else_suite _come_froms
  259. forelselaststmt ::= SETUP_LOOP expr get_for_iter store
  260. for_block POP_BLOCK else_suitec _come_froms
  261. forelselaststmtl ::= SETUP_LOOP expr get_for_iter store
  262. for_block POP_BLOCK else_suitel _come_froms
  263. """
  264. def p_import20(self, args):
  265. """
  266. stmt ::= import
  267. stmt ::= import_from
  268. stmt ::= import_from_star
  269. stmt ::= importmultiple
  270. importlist ::= importlist alias
  271. importlist ::= alias
  272. alias ::= IMPORT_NAME store
  273. alias ::= IMPORT_FROM store
  274. alias ::= IMPORT_NAME attributes store
  275. import ::= LOAD_CONST LOAD_CONST alias
  276. import_from_star ::= LOAD_CONST LOAD_CONST IMPORT_NAME IMPORT_STAR
  277. import_from_star ::= LOAD_CONST LOAD_CONST IMPORT_NAME_ATTR IMPORT_STAR
  278. import_from ::= LOAD_CONST LOAD_CONST IMPORT_NAME importlist POP_TOP
  279. importmultiple ::= LOAD_CONST LOAD_CONST alias imports_cont
  280. imports_cont ::= import_cont+
  281. import_cont ::= LOAD_CONST LOAD_CONST alias
  282. attributes ::= LOAD_ATTR+
  283. """
  284. def p_import37(self, args):
  285. """
  286. # The 3.7base scanner adds IMPORT_NAME_ATTR
  287. alias ::= IMPORT_NAME_ATTR attributes store
  288. alias ::= IMPORT_NAME_ATTR store
  289. alias37 ::= IMPORT_NAME store
  290. alias37 ::= IMPORT_FROM store
  291. attribute37 ::= expr LOAD_METHOD
  292. import_as37 ::= LOAD_CONST LOAD_CONST importlist37 store POP_TOP
  293. import_from ::= LOAD_CONST LOAD_CONST importlist POP_TOP
  294. import_from37 ::= LOAD_CONST LOAD_CONST IMPORT_NAME_ATTR importlist37 POP_TOP
  295. import_from_as37 ::= LOAD_CONST LOAD_CONST import_from_attr37 store POP_TOP
  296. # A single entry in a dotted import a.b.c.d
  297. import_one ::= importlists ROT_TWO IMPORT_FROM
  298. import_one ::= importlists ROT_TWO POP_TOP IMPORT_FROM
  299. # Semantic checks distinguish importattr37 from import_from_attr37
  300. # in the former the "from" slot in a prior LOAD_CONST is null.
  301. # Used in: import .. as ..
  302. importattr37 ::= IMPORT_NAME_ATTR IMPORT_FROM
  303. # Used in: from xx import .. as ..
  304. import_from_attr37 ::= IMPORT_NAME_ATTR IMPORT_FROM
  305. importlist37 ::= import_one
  306. importlist37 ::= importattr37
  307. importlist37 ::= alias37+
  308. importlists ::= importlist37+
  309. stmt ::= import_as37
  310. stmt ::= import_from37
  311. stmt ::= import_from_as37
  312. """
  313. def p_list_comprehension(self, args):
  314. """
  315. expr ::= list_comp
  316. list_iter ::= list_for
  317. list_iter ::= list_if
  318. list_iter ::= list_if_not
  319. list_iter ::= lc_body
  320. list_if ::= expr jmp_false list_iter
  321. list_if_not ::= expr jmp_true list_iter
  322. """
  323. def p_gen_comp37(self, args):
  324. """
  325. comp_iter ::= comp_for
  326. comp_body ::= gen_comp_body
  327. gen_comp_body ::= expr YIELD_VALUE POP_TOP
  328. comp_if ::= expr jmp_false comp_iter
  329. """
  330. def p_store(self, args):
  331. """
  332. # Note. The below is right-recursive:
  333. designList ::= store store
  334. designList ::= store DUP_TOP designList
  335. ## Can we replace with left-recursive, and redo with:
  336. ##
  337. ## designList ::= designLists store store
  338. ## designLists ::= designLists store DUP_TOP
  339. ## designLists ::=
  340. ## Will need to redo semantic actiion
  341. store ::= STORE_FAST
  342. store ::= STORE_NAME
  343. store ::= STORE_GLOBAL
  344. store ::= STORE_DEREF
  345. store ::= expr STORE_ATTR
  346. store ::= store_subscript
  347. store_subscript ::= expr expr STORE_SUBSCR
  348. store ::= unpack
  349. """
  350. def p_32on(self, args):
  351. """
  352. if_exp::= expr jmp_false expr jump_forward_else expr COME_FROM
  353. # compare_chained_right is used in a "chained_compare": x <= y <= z
  354. # used exclusively in compare_chained
  355. compare_chained_right ::= expr COMPARE_OP RETURN_VALUE
  356. compare_chained_right ::= expr COMPARE_OP RETURN_VALUE_LAMBDA
  357. # Python < 3.5 no POP BLOCK
  358. whileTruestmt ::= SETUP_LOOP l_stmts_opt JUMP_BACK COME_FROM_LOOP
  359. # Python 3.5+ has jump optimization to remove the redundant
  360. # jump_excepts. But in 3.3 we need them added
  361. except_handler ::= JUMP_FORWARD COME_FROM_EXCEPT except_stmts
  362. END_FINALLY
  363. tryelsestmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
  364. except_handler else_suite
  365. jump_excepts come_from_except_clauses
  366. jump_excepts ::= jump_except+
  367. subscript2 ::= expr expr DUP_TOP_TWO BINARY_SUBSCR
  368. # FIXME: The below rule was in uncompyle6.
  369. # In decompyle6 though "_ifstmts_jump" is part of an "ifstmt"
  370. # where as the below rule is appropriate for an "ifelsesmt"
  371. # Investigate and reconcile
  372. # _ifstmts_jump ::= c_stmts_opt JUMP_FORWARD _come_froms
  373. kv3 ::= expr expr STORE_MAP
  374. """
  375. return
  376. def p_33on(self, args):
  377. """
  378. # Python 3.3+ adds yield from.
  379. expr ::= yield_from
  380. yield_from ::= expr GET_YIELD_FROM_ITER LOAD_CONST YIELD_FROM
  381. # We do the grammar hackery below for semantics
  382. # actions that want c_stmts_opt at index 1
  383. # Python 3.5+ has jump optimization to remove the redundant
  384. # jump_excepts. But in 3.3 we need them added
  385. try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
  386. except_handler
  387. jump_excepts come_from_except_clauses
  388. """
  389. def p_34on(self, args):
  390. """
  391. whilestmt ::= setup_loop testexpr returns come_froms POP_BLOCK COME_FROM_LOOP
  392. # Seems to be needed starting 3.4.4 or so
  393. while1stmt ::= setup_loop l_stmts
  394. COME_FROM JUMP_BACK POP_BLOCK COME_FROM_LOOP
  395. while1stmt ::= setup_loop l_stmts
  396. POP_BLOCK COME_FROM_LOOP
  397. # FIXME the below masks a bug in not detecting COME_FROM_LOOP
  398. # grammar rules with COME_FROM -> COME_FROM_LOOP already exist
  399. whileelsestmt ::= setup_loop testexpr l_stmts_opt JUMP_BACK POP_BLOCK
  400. else_suitel COME_FROM
  401. while1elsestmt ::= setup_loop l_stmts JUMP_BACK _come_froms POP_BLOCK else_suitel
  402. COME_FROM_LOOP
  403. # Python 3.4+ optimizes the trailing two JUMPS away
  404. _ifstmts_jump ::= c_stmts_opt JUMP_ABSOLUTE JUMP_FORWARD _come_froms
  405. """
  406. def p_35_on(self, args):
  407. """
  408. while1elsestmt ::= setup_loop l_stmts JUMP_BACK
  409. POP_BLOCK else_suite COME_FROM_LOOP
  410. # The following rule is for Python 3.5+ where we can have stuff like
  411. # while ..
  412. # if
  413. # ...
  414. # the end of the if will jump back to the loop and there will be a COME_FROM
  415. # after the jump
  416. l_stmts ::= lastl_stmt come_froms l_stmts
  417. # Python 3.5+ Await statement
  418. expr ::= await_expr
  419. await_expr ::= expr GET_AWAITABLE LOAD_CONST YIELD_FROM
  420. stmt ::= await_stmt
  421. await_stmt ::= await_expr POP_TOP
  422. # Python 3.5+ async additions
  423. inplace_op ::= INPLACE_MATRIX_MULTIPLY
  424. binary_operator ::= BINARY_MATRIX_MULTIPLY
  425. # Python 3.5+ does jump optimization
  426. # In <.3.5 the below is a JUMP_FORWARD to a JUMP_ABSOLUTE.
  427. return_if_stmt ::= return_expr RETURN_END_IF POP_BLOCK
  428. return_if_lambda ::= RETURN_END_IF_LAMBDA COME_FROM
  429. jb_else ::= JUMP_BACK ELSE
  430. jb_else ::= JUMP_BACK COME_FROM
  431. ifelsestmtc ::= testexpr c_stmts_opt JUMP_FORWARD else_suitec
  432. ifelsestmtl ::= testexpr c_stmts_opt jb_else else_suitel
  433. # We want to keep the positions of the "then" and
  434. # "else" statements in "ifelstmtl" similar to others of this ilk.
  435. testexpr_cf ::= testexpr come_froms
  436. ifelsestmtl ::= testexpr_cf c_stmts_opt jb_else else_suitel
  437. # 3.5 Has jump optimization which can route the end of an
  438. # "if/then" back to a loop just before an else.
  439. jump_absolute_else ::= jb_else
  440. jump_absolute_else ::= CONTINUE ELSE
  441. # Our hacky "ELSE" determination doesn't do a good job and really
  442. # determine the start of an "else". It could also be the end of an
  443. # "if-then" which ends in a "continue". Perhaps with real control-flow
  444. # analysis we'll sort this out. Or call "ELSE" something more appropriate.
  445. _ifstmts_jump ::= c_stmts_opt ELSE
  446. # ifstmt ::= testexpr c_stmts_opt
  447. iflaststmt ::= testexpr c_stmts_opt JUMP_FORWARD
  448. """
  449. def p_37_async(self, args):
  450. """
  451. stmt ::= async_for_stmt37
  452. stmt ::= async_for_stmt
  453. stmt ::= async_forelse_stmt
  454. async_for_stmt ::= setup_loop expr
  455. GET_AITER
  456. SETUP_EXCEPT GET_ANEXT LOAD_CONST
  457. YIELD_FROM
  458. store
  459. POP_BLOCK JUMP_FORWARD COME_FROM_EXCEPT DUP_TOP
  460. LOAD_GLOBAL COMPARE_OP POP_JUMP_IF_TRUE
  461. END_FINALLY COME_FROM
  462. for_block
  463. COME_FROM
  464. POP_TOP POP_TOP POP_TOP POP_EXCEPT POP_TOP POP_BLOCK
  465. COME_FROM_LOOP
  466. # Order of LOAD_CONST YIELD_FROM is switched from 3.6 to save a LOAD_CONST
  467. async_for_stmt37 ::= setup_loop expr
  468. GET_AITER
  469. _come_froms
  470. SETUP_EXCEPT GET_ANEXT
  471. LOAD_CONST YIELD_FROM
  472. store
  473. POP_BLOCK JUMP_BACK COME_FROM_EXCEPT DUP_TOP
  474. LOAD_GLOBAL COMPARE_OP POP_JUMP_IF_TRUE
  475. END_FINALLY for_block COME_FROM
  476. POP_TOP POP_TOP POP_TOP POP_EXCEPT
  477. POP_TOP POP_BLOCK
  478. COME_FROM_LOOP
  479. async_forelse_stmt ::= setup_loop expr
  480. GET_AITER
  481. _come_froms
  482. SETUP_EXCEPT GET_ANEXT LOAD_CONST
  483. YIELD_FROM
  484. store
  485. POP_BLOCK JUMP_FORWARD COME_FROM_EXCEPT DUP_TOP
  486. LOAD_GLOBAL COMPARE_OP POP_JUMP_IF_TRUE
  487. END_FINALLY COME_FROM
  488. for_block
  489. COME_FROM
  490. POP_TOP POP_TOP POP_TOP POP_EXCEPT POP_TOP POP_BLOCK
  491. else_suite COME_FROM_LOOP
  492. """
  493. def p_37_chained(self, args):
  494. """
  495. testtrue ::= compare_chained37
  496. testfalse ::= compare_chained37_false
  497. compare_chained ::= compare_chained37
  498. compare_chained ::= compare_chained37_false
  499. compare_chained37 ::= expr compared_chained_middlea_37
  500. compare_chained37 ::= expr compared_chained_middlec_37
  501. compare_chained37_false ::= expr compared_chained_middle_false_37
  502. compare_chained37_false ::= expr compared_chained_middleb_false_37
  503. compare_chained37_false ::= expr compare_chained_right_false_37
  504. compared_chained_middlea_37 ::= expr DUP_TOP ROT_THREE COMPARE_OP POP_JUMP_IF_FALSE
  505. compared_chained_middlea_37 ::= expr DUP_TOP ROT_THREE COMPARE_OP POP_JUMP_IF_FALSE
  506. compare_chained_righta_37 COME_FROM POP_TOP COME_FROM
  507. compared_chained_middleb_false_37 ::= expr DUP_TOP ROT_THREE COMPARE_OP POP_JUMP_IF_FALSE
  508. compare_chained_rightb_false_37 POP_TOP _jump COME_FROM
  509. compared_chained_middlec_37 ::= expr DUP_TOP ROT_THREE COMPARE_OP POP_JUMP_IF_FALSE
  510. compare_chained_righta_37 POP_TOP
  511. compared_chained_middle_false_37 ::= expr DUP_TOP ROT_THREE COMPARE_OP POP_JUMP_IF_FALSE
  512. compare_chained_rightc_37 POP_TOP JUMP_FORWARD COME_FROM
  513. compared_chained_middle_false_37 ::= expr DUP_TOP ROT_THREE COMPARE_OP POP_JUMP_IF_FALSE
  514. compare_chained_rightb_false_37 POP_TOP _jump COME_FROM
  515. compare_chained_right_false_37 ::= expr DUP_TOP ROT_THREE COMPARE_OP POP_JUMP_IF_FALSE
  516. compare_chained_righta_false_37 POP_TOP JUMP_BACK COME_FROM
  517. compare_chained_righta_37 ::= expr COMPARE_OP come_from_opt POP_JUMP_IF_TRUE JUMP_FORWARD
  518. compare_chained_righta_37 ::= expr COMPARE_OP come_from_opt POP_JUMP_IF_TRUE JUMP_BACK
  519. compare_chained_righta_false_37 ::= expr COMPARE_OP come_from_opt POP_JUMP_IF_FALSE jf_cfs
  520. compare_chained_rightb_false_37 ::= expr COMPARE_OP come_from_opt POP_JUMP_IF_FALSE JUMP_FORWARD COME_FROM
  521. compare_chained_rightb_false_37 ::= expr COMPARE_OP come_from_opt POP_JUMP_IF_FALSE JUMP_FORWARD
  522. compare_chained_rightc_37 ::= expr DUP_TOP ROT_THREE COMPARE_OP come_from_opt POP_JUMP_IF_FALSE
  523. compare_chained_righta_false_37 ELSE
  524. compare_chained_rightc_37 ::= expr DUP_TOP ROT_THREE COMPARE_OP come_from_opt POP_JUMP_IF_FALSE
  525. compare_chained_righta_false_37
  526. """
  527. def p_37_conditionals(self, args):
  528. """
  529. expr ::= if_exp37
  530. if_exp37 ::= expr expr jf_cfs expr COME_FROM
  531. jf_cfs ::= JUMP_FORWARD _come_froms
  532. ifelsestmt ::= testexpr c_stmts_opt jf_cfs else_suite
  533. opt_come_from_except
  534. # This is probably more realistically an "ifstmt" (with a null else)
  535. # see _cmp() of python3.8/distutils/__pycache__/version.cpython-38.opt-1.pyc
  536. ifelsestmt ::= testexpr stmts jf_cfs else_suite_opt
  537. opt_come_from_except
  538. expr_pjit ::= expr POP_JUMP_IF_TRUE
  539. expr_jit ::= expr JUMP_IF_TRUE
  540. expr_jt ::= expr jmp_true
  541. jmp_false37 ::= POP_JUMP_IF_FALSE COME_FROM
  542. list_if ::= expr jmp_false37 list_iter
  543. list_iter ::= list_if37
  544. list_iter ::= list_if37_not
  545. list_if37 ::= compare_chained37_false list_iter
  546. list_if37_not ::= compare_chained37 list_iter
  547. _ifstmts_jump ::= c_stmts_opt come_froms
  548. _ifstmts_jump ::= COME_FROM c_stmts come_froms
  549. and_not ::= expr jmp_false expr POP_JUMP_IF_TRUE
  550. testfalse ::= and_not
  551. expr ::= if_exp_37a
  552. expr ::= if_exp_37b
  553. if_exp_37a ::= and_not expr JUMP_FORWARD come_froms expr COME_FROM
  554. if_exp_37b ::= expr jmp_false expr POP_JUMP_IF_FALSE
  555. jump_forward_else expr
  556. jmp_false_cf ::= POP_JUMP_IF_FALSE COME_FROM
  557. comp_if ::= or jmp_false_cf comp_iter
  558. """
  559. def p_comprehension3(self, args):
  560. """
  561. # Python3 scanner adds LOAD_LISTCOMP. Python3 does list comprehension like
  562. # other comprehensions (set, dictionary).
  563. # Our "continue" heuristic - in two successive JUMP_BACKS, the first
  564. # one may be a continue - sometimes classifies a JUMP_BACK
  565. # as a CONTINUE. The two are kind of the same in a comprehension.
  566. comp_for ::= expr get_for_iter store comp_iter CONTINUE
  567. comp_for ::= expr get_for_iter store comp_iter JUMP_BACK
  568. for_iter ::= _come_froms FOR_ITER
  569. list_comp ::= BUILD_LIST_0 list_iter
  570. lc_body ::= expr LIST_APPEND
  571. list_for ::= expr_or_arg
  572. for_iter
  573. store list_iter
  574. jb_or_c _come_froms
  575. set_for ::= expr_or_arg
  576. for_iter
  577. store set_iter
  578. jb_or_c _come_froms
  579. # This is seen in PyPy, but possibly it appears on other Python 3?
  580. list_if ::= expr jmp_false list_iter COME_FROM
  581. list_if_not ::= expr jmp_true list_iter COME_FROM
  582. jb_or_c ::= JUMP_BACK
  583. jb_or_c ::= CONTINUE
  584. stmt ::= set_comp_func
  585. # TODO: simplify this
  586. set_comp_func ::= BUILD_SET_0 LOAD_ARG for_iter store comp_iter
  587. JUMP_BACK ending_return
  588. set_comp_func ::= BUILD_SET_0 LOAD_ARG for_iter store comp_iter
  589. COME_FROM JUMP_BACK ending_return
  590. comp_body ::= dict_comp_body
  591. comp_body ::= set_comp_body
  592. dict_comp_body ::= expr expr MAP_ADD
  593. set_comp_body ::= expr SET_ADD
  594. # See also common Python p_list_comprehension
  595. """
  596. def p_dict_comp3(self, args):
  597. """ "
  598. expr ::= dict_comp
  599. stmt ::= dict_comp_func
  600. dict_comp_func ::= BUILD_MAP_0 LOAD_ARG for_iter store
  601. comp_iter JUMP_BACK ending_return
  602. comp_iter ::= comp_if
  603. comp_iter ::= comp_if_not
  604. comp_if_not ::= expr jmp_true comp_iter
  605. comp_iter ::= comp_body
  606. expr_or_arg ::= LOAD_ARG
  607. expr_or_arg ::= expr
  608. """
  609. def p_expr3(self, args):
  610. """
  611. expr ::= if_exp_not
  612. if_exp_not ::= expr jmp_true expr jump_forward_else expr COME_FROM
  613. # a JUMP_FORWARD to another JUMP_FORWARD can get turned into
  614. # a JUMP_ABSOLUTE with no COME_FROM
  615. if_exp ::= expr jmp_false expr jump_absolute_else expr
  616. # if_exp_true are for conditions which always evaluate true
  617. # There is dead or non-optional remnants of the condition code though,
  618. # and we use that to match on to reconstruct the source more accurately
  619. expr ::= if_exp_true
  620. if_exp_true ::= expr JUMP_FORWARD expr COME_FROM
  621. """
  622. def p_generator_exp3(self, args):
  623. """
  624. load_genexpr ::= LOAD_GENEXPR
  625. load_genexpr ::= BUILD_TUPLE_1 LOAD_GENEXPR LOAD_STR
  626. """
  627. def p_grammar(self, args):
  628. """
  629. sstmt ::= stmt
  630. sstmt ::= ifelsestmtr
  631. sstmt ::= return RETURN_LAST
  632. return_if_stmts ::= return_if_stmt come_from_opt
  633. return_if_stmts ::= _stmts return_if_stmt _come_froms
  634. return_if_stmt ::= return_expr RETURN_END_IF
  635. returns ::= _stmts return_if_stmt
  636. stmt ::= break
  637. break ::= BREAK_LOOP
  638. stmt ::= continue
  639. continue ::= CONTINUE
  640. continues ::= _stmts lastl_stmt continue
  641. continues ::= lastl_stmt continue
  642. continues ::= continue
  643. kwarg ::= LOAD_STR expr
  644. kwargs ::= kwarg+
  645. classdef ::= build_class store
  646. # FIXME: we need to add these because don't detect this properly
  647. # in custom rules. Specifically if one of the exprs is CALL_FUNCTION
  648. # then we'll mistake that for the final CALL_FUNCTION.
  649. # We can fix by triggering on the CALL_FUNCTION op
  650. # Python3 introduced LOAD_BUILD_CLASS
  651. # Other definitions are in a custom rule
  652. build_class ::= LOAD_BUILD_CLASS mkfunc expr call CALL_FUNCTION_3
  653. build_class ::= LOAD_BUILD_CLASS mkfunc expr call expr CALL_FUNCTION_4
  654. stmt ::= classdefdeco
  655. classdefdeco ::= classdefdeco1 store
  656. # In 3.7 there are some LOAD_GLOBALs we don't convert to LOAD_ASSERT
  657. stmt ::= assert2
  658. assert2 ::= expr jmp_true LOAD_GLOBAL expr CALL_FUNCTION_1 RAISE_VARARGS_1
  659. # "assert_invert" tests on the negative of the condition given
  660. stmt ::= assert_invert
  661. assert_invert ::= testtrue LOAD_GLOBAL RAISE_VARARGS_1
  662. expr ::= LOAD_ASSERT
  663. # FIXME: add this:
  664. # expr ::= assert_expr_or
  665. ifstmt ::= testexpr _ifstmts_jump
  666. testexpr ::= testfalse
  667. testexpr ::= testtrue
  668. testfalse ::= expr jmp_false
  669. testtrue ::= expr jmp_true
  670. _ifstmts_jump ::= return_if_stmts
  671. _ifstmts_jump ::= c_stmts_opt COME_FROM
  672. iflaststmt ::= testexpr c_stmts
  673. iflaststmt ::= testexpr c_stmts JUMP_ABSOLUTE
  674. iflaststmtl ::= testexpr c_stmts JUMP_BACK
  675. iflaststmtl ::= testexpr c_stmts JUMP_BACK COME_FROM_LOOP
  676. iflaststmtl ::= testexpr c_stmts JUMP_BACK POP_BLOCK
  677. # These are used to keep parse tree indices the same
  678. jump_forward_else ::= JUMP_FORWARD
  679. jump_forward_else ::= JUMP_FORWARD ELSE
  680. jump_forward_else ::= JUMP_FORWARD COME_FROM
  681. jump_absolute_else ::= JUMP_ABSOLUTE ELSE
  682. jump_absolute_else ::= JUMP_ABSOLUTE _come_froms
  683. jump_absolute_else ::= come_froms _jump COME_FROM
  684. # Note: in if/else kinds of statements, we err on the side
  685. # of missing "else" clauses. Therefore we include grammar
  686. # rules with and without ELSE.
  687. ifelsestmt ::= testexpr c_stmts_opt JUMP_FORWARD
  688. else_suite opt_come_from_except
  689. ifelsestmt ::= testexpr c_stmts_opt jump_forward_else
  690. else_suite _come_froms
  691. # This handles the case where a "JUMP_ABSOLUTE" is part
  692. # of an inner if in c_stmts_opt
  693. ifelsestmt ::= testexpr c_stmts come_froms
  694. else_suite come_froms
  695. # ifelsestmt ::= testexpr c_stmts_opt jump_forward_else
  696. # pass _come_froms
  697. ifelsestmtc ::= testexpr c_stmts_opt JUMP_ABSOLUTE else_suitec
  698. ifelsestmtc ::= testexpr c_stmts_opt jump_absolute_else else_suitec
  699. ifelsestmtr ::= testexpr return_if_stmts returns
  700. ifelsestmtl ::= testexpr c_stmts_opt cf_jump_back else_suitel
  701. cf_jump_back ::= COME_FROM JUMP_BACK
  702. # FIXME: this feels like a hack. Is it just 1 or two
  703. # COME_FROMs? the parsed tree for this and even with just the
  704. # one COME_FROM for Python 2.7 seems to associate the
  705. # COME_FROM targets from the wrong places
  706. # this is nested inside a try_except
  707. tryfinallystmt ::= SETUP_FINALLY suite_stmts_opt
  708. POP_BLOCK LOAD_CONST
  709. COME_FROM_FINALLY suite_stmts_opt END_FINALLY
  710. except_handler ::= jmp_abs COME_FROM except_stmts
  711. _come_froms END_FINALLY
  712. except_handler ::= jmp_abs COME_FROM_EXCEPT except_stmts
  713. _come_froms END_FINALLY
  714. # FIXME: remove this
  715. except_handler ::= JUMP_FORWARD COME_FROM except_stmts
  716. come_froms END_FINALLY come_from_opt
  717. except_stmts ::= except_stmt+
  718. except_stmt ::= except_cond1 except_suite come_from_opt
  719. except_stmt ::= except_cond2 except_suite come_from_opt
  720. except_stmt ::= except_cond2 except_suite_finalize
  721. except_stmt ::= except
  722. ## FIXME: what's except_pop_except?
  723. except_stmt ::= except_pop_except
  724. # Python3 introduced POP_EXCEPT
  725. except_suite ::= c_stmts_opt POP_EXCEPT jump_except
  726. jump_except ::= JUMP_ABSOLUTE
  727. jump_except ::= JUMP_BACK
  728. jump_except ::= JUMP_FORWARD
  729. jump_except ::= CONTINUE
  730. # This is used in Python 3 in
  731. # "except ... as e" to remove 'e' after the c_stmts_opt finishes
  732. except_suite_finalize ::= SETUP_FINALLY c_stmts_opt except_var_finalize
  733. END_FINALLY _jump
  734. except_var_finalize ::= POP_BLOCK POP_EXCEPT LOAD_CONST COME_FROM_FINALLY
  735. LOAD_CONST store delete
  736. except_suite ::= returns
  737. except_cond1 ::= DUP_TOP expr COMPARE_OP
  738. jmp_false POP_TOP POP_TOP POP_TOP
  739. except_cond2 ::= DUP_TOP expr COMPARE_OP
  740. jmp_false POP_TOP store POP_TOP come_from_opt
  741. except ::= POP_TOP POP_TOP POP_TOP c_stmts_opt POP_EXCEPT _jump
  742. except ::= POP_TOP POP_TOP POP_TOP returns
  743. jmp_abs ::= JUMP_ABSOLUTE
  744. jmp_abs ::= JUMP_BACK
  745. """
  746. def p_misc3(self, args):
  747. """
  748. except_handler ::= JUMP_FORWARD COME_FROM_EXCEPT except_stmts
  749. come_froms END_FINALLY
  750. for_block ::= l_stmts_opt COME_FROM_LOOP JUMP_BACK
  751. for_block ::= l_stmts
  752. for_block ::= l_stmts JUMP_BACK
  753. iflaststmtl ::= testexpr c_stmts
  754. """
  755. def p_come_from3(self, args):
  756. """
  757. opt_come_from_except ::= COME_FROM_EXCEPT
  758. opt_come_from_except ::= _come_froms
  759. opt_come_from_except ::= come_from_except_clauses
  760. come_from_except_clauses ::= COME_FROM_EXCEPT_CLAUSE+
  761. """
  762. def p_jump3(self, args):
  763. """
  764. jmp_false ::= POP_JUMP_IF_FALSE
  765. jmp_true ::= POP_JUMP_IF_TRUE
  766. # FIXME: Common with 2.7
  767. ret_and ::= expr JUMP_IF_FALSE_OR_POP return_expr_or_cond COME_FROM
  768. ret_or ::= expr JUMP_IF_TRUE_OR_POP return_expr_or_cond COME_FROM
  769. if_exp_ret ::= expr POP_JUMP_IF_FALSE expr RETURN_END_IF COME_FROM return_expr_or_cond
  770. jitop_come_from_expr ::= JUMP_IF_TRUE_OR_POP come_froms expr
  771. jifop_come_from ::= JUMP_IF_FALSE_OR_POP come_froms
  772. expr_jitop ::= expr JUMP_IF_TRUE_OR_POP
  773. or ::= and jitop_come_from_expr COME_FROM
  774. or ::= expr_jitop expr COME_FROM
  775. or ::= expr_jit expr COME_FROM
  776. or ::= expr_pjit expr POP_JUMP_IF_FALSE COME_FROM
  777. testfalse_not_or ::= expr jmp_false expr jmp_false COME_FROM
  778. testfalse_not_and ::= and jmp_true come_froms
  779. testfalse_not_and ::= expr jmp_false expr jmp_true COME_FROM
  780. testfalse ::= testfalse_not_or
  781. testfalse ::= testfalse_not_and
  782. testfalse ::= or jmp_false COME_FROM
  783. iflaststmtl ::= testexprl c_stmts JUMP_BACK
  784. iflaststmtl ::= testexprl c_stmts JUMP_BACK COME_FROM_LOOP
  785. iflaststmtl ::= testexprl c_stmts JUMP_BACK POP_BLOCK
  786. testexprl ::= testfalsel
  787. testfalsel ::= expr jmp_true
  788. or ::= expr_jt expr
  789. and ::= expr JUMP_IF_FALSE_OR_POP expr come_from_opt
  790. and ::= expr jifop_come_from expr
  791. expr_pjit_come_from ::= expr POP_JUMP_IF_TRUE COME_FROM
  792. or ::= expr_pjit_come_from expr
  793. ## Note that "jmp_false" is what we check on in the "and" reduce rule.
  794. and ::= expr jmp_false expr COME_FROM
  795. or ::= expr_jt expr COME_FROM
  796. # compared_chained_middle is used exclusively in chained_compare
  797. compared_chained_middle ::= expr DUP_TOP ROT_THREE COMPARE_OP JUMP_IF_FALSE_OR_POP
  798. compared_chained_middle COME_FROM
  799. compared_chained_middle ::= expr DUP_TOP ROT_THREE COMPARE_OP JUMP_IF_FALSE_OR_POP
  800. compare_chained_right COME_FROM
  801. """
  802. def p_stmt3(self, args):
  803. """
  804. stmt ::= if_exp_lambda
  805. stmt ::= if_exp_not_lambda
  806. # If statement inside a loop:
  807. stmt ::= ifstmtl
  808. if_exp_lambda ::= expr jmp_false expr return_if_lambda
  809. return_stmt_lambda LAMBDA_MARKER
  810. if_exp_not_lambda
  811. ::= expr jmp_true expr return_if_lambda
  812. return_stmt_lambda LAMBDA_MARKER
  813. return_stmt_lambda ::= return_expr RETURN_VALUE_LAMBDA
  814. return_if_lambda ::= RETURN_END_IF_LAMBDA
  815. stmt ::= return_closure
  816. return_closure ::= LOAD_CLOSURE RETURN_VALUE RETURN_LAST
  817. stmt ::= whileTruestmt
  818. ifelsestmt ::= testexpr c_stmts_opt JUMP_FORWARD else_suite _come_froms
  819. ifelsestmtl ::= testexpr c_stmts_opt jump_forward_else else_suitec
  820. ifstmtl ::= testexpr _ifstmts_jumpl
  821. _ifstmts_jumpl ::= c_stmts JUMP_BACK
  822. _ifstmts_jumpl ::= _ifstmts_jump
  823. # The following can happen when the jump offset is large and
  824. # Python is looking to do a small jump to a larger jump to get
  825. # around the problem that the offset can't be represented in
  826. # the size allowed for the jump offset. This is more likely to
  827. # happen in wordcode Python since the offset range has been
  828. # reduced. FIXME: We should add a reduction check that the
  829. # final jump goes to another jump.
  830. _ifstmts_jumpl ::= COME_FROM c_stmts JUMP_BACK
  831. _ifstmts_jumpl ::= COME_FROM c_stmts JUMP_FORWARD
  832. """
  833. def p_loop_stmt3(self, args):
  834. """
  835. setup_loop ::= SETUP_LOOP _come_froms
  836. for ::= setup_loop expr get_for_iter store for_block POP_BLOCK
  837. for ::= setup_loop expr get_for_iter store for_block POP_BLOCK
  838. COME_FROM_LOOP
  839. forelsestmt ::= setup_loop expr get_for_iter store for_block POP_BLOCK else_suite
  840. COME_FROM_LOOP
  841. forelselaststmt ::= setup_loop expr get_for_iter store for_block POP_BLOCK else_suitec
  842. COME_FROM_LOOP
  843. forelselaststmtl ::= setup_loop expr get_for_iter store for_block POP_BLOCK else_suitel
  844. COME_FROM_LOOP
  845. whilestmt ::= setup_loop testexpr l_stmts_opt COME_FROM JUMP_BACK POP_BLOCK
  846. COME_FROM_LOOP
  847. whilestmt ::= setup_loop testexpr l_stmts_opt JUMP_BACK POP_BLOCK
  848. COME_FROM_LOOP
  849. whilestmt ::= setup_loop testexpr returns POP_BLOCK
  850. COME_FROM_LOOP
  851. # We can be missing a COME_FROM_LOOP if the "while" statement is nested inside an if/else
  852. # so after the POP_BLOCK we have a JUMP_FORWARD which forms the "else" portion of the "if"
  853. # This is undoubtedly some sort of JUMP optimization going on.
  854. whilestmt ::= setup_loop testexpr l_stmts_opt JUMP_BACK come_froms
  855. POP_BLOCK
  856. while1elsestmt ::= setup_loop l_stmts JUMP_BACK
  857. else_suitel
  858. whileelsestmt ::= setup_loop testexpr l_stmts_opt JUMP_BACK POP_BLOCK
  859. else_suitel COME_FROM_LOOP
  860. whileTruestmt ::= setup_loop l_stmts_opt JUMP_BACK POP_BLOCK
  861. _come_froms
  862. # FIXME: Python 3.? starts adding branch optimization? Put this starting there.
  863. while1stmt ::= setup_loop l_stmts COME_FROM_LOOP
  864. while1stmt ::= setup_loop l_stmts COME_FROM_LOOP JUMP_BACK POP_BLOCK COME_FROM_LOOP
  865. while1stmt ::= setup_loop l_stmts COME_FROM JUMP_BACK COME_FROM_LOOP
  866. while1elsestmt ::= setup_loop l_stmts JUMP_BACK
  867. else_suite COME_FROM_LOOP
  868. # FIXME: investigate - can code really produce a NOP?
  869. for ::= setup_loop expr get_for_iter store for_block POP_BLOCK NOP
  870. COME_FROM_LOOP
  871. """
  872. def p_36misc(self, args):
  873. """
  874. sstmt ::= sstmt RETURN_LAST
  875. # 3.6 redoes how return_closure works. FIXME: Isolate to LOAD_CLOSURE
  876. return_closure ::= LOAD_CLOSURE DUP_TOP STORE_NAME RETURN_VALUE RETURN_LAST
  877. for_block ::= l_stmts_opt come_from_loops JUMP_BACK
  878. come_from_loops ::= COME_FROM_LOOP*
  879. whilestmt ::= setup_loop testexpr l_stmts_opt
  880. JUMP_BACK come_froms POP_BLOCK COME_FROM_LOOP
  881. whilestmt ::= setup_loop testexpr l_stmts_opt
  882. come_froms JUMP_BACK come_froms POP_BLOCK COME_FROM_LOOP
  883. # 3.6 due to jump optimization, we sometimes add RETURN_END_IF where
  884. # RETURN_VALUE is meant. Specifically this can happen in
  885. # ifelsestmt -> ...else_suite _. suite_stmts... (last) stmt
  886. return ::= return_expr RETURN_END_IF
  887. return ::= return_expr RETURN_VALUE COME_FROM
  888. return_stmt_lambda ::= return_expr RETURN_VALUE_LAMBDA COME_FROM
  889. # A COME_FROM is dropped off because of JUMP-to-JUMP optimization
  890. and ::= expr jmp_false expr
  891. and ::= expr jmp_false expr jmp_false
  892. jf_cf ::= JUMP_FORWARD COME_FROM
  893. cf_jf_else ::= come_froms JUMP_FORWARD ELSE
  894. if_exp ::= expr jmp_false expr jf_cf expr COME_FROM
  895. async_for_stmt ::= setup_loop expr
  896. GET_AITER
  897. LOAD_CONST YIELD_FROM SETUP_EXCEPT GET_ANEXT LOAD_CONST
  898. YIELD_FROM
  899. store
  900. POP_BLOCK JUMP_FORWARD COME_FROM_EXCEPT DUP_TOP
  901. LOAD_GLOBAL COMPARE_OP POP_JUMP_IF_FALSE
  902. POP_TOP POP_TOP POP_TOP POP_EXCEPT POP_BLOCK
  903. JUMP_ABSOLUTE END_FINALLY COME_FROM
  904. for_block POP_BLOCK
  905. COME_FROM_LOOP
  906. # Adds a COME_FROM_ASYNC_WITH over 3.5
  907. # FIXME: remove corresponding rule for 3.5?
  908. except_suite ::= c_stmts_opt COME_FROM POP_EXCEPT jump_except COME_FROM
  909. jb_cfs ::= come_from_opt JUMP_BACK come_froms
  910. ifelsestmtl ::= testexpr c_stmts_opt jb_cfs else_suitel
  911. ifelsestmtl ::= testexpr c_stmts_opt cf_jf_else else_suitel
  912. # In 3.6+, A sequence of statements ending in a RETURN can cause
  913. # JUMP_FORWARD END_FINALLY to be omitted from try middle
  914. except_return ::= POP_TOP POP_TOP POP_TOP returns
  915. except_handler ::= JUMP_FORWARD COME_FROM_EXCEPT except_return
  916. # Try middle following a returns
  917. except_handler36 ::= COME_FROM_EXCEPT except_stmts END_FINALLY
  918. stmt ::= try_except36
  919. try_except36 ::= SETUP_EXCEPT returns except_handler36
  920. opt_come_from_except
  921. try_except36 ::= SETUP_EXCEPT suite_stmts
  922. try_except36 ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
  923. except_handler36 come_from_opt
  924. # 3.6 omits END_FINALLY sometimes
  925. except_handler36 ::= COME_FROM_EXCEPT except_stmts
  926. except_handler36 ::= JUMP_FORWARD COME_FROM_EXCEPT except_stmts
  927. except_handler ::= jmp_abs COME_FROM_EXCEPT except_stmts
  928. stmt ::= tryfinally36
  929. tryfinally36 ::= SETUP_FINALLY returns
  930. COME_FROM_FINALLY suite_stmts
  931. tryfinally36 ::= SETUP_FINALLY returns
  932. COME_FROM_FINALLY suite_stmts_opt END_FINALLY
  933. except_suite_finalize ::= SETUP_FINALLY returns
  934. COME_FROM_FINALLY suite_stmts_opt END_FINALLY _jump
  935. stmt ::= tryfinally_return_stmt
  936. tryfinally_return_stmt ::= SETUP_FINALLY suite_stmts_opt POP_BLOCK LOAD_CONST
  937. COME_FROM_FINALLY
  938. compare_chained_right ::= expr COMPARE_OP come_froms JUMP_FORWARD
  939. """
  940. def p_37_misc(self, args):
  941. """
  942. # long except clauses in a loop can sometimes cause a JUMP_BACK to turn into a
  943. # JUMP_FORWARD to a JUMP_BACK. And when this happens there is an additional
  944. # ELSE added to the except_suite. With better flow control perhaps we can
  945. # sort this out better.
  946. except_suite ::= c_stmts_opt POP_EXCEPT jump_except ELSE
  947. # FIXME: the below is to work around test_grammar expecting a "call" to be
  948. # on the LHS because it is also somewhere on in a rule.
  949. call ::= expr CALL_METHOD_0
  950. """
  951. def customize_grammar_rules(self, tokens, customize):
  952. super(Python37Parser, self).customize_grammar_rules(tokens, customize)
  953. self.check_reduce["call_kw"] = "AST"
  954. # Opcode names in the custom_ops_processed set have rules that get added
  955. # unconditionally and the rules are constant. So they need to be done
  956. # only once and if we see the opcode a second we don't have to consider
  957. # adding more rules.
  958. #
  959. # Note: BUILD_TUPLE_UNPACK_WITH_CALL gets considered by
  960. # default because it starts with BUILD. So we'll set to ignore it from
  961. # the start.
  962. custom_ops_processed = set()
  963. for i, token in enumerate(tokens):
  964. opname = token.kind
  965. if opname == "LOAD_ASSERT":
  966. if "PyPy" in customize:
  967. rules_str = """
  968. stmt ::= JUMP_IF_NOT_DEBUG stmts COME_FROM
  969. """
  970. self.add_unique_doc_rules(rules_str, customize)
  971. elif opname == "FORMAT_VALUE":
  972. rules_str = """
  973. expr ::= formatted_value1
  974. formatted_value1 ::= expr FORMAT_VALUE
  975. """
  976. self.add_unique_doc_rules(rules_str, customize)
  977. elif opname == "FORMAT_VALUE_ATTR":
  978. rules_str = """
  979. expr ::= formatted_value2
  980. formatted_value2 ::= expr expr FORMAT_VALUE_ATTR
  981. """
  982. self.add_unique_doc_rules(rules_str, customize)
  983. elif opname == "MAKE_FUNCTION_CLOSURE":
  984. if "LOAD_DICTCOMP" in self.seen_ops:
  985. # Is there something general going on here?
  986. rule = """
  987. dict_comp ::= load_closure LOAD_DICTCOMP LOAD_STR
  988. MAKE_FUNCTION_CLOSURE expr
  989. GET_ITER CALL_FUNCTION_1
  990. """
  991. self.addRule(rule, nop_func)
  992. elif "LOAD_SETCOMP" in self.seen_ops:
  993. rule = """
  994. set_comp ::= load_closure LOAD_SETCOMP LOAD_STR
  995. MAKE_FUNCTION_CLOSURE expr
  996. GET_ITER CALL_FUNCTION_1
  997. """
  998. self.addRule(rule, nop_func)
  999. elif opname == "BEFORE_ASYNC_WITH":
  1000. rules_str = """
  1001. stmt ::= async_with_stmt SETUP_ASYNC_WITH
  1002. async_with_pre ::= BEFORE_ASYNC_WITH GET_AWAITABLE LOAD_CONST YIELD_FROM SETUP_ASYNC_WITH
  1003. async_with_post ::= COME_FROM_ASYNC_WITH
  1004. WITH_CLEANUP_START GET_AWAITABLE LOAD_CONST YIELD_FROM
  1005. WITH_CLEANUP_FINISH END_FINALLY
  1006. stmt ::= async_with_as_stmt
  1007. async_with_as_stmt ::= expr
  1008. async_with_pre
  1009. store
  1010. suite_stmts_opt
  1011. POP_BLOCK LOAD_CONST
  1012. async_with_post
  1013. async_with_stmt ::= expr
  1014. async_with_pre
  1015. POP_TOP
  1016. suite_stmts_opt
  1017. POP_BLOCK LOAD_CONST
  1018. async_with_post
  1019. async_with_stmt ::= expr
  1020. async_with_pre
  1021. POP_TOP
  1022. suite_stmts_opt
  1023. async_with_post
  1024. """
  1025. self.addRule(rules_str, nop_func)
  1026. elif opname.startswith("BUILD_STRING"):
  1027. v = token.attr
  1028. rules_str = """
  1029. expr ::= joined_str
  1030. joined_str ::= %sBUILD_STRING_%d
  1031. """ % (
  1032. "expr " * v,
  1033. v,
  1034. )
  1035. self.add_unique_doc_rules(rules_str, customize)
  1036. if "FORMAT_VALUE_ATTR" in self.seen_ops:
  1037. rules_str = """
  1038. formatted_value_attr ::= expr expr FORMAT_VALUE_ATTR expr BUILD_STRING
  1039. expr ::= formatted_value_attr
  1040. """
  1041. self.add_unique_doc_rules(rules_str, customize)
  1042. elif opname.startswith("BUILD_MAP_UNPACK_WITH_CALL"):
  1043. v = token.attr
  1044. rule = "build_map_unpack_with_call ::= %s%s" % ("expr " * v, opname)
  1045. self.addRule(rule, nop_func)
  1046. elif opname.startswith("BUILD_TUPLE_UNPACK_WITH_CALL"):
  1047. v = token.attr
  1048. rule = (
  1049. "build_tuple_unpack_with_call ::= "
  1050. + "expr1024 " * int(v // 1024)
  1051. + "expr32 " * int((v // 32) % 32)
  1052. + "expr " * (v % 32)
  1053. + opname
  1054. )
  1055. self.addRule(rule, nop_func)
  1056. rule = "starred ::= %s %s" % ("expr " * v, opname)
  1057. self.addRule(rule, nop_func)
  1058. elif opname == "GET_AITER":
  1059. self.add_unique_doc_rules("get_aiter ::= expr GET_AITER", customize)
  1060. if not {"MAKE_FUNCTION_0", "MAKE_FUNCTION_CLOSURE"} in self.seen_ops:
  1061. self.addRule(
  1062. """
  1063. expr ::= dict_comp_async
  1064. expr ::= generator_exp_async
  1065. expr ::= list_comp_async
  1066. dict_comp_async ::= LOAD_DICTCOMP
  1067. LOAD_STR
  1068. MAKE_FUNCTION_0
  1069. get_aiter
  1070. CALL_FUNCTION_1
  1071. dict_comp_async ::= BUILD_MAP_0 LOAD_ARG
  1072. dict_comp_async
  1073. func_async_middle ::= POP_BLOCK JUMP_FORWARD COME_FROM_EXCEPT
  1074. DUP_TOP LOAD_GLOBAL COMPARE_OP POP_JUMP_IF_TRUE
  1075. END_FINALLY COME_FROM
  1076. func_async_prefix ::= _come_froms SETUP_EXCEPT GET_ANEXT LOAD_CONST YIELD_FROM
  1077. generator_exp_async ::= load_genexpr LOAD_STR MAKE_FUNCTION_0
  1078. get_aiter CALL_FUNCTION_1
  1079. genexpr_func_async ::= LOAD_ARG func_async_prefix
  1080. store func_async_middle comp_iter
  1081. JUMP_BACK COME_FROM
  1082. POP_TOP POP_TOP POP_TOP POP_EXCEPT POP_TOP
  1083. # FIXME this is a workaround for probably some bug in the Earley parser
  1084. # if we use get_aiter, then list_comp_async doesn't match, and I don't
  1085. # understand why.
  1086. expr_get_aiter ::= expr GET_AITER
  1087. list_afor ::= get_aiter list_afor2
  1088. list_afor2 ::= func_async_prefix
  1089. store func_async_middle list_iter
  1090. JUMP_BACK COME_FROM
  1091. POP_TOP POP_TOP POP_TOP POP_EXCEPT POP_TOP
  1092. list_comp_async ::= BUILD_LIST_0 LOAD_ARG list_afor2
  1093. list_comp_async ::= LOAD_LISTCOMP LOAD_STR MAKE_FUNCTION_0
  1094. expr_get_aiter CALL_FUNCTION_1
  1095. GET_AWAITABLE LOAD_CONST
  1096. YIELD_FROM
  1097. list_iter ::= list_afor
  1098. set_comp_async ::= LOAD_SETCOMP
  1099. LOAD_STR
  1100. MAKE_FUNCTION_0
  1101. get_aiter
  1102. CALL_FUNCTION_1
  1103. set_comp_async ::= LOAD_CLOSURE
  1104. BUILD_TUPLE_1
  1105. LOAD_SETCOMP
  1106. LOAD_STR MAKE_FUNCTION_CLOSURE
  1107. get_aiter CALL_FUNCTION_1
  1108. await
  1109. """,
  1110. nop_func,
  1111. )
  1112. custom_ops_processed.add(opname)
  1113. self.addRule(
  1114. """
  1115. dict_comp_async ::= BUILD_MAP_0 LOAD_ARG
  1116. dict_comp_async
  1117. expr ::= dict_comp_async
  1118. expr ::= generator_exp_async
  1119. expr ::= list_comp_async
  1120. expr ::= set_comp_async
  1121. func_async_middle ::= POP_BLOCK JUMP_FORWARD COME_FROM_EXCEPT
  1122. DUP_TOP LOAD_GLOBAL COMPARE_OP POP_JUMP_IF_TRUE
  1123. END_FINALLY _come_froms
  1124. # async_iter ::= block_break SETUP_EXCEPT GET_ANEXT LOAD_CONST YIELD_FROM
  1125. get_aiter ::= expr GET_AITER
  1126. list_afor ::= get_aiter list_afor2
  1127. list_comp_async ::= BUILD_LIST_0 LOAD_ARG list_afor2
  1128. list_iter ::= list_afor
  1129. set_afor ::= get_aiter set_afor2
  1130. set_iter ::= set_afor
  1131. set_iter ::= set_for
  1132. set_comp_async ::= BUILD_SET_0 LOAD_ARG
  1133. set_comp_async
  1134. """,
  1135. nop_func,
  1136. )
  1137. custom_ops_processed.add(opname)
  1138. elif opname == "GET_ANEXT":
  1139. self.addRule(
  1140. """
  1141. expr ::= genexpr_func_async
  1142. expr ::= BUILD_MAP_0 genexpr_func_async
  1143. expr ::= list_comp_async
  1144. dict_comp_async ::= BUILD_MAP_0 genexpr_func_async
  1145. async_iter ::= _come_froms
  1146. SETUP_EXCEPT GET_ANEXT LOAD_CONST YIELD_FROM
  1147. store_async_iter_end ::= store
  1148. POP_BLOCK JUMP_FORWARD COME_FROM_EXCEPT
  1149. DUP_TOP LOAD_GLOBAL COMPARE_OP POP_JUMP_IF_TRUE
  1150. END_FINALLY COME_FROM
  1151. # We use store_async_iter_end to make comp_iter come out in the right position,
  1152. # (after the logical "store")
  1153. genexpr_func_async ::= LOAD_ARG async_iter
  1154. store_async_iter_end
  1155. comp_iter
  1156. JUMP_BACK COME_FROM
  1157. POP_TOP POP_TOP POP_TOP POP_EXCEPT POP_TOP
  1158. list_afor2 ::= async_iter
  1159. store
  1160. list_iter
  1161. JUMP_BACK
  1162. COME_FROM_FINALLY
  1163. END_ASYNC_FOR
  1164. list_comp_async ::= BUILD_LIST_0 LOAD_ARG list_afor2
  1165. set_afor2 ::= async_iter
  1166. store
  1167. func_async_middle
  1168. set_iter
  1169. JUMP_BACK COME_FROM
  1170. POP_TOP POP_TOP POP_TOP POP_EXCEPT POP_TOP
  1171. set_afor2 ::= expr_or_arg
  1172. set_iter_async
  1173. set_comp_async ::= BUILD_SET_0 set_afor2
  1174. set_iter_async ::= async_iter
  1175. store
  1176. set_iter
  1177. JUMP_BACK
  1178. _come_froms
  1179. END_ASYNC_FOR
  1180. return_expr_lambda ::= genexpr_func_async
  1181. LOAD_CONST RETURN_VALUE
  1182. RETURN_VALUE_LAMBDA
  1183. return_expr_lambda ::= BUILD_SET_0 genexpr_func_async
  1184. RETURN_VALUE_LAMBDA LAMBDA_MARKER
  1185. """,
  1186. nop_func,
  1187. )
  1188. custom_ops_processed.add(opname)
  1189. elif opname == "GET_AWAITABLE":
  1190. rule_str = """
  1191. await_expr ::= expr GET_AWAITABLE LOAD_CONST YIELD_FROM
  1192. expr ::= await_expr
  1193. """
  1194. self.add_unique_doc_rules(rule_str, customize)
  1195. elif opname == "GET_ITER":
  1196. self.addRule(
  1197. """
  1198. expr ::= get_iter
  1199. get_iter ::= expr GET_ITER
  1200. """,
  1201. nop_func,
  1202. )
  1203. custom_ops_processed.add(opname)
  1204. elif opname == "LOAD_ASSERT":
  1205. if "PyPy" in customize:
  1206. rules_str = """
  1207. stmt ::= JUMP_IF_NOT_DEBUG stmts COME_FROM
  1208. """
  1209. self.add_unique_doc_rules(rules_str, customize)
  1210. elif opname == "LOAD_ATTR":
  1211. self.addRule(
  1212. """
  1213. expr ::= attribute
  1214. attribute ::= expr LOAD_ATTR
  1215. """,
  1216. nop_func,
  1217. )
  1218. custom_ops_processed.add(opname)
  1219. elif opname == "SETUP_WITH":
  1220. rules_str = """
  1221. with ::= expr SETUP_WITH POP_TOP suite_stmts_opt COME_FROM_WITH
  1222. WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
  1223. # Removes POP_BLOCK LOAD_CONST from 3.6-
  1224. with_as ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
  1225. WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
  1226. """
  1227. if self.version < (3, 8):
  1228. rules_str += """
  1229. with ::= expr SETUP_WITH POP_TOP suite_stmts_opt POP_BLOCK
  1230. LOAD_CONST
  1231. WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
  1232. """
  1233. else:
  1234. rules_str += """
  1235. with ::= expr SETUP_WITH POP_TOP suite_stmts_opt POP_BLOCK
  1236. BEGIN_FINALLY COME_FROM_WITH
  1237. WITH_CLEANUP_START WITH_CLEANUP_FINISH
  1238. END_FINALLY
  1239. """
  1240. self.addRule(rules_str, nop_func)
  1241. pass
  1242. pass
  1243. def custom_classfunc_rule(self, opname, token, customize, next_token):
  1244. args_pos, args_kw = self.get_pos_kw(token)
  1245. # Additional exprs for * and ** args:
  1246. # 0 if neither
  1247. # 1 for CALL_FUNCTION_VAR or CALL_FUNCTION_KW
  1248. # 2 for * and ** args (CALL_FUNCTION_VAR_KW).
  1249. # Yes, this computation based on instruction name is a little bit hoaky.
  1250. nak = (len(opname) - len("CALL_FUNCTION")) // 3
  1251. uniq_param = args_kw + args_pos
  1252. if frozenset(("GET_AWAITABLE", "YIELD_FROM")).issubset(self.seen_ops):
  1253. rule = (
  1254. """
  1255. await ::= GET_AWAITABLE LOAD_CONST YIELD_FROM
  1256. await_expr ::= expr await
  1257. expr ::= await_expr
  1258. async_call ::= expr """
  1259. + ("pos_arg " * args_pos)
  1260. + ("kwarg " * args_kw)
  1261. + "expr " * nak
  1262. + token.kind
  1263. + " GET_AWAITABLE LOAD_CONST YIELD_FROM"
  1264. )
  1265. self.add_unique_doc_rules(rule, customize)
  1266. self.add_unique_rule(
  1267. "expr ::= async_call", token.kind, uniq_param, customize
  1268. )
  1269. if opname.startswith("CALL_FUNCTION_KW"):
  1270. self.addRule("expr ::= call_kw36", nop_func)
  1271. values = "expr " * token.attr
  1272. rule = "call_kw36 ::= expr {values} LOAD_CONST {opname}".format(**locals())
  1273. self.add_unique_rule(rule, token.kind, token.attr, customize)
  1274. elif opname == "CALL_FUNCTION_EX_KW":
  1275. # Note: this doesn't exist in 3.7 and later
  1276. self.addRule(
  1277. """expr ::= call_ex_kw4
  1278. call_ex_kw4 ::= expr
  1279. expr
  1280. expr
  1281. CALL_FUNCTION_EX_KW
  1282. """,
  1283. nop_func,
  1284. )
  1285. if "BUILD_MAP_UNPACK_WITH_CALL" in self.seen_op_basenames:
  1286. self.addRule(
  1287. """expr ::= call_ex_kw
  1288. call_ex_kw ::= expr expr build_map_unpack_with_call
  1289. CALL_FUNCTION_EX_KW
  1290. """,
  1291. nop_func,
  1292. )
  1293. if "BUILD_TUPLE_UNPACK_WITH_CALL" in self.seen_op_basenames:
  1294. # FIXME: should this be parameterized by EX value?
  1295. self.addRule(
  1296. """expr ::= call_ex_kw3
  1297. call_ex_kw3 ::= expr
  1298. build_tuple_unpack_with_call
  1299. expr
  1300. CALL_FUNCTION_EX_KW
  1301. """,
  1302. nop_func,
  1303. )
  1304. if "BUILD_MAP_UNPACK_WITH_CALL" in self.seen_op_basenames:
  1305. # FIXME: should this be parameterized by EX value?
  1306. self.addRule(
  1307. """expr ::= call_ex_kw2
  1308. call_ex_kw2 ::= expr
  1309. build_tuple_unpack_with_call
  1310. build_map_unpack_with_call
  1311. CALL_FUNCTION_EX_KW
  1312. """,
  1313. nop_func,
  1314. )
  1315. elif opname == "CALL_FUNCTION_EX":
  1316. self.addRule(
  1317. """
  1318. expr ::= call_ex
  1319. starred ::= expr
  1320. call_ex ::= expr starred CALL_FUNCTION_EX
  1321. """,
  1322. nop_func,
  1323. )
  1324. if "BUILD_MAP_UNPACK_WITH_CALL" in self.seen_ops:
  1325. self.addRule(
  1326. """
  1327. expr ::= call_ex_kw
  1328. call_ex_kw ::= expr expr
  1329. build_map_unpack_with_call CALL_FUNCTION_EX
  1330. """,
  1331. nop_func,
  1332. )
  1333. if "BUILD_TUPLE_UNPACK_WITH_CALL" in self.seen_ops:
  1334. self.addRule(
  1335. """
  1336. expr ::= call_ex_kw3
  1337. call_ex_kw3 ::= expr
  1338. build_tuple_unpack_with_call
  1339. %s
  1340. CALL_FUNCTION_EX
  1341. """
  1342. % "expr "
  1343. * token.attr,
  1344. nop_func,
  1345. )
  1346. pass
  1347. # FIXME: Is this right?
  1348. self.addRule(
  1349. """
  1350. expr ::= call_ex_kw4
  1351. call_ex_kw4 ::= expr
  1352. expr
  1353. expr
  1354. CALL_FUNCTION_EX
  1355. """,
  1356. nop_func,
  1357. )
  1358. pass
  1359. else:
  1360. super(Python37Parser, self).custom_classfunc_rule(
  1361. opname, token, customize, next_token
  1362. )
  1363. def reduce_is_invalid(self, rule, ast, tokens, first, last):
  1364. invalid = super(Python37Parser, self).reduce_is_invalid(
  1365. rule, ast, tokens, first, last
  1366. )
  1367. if invalid:
  1368. return invalid
  1369. if rule[0] == "call_kw":
  1370. # Make sure we don't derive call_kw
  1371. nt = ast[0]
  1372. while not isinstance(nt, Token):
  1373. if nt[0] == "call_kw":
  1374. return True
  1375. nt = nt[0]
  1376. pass
  1377. pass
  1378. return False
  1379. def info(args):
  1380. # Check grammar
  1381. p = Python37Parser()
  1382. if len(args) > 0:
  1383. arg = args[0]
  1384. if arg == "3.7":
  1385. from uncompyle6.parser.parse37 import Python37Parser
  1386. p = Python37Parser()
  1387. elif arg == "3.8":
  1388. from uncompyle6.parser.parse38 import Python38Parser
  1389. p = Python38Parser()
  1390. else:
  1391. raise RuntimeError("Only 3.7 and 3.8 supported")
  1392. p.check_grammar()
  1393. if len(sys.argv) > 1 and sys.argv[1] == "dump":
  1394. print("-" * 50)
  1395. p.dump_grammar()
  1396. class Python37ParserSingle(Python37Parser, PythonParserSingle):
  1397. pass
  1398. if __name__ == "__main__":
  1399. # Check grammar
  1400. # FIXME: DRY this with other parseXX.py routines
  1401. p = Python37Parser()
  1402. p.check_grammar()
  1403. from xdis.version_info import IS_PYPY, PYTHON_VERSION_TRIPLE
  1404. if PYTHON_VERSION_TRIPLE[:2] == (3, 7):
  1405. lhs, rhs, tokens, right_recursive, dup_rhs = p.check_sets()
  1406. from uncompyle6.scanner import get_scanner
  1407. s = get_scanner(PYTHON_VERSION_TRIPLE, IS_PYPY)
  1408. opcode_set = set(s.opc.opname).union(
  1409. set(
  1410. """JUMP_BACK CONTINUE RETURN_END_IF COME_FROM
  1411. LOAD_GENEXPR LOAD_ASSERT LOAD_SETCOMP LOAD_DICTCOMP LOAD_CLASSNAME
  1412. LAMBDA_MARKER RETURN_LAST
  1413. """.split()
  1414. )
  1415. )
  1416. remain_tokens = set(tokens) - opcode_set
  1417. import re
  1418. remain_tokens = set([re.sub(r"_\d+$", "", t) for t in remain_tokens])
  1419. remain_tokens = set([re.sub("_CONT$", "", t) for t in remain_tokens])
  1420. remain_tokens = set(remain_tokens) - opcode_set
  1421. print(remain_tokens)
  1422. import sys
  1423. if len(sys.argv) > 1:
  1424. from spark_parser.spark import rule2str
  1425. for rule in sorted(p.rule2name.items()):
  1426. print(rule2str(rule[0]))