-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmy_grammar.py
191 lines (169 loc) · 4.39 KB
/
my_grammar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# Operators
PLUS = '+'
MINUS = '-'
MUL = '*'
DIV = '/'
FLOORDIV = '//'
MOD = '%'
POWER = '**'
ARITHMATIC_LEFT_SHIFT = '<<<' # TODO
ARITHMATIC_RIGHT_SHIFT = '>>>' # TODO
XOR = 'xor'
BINARY_ONES_COMPLIMENT = '~' # TODO
BINARY_LEFT_SHIFT = '<<' # TODO
BINARY_RIGHT_SHIFT = '>>' # TODO
AND = 'and'
OR = 'or'
NOT = 'not'
IN = 'in' # TODO
NOT_IN = 'not in' # TODO
IS = 'is' # TODO
IS_NOT = 'is not' # TODO
AMPERSAND = '&' # TODO
PIPE = '|' # TODO
LPAREN = '('
RPAREN = ')'
LSQUAREBRACKET = '['
RSQUAREBRACKET = ']'
LCURLYBRACKET = '{'
RCURLYBRACKET = '}'
COMMA = ','
COLON = ':'
DOT = '.'
RANGE = '..'
ELLIPSIS = '...' # TODO
ARROW = '->'
CAST = '::'
ASSIGN = '='
PLUS_ASSIGN = '+='
MINUS_ASSIGN = '-='
MUL_ASSIGN = '*='
DIV_ASSIGN = '/='
FLOORDIV_ASSIGN = '//='
MOD_ASSIGN = '%='
POWER_ASSIGN = '**='
EQUALS = '=='
NOT_BANG = '!'
NOT_EQUALS = '!='
LESS_THAN = '<'
GREATER_THAN = '>'
LESS_THAN_OR_EQUAL_TO = '<='
GREATER_THAN_OR_EQUAL_TO = '>='
DECORATOR = '@' # TODO
# Types
ANY = 'Any'
INT = 'Int'
INT8 = 'Int8'
INT32 = 'Int32'
INT64 = 'Int64' # same as int but doesn't automatically promote to larger integer type upon overflow
INT128 = 'Int128'
DEC = 'Dec'
FLOAT = 'Float'
COMPLEX = 'Complex' # TODO
STR = 'Str'
BOOL = 'Bool'
BYTES = 'Bytes' # TODO
ARRAY = 'Array'
LIST = 'List'
SET = 'Set' # TODO
DICT = 'Dict'
ENUM = 'Enum' # TODO
FUNC = 'Func'
STRUCT = 'Struct'
# Contstants
TRUE = 'true'
FALSE = 'false'
NAN = 'nan'
INF = 'inf'
NEGATIVE_INF = '-inf'
# Keywords
IF = 'if'
ELSE_IF = 'else if'
ELSE = 'else'
FOR = 'for'
WHILE = 'while'
SWITCH = 'switch'
CASE = 'case'
DEFAULT = 'default'
DEF = 'def'
CLASS = 'class' # TODO
ACTOR = 'actor' # TODO
CONST = 'const'
NEW = 'new' # TODO
SUPER = 'super' # TODO
THIS = 'this' # TODO
RETURN = 'return'
TEST = 'test' # TODO
TRY = 'try' # TODO
CATCH = 'catch' # TODO
FINALLY = 'finally' # TODO
THEN = 'then' # TODO
YIELD = 'yield' # TODO
BREAK = 'break'
CONTINUE = 'continue'
MATCH = 'match' # TODO
DEL = 'del' # TODO
FROM = 'from' # TODO
IMPORT = 'import' # TODO
WILDCARD = '*' # TODO
WITH = 'with' # TODO
AS = 'as' # TODO
PASS = 'pass'
VOID = 'void'
RAISE = 'raise' # TODO
ALIAS = 'alias'
REQUIRE = 'require' # TODO
ENSURE = 'ensure' # TODO
OVERRIDE = 'override' # TODO
DOC = 'doc' # TODO allow unquoted text in doc block
ABSTRACT = 'abstract' # TODO
GETTER = 'getter' # TODO
SETTER = 'setter' # TODO
ASSERT = 'assert' # TODO
ARITHMETIC_OP = (PLUS, MINUS, MUL, DIV, MOD, FLOORDIV, POWER, ARITHMATIC_LEFT_SHIFT, ARITHMATIC_RIGHT_SHIFT)
ASSIGNMENT_OP = (ASSIGN, PLUS_ASSIGN, MINUS_ASSIGN, MUL_ASSIGN, DIV_ASSIGN, FLOORDIV_ASSIGN, MOD_ASSIGN, POWER_ASSIGN)
ARITHMETIC_ASSIGNMENT_OP = (PLUS_ASSIGN, MINUS_ASSIGN, MUL_ASSIGN, DIV_ASSIGN, FLOORDIV_ASSIGN, MOD_ASSIGN, POWER_ASSIGN)
COMPARISON_OP = (EQUALS, NOT_BANG, NOT_EQUALS, LESS_THAN, GREATER_THAN, GREATER_THAN_OR_EQUAL_TO, LESS_THAN_OR_EQUAL_TO)
LOGICAL_OP = (AND, OR, NOT)
BINARY_OP = (XOR, BINARY_ONES_COMPLIMENT, BINARY_LEFT_SHIFT, BINARY_RIGHT_SHIFT)
MEMBERSHIP_OP = (IN, NOT_IN)
IDENTITY_OP = (IS, IS_NOT)
TYPE_OP = (AMPERSAND, PIPE, COMMA)
MULTI_WORD_OPERATORS = (IS, IS_NOT, IN, NOT_IN, NOT)
OPERATORS = (
LPAREN, RPAREN, LSQUAREBRACKET, RSQUAREBRACKET, LCURLYBRACKET, RCURLYBRACKET,
ARROW, COMMA, COLON, DOT, DECORATOR, CAST, RANGE, ELLIPSIS,
) + ARITHMETIC_OP + ASSIGNMENT_OP + COMPARISON_OP + LOGICAL_OP + BINARY_OP + MEMBERSHIP_OP + IDENTITY_OP + TYPE_OP
SINGLE_OPERATORS = (
LPAREN, RPAREN, LSQUAREBRACKET, RSQUAREBRACKET, LCURLYBRACKET, RCURLYBRACKET,
BINARY_ONES_COMPLIMENT, COMMA, DECORATOR, AMPERSAND, PIPE
)
KEYWORDS = (
IF, ELSE, WHILE, FOR, SWITCH, CASE, DEF, CLASS, SUPER, THIS, RETURN, TEST, TRY, CATCH,
FINALLY, THEN, YIELD, BREAK, CONTINUE, DEL, IMPORT, FROM, WITH, AS, PASS, VOID, RAISE, ACTOR,
CONST, REQUIRE, ENSURE, OVERRIDE, DOC, ABSTRACT, GETTER, SETTER, ASSERT, DEFAULT, NEW, ALIAS
)
MULTI_WORD_KEYWORDS = (IF, ELSE, ELSE_IF)
TYPES = (ANY, INT, INT8, INT32, INT64, INT128, DEC, FLOAT, COMPLEX, STR, BOOL, BYTES, ARRAY, LIST, DICT, ENUM, FUNC, STRUCT)
CONSTANTS = (TRUE, FALSE, NAN, INF, NEGATIVE_INF)
PRINT = 'print'
INPUT = 'input'
BUILTIN_FUNCTIONS = (PRINT, INPUT)
# For Lexer
TYPE = 'TYPE'
NUMBER = 'NUMBER'
STRING = 'STRING'
OP = 'OP'
CONSTANT = 'CONSTANT'
NEWLINE = 'NEWLINE'
INDENT = 'INDENT'
KEYWORD = 'KEYWORD'
ANON = 'ANON'
NAME = 'NAME'
EOF = 'EOF'
ALPHANUMERIC = 'alphanumeric'
NUMERIC = 'numeric'
OPERATIC = 'operatic'
WHITESPACE = 'whitespace'
COMMENT = 'comment'
ESCAPE = 'escape'