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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
options {
STATIC = false;
LOOKAHEAD = 2;
UNICODE_INPUT = true;
NODE_PREFIX = "AST";
}
PARSER_BEGIN(LambdaParser)
package coffee.liz.lambda.parser;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import coffee.liz.lambda.ast.Expression.AbstractionExpression;
import coffee.liz.lambda.ast.Expression.ApplicationExpression;
import coffee.liz.lambda.ast.Expression.IdentifierExpression;
import coffee.liz.lambda.ast.LambdaProgram;
import coffee.liz.lambda.ast.Macro;
import coffee.liz.lambda.ast.Expression;
import coffee.liz.lambda.ast.SourceSpan;
import coffee.liz.lambda.ast.SourceComment;
public class LambdaParser {
public static void main(final String[] args) throws Exception {
final LambdaParser parser = new LambdaParser(System.in);
final LambdaProgram program = parser.Program();
System.out.println(program);
}
private static SourceSpan spanFrom(final Token start, final Token end) {
return new SourceSpan(start.beginLine, start.beginColumn, end.endLine, end.endColumn);
}
private static SourceSpan spanFrom(final Token start, final Expression end) {
return new SourceSpan(start.beginLine, start.beginColumn, end.span().endLine(), end.span().endColumn());
}
private static SourceSpan spanFromInts(final int startLine, final int startColumn, final Token end) {
return new SourceSpan(startLine, startColumn, end.endLine, end.endColumn);
}
private static SourceSpan spanFromInts(final int startLine, final int startColumn, final Expression end) {
return new SourceSpan(startLine, startColumn, end.span().endLine(), end.span().endColumn());
}
private static Expression withComment(final Expression expr, final Optional<SourceComment> comment) {
if (comment.isEmpty()) {
return expr;
}
if (expr instanceof AbstractionExpression) {
final AbstractionExpression a = (AbstractionExpression) expr;
return new AbstractionExpression(comment, a.span(), a.parameter(), a.body());
}
if (expr instanceof ApplicationExpression) {
final ApplicationExpression a = (ApplicationExpression) expr;
return new ApplicationExpression(comment, a.span(), a.applicable(), a.argument());
}
if (expr instanceof IdentifierExpression) {
final IdentifierExpression a = (IdentifierExpression) expr;
return new IdentifierExpression(comment, a.span(), a.name());
}
return expr;
}
private static Expression withSpan(final Expression expr, final SourceSpan span) {
if (expr instanceof AbstractionExpression) {
final AbstractionExpression a = (AbstractionExpression) expr;
return new AbstractionExpression(a.comment(), span, a.parameter(), a.body());
}
if (expr instanceof ApplicationExpression) {
final ApplicationExpression a = (ApplicationExpression) expr;
return new ApplicationExpression(a.comment(), span, a.applicable(), a.argument());
}
if (expr instanceof IdentifierExpression) {
final IdentifierExpression a = (IdentifierExpression) expr;
return new IdentifierExpression(a.comment(), span, a.name());
}
return expr;
}
private static Expression withCommentAndSpan(final Expression expr, final Optional<SourceComment> comment, final SourceSpan span) {
if (expr instanceof AbstractionExpression) {
final AbstractionExpression a = (AbstractionExpression) expr;
return new AbstractionExpression(comment, span, a.parameter(), a.body());
}
if (expr instanceof ApplicationExpression) {
final ApplicationExpression a = (ApplicationExpression) expr;
return new ApplicationExpression(comment, span, a.applicable(), a.argument());
}
if (expr instanceof IdentifierExpression) {
final IdentifierExpression a = (IdentifierExpression) expr;
return new IdentifierExpression(comment, span, a.name());
}
return expr;
}
private static Optional<SourceComment> extractComment(final Token t) {
if (t == null || t.specialToken == null) {
return Optional.empty();
}
Token st = t.specialToken;
while (st.specialToken != null) {
st = st.specialToken;
}
final Token firstComment = st;
final StringBuilder sb = new StringBuilder();
Token lastComment = st;
int lastEndLine = 0;
while (st != null) {
if (sb.length() > 0) {
final int blankLines = st.beginLine - lastEndLine - 1;
for (int i = 0; i < blankLines; i++) {
sb.append("\n");
}
sb.append("\n");
}
String image = st.image;
while (image.endsWith("\n") || image.endsWith("\r")) {
image = image.substring(0, image.length() - 1);
}
sb.append(image);
lastEndLine = st.endLine;
lastComment = st;
st = st.next;
}
final SourceSpan span = new SourceSpan(
firstComment.beginLine, firstComment.beginColumn,
lastComment.endLine, lastComment.endColumn
);
return Optional.of(new SourceComment(sb.toString(), span));
}
}
PARSER_END(LambdaParser)
SKIP : {
" " | "\t" | "\r" | "\n"
}
SPECIAL_TOKEN : {
< COMMENT: "--" (~["\n","\r"])* ("\n"|"\r"|"\r\n")? >
}
TOKEN : {
< LET: "let" >
| < LAMBDA: "λ" | "\\" >
| < DOT: "." >
| < EQ: "=" >
| < SEMI: ";" >
| < LPAREN: "(" >
| < RPAREN: ")" >
| < IDENT: (["a"-"z","A"-"Z","0"-"9","_"])+ >
}
LambdaProgram Program() :
{
final List<Macro> macros = new ArrayList<Macro>();
Macro m;
Expression body;
Token eofToken;
Optional<SourceComment> eofComment;
}
{
(
m = Macro()
{ macros.add(m); }
)*
body = Expression()
eofToken = <EOF>
{ eofComment = extractComment(eofToken); }
{
final SourceSpan span;
if (!macros.isEmpty()) {
span = spanFromInts(macros.get(0).span().startLine(), macros.get(0).span().startColumn(), eofToken);
} else {
span = new SourceSpan(body.span().startLine(), body.span().startColumn(), eofToken.endLine, eofToken.endColumn);
}
if (eofComment.isPresent() && body.comment().isEmpty()) {
body = withComment(body, eofComment);
}
return new LambdaProgram(span, macros, body);
}
}
Macro Macro() :
{
Token letToken;
Token name;
Token semiToken;
Expression value;
Optional<SourceComment> comment;
}
{
letToken = <LET>
{ comment = extractComment(letToken); }
name = <IDENT>
<EQ>
value = Expression()
semiToken = <SEMI>
{
return new Macro(comment, spanFrom(letToken, semiToken), name.image, value);
}
}
Expression Expression() :
{
Expression e;
}
{
(
e = Lambda()
|
e = Application()
)
{
return e;
}
}
Expression Lambda() :
{
Token lambdaToken;
Token param;
Expression body;
Optional<SourceComment> comment;
}
{
lambdaToken = <LAMBDA>
{ comment = extractComment(lambdaToken); }
param = <IDENT>
<DOT>
body = Expression()
{
return new AbstractionExpression(comment, spanFrom(lambdaToken, body), param.image, body);
}
}
Expression Application() :
{
Expression e;
Expression arg;
}
{
e = Atom()
(
arg = Atom()
{
e = new ApplicationExpression(Optional.empty(), spanFromInts(e.span().startLine(), e.span().startColumn(), arg), e, arg);
}
)*
{
return e;
}
}
Expression Atom() :
{
Token id;
Token lparen;
Token rparen;
Expression e;
Optional<SourceComment> comment;
}
{
id = <IDENT>
{ comment = extractComment(id); }
{
return new IdentifierExpression(comment, spanFrom(id, id), id.image);
}
|
lparen = <LPAREN>
{ comment = extractComment(lparen); }
e = Expression()
rparen = <RPAREN>
{
final SourceSpan parenSpan = spanFrom(lparen, rparen);
if (comment.isPresent() && e.comment().isEmpty()) {
return withCommentAndSpan(e, comment, parenSpan);
}
return withSpan(e, parenSpan);
}
|
e = Lambda()
{
return e;
}
}
|