-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynGrace.rsc
308 lines (251 loc) · 7.68 KB
/
DynGrace.rsc
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
module DynGrace
import IO;
import Offside;
import ParseTree;
start syntax Program = CodeSequence;
syntax Statement
= ret: "return" Expression
| ret0: "return"
| exp: Expression
| assign: Expression ":=" Expression
;
syntax Declaration
= var: "var" Identifier Annos? (":=" Expression)?
| def: "def" Identifier Annos? "=" Expression
| class: "class" MethodHeader!prefix!operator!assignment Annos? "{" Extend* CodeSequence "}"
| trait: "trait" MethodHeader!prefix!operator!assignment Annos? "{" CodeSequence "}"
| method: "method" MethodHeader Annos? "{" CodeSequence "}"
;
syntax Code
= decl: Declaration ";"?
| stat: Statement ";"?
;
syntax CodeSequence // TODO: this should be a proper list (e.g. Code*, but filtering does not seem to work)
= code: Code
| empty:
| right seq: CodeSequence!empty CodeSequence!empty
;
syntax Annos = "is" {Anno ","}+;
syntax Anno
= "public" | "writable" | "readable" | "overrides" | "manifest"
| "confidential" | "required"
;
syntax MethodHeader
= assignment: Identifier ":=" "(" Identifier ")"
| keywords: ArgumentHeader+
| unary: Identifier
| operator: OtherOp "(" Identifier ")"
| prefix: "prefix" !>> [ \n\r] OtherOp
;
syntax ArgumentHeader = Identifier keyword "(" {Identifier ","}+ formals ")";
syntax Extend
= inherit: "inherit" Expression
| use: "use" Expression
;
// Expressions
syntax Dot = ".";
syntax Star = "*";
syntax Slash = "/";
syntax Plus = "+";
syntax Dash = "-";
syntax OpenParen = "(";
syntax CloseParen = ")";
syntax Ellipsis = "...";
syntax Expression
= lit: Literal
| ellipsis: Ellipsis
| implicitUnary: Identifier () !>> [{(\"\[]
| bracket parens: OpenParen {Expression ";"}+ CloseParen
| implicitMulti: ArgumentClause+
| multi: Expression Dot ArgumentClause+
| unary: Expression Dot Identifier () !>> [{(\"\[]
> prefix: OtherOp Expression
> left (
star: Expression Star Expression
| slash: Expression Slash Expression
)
> left (
plus: Expression Plus Expression
| dash: Expression Dash !>> "\>" !>> [0-9] Expression
)
> left binaryOther: Expression OtherOp op Expression
;
syntax Argument
= exps: OpenParen {Expression ","}+ CloseParen
| block: BlockLiteral
| string: StringLiteral
| number: NumberLiteral
| lineUp: LineUpLiteral
;
syntax ArgumentClause
= Identifier Argument
;
lexical Operator
= [!?@#$%^&|~=+\-*/\>\<:.\u2200–\u22FF]+ !>> [!?@#$%^&|~=+\-*/\>\<:.\u2200–\u22FF]
;
keyword ReservedOperator
= "." | "..." | ":=" | "-" | "+" | "*" | "/" | "\<" | "\>" | "-\>" | "→" | "[" | "]";
syntax OtherOp
= Operator \ ReservedOperator
;
// Literals
syntax Literal
= StringLiteral
| SelfLiteral
| BlockLiteral
| NumberLiteral
| ObjectLiteral
| LineUpLiteral
;
lexical StringLiteral = [\"] StringChar* [\"];
lexical StringChar
= EscapeChar
| ![\\\"\t\f\b] // does \" belong here?
;
lexical EscapeChar = [\\] [\\\"\'{}bnrtlfe ];
// NB: otherChars, needs to escape \
lexical Arrow = "→" | "-\>";
syntax BlockLiteral
// TODO: check, no methods allowed in code sequence
= noArgs: "{" CodeSequence body "}"
| withArgs: "{" {Identifier ","}+ formals Arrow CodeSequence body "}"
;
syntax SelfLiteral = "self";
lexical Int = [+\-]? [0-9]+ !>> [0-9];
lexical Float = Int "." [0-9]+ !>> [0-9] ([eE] Int)?;
lexical Radix = ([0]|[2-9]|([1-2][0-9])|([3][1-5])) "x" [0-9A-Za-z]+ !>> [0-9A-Za-z];
syntax NumberLiteral
= integer: Int
| float: Float
| radix: Radix
;
syntax ObjectLiteral = "object" Annos? "{" Extend* CodeSequence "}";
syntax LineUpLiteral = "[" {Expression ","}+ "]";
// Lexical stuff
lexical Identifier
= [_]
| ([a-zA-Z0-9\'] !<< [a-zA-Z][a-zA-Z0-9\']* !>> [a-zA-Z0-9\']) \ Reserved
;
keyword Reserved
= "self" | "inherit" | "class" | "object" | "type" | "where" | "def" | "var"
| "method" | "prefix" | "alias" | "as" | "dialect" | "exclude" | "import"
| "is" | "outer" | "required" | "return" | "Self" | "trait" | "use"
;
layout Default = LAYOUT* !>> [\ \n\r] !>> "//";
lexical LAYOUT
= Comment
| [\ \n\r]
;
lexical Comment = @category="Comment" "//" ![\n\r]* $ ;
/*
* Post-parse disambiguation
*/
// Reject direct binary operator nesting with different operators
Expression binaryOther(Expression lhs, OtherOp op, Expression rhs) {
if (lhs is binaryOther, op != lhs.op) {
filter;
}
fail;
}
// Statements must not contain syntactic elements with less or equal indentation
// except whitespace, or closing }
Code stat(Statement s0, ";"? _) {
int at = s0@\loc.begin.column;
int atLine = s0@\loc.begin.line;
for (/Tree s := s0) {
if (ArgumentClause a := s, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (Dot a := s, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (Slash a := s, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (Plus a := s, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (Dash a := s, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (OpenParen a := s, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (CloseParen a := s, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (Identifier a := s, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (Ellipsis a := s, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (OtherOp a := s, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (Argument a := s, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (Expression e := s, e@\loc.begin.column <= at, e@\loc.begin.line > atLine) {
//println("Stat Offside expression: <e>");
filter;
}
}
fail;
}
// Idem for declarations.
Code decl(Declaration d, ";"? _) {
int at = d@\loc.begin.column;
int atLine = d@\loc.begin.line;
if (/ArgumentClause a := d, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (/Dot a := d, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (/Slash a := d, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (/Plus a := d, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (/Dash a := d, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (/OpenParen a := d, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (/CloseParen a := d, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (/Identifier a := d, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (/Ellipsis a := d, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (/OtherOp a := d, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (/Argument a := d, a@\loc.begin.column <= at, a@\loc.begin.line > atLine) {
filter;
}
if (/Expression e := d, e@\loc.begin.column <= at, e@\loc.begin.line > atLine) {
//println("Stat Offside expression: <e>");
filter;
}
fail;
}
bool endsWithSemi((Code)`<Statement c>;`) = true;
bool endsWithSemi((Code)`<Declaration c>;`) = true;
default bool endsWithSemi(Code _) = false;
bool endsWithSemi((CodeSequence)`<Code c>`) = endsWithSemi(c);
bool endsWithSemi((CodeSequence)`<CodeSequence c1> <CodeSequence c2>`) = endsWithSemi(c2);
// Code elements can only be sequenced if they are on separate lines
// or the left sequence is terminated by a semicolon.
CodeSequence seq(CodeSequence lhs, CodeSequence rhs) {
if (horizontal(lhs, rhs) && !endsWithSemi(lhs)) {
filter;
}
fail;
}