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.

226 lines
7.7 KiB

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