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.

193 lines
7.1 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. varargslist: (vfpdef ["=" test] ("," vfpdef ["=" test])* ["," [ "*" [vfpdef] ("," vfpdef ["=" test])* ["," ["**" vfpdef [","]]] | "**" vfpdef [","]]]
  29. | "*" [vfpdef] ("," vfpdef ["=" test])* ["," ["**" vfpdef [","]]]
  30. | "**" vfpdef [","])
  31. vfpdef: NAME
  32. ?stmt: simple_stmt | compound_stmt
  33. ?simple_stmt: small_stmt (";" small_stmt)* [";"] _NEWLINE
  34. ?small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
  35. ?expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist)
  36. | ("=" (yield_expr|testlist_star_expr))*)
  37. annassign: ":" test ["=" test]
  38. ?testlist_star_expr: (test|star_expr) ("," (test|star_expr))* [","]
  39. !augassign: ("+=" | "-=" | "*=" | "@=" | "/=" | "%=" | "&=" | "|=" | "^=" | "<<=" | ">>=" | "**=" | "//=")
  40. // For normal and annotated assignments, additional restrictions enforced by the interpreter
  41. del_stmt: "del" exprlist
  42. pass_stmt: "pass"
  43. ?flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
  44. break_stmt: "break"
  45. continue_stmt: "continue"
  46. return_stmt: "return" [testlist]
  47. yield_stmt: yield_expr
  48. raise_stmt: "raise" [test ["from" test]]
  49. import_stmt: import_name | import_from
  50. import_name: "import" dotted_as_names
  51. // note below: the ("." | "...") is necessary because "..." is tokenized as ELLIPSIS
  52. import_from: "from" (dots? dotted_name | dots) "import" ("*" | "(" import_as_names ")" | import_as_names)
  53. !dots: "."+
  54. import_as_name: NAME ["as" NAME]
  55. dotted_as_name: dotted_name ["as" NAME]
  56. import_as_names: import_as_name ("," import_as_name)* [","]
  57. dotted_as_names: dotted_as_name ("," dotted_as_name)*
  58. dotted_name: NAME ("." NAME)*
  59. global_stmt: "global" NAME ("," NAME)*
  60. nonlocal_stmt: "nonlocal" NAME ("," NAME)*
  61. assert_stmt: "assert" test ["," test]
  62. compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt
  63. async_stmt: "async" (funcdef | with_stmt | for_stmt)
  64. if_stmt: "if" test ":" suite ("elif" test ":" suite)* ["else" ":" suite]
  65. while_stmt: "while" test ":" suite ["else" ":" suite]
  66. for_stmt: "for" exprlist "in" testlist ":" suite ["else" ":" suite]
  67. try_stmt: ("try" ":" suite ((except_clause ":" suite)+ ["else" ":" suite] ["finally" ":" suite] | "finally" ":" suite))
  68. with_stmt: "with" with_item ("," with_item)* ":" suite
  69. with_item: test ["as" expr]
  70. // NB compile.c makes sure that the default except clause is last
  71. except_clause: "except" [test ["as" NAME]]
  72. suite: simple_stmt | _NEWLINE _INDENT stmt+ _DEDENT
  73. ?test: or_test ("if" or_test "else" test)? | lambdef
  74. ?test_nocond: or_test | lambdef_nocond
  75. lambdef: "lambda" [varargslist] ":" test
  76. lambdef_nocond: "lambda" [varargslist] ":" test_nocond
  77. ?or_test: and_test ("or" and_test)*
  78. ?and_test: not_test ("and" not_test)*
  79. ?not_test: "not" not_test -> not
  80. | comparison
  81. ?comparison: expr (_comp_op expr)*
  82. star_expr: "*" expr
  83. ?expr: xor_expr ("|" xor_expr)*
  84. ?xor_expr: and_expr ("^" and_expr)*
  85. ?and_expr: shift_expr ("&" shift_expr)*
  86. ?shift_expr: arith_expr (_shift_op arith_expr)*
  87. ?arith_expr: term (_add_op term)*
  88. ?term: factor (_mul_op factor)*
  89. ?factor: _factor_op factor | power
  90. !_factor_op: "+"|"-"|"~"
  91. !_add_op: "+"|"-"
  92. !_shift_op: "<<"|">>"
  93. !_mul_op: "*"|"@"|"/"|"%"|"//"
  94. // <> isn't actually a valid comparison operator in Python. It's here for the
  95. // sake of a __future__ import described in PEP 401 (which really works :-)
  96. !_comp_op: "<"|">"|"=="|">="|"<="|"<>"|"!="|"in"|"not" "in"|"is"|"is" "not"
  97. ?power: await_expr ("**" factor)?
  98. ?await_expr: AWAIT? atom_expr
  99. AWAIT: "await"
  100. ?atom_expr: atom_expr "(" [arguments] ")" -> funccall
  101. | atom_expr "[" subscriptlist "]" -> getitem
  102. | atom_expr "." NAME -> getattr
  103. | atom
  104. ?atom: "(" [yield_expr|tuplelist_comp] ")" -> tuple
  105. | "[" [testlist_comp] "]" -> list
  106. | "{" [dict_comp] "}" -> dict
  107. | "{" set_comp "}" -> set
  108. | NAME -> var
  109. | number | string+
  110. | "(" test ")"
  111. | "..." -> ellipsis
  112. | "None" -> const_none
  113. | "True" -> const_true
  114. | "False" -> const_false
  115. ?testlist_comp: test | tuplelist_comp
  116. tuplelist_comp: (test|star_expr) (comp_for | ("," (test|star_expr))+ [","] | ",")
  117. ?subscriptlist: subscript
  118. | subscript (("," subscript)+ [","] | ",") -> subscript_tuple
  119. subscript: test | ([test] ":" [test] [sliceop]) -> slice
  120. sliceop: ":" [test]
  121. exprlist: (expr|star_expr)
  122. | (expr|star_expr) (("," (expr|star_expr))+ [","]|",") -> exprlist_tuple
  123. testlist: test | testlist_tuple
  124. testlist_tuple: test (("," test)+ [","] | ",")
  125. dict_comp: key_value comp_for
  126. | (key_value | "**" expr) ("," (key_value | "**" expr))* [","]
  127. key_value: test ":" test
  128. set_comp: test comp_for
  129. | (test|star_expr) ("," (test | star_expr))* [","]
  130. classdef: "class" NAME ["(" [arguments] ")"] ":" suite
  131. arguments: argvalue ("," argvalue)* ("," [ starargs | kwargs])?
  132. | starargs
  133. | kwargs
  134. | test comp_for
  135. starargs: "*" test ("," "*" test)* ("," argvalue)* ["," kwargs]
  136. kwargs: "**" test
  137. ?argvalue: test ("=" test)?
  138. comp_iter: comp_for | comp_if | async_for
  139. async_for: "async" "for" exprlist "in" or_test [comp_iter]
  140. comp_for: "for" exprlist "in" or_test [comp_iter]
  141. comp_if: "if" test_nocond [comp_iter]
  142. // not used in grammar, but may appear in "node" passed from Parser to Compiler
  143. encoding_decl: NAME
  144. yield_expr: "yield" [yield_arg]
  145. yield_arg: "from" test | testlist
  146. number: DEC_NUMBER | HEX_NUMBER | BIN_NUMBER | OCT_NUMBER | FLOAT_NUMBER | IMAG_NUMBER
  147. string: STRING | LONG_STRING
  148. // Import terminals from standard library (grammars/python.lark)
  149. %import python (NAME, COMMENT, STRING, LONG_STRING)
  150. %import python (DEC_NUMBER, HEX_NUMBER, OCT_NUMBER, BIN_NUMBER, FLOAT_NUMBER, IMAG_NUMBER)
  151. // Other terminals
  152. _NEWLINE: ( /\r?\n[\t ]*/ | COMMENT )+
  153. %ignore /[\t \f]+/ // WS
  154. %ignore /\\[\t \f]*\r?\n/ // LINE_CONT
  155. %ignore COMMENT
  156. %declare _INDENT _DEDENT