OILS / pgen2 / oil.grammar View on Github | oilshell.org

131 lines, 113 significant
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!
12single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
13file_input: (NEWLINE | stmt)* ENDMARKER
14eval_input: testlist NEWLINE* ENDMARKER
15
16# added for Oil
17arglist_input: arglist NEWLINE* ENDMARKER
18
19funcdef: 'def' NAME parameters ':' suite
20parameters: '(' [varargslist] ')'
21varargslist: ((fpdef ['=' test] ',')*
22 ('*' NAME [',' '**' NAME] | '**' NAME) |
23 fpdef ['=' test] (',' fpdef ['=' test])* [','])
24fpdef: NAME | '(' fplist ')'
25fplist: fpdef (',' fpdef)* [',']
26
27stmt: simple_stmt | compound_stmt
28simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
29small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
30 assert_stmt)
31expr_stmt: testlist (augassign (yield_expr|testlist) |
32 ('=' (yield_expr|testlist))*)
33augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | 'xor=' |
34 '<<=' | '>>=' | '^=' | 'div=')
35# For normal assignments, additional restrictions enforced by the interpreter
36print_stmt: 'print' ( [ test (',' test)* [','] ] |
37 '>>' test [ (',' test)+ [','] ] )
38del_stmt: 'del' exprlist
39pass_stmt: 'pass'
40flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
41break_stmt: 'break'
42continue_stmt: 'continue'
43return_stmt: 'return' [testlist]
44yield_stmt: yield_expr
45raise_stmt: 'raise' [test [',' test [',' test]]]
46assert_stmt: 'assert' test [',' test]
47
48compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef
49if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
50while_stmt: 'while' test ':' suite ['else' ':' suite]
51for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
52try_stmt: ('try' ':' suite
53 ((except_clause ':' suite)+
54 ['else' ':' suite]
55 ['finally' ':' suite] |
56 'finally' ':' suite))
57with_stmt: 'with' with_item (',' with_item)* ':' suite
58with_item: test ['as' expr]
59# NB compile.c makes sure that the default except clause is last
60except_clause: 'except' [test [('as' | ',') test]]
61suite: 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)
68testlist_safe: old_test [(',' old_test)+ [',']]
69old_test: or_test | old_lambdef
70old_lambdef: 'lambda' [varargslist] ':' old_test
71
72test: or_test ['if' or_test 'else' test] | lambdef
73or_test: and_test ('or' and_test)*
74and_test: not_test ('and' not_test)*
75not_test: 'not' not_test | comparison
76comparison: expr (comp_op expr)*
77comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'
78expr: xor_expr ('|' xor_expr)*
79xor_expr: and_expr ('^' and_expr)*
80and_expr: shift_expr ('&' shift_expr)*
81shift_expr: arith_expr (('<<'|'>>') arith_expr)*
82arith_expr: term (('+'|'-') term)*
83term: factor (('*'|'/'|'%'|'div') factor)*
84factor: ('+'|'-'|'~') factor | power
85power: atom trailer* ['^' factor]
86atom: ('(' [yield_expr|testlist_comp] ')' |
87 '[' [listmaker] ']' |
88 '{' [dictorsetmaker] '}' |
89 NAME | NUMBER) # removed string!
90listmaker: test ( list_for | (',' test)* [','] )
91testlist_comp: test ( comp_for | (',' test)* [','] )
92lambdef: 'lambda' [varargslist] ':' test
93trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
94subscriptlist: subscript (',' subscript)* [',']
95subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
96sliceop: ':' [test]
97exprlist: expr (',' expr)* [',']
98testlist: test (',' test)* [',']
99dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) |
100 (test (comp_for | (',' test)* [','])) )
101
102classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
103
104arglist: (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.
109argument: test [comp_for] | test '=' test
110
111list_iter: list_for | list_if
112list_for: 'for' exprlist 'in' testlist_safe [list_iter]
113list_if: 'if' old_test [list_iter]
114
115comp_iter: comp_for | comp_if
116comp_for: 'for' exprlist 'in' or_test [comp_iter]
117comp_if: 'if' old_test [comp_iter]
118
119testlist1: test (',' test)*
120
121yield_expr: 'yield' [testlist]
122
123
124# Oil types are like MyPy types
125# Python 3 uses the much more general "test" production.
126
127type_expr: NAME ['<' type_expr (',' type_expr)* '>' ]
128
129# added for Oil
130type_input: type_expr NEWLINE* ENDMARKER
131