-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cup
147 lines (119 loc) · 3.75 KB
/
parser.cup
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
// Lite parser, by duangsuse and written in cup
// (C) 2018 no rights is reserved
// for the scanner see scanner.flex
// for unicode escape see unicode.flex
import java_cup.runtime.*;
// Begin parser
parser code {:
// Connect this parser to a scanner!
Lexer lites;
/* Report a parsing error */
/* Error(err): message */
public void report_error(String message, Object info) {
StringBuilder m = new StringBuilder("Error ");
if (info instanceof java_cup.runtime.Symbol)
m.append("(" + info.toString() + ")");
m.append(": " + message);
System.out.println(m);
}
/* Report a fatal error */
public void report_fatal_error(String message, Object info) {
report_error(message, info);
throw new RuntimeException("Fatal Syntax Error");
}
Parser(Lexer scanner) { this.lites = scanner; }
:};
/* Terminals (tokens returned by the scanner). */
// Terminal keywords
terminal DEFINE, DO, BREAK, NEXT, RETURN, SCOPE, WHILE, FOR, IN, AS, TO, IF, ELIF, ELSE, IMPORT, REQUIRE, END, ANDK, ORK;
// Terminal value
terminal Boolean BOOLEAN_LITERAL; // true false
terminal NULL_LITERAL; // nil
// Terminal seperators
terminal LPAREN, RPAREN, LBRACE, RBRACE, LBRACK, RBRACK, SEMICOLON, COMMA, DOT, AT;
// Terminal operators
terminal EQ, GT, LT, NOT, COLON, EQUAL, GE, LE, NOTEQ, FULLEQ, NOTFULLEQ, INC, DEC, PLUS, SUB, MULT, DIV, AND, OR, XOR, MOD, PWR, LSHIFT, RSHIFT;
terminal PLUSEQ, SUBEQ, MULTEQ, SQUARE;
// Fake terminal (unary sub)
terminal USUB;
// Terminal stateful value
terminal Number INTEGER_LITERAL, FLOATING_POINT_LITERAL;
terminal String STRING_LITERAL, STRING_LITERAL_SINGLE;
// Identifier
terminal String IDENTIFIER;
// Newline
terminal NEWLINE;
/* non terminals */
non terminal ExpList;
non terminal KvList;
non terminal KvListItem;
/* Lite precedence */
/* See https://github.com/duangsuse/Lite/blob/master/lite.ohm */
precedence left IN;
precedence left OR, ORK;
precedence left AND, ANDK;
precedence left GT, LT, GE, LE, NOTEQ, EQUAL, NOTFULLEQ, FULLEQ;
precedence left LSHIFT, RSHIFT;
precedence left TO;
/* Now start add/sub */
precedence left PLUS, SUB;
precedence left MULT, DIV, MOD;
precedence left PWR, SQUARE, AS;
/* Primitive expression excluding unary sub */
precedence left USUB, NOT, INC, DEC;
/* Literal */
NumberLiteral ::= INTEGER_LITERAL | FLOATING_POINT_LITERAL;
StringLiteral ::= STRING_LITERAL | STRING_LITERAL_SINGLE;
Literal ::= BOOLEAN_LITERAL
| NULL_LITERAL
| NumberLiteral
| StringLiteral
;
/* Lite math expression */
Exp ::= Exp OR Exp
| Exp ORK Exp
| Exp IN Exp
| Exp AND Exp
| Exp ANDK Exp
| Exp GT Exp
| Exp LT Exp
| Exp GE Exp
| Exp LE Exp
| Exp NOTEQ Exp
| Exp EQUAL Exp
| Exp NOTFULLEQ Exp
| Exp FULLEQ Exp
| Exp LSHIFT Exp
| Exp RSHIFT Exp
| Exp TO Exp
| Exp PLUS Exp
| Exp SUB Exp
| Exp MULT Exp
| Exp DIV Exp
| Exp MOD Exp
| Exp PWR Exp
| Exp SQUARE Exp
| Exp AS Exp
| LPAREN Exp RPAREN
| SUB Exp %prec USUB
| NOT Exp
| IDENTIFIER INC
| IDENTIFIER DEC
| Literal
| Call
| LiteExp
;
/* Lite primitive expression */
LiteExp ::= List // [a, b, c] :[foo bar baz]
| Table // { a: b, b:c } { a:b NEWLINE b:c, c: 1 } {}
| BraceBlock // { puts 1 } { |a| lite.delete a: puts a: exit 1 }
| DoBlock // do || end do puts; end
;
List ::= LBRACK ExpList RBRACK // [ 1, 2, 3 ]
| LBRACK RBRACK // []
| COLON LBRACK ExpList RBRACK // :[ 1, 2, 3]
;
Table ::= LBRACE RBRACE // {}
| LBRACE KvList RBRACE // {a:2}
;
ExpList ::= ExpList COMMA Exp;