-
Notifications
You must be signed in to change notification settings - Fork 0
/
La.g4
139 lines (139 loc) · 5.69 KB
/
La.g4
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
grammar La;
import LaOperator;
// $antlr-format useTab false ;reflowComments false;
// $antlr-format alignColons hanging;
program: statement* EOF;
statement
: emptyStatement
| literalStatement eos?
| expressionStatement eos?
| assign_statement eos?;
/*====================================================================================================================*/
emptyStatement: eos;
eos: Semicolon;
symbol: Identifier (DOT Identifier)*;
/*====================================================================================================================*/
literalStatement: Number | String | Identifier;
/*====================================================================================================================*/
expressionStatement: expression (COMMA expression)*;
// High computing priority in the front
expression
: op = prefix_ops right = expression # PrefixExpression
| left = expression op = postfix_ops # PostfixExpression
| left = symbol op = DOT right = expression # MethodApply
| function_apply # FunctionApply
| Number right = expression # SpaceOperator
| left = expression op = left_ops right = expression # LeftOperator
| <assoc = right> left = expression op = right_ops right = expression # RightOperator
| <assoc = right> id = assign_lhs op = assign_ops expr = assignable # AssignOperator
| data = listLiteral # List
| left = expression data = indexLiteral # Index
| data = dictLiteral # Dict
| atom = literalStatement # Literal
| atom = symbol # SymbolExpression
| '(' expression ')' # PriorityExpression;
/*====================================================================================================================*/
function_apply: symbol function_curry+;
function_curry: '(' function_params? ')';
function_params: expression (COMMA expression)*;
/*====================================================================================================================*/
assign_statement
: id = assign_lhs expr = assignable # AssignStatement;
assignable: expression;
assign_lhs
: Identifier # AssignValue
| Identifier (DOT Identifier)+ # AssignAttribute
| Identifier '(' Identifier ')' # AssignFunction
| Identifier '[' Integer ']' # AssignWithList;
assign_pass: Tilde | symbol;
/*====================================================================================================================*/
listLiteral
: '{' element (COMMA? element)* COMMA? '}' # LiteralList
| '{' COMMA? '}' # EmptyList;
indexLiteral: '[' index_valid (COMMA? index_valid)+? ']';
dictLiteral
: '{' keyValue (COMMA keyValue)* COMMA? '}' # LiteralDict
| '{' Colon '}' # EmptyDict;
keyValue
: key = validKey Colon value = element # NormalKey
| key = Identifier Colon value = element # SymbolKey
| Power key = symbol Colon value = element # RawKey;
// $antlr-format alignColons trailing;
validKey : Number | String | raw;
raw : 'Raw' '(' text = String ')';
element : (expression | dictLiteral | listLiteral | raw);
index_valid : (symbol | Integer) Colon?;
signedInteger : (Plus | Minus)? Integer;
//FIXME: replace NUMBER with signedInteger
/*====================================================================================================================*/
LineComment : Shebang ~[\r\n]* -> channel(HIDDEN);
PartComment : Comment .*? Comment -> channel(HIDDEN);
WhiteSpace : UnicodeWhiteSpace+ -> skip;
NewLine : ('\r'? '\n' | '\r')+ -> skip;
String : SimpleString;
Identifier : NameStartCharacter NameCharacter*;
Number : Float | Integer;
Float : Digit+ Dot Digit* | Dot Digit+;
Integer : Digit+;
fragment Digit : [0-9];
fragment OctalDigit : [0-7];
fragment HexDigit : [0-9a-fA-F];
fragment Letter : [a-zA-Z];
fragment UnicodeWhiteSpace : [\p{White_Space}];
fragment SimpleString : DoubleQuotation .*? DoubleQuotation;
fragment EmojiCharacter : [\p{Emoji}];
fragment NameCharacter : NameStartCharacter | Digit;
/*====================================================================================================================*/
// $antlr-format alignColons hanging;
prefix_ops
: Plus
| Minus
| BitNot
| LogicNot
| Reciprocal
| Increase;
postfix_ops: Increase;
left_ops
: Plus
| Minus
| LeftShift
| RightShift
| LogicAnd
| LogicNot
| LogicOr
| LogicXor
| Equal
| NotEqual
| Equivalent
| NotEquivalent
| Grater
| GraterEqual
| Less
| LessEqual
| LogicAnd
| LogicOr
| Divide
| Times
| Multiply
| Kronecker
| TensorProduct
| Concat
| LeftShift
| RightShift;
right_ops: Power | Surd;
assign_ops
: Assign
| PlusTo
| MinusFrom
| LetAssign
| FinalAssign
| DelayedAssign;
fragment NameStartCharacter
: Dollar
| [\p{Latin}]
| [\p{Han}]
| [\p{Hiragana}]
| [\p{Katakana}]
| [\p{Greek}];
//UNICODE_ID : [\p{General_Category=Other_Letter}]*; May Allow # $ % with special meaning English +
// Chinese + Japanese + Greeks