Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2021
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.05 KB | None | 0 0
  1. single_input: _NEWLINE | simple_stmt | compound_stmt _NEWLINE
  2. file_input: (_NEWLINE | stmt)*
  3. eval_input: testlist _NEWLINE*
  4.  
  5. decorator: "@" dotted_name [ "(" [arguments] ")" ] _NEWLINE
  6. decorators: decorator+
  7. decorated: decorators (classdef | funcdef | async_funcdef)
  8.  
  9. // Custom Greek terminals
  10. FUNCDECL: "συνάρτηση" | "συναρτηση"
  11. PRINT: "τύπωσε" | "τυπωσε"
  12. FROM: "από" | "απο"
  13. IMPORT: "εισήγαγε" | "εισηγαγε"
  14.  
  15. // -------------------------- //
  16.  
  17. async_funcdef: "async" funcdef
  18. funcdef: FUNCDECL NAME "(" parameters? ")" ["->" test] ":" suite
  19.  
  20. parameters: paramvalue ("," paramvalue)* ["," SLASH] ["," [starparams | kwparams]]
  21.           | starparams
  22.           | kwparams
  23.  
  24. SLASH: "/" // Otherwise the it will completely disappear and it will be undisguisable in the result
  25. starparams: "*" typedparam? ("," paramvalue)* ["," kwparams]
  26. kwparams: "**" typedparam
  27.  
  28. ?paramvalue: typedparam ["=" test]
  29. ?typedparam: NAME [":" test]
  30.  
  31. varargslist: (vfpdef ["=" test] ("," vfpdef ["=" test])* ["," [ "*" [vfpdef] ("," vfpdef ["=" test])* ["," ["**" vfpdef [","]]] | "**" vfpdef [","]]]
  32.   | "*" [vfpdef] ("," vfpdef ["=" test])* ["," ["**" vfpdef [","]]]
  33.   | "**" vfpdef [","])
  34.  
  35. vfpdef: NAME
  36.  
  37. ?stmt: simple_stmt | compound_stmt
  38. ?simple_stmt: small_stmt (";" small_stmt)* [";"] _NEWLINE
  39. ?small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
  40. ?expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist)
  41.          | ("=" (yield_expr|testlist_star_expr))*)
  42. annassign: ":" test ["=" test]
  43. ?testlist_star_expr: (test|star_expr) ("," (test|star_expr))* [","]
  44. !augassign: ("+=" | "-=" | "*=" | "@=" | "/=" | "%=" | "&=" | "|=" | "^=" | "<<=" | ">>=" | "**=" | "//=")
  45. // For normal and annotated assignments, additional restrictions enforced by the interpreter
  46. del_stmt: "del" exprlist
  47. pass_stmt: "pass"
  48. ?flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
  49. break_stmt: "break"
  50. continue_stmt: "continue"
  51. return_stmt: "return" [testlist]
  52. yield_stmt: yield_expr
  53. raise_stmt: "raise" [test ["from" test]]
  54. import_stmt: import_name | import_from
  55. import_name: "import" dotted_as_names
  56. // note below: the ("." | "...") is necessary because "..." is tokenized as ELLIPSIS
  57. import_from: FROM (dots? dotted_name | dots) IMPORT ("*" | "(" import_as_names ")" | import_as_names)
  58. !dots: "."+
  59. import_as_name: NAME ["as" NAME]
  60. dotted_as_name: dotted_name ["as" NAME]
  61. import_as_names: import_as_name ("," import_as_name)* [","]
  62. dotted_as_names: dotted_as_name ("," dotted_as_name)*
  63. dotted_name: NAME ("." NAME)*
  64. global_stmt: "global" NAME ("," NAME)*
  65. nonlocal_stmt: "nonlocal" NAME ("," NAME)*
  66. assert_stmt: "assert" test ["," test]
  67.  
  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 ("elif" test ":" suite)* ["else" ":" suite]
  71. while_stmt: "while" test ":" suite ["else" ":" suite]
  72. for_stmt: "for" exprlist "in" testlist ":" suite ["else" ":" suite]
  73. try_stmt: ("try" ":" suite ((except_clause ":" suite)+ ["else" ":" suite] ["finally" ":" suite] | "finally" ":" suite))
  74. with_stmt: "with" with_item ("," with_item)*  ":" suite
  75. with_item: test ["as" expr]
  76. // NB compile.c makes sure that the default except clause is last
  77. except_clause: "except" [test ["as" NAME]]
  78. suite: simple_stmt | _NEWLINE _INDENT stmt+ _DEDENT
  79.  
  80. ?test: or_test ("if" or_test "else" test)? | lambdef
  81. ?test_nocond: or_test | lambdef_nocond
  82. lambdef: "lambda" [varargslist] ":" test
  83. lambdef_nocond: "lambda" [varargslist] ":" test_nocond
  84. ?or_test: and_test ("or" and_test)*
  85. ?and_test: not_test ("and" not_test)*
  86. ?not_test: "not" not_test -> not
  87.          | comparison
  88. ?comparison: expr (_comp_op expr)*
  89. star_expr: "*" expr
  90. ?expr: xor_expr ("|" xor_expr)*
  91. ?xor_expr: and_expr ("^" and_expr)*
  92. ?and_expr: shift_expr ("&" shift_expr)*
  93. ?shift_expr: arith_expr (_shift_op arith_expr)*
  94. ?arith_expr: term (_add_op term)*
  95. ?term: factor (_mul_op factor)*
  96. ?factor: _factor_op factor | power
  97.  
  98. !_factor_op: "+"|"-"|"~"
  99. !_add_op: "+"|"-"
  100. !_shift_op: "<<"|">>"
  101. !_mul_op: "*"|"@"|"/"|"%"|"//"
  102. // <> isn't actually a valid comparison operator in Python. It's here for the
  103. // sake of a __future__ import described in PEP 401 (which really works :-)
  104. !_comp_op: "<"|">"|"=="|">="|"<="|"<>"|"!="|"in"|"not" "in"|"is"|"is" "not"
  105.  
  106. ?power: await_expr ("**" factor)?
  107. ?await_expr: AWAIT? atom_expr
  108. AWAIT: "await"
  109.  
  110. ?atom_expr: (atom_expr | PRINT) "(" [arguments] ")"      -> funccall
  111.           | atom_expr "[" subscriptlist "]"  -> getitem
  112.           | atom_expr "." NAME               -> getattr
  113.           | atom
  114.  
  115. ?atom: "(" [yield_expr|tuplelist_comp] ")" -> tuple
  116.      | "[" [testlist_comp] "]"  -> list
  117.      | "{" [dict_comp] "}" -> dict
  118.      | "{" set_comp "}" -> set
  119.      | NAME -> var
  120.      | number | string+
  121.      | "(" test ")"
  122.      | "..." -> ellipsis
  123.      | "None"    -> const_none
  124.      | "True"    -> const_true
  125.      | "False"   -> const_false
  126.  
  127. ?testlist_comp: test | tuplelist_comp
  128. tuplelist_comp: (test|star_expr) (comp_for | ("," (test|star_expr))+ [","] | ",")
  129. ?subscriptlist: subscript
  130.               | subscript (("," subscript)+ [","] | ",") -> subscript_tuple
  131. subscript: test | ([test] ":" [test] [sliceop]) -> slice
  132. sliceop: ":" [test]
  133. exprlist: (expr|star_expr)
  134.         | (expr|star_expr) (("," (expr|star_expr))+ [","]|",") -> exprlist_tuple
  135. testlist: test | testlist_tuple
  136. testlist_tuple: test (("," test)+ [","] | ",")
  137. dict_comp: key_value comp_for
  138.          | (key_value | "**" expr) ("," (key_value | "**" expr))* [","]
  139.  
  140. key_value: test ":"  test
  141.  
  142. set_comp: test comp_for
  143.         | (test|star_expr) ("," (test | star_expr))* [","]
  144.  
  145. classdef: "class" NAME ["(" [arguments] ")"] ":" suite
  146.  
  147. arguments: argvalue ("," argvalue)*  ("," [ starargs | kwargs])?
  148.          | starargs
  149.          | kwargs
  150.          | test comp_for
  151.  
  152. starargs: "*" test ("," "*" test)* ("," argvalue)* ["," kwargs]
  153. kwargs: "**" test
  154.  
  155. ?argvalue: test ("=" test)?
  156.  
  157.  
  158.  
  159. comp_iter: comp_for | comp_if | async_for
  160. async_for: "async" "for" exprlist "in" or_test [comp_iter]
  161. comp_for: "for" exprlist "in" or_test [comp_iter]
  162. comp_if: "if" test_nocond [comp_iter]
  163.  
  164. // not used in grammar, but may appear in "node" passed from Parser to Compiler
  165. encoding_decl: NAME
  166.  
  167. yield_expr: "yield" [yield_arg]
  168. yield_arg: "from" test | testlist
  169.  
  170.  
  171. number: DEC_NUMBER | HEX_NUMBER | BIN_NUMBER | OCT_NUMBER | FLOAT_NUMBER | IMAG_NUMBER
  172. string: STRING | LONG_STRING
  173.  
  174. // Import terminals from standard library (grammars/python.lark)
  175. // %import python (NAME, COMMENT, STRING, LONG_STRING)  // Extend NAME to include greek keywords
  176. NAME: /([a-zA-Z_]\w*|[\p{Greek}]*)/
  177. %import python (COMMENT, STRING, LONG_STRING)
  178. %import python (DEC_NUMBER, HEX_NUMBER, OCT_NUMBER, BIN_NUMBER, FLOAT_NUMBER, IMAG_NUMBER)
  179.  
  180.  
  181. // Other terminals
  182.  
  183. _NEWLINE: ( /\r?\n[\t ]*/ | COMMENT )+
  184.  
  185. %ignore /[\t \f]+/  // WS
  186. %ignore /\\[\t \f]*\r?\n/   // LINE_CONT
  187. %ignore COMMENT
  188. %declare _INDENT _DEDENT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement