1 | # Demo of grammar for Oil.
|
2 | #
|
3 | # We might use pgen2 for the expression language, since there are many cases
|
4 | # that don't fit within the model of Pratt parsing (literals, list
|
5 | # comprehensions, type expressions, etc.)
|
6 |
|
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)* ENDMARKER
|
14 | eval_input: testlist NEWLINE* ENDMARKER
|
15 |
|
16 | # added for Oil
|
17 | arglist_input: arglist NEWLINE* ENDMARKER
|
18 |
|
19 | funcdef: 'def' NAME parameters ':' suite
|
20 | parameters: '(' [varargslist] ')'
|
21 | varargslist: ((fpdef ['=' test] ',')*
|
22 | ('*' NAME [',' '**' NAME] | '**' NAME) |
|
23 | fpdef ['=' test] (',' fpdef ['=' test])* [','])
|
24 | fpdef: NAME | '(' fplist ')'
|
25 | fplist: fpdef (',' fpdef)* [',']
|
26 |
|
27 | stmt: simple_stmt | compound_stmt
|
28 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
|
29 | small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
|
30 | assert_stmt)
|
31 | expr_stmt: testlist (augassign (yield_expr|testlist) |
|
32 | ('=' (yield_expr|testlist))*)
|
33 | augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | 'xor=' |
|
34 | '<<=' | '>>=' | '^=' | 'div=')
|
35 | # For normal assignments, additional restrictions enforced by the interpreter
|
36 | print_stmt: 'print' ( [ test (',' test)* [','] ] |
|
37 | '>>' 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 | assert_stmt: 'assert' test [',' test]
|
47 |
|
48 | compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef
|
49 | if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
|
50 | while_stmt: 'while' test ':' suite ['else' ':' suite]
|
51 | for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
|
52 | try_stmt: ('try' ':' suite
|
53 | ((except_clause ':' suite)+
|
54 | ['else' ':' suite]
|
55 | ['finally' ':' suite] |
|
56 | 'finally' ':' suite))
|
57 | with_stmt: 'with' with_item (',' with_item)* ':' suite
|
58 | with_item: test ['as' expr]
|
59 | # NB compile.c makes sure that the default except clause is last
|
60 | except_clause: 'except' [test [('as' | ',') test]]
|
61 | suite: simple_stmt # removed
|
62 |
|
63 | # Backward compatibility cruft to support:
|
64 | # [ x for x in lambda: True, lambda: False if x() ]
|
65 | # even while also allowing:
|
66 | # lambda x: 5 if x else 2
|
67 | # (But not a mix of the two)
|
68 | testlist_safe: old_test [(',' old_test)+ [',']]
|
69 | old_test: or_test | old_lambdef
|
70 | old_lambdef: 'lambda' [varargslist] ':' old_test
|
71 |
|
72 | test: or_test ['if' or_test 'else' test] | lambdef
|
73 | or_test: and_test ('or' and_test)*
|
74 | and_test: not_test ('and' not_test)*
|
75 | not_test: 'not' not_test | comparison
|
76 | comparison: expr (comp_op expr)*
|
77 | comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'
|
78 | expr: xor_expr ('|' xor_expr)*
|
79 | xor_expr: and_expr ('^' and_expr)*
|
80 | and_expr: shift_expr ('&' shift_expr)*
|
81 | shift_expr: arith_expr (('<<'|'>>') arith_expr)*
|
82 | arith_expr: term (('+'|'-') term)*
|
83 | term: factor (('*'|'/'|'%'|'div') factor)*
|
84 | factor: ('+'|'-'|'~') factor | power
|
85 | power: atom trailer* ['^' factor]
|
86 | atom: ('(' [yield_expr|testlist_comp] ')' |
|
87 | '[' [listmaker] ']' |
|
88 | '{' [dictorsetmaker] '}' |
|
89 | NAME | NUMBER) # removed string!
|
90 | listmaker: test ( list_for | (',' test)* [','] )
|
91 | testlist_comp: test ( comp_for | (',' test)* [','] )
|
92 | lambdef: 'lambda' [varargslist] ':' test
|
93 | trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
|
94 | subscriptlist: subscript (',' subscript)* [',']
|
95 | subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
|
96 | sliceop: ':' [test]
|
97 | exprlist: expr (',' expr)* [',']
|
98 | testlist: test (',' test)* [',']
|
99 | dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) |
|
100 | (test (comp_for | (',' test)* [','])) )
|
101 |
|
102 | classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
|
103 |
|
104 | arglist: (argument ',')* (argument [',']
|
105 | |'*' test (',' argument)* [',' '**' test]
|
106 | |'**' test)
|
107 | # The reason that keywords are test nodes instead of NAME is that using NAME
|
108 | # results in an ambiguity. ast.c makes sure it's a NAME.
|
109 | argument: test [comp_for] | test '=' test
|
110 |
|
111 | list_iter: list_for | list_if
|
112 | list_for: 'for' exprlist 'in' testlist_safe [list_iter]
|
113 | list_if: 'if' old_test [list_iter]
|
114 |
|
115 | comp_iter: comp_for | comp_if
|
116 | comp_for: 'for' exprlist 'in' or_test [comp_iter]
|
117 | comp_if: 'if' old_test [comp_iter]
|
118 |
|
119 | testlist1: test (',' test)*
|
120 |
|
121 | yield_expr: 'yield' [testlist]
|
122 |
|
123 |
|
124 | # Oil types are like MyPy types
|
125 | # Python 3 uses the much more general "test" production.
|
126 |
|
127 | type_expr: NAME ['<' type_expr (',' type_expr)* '>' ]
|
128 |
|
129 | # added for Oil
|
130 | type_input: type_expr NEWLINE* ENDMARKER
|
131 |
|