This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
7.8 KiB

  1. // Python 3 grammar for Lark
  2. // NOTE: Work in progress!!! (XXX TODO)
  3. // This grammar should parse all python 3.x code successfully,
  4. // but the resulting parse-tree is still not well-organized.
  5. // Adapted from: https://docs.python.org/3/reference/grammar.html
  6. // Adapted by: Erez Shinan
  7. // Start symbols for the grammar:
  8. // single_input is a single interactive statement;
  9. // file_input is a module or sequence of commands read from an input file;
  10. // eval_input is the input for the eval() functions.
  11. // NB: compound_stmt in single_input is followed by extra NEWLINE!
  12. single_input: _NEWLINE | simple_stmt | compound_stmt _NEWLINE
  13. file_input: (_NEWLINE | stmt)*
  14. eval_input: testlist _NEWLINE*
  15. decorator: "@" dotted_name [ "(" [arguments] ")" ] _NEWLINE
  16. decorators: decorator+
  17. decorated: decorators (classdef | funcdef | async_funcdef)
  18. async_funcdef: "async" funcdef
  19. funcdef: "def" NAME "(" [parameters] ")" ["->" test] ":" suite
  20. parameters: paramvalue ("," paramvalue)* ["," SLASH] ["," [starparams | kwparams]]
  21. | starparams
  22. | kwparams
  23. SLASH: "/" // Otherwise the it will completely disappear and it will be undisguisable in the result
  24. starparams: "*" typedparam? ("," paramvalue)* ["," kwparams]
  25. kwparams: "**" typedparam ","?
  26. ?paramvalue: typedparam ("=" test)?
  27. ?typedparam: NAME (":" test)?
  28. lambdef: "lambda" [lambda_params] ":" test
  29. lambdef_nocond: "lambda" [lambda_params] ":" test_nocond
  30. lambda_params: lambda_paramvalue ("," lambda_paramvalue)* ["," [lambda_starparams | lambda_kwparams]]
  31. | lambda_starparams
  32. | lambda_kwparams
  33. ?lambda_paramvalue: NAME ("=" test)?
  34. lambda_starparams: "*" [NAME] ("," lambda_paramvalue)* ["," [lambda_kwparams]]
  35. lambda_kwparams: "**" NAME ","?
  36. ?stmt: simple_stmt | compound_stmt
  37. ?simple_stmt: small_stmt (";" small_stmt)* [";"] _NEWLINE
  38. ?small_stmt: (expr_stmt | assign_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
  39. expr_stmt: testlist_star_expr
  40. assign_stmt: annassign | augassign | assign
  41. annassign: testlist_star_expr ":" test ["=" test]
  42. assign: testlist_star_expr ("=" (yield_expr|testlist_star_expr))+
  43. augassign: testlist_star_expr augassign_op (yield_expr|testlist)
  44. !augassign_op: "+=" | "-=" | "*=" | "@=" | "/=" | "%=" | "&=" | "|=" | "^=" | "<<=" | ">>=" | "**=" | "//="
  45. ?testlist_star_expr: test_or_star_expr
  46. | test_or_star_expr ("," test_or_star_expr)+ ","? -> tuple
  47. | test_or_star_expr "," -> tuple
  48. // For normal and annotated assignments, additional restrictions enforced by the interpreter
  49. del_stmt: "del" exprlist
  50. pass_stmt: "pass"
  51. ?flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
  52. break_stmt: "break"
  53. continue_stmt: "continue"
  54. return_stmt: "return" [testlist]
  55. yield_stmt: yield_expr
  56. raise_stmt: "raise" [test ["from" test]]
  57. import_stmt: import_name | import_from
  58. import_name: "import" dotted_as_names
  59. // note below: the ("." | "...") is necessary because "..." is tokenized as ELLIPSIS
  60. import_from: "from" (dots? dotted_name | dots) "import" ("*" | "(" import_as_names ")" | import_as_names)
  61. !dots: "."+
  62. import_as_name: NAME ["as" NAME]
  63. dotted_as_name: dotted_name ["as" NAME]
  64. import_as_names: import_as_name ("," import_as_name)* [","]
  65. dotted_as_names: dotted_as_name ("," dotted_as_name)*
  66. dotted_name: NAME ("." NAME)*
  67. global_stmt: "global" NAME ("," NAME)*
  68. nonlocal_stmt: "nonlocal" NAME ("," NAME)*
  69. assert_stmt: "assert" test ["," test]
  70. ?compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt
  71. async_stmt: "async" (funcdef | with_stmt | for_stmt)
  72. if_stmt: "if" test ":" suite elifs ["else" ":" suite]
  73. elifs: elif_*
  74. elif_: "elif" test ":" suite
  75. while_stmt: "while" test ":" suite ["else" ":" suite]
  76. for_stmt: "for" exprlist "in" testlist ":" suite ["else" ":" suite]
  77. try_stmt: "try" ":" suite except_clauses ["else" ":" suite] [finally]
  78. | "try" ":" suite finally -> try_finally
  79. finally: "finally" ":" suite
  80. except_clauses: except_clause+
  81. except_clause: "except" [test ["as" NAME]] ":" suite
  82. with_stmt: "with" with_items ":" suite
  83. with_items: with_item ("," with_item)*
  84. with_item: test ["as" expr]
  85. // NB compile.c makes sure that the default except clause is last
  86. suite: simple_stmt | _NEWLINE _INDENT stmt+ _DEDENT
  87. ?test: or_test ("if" or_test "else" test)?
  88. | lambdef
  89. ?test_nocond: or_test | lambdef_nocond
  90. ?or_test: and_test ("or" and_test)*
  91. ?and_test: not_test ("and" not_test)*
  92. ?not_test: "not" not_test -> not_test
  93. | comparison
  94. ?comparison: expr (comp_op expr)*
  95. star_expr: "*" expr
  96. ?expr: or_expr
  97. ?or_expr: xor_expr ("|" xor_expr)*
  98. ?xor_expr: and_expr ("^" and_expr)*
  99. ?and_expr: shift_expr ("&" shift_expr)*
  100. ?shift_expr: arith_expr (_shift_op arith_expr)*
  101. ?arith_expr: term (_add_op term)*
  102. ?term: factor (_mul_op factor)*
  103. ?factor: _unary_op factor | power
  104. !_unary_op: "+"|"-"|"~"
  105. !_add_op: "+"|"-"
  106. !_shift_op: "<<"|">>"
  107. !_mul_op: "*"|"@"|"/"|"%"|"//"
  108. // <> isn't actually a valid comparison operator in Python. It's here for the
  109. // sake of a __future__ import described in PEP 401 (which really works :-)
  110. !comp_op: "<"|">"|"=="|">="|"<="|"<>"|"!="|"in"|"not" "in"|"is"|"is" "not"
  111. ?power: await_expr ("**" factor)?
  112. ?await_expr: AWAIT? atom_expr
  113. AWAIT: "await"
  114. ?atom_expr: atom_expr "(" [arguments] ")" -> funccall
  115. | atom_expr "[" subscriptlist "]" -> getitem
  116. | atom_expr "." NAME -> getattr
  117. | atom
  118. ?atom: "(" yield_expr ")"
  119. | "(" _tuple_inner? ")" -> tuple
  120. | "(" comprehension{test_or_star_expr} ")" -> tuple_comprehension
  121. | "[" _testlist_comp? "]" -> list
  122. | "[" comprehension{test_or_star_expr} "]" -> list_comprehension
  123. | "{" _dict_exprlist? "}" -> dict
  124. | "{" comprehension{key_value} "}" -> dict_comprehension
  125. | "{" _set_exprlist "}" -> set
  126. | "{" comprehension{test} "}" -> set_comprehension
  127. | NAME -> var
  128. | number
  129. | string_concat
  130. | "(" test ")"
  131. | "..." -> ellipsis
  132. | "None" -> const_none
  133. | "True" -> const_true
  134. | "False" -> const_false
  135. ?string_concat: string+
  136. _testlist_comp: test | _tuple_inner
  137. _tuple_inner: test_or_star_expr (("," test_or_star_expr)+ [","] | ",")
  138. ?test_or_star_expr: test
  139. | star_expr
  140. ?subscriptlist: subscript
  141. | subscript (("," subscript)+ [","] | ",") -> subscript_tuple
  142. ?subscript: test | ([test] ":" [test] [sliceop]) -> slice
  143. sliceop: ":" [test]
  144. ?exprlist: (expr|star_expr)
  145. | (expr|star_expr) (("," (expr|star_expr))+ [","]|",")
  146. ?testlist: test | testlist_tuple
  147. testlist_tuple: test (("," test)+ [","] | ",")
  148. _dict_exprlist: (key_value | "**" expr) ("," (key_value | "**" expr))* [","]
  149. key_value: test ":" test
  150. _set_exprlist: test_or_star_expr ("," test_or_star_expr)* [","]
  151. classdef: "class" NAME ["(" [arguments] ")"] ":" suite
  152. arguments: argvalue ("," argvalue)* ("," [ starargs | kwargs])?
  153. | starargs
  154. | kwargs
  155. | comprehension{test}
  156. starargs: stararg ("," stararg)* ("," argvalue)* ["," kwargs]
  157. stararg: "*" test
  158. kwargs: "**" test
  159. ?argvalue: test ("=" test)?
  160. comprehension{comp_result}: comp_result comp_fors [comp_if]
  161. comp_fors: comp_for+
  162. comp_for: [ASYNC] "for" exprlist "in" or_test
  163. ASYNC: "async"
  164. ?comp_if: "if" test_nocond
  165. // not used in grammar, but may appear in "node" passed from Parser to Compiler
  166. encoding_decl: NAME
  167. yield_expr: "yield" [testlist]
  168. | "yield" "from" test -> yield_from
  169. number: DEC_NUMBER | HEX_NUMBER | BIN_NUMBER | OCT_NUMBER | FLOAT_NUMBER | IMAG_NUMBER
  170. string: STRING | LONG_STRING
  171. // Import terminals from standard library (grammars/python.lark)
  172. %import python (NAME, COMMENT, STRING, LONG_STRING)
  173. %import python (DEC_NUMBER, HEX_NUMBER, OCT_NUMBER, BIN_NUMBER, FLOAT_NUMBER, IMAG_NUMBER)
  174. // Other terminals
  175. _NEWLINE: ( /\r?\n[\t ]*/ | COMMENT )+
  176. %ignore /[\t \f]+/ // WS
  177. %ignore /\\[\t \f]*\r?\n/ // LINE_CONT
  178. %ignore COMMENT
  179. %declare _INDENT _DEDENT