-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSexprParser.java
More file actions
174 lines (143 loc) · 3.94 KB
/
Copy pathSexprParser.java
File metadata and controls
174 lines (143 loc) · 3.94 KB
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
package loop.lisp;
import loop.AnnotatedError;
import loop.Parser;
import loop.Token;
import loop.ast.BooleanLiteral;
import loop.ast.ClassDecl;
import loop.ast.InlineListDef;
import loop.ast.IntLiteral;
import loop.ast.LongLiteral;
import loop.ast.Node;
import loop.ast.RegexLiteral;
import loop.ast.StringLiteral;
import loop.ast.Variable;
import loop.ast.script.FunctionDecl;
import loop.ast.script.ModuleDecl;
import loop.ast.script.Unit;
import java.util.ArrayList;
import java.util.List;
/**
* @author [email protected] (Dhanji R. Prasanna)
*/
public class SexprParser implements Parser {
private final List<Token> tokens;
private int i;
public SexprParser(List<Token> tokens) {
this.tokens = tokens;
}
/**
* Parses an S-expression.
* <p/>
* sexpr := atom | (LPAREN sexpr* RPAREN)
*/
@Override
public Node parse() {
InlineListDef list = new InlineListDef(false);
while (!endOfInput()) {
Node sexpr = sexpr();
if (sexpr != null)
list.add(new SexprAstTransformer(sexpr).transform());
}
return list;
}
private Node sexpr() {
Node atom = atom();
if (atom != null)
return atom;
List<Token> startParen = match(Token.Kind.LPAREN);
if (startParen == null)
return null;
InlineListDef listDef = new InlineListDef(false).sourceLocation(startParen);
Node inner;
while ((inner = sexpr()) != null) {
listDef.add(inner);
}
if (match(Token.Kind.RPAREN) == null)
throw new RuntimeException("Expected ')'");
return listDef;
}
/**
* atom := symbol
*/
private Node atom() {
Token token = tokens.get(i);
if (token.kind == Token.Kind.LPAREN || token.kind == Token.Kind.RPAREN)
return null;
i++;
switch (token.kind) {
case INTEGER:
return new IntLiteral(token.value);
case LONG:
return new LongLiteral(token.value);
case STRING:
return new StringLiteral(token.value);
case REGEX:
return new RegexLiteral(token.value);
}
if ("#t".equals(token.value) || "#true".equals(token.value))
return new BooleanLiteral(new Token(token.value, Token.Kind.TRUE, token.line, token.column));
else if ("#f".equals(token.value) || "#false".equals(token.value))
return new BooleanLiteral(new Token(token.value, Token.Kind.FALSE, token.line, token.column));
if (token.value.trim().isEmpty())
return null;
return new Variable(token.value);
}
@Override
public Unit script(String file) {
Unit unit = new Unit(file, ModuleDecl.DEFAULT);
Node parse = parse();
for (Node child : parse.children()) {
if (child instanceof FunctionDecl)
unit.declare((FunctionDecl) child);
else
unit.addToInitializer(child);
}
return unit;
}
@Override
public List<AnnotatedError> getErrors() {
return new ArrayList<AnnotatedError>();
}
@Override
public Node line() {
return null;
}
@Override
public FunctionDecl functionDecl() {
return null;
}
@Override
public ClassDecl classDecl() {
throw new UnsupportedOperationException("The S-expression parser does not support classes");
}
private List<Token> match(Token.Kind... ident) {
return match(false, ident);
}
private List<Token> matchNot(Token.Kind... ident) {
return match(true, ident);
}
private List<Token> match(boolean not, Token.Kind... ident) {
int cursor = i;
for (Token.Kind kind : ident) {
// What we want is more than the size of the token stream.
if (cursor >= tokens.size()) {
return null;
}
Token token = tokens.get(cursor);
if (not) {
if (token.kind == kind)
return null;
} else if (token.kind != kind) {
return null;
}
cursor++;
}
// Forward cursor in token stream to match point.
int start = i;
i = cursor;
return tokens.subList(start, i);
}
private boolean endOfInput() {
return i == tokens.size();
}
}