This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

170 righe
6.3 KiB

  1. // Python 2 grammar for Lark
  2. // NOTE: Work in progress!!! (XXX TODO)
  3. // This grammar should parse all python 2.x code successfully,
  4. // but the resulting parse-tree is still not well-organized.
  5. // Adapted from: https://docs.python.org/2/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() and input() 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 [ "(" [arglist] ")" ] _NEWLINE
  16. decorators: decorator+
  17. decorated: decorators (classdef | funcdef)
  18. funcdef: "def" NAME "(" parameters ")" ":" suite
  19. parameters: [paramlist]
  20. paramlist: param ("," param)* ["," [star_params ["," kw_params] | kw_params]]
  21. | star_params ["," kw_params]
  22. | kw_params
  23. star_params: "*" NAME
  24. kw_params: "**" NAME
  25. param: fpdef ["=" test]
  26. fpdef: NAME | "(" fplist ")"
  27. fplist: fpdef ("," fpdef)* [","]
  28. ?stmt: simple_stmt | compound_stmt
  29. ?simple_stmt: small_stmt (";" small_stmt)* [";"] _NEWLINE
  30. ?small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
  31. | import_stmt | global_stmt | exec_stmt | assert_stmt)
  32. expr_stmt: testlist augassign (yield_expr|testlist) -> augassign2
  33. | testlist ("=" (yield_expr|testlist))+ -> assign
  34. | testlist
  35. augassign: ("+=" | "-=" | "*=" | "/=" | "%=" | "&=" | "|=" | "^=" | "<<=" | ">>=" | "**=" | "//=")
  36. // For normal assignments, additional restrictions enforced by the interpreter
  37. print_stmt: "print" ( [ test ("," test)* [","] ] | ">>" test [ ("," test)+ [","] ] )
  38. del_stmt: "del" exprlist
  39. pass_stmt: "pass"
  40. ?flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
  41. break_stmt: "break"
  42. continue_stmt: "continue"
  43. return_stmt: "return" [testlist]
  44. yield_stmt: yield_expr
  45. raise_stmt: "raise" [test ["," test ["," test]]]
  46. import_stmt: import_name | import_from
  47. import_name: "import" dotted_as_names
  48. import_from: "from" ("."* dotted_name | "."+) "import" ("*" | "(" import_as_names ")" | import_as_names)
  49. ?import_as_name: NAME ["as" NAME]
  50. ?dotted_as_name: dotted_name ["as" NAME]
  51. import_as_names: import_as_name ("," import_as_name)* [","]
  52. dotted_as_names: dotted_as_name ("," dotted_as_name)*
  53. dotted_name: NAME ("." NAME)*
  54. global_stmt: "global" NAME ("," NAME)*
  55. exec_stmt: "exec" expr ["in" test ["," test]]
  56. assert_stmt: "assert" test ["," test]
  57. ?compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
  58. if_stmt: "if" test ":" suite ("elif" test ":" suite)* ["else" ":" suite]
  59. while_stmt: "while" test ":" suite ["else" ":" suite]
  60. for_stmt: "for" exprlist "in" testlist ":" suite ["else" ":" suite]
  61. try_stmt: ("try" ":" suite ((except_clause ":" suite)+ ["else" ":" suite] ["finally" ":" suite] | "finally" ":" suite))
  62. with_stmt: "with" with_item ("," with_item)* ":" suite
  63. with_item: test ["as" expr]
  64. // NB compile.c makes sure that the default except clause is last
  65. except_clause: "except" [test [("as" | ",") test]]
  66. suite: simple_stmt | _NEWLINE _INDENT _NEWLINE? stmt+ _DEDENT _NEWLINE?
  67. // Backward compatibility cruft to support:
  68. // [ x for x in lambda: True, lambda: False if x() ]
  69. // even while also allowing:
  70. // lambda x: 5 if x else 2
  71. // (But not a mix of the two)
  72. testlist_safe: old_test [("," old_test)+ [","]]
  73. old_test: or_test | old_lambdef
  74. old_lambdef: "lambda" [paramlist] ":" old_test
  75. ?test: or_test ["if" or_test "else" test] | lambdef
  76. ?or_test: and_test ("or" and_test)*
  77. ?and_test: not_test ("and" not_test)*
  78. ?not_test: "not" not_test | comparison
  79. ?comparison: expr (comp_op expr)*
  80. comp_op: "<"|">"|"=="|">="|"<="|"<>"|"!="|"in"|"not" "in"|"is"|"is" "not"
  81. ?expr: xor_expr ("|" xor_expr)*
  82. ?xor_expr: and_expr ("^" and_expr)*
  83. ?and_expr: shift_expr ("&" shift_expr)*
  84. ?shift_expr: arith_expr (("<<"|">>") arith_expr)*
  85. ?arith_expr: term (("+"|"-") term)*
  86. ?term: factor (("*"|"/"|"%"|"//") factor)*
  87. ?factor: ("+"|"-"|"~") factor | power
  88. ?power: molecule ["**" factor]
  89. // _trailer: "(" [arglist] ")" | "[" subscriptlist "]" | "." NAME
  90. ?molecule: molecule "(" [arglist] ")" -> func_call
  91. | molecule "[" [subscriptlist] "]" -> getitem
  92. | molecule "." NAME -> getattr
  93. | atom
  94. ?atom: "(" [yield_expr|testlist_comp] ")"
  95. | "[" [listmaker] "]"
  96. | "{" [dictorsetmaker] "}"
  97. | "`" testlist1 "`"
  98. | NAME | number | string+
  99. listmaker: test ( list_for | ("," test)* [","] )
  100. ?testlist_comp: test ( comp_for | ("," test)* [","] )
  101. lambdef: "lambda" [paramlist] ":" test
  102. ?subscriptlist: subscript ("," subscript)* [","]
  103. subscript: "." "." "." | test | [test] ":" [test] [sliceop]
  104. sliceop: ":" [test]
  105. ?exprlist: expr ("," expr)* [","]
  106. ?testlist: test ("," test)* [","]
  107. dictorsetmaker: ( (test ":" test (comp_for | ("," test ":" test)* [","])) | (test (comp_for | ("," test)* [","])) )
  108. classdef: "class" NAME ["(" [testlist] ")"] ":" suite
  109. arglist: (argument ",")* (argument [","]
  110. | star_args ["," kw_args]
  111. | kw_args)
  112. star_args: "*" test
  113. kw_args: "**" test
  114. // The reason that keywords are test nodes instead of NAME is that using NAME
  115. // results in an ambiguity. ast.c makes sure it's a NAME.
  116. argument: test [comp_for] | test "=" test
  117. list_iter: list_for | list_if
  118. list_for: "for" exprlist "in" testlist_safe [list_iter]
  119. list_if: "if" old_test [list_iter]
  120. comp_iter: comp_for | comp_if
  121. comp_for: "for" exprlist "in" or_test [comp_iter]
  122. comp_if: "if" old_test [comp_iter]
  123. testlist1: test ("," test)*
  124. yield_expr: "yield" [testlist]
  125. number: DEC_NUMBER | HEX_NUMBER | OCT_NUMBER | FLOAT | IMAG_NUMBER
  126. string: STRING | LONG_STRING
  127. // Tokens
  128. COMMENT: /\#[^\n]*/
  129. _NEWLINE: ( /\r?\n[\t ]*/ | COMMENT )+
  130. %ignore /[\t \f]+/ // WS
  131. %ignore /\\\\[\t \f]*\r?\n/ // LINE_CONT
  132. %ignore COMMENT
  133. STRING : /(?i)[ub]?r?("(?!"").*?(?<!\\\\)(\\\\\\\\)*?"|'(?!'').*?(?<!\\\\)(\\\\\\\\)*?')/
  134. LONG_STRING: /(?i)(?s)[ub]?r?(""".*?(?<!\\\\)(\\\\\\\\)*?"""|'''.*?(?<!\\\\)(\\\\\\\\)*?''')/
  135. DEC_NUMBER: /(?i)[1-9]\d*l?/
  136. HEX_NUMBER: /(?i)0x[\da-f]*l?/
  137. OCT_NUMBER: /(?i)0o?[0-7]*l?/
  138. %import common.FLOAT -> FLOAT
  139. %import common.INT -> _INT
  140. %import common.CNAME -> NAME
  141. IMAG_NUMBER: (_INT | FLOAT) ("j"|"J")
  142. _DEDENT: "<DEDENT>"
  143. _INDENT: "<INDENT>"