-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsmalltalkParser.dart
226 lines (185 loc) · 6.78 KB
/
smalltalkParser.dart
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
/*
Copyright 2008 Cadence Design Systems, Inc.
Copyright 2013 Google Inc.
Licensed under the Apache License, Version 2.0 (the ''License'');
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*/
library smalltalk_parser;
import 'CombinatorialParsing.dart';
import 'smalltalkGrammar.dart';
class StringAST extends AST {
String val;
StringAST(this.val);
}
class VarDeclAST extends AST {
var name, type;
VarDeclAST(this.name, this.type);
}
class PackageAST extends AST {
var name, classes, extendedClasses;
PackageAST(this.name, this.classes, this.extendedClasses);
}
class MessageAST extends AST {
var sel, args;
MessageAST(this.sel, this.args) {
assert(sel is String);
assert(args is List);
}
}
class MethodAST extends AST {
var pattern, body, visibility;
MethodAST(this.pattern, this.body, this.visibility);
}
class AST {
var start, end, cStart, cEnd;
get concreteEnd => cEnd == null ? end : cEnd;
set concreteEnd(p) => cEnd = p;
get concreteStart => cStart == null ? start : cStart;
set concreteStart(p) => cStart = p;
}
class SideAST extends AST {
var vars, categories, classComment, classPoolVars, sharedPoolVars;
}
class ClassAST extends AST {
var format, name, superclassName, instanceSide, classSide, category;
}
class MessagePatternAST extends AST {
var selector, parameters, returnType, typePattern;
MessagePatternAST(this.selector, this.parameters);
}
class CategoryAST extends AST {
var name, methods;
CategoryAST(this.name, this.methods);
}
class SmalltalkParser extends SmalltalkGrammar {
get binaryMsgPattern {
return super.binaryMsgPattern.wrapper((Token sel, p) {
var params = new List();
params.add(p);
return new MessagePatternAST(sel.token, params)
..start = sel.start
..end = p.end;
});
}
get category {
return super.category.wrapper((StringAST cn, List<AST> ms) {
var end = ms.isEmpty ? cn.end : ms.last.end;
return new CategoryAST(cn.val, ms)
..start = cn.start
..end = end;
});
}
String charsToString(List<String> cs) => cs.fold('', (s, c) => '$s $c');
get classBody {
return super.classBody.wrapper((Token lp, cmnt, instVars, classPoolVars, sharedPoolVars, List categories, Token rp) {
var vs = instVars == null? new List() : instVars;
return new SideAST()
..vars = vs
..categories = categories
..classComment = cmnt
..start = lp.start
..end = rp.end
..classPoolVars = classPoolVars
..sharedPoolVars = sharedPoolVars;
});
}
get classComment {
return super.classComment.wrapper((ws, c) => (c[2]) .fold('', (s, ch) => '$s$ch'));
}
get classDefinition {
return super.classDefinition.wrapper((cat, format, className, eq, superClassName, instSide, classSide) {
var klassSide = classSide == null ? (new SideAST()..vars = new List() ..categories = new List())
: classSide;
var kat = cat == null ? 'Newsqueak-tests' : cat.val;
return new ClassAST()
..name = className.snd
..superclassName = superClassName.snd
..instanceSide = instSide
..classSide = klassSide
..start = className.start
..end = (classSide == null ? instSide.end :classSide.end)
..category = kat
..format = format.token;
});
}
get classSideDecl => super.classSideDecl.wrapper((cn, side) => side);
get extendedClass {
return super.extendedClass.wrapper((extensions, name, eq, lp, instCategories, rp, colon, lp2, classCategories, rp2) =>
new ClassAST()
..name = name.snd
..instanceSide = (new SideAST()..categories = instCategories)
..classSide = (new SideAST()..categories = classCategories)
);
}
get keywordMsgPattern {
return super.keywordMsgPattern.wrap((List kws) {
var params = new List();
assert(!kws.isEmpty);
var sel = kws.fold('', (s , kwp) {
params.add(kwp.last);
'$s ${kwp.first.token}';
});
return new MessagePatternAST(sel, params)
..start = kws.first.first.start
..end = params.last.end;
});
}
get method {
return super.method.wrapper((msg, cb, ei) =>
new MethodAST(msg, cb,'public')
..start = msg.start
// ..end = cb.end
// ..concreteEnd = cb.concreteEnd
);
}
get methodDecl {
return super.methodDecl.wrapper((msg, eq, lp, cb, rp) =>
new MethodAST(msg, cb,'public')
..start = msg.start
..end = rp.end
);
}
get package {
return super.package.wrapper((langId, pkg, name, eq, lp, classes, extendedClasses, rp, end) =>
new PackageAST(name.val,
classes == null ? new List(): classes,
extendedClasses == null ? new List(): extendedClasses
)
);
}
get string {
return super.string.wrap((t) => new StringAST(t.token)
..start = t.start
..end = t.end);
}
get unaryMsgPattern {
return super.unaryMsgPattern.wrap((sel) =>
new MessagePatternAST(sel.sel, new List())
..start = sel.start.. end = sel.end
);
}
get unarySelector {
return super.unarySelector.wrap((u) =>
new MessageAST(u.token, new List())
..start = u.start
..end = u.end
);
}
get varDecls {
return super.varDecls.wrapper((vb1, vds, vb2) {
if (!vds.isEmpty) {
vds.first.concreteStart = vb1.start;
vds.last.concreteEnd = vb2.end;
};
return vds;
});
}
get variableDecl {
return super.variableDecl.wrapper((Token n) =>
new VarDeclAST(n.token, null)
..start = n.start
..end = n.end
);
}
}