forked from dlang-community/DGrammar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphgen.d
448 lines (394 loc) · 11 KB
/
graphgen.d
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/**
* Written by Brian Schott (@Hackerpilot on github)
*
* Licensed under the WTFPL
*/
import std.d.lexer;
import std.stdio;
import std.range;
import std.conv;
import std.ascii;
import std.string;
class Node
{
string id;
abstract void assignIds(string id);
abstract void print(File f);
abstract string getStartId();
abstract string getEndId();
}
class RuleDefinition : Node
{
string name;
Alternatives alternatives;
override void assignIds(string id)
{
this.id = id;
alternatives.assignIds(id ~ "_0");
}
override void print(File f)
{
string startDot = format("%s_start", id);
string endDot = format("%s_end", id);
f.writefln(`%s[shape=point, label="", width=.05, height=.05, fixedsize=true]`, startDot);
f.writefln(`%s[shape=point, label="", width=.05, height=.05, fixedsize=true]`, endDot);
//printDotNode(f, startDot);
//printDotNode(f, endDot);
alternatives.print(f);
printHeavyArrow(f, startDot, alternatives.getStartId());
printHeavyArrow(f, alternatives.getEndId(), endDot);
}
override string getStartId() { return null; }
override string getEndId() { return null; }
}
class Alternatives : Node
{
Node[] alternatives;
string startDot;
string endDot;
override void assignIds(string id)
{
this.id = id;
startDot = id ~ "start";
endDot = id ~ "end";
foreach (i, alt; alternatives)
alt.assignIds(format("%s_%d", id, i));
}
override void print(File f)
{
if (alternatives.length > 1)
{
//f.writeln("subgraph cluster_", id, " {\nstyle=invis");
printDotNode(f, startDot);
printDotNode(f, endDot);
foreach (i, alt; alternatives)
{
alt.print(f);
if (i > 0)
{
printHeavyArrow(f, startDot, alt.getStartId());
printHeavyArrow(f, alt.getEndId(), endDot);
}
else
{
printLightArrow(f, startDot, alt.getStartId());
printLightArrow(f, alt.getEndId(), endDot);
}
}
//f.writeln("}");
}
else
{
alternatives[0].print(f);
}
}
override string getStartId()
{
if (alternatives.length > 1)
return startDot;
else
return alternatives[0].getStartId();
}
override string getEndId()
{
if (alternatives.length > 1)
return endDot;
else
return alternatives[0].getEndId();
}
}
class Sequence : Node
{
Node[] items;
override void assignIds(string id)
{
this.id = id;
foreach (i, item; items)
item.assignIds(format("%s_%d", id, i));
}
override void print(File f)
{
//if (items.length > 1) f.writeln("subgraph cluster_", id, " {\nstyle=invis");
for (int i = 0; i < items.length; i++)
{
items[i].print(f);
if (i > 0)
printHeavyArrow(f, items[i - 1].getEndId(), items[i].getStartId());
}
//if (items.length > 1) f.writeln("}");
}
override string getStartId()
{
return items[0].getStartId();
}
override string getEndId()
{
return items[$ - 1].getEndId();
}
}
class Terminal : Node
{
string terminal;
override void assignIds(string id)
{
this.id = id;
}
override void print(File f)
{
f.writeln(id, `[shape=rectangle, style=rounded, label="`, terminal.replace(`"`, `\"`), `"]`);
}
override string getStartId() { return id; }
override string getEndId() { return id; }
}
class RuleReference : Node
{
string ruleReference;
override void assignIds(string id)
{
this.id = id;
}
override void print(File f)
{
f.writeln(id, `[shape=rectangle, label="`, ruleReference.replace(`"`, `\"`), `"]`);
}
override string getStartId() { return id; }
override string getEndId() { return id; }
}
class OptionOrRepeat : Node
{
Node node;
Qualifier qualifier;
string repeatStart;
string repeatEnd;
string optionStart;
string optionEnd;
override void assignIds(string id)
{
this.id = id;
repeatStart = id ~ "_repeatStart";
repeatEnd = id ~ "_repeatEnd";
optionStart = id ~ "_optionStart";
optionEnd = id ~ "_optionEnd";
node.assignIds(id ~ "_0");
}
override void print(File f)
{
//f.writeln("subgraph cluster_", id, "{\nstyle=invis");
final switch (qualifier)
{
case Qualifier.star:
printDotNode(f, optionStart);
printDotNode(f, optionEnd);
printOptionArrow(f, optionStart, optionEnd);
printDotNode(f, repeatEnd);
printDotNode(f, repeatStart);
printHeavyArrow(f, repeatEnd, optionEnd);
printBackArrow(f, repeatStart, repeatEnd);
printHeavyArrow(f, optionStart, repeatStart);
node.print(f);
printHeavyArrow(f, repeatStart, node.getStartId());
printHeavyArrow(f, node.getEndId(), repeatEnd);
break;
case Qualifier.question:
printDotNode(f, optionStart);
printDotNode(f, optionEnd);
printLightArrow(f, optionStart, optionEnd);
node.print(f);
printHeavyArrow(f, optionStart, node.getStartId());
printHeavyArrow(f, node.getEndId(), optionEnd);
break;
case Qualifier.plus:
printDotNode(f, repeatStart);
printDotNode(f, repeatEnd);
printBackArrow(f, repeatStart, repeatEnd);
node.print(f);
printHeavyArrow(f, repeatStart, node.getStartId());
printHeavyArrow(f, node.getEndId(), repeatEnd);
break;
}
//f.writeln("}");
}
override string getStartId()
{
if (qualifier == Qualifier.star || qualifier == Qualifier.question)
return optionStart;
else
return repeatStart;
}
override string getEndId()
{
if (qualifier == Qualifier.star || qualifier == Qualifier.question)
return optionEnd;
else
return repeatEnd;
}
}
enum Qualifier
{
star,
question,
plus
}
RuleDefinition parseRuleDefinition(T)(ref T tokens)
{
assert (tokens.front == TokenType.identifier || isKeyword(tokens.front));
auto rd = new RuleDefinition;
rd.name = tokens.front.value;
tokens.popFront();
assert (tokens.front == TokenType.colon);
tokens.popFront();
rd.alternatives = parseAlternatives(tokens);
assert (tokens.front == TokenType.semicolon, tokens.front.value);
tokens.popFront();
return rd;
}
Alternatives parseAlternatives(T)(ref T tokens)
{
auto alt = new Alternatives;
auto seq = new Sequence;
while (true)
{
if (tokens.front == TokenType.stringLiteral
|| isKeyword(tokens.front)
|| tokens.front == TokenType.identifier)
{
Node node;
if (tokens.front == TokenType.stringLiteral
|| (tokens.front == TokenType.identifier && isUpper(tokens.front.value[0])))
{
auto terminal = new Terminal;
terminal.terminal = tokens.front.value;
node = terminal;
}
else
{
auto ruleRef = new RuleReference;
ruleRef.ruleReference = tokens.front.value;
node = ruleRef;
}
tokens.popFront();
if (tokens.front == TokenType.star
|| tokens.front == TokenType.ternary
|| tokens.front == TokenType.plus)
{
auto option = new OptionOrRepeat;
if (tokens.front == TokenType.star)
option.qualifier = Qualifier.star;
if (tokens.front == TokenType.ternary)
option.qualifier = Qualifier.question;
if (tokens.front == TokenType.plus)
option.qualifier = Qualifier.plus;
tokens.popFront();
option.node = node;
seq.items ~= option;
}
else
seq.items ~= node;
}
else if (tokens.front == TokenType.rParen
|| tokens.front == TokenType.semicolon)
{
alt.alternatives ~= seq;
break;
}
else if (tokens.front == TokenType.lParen)
{
tokens.popFront();
auto item = parseAlternatives(tokens);
assert (tokens.front == TokenType.rParen);
tokens.popFront();
if (tokens.front == TokenType.star
|| tokens.front == TokenType.ternary
|| tokens.front == TokenType.plus)
{
auto option = new OptionOrRepeat;
if (tokens.front == TokenType.star)
option.qualifier = Qualifier.star;
if (tokens.front == TokenType.ternary)
option.qualifier = Qualifier.question;
if (tokens.front == TokenType.plus)
option.qualifier = Qualifier.plus;
tokens.popFront();
option.node = item;
seq.items ~= option;
}
else
seq.items ~= item;
}
else if (tokens.front == TokenType.bitOr)
{
alt.alternatives ~= seq;
seq = new Sequence;
tokens.popFront();
}
}
return alt;
}
void main(string[] args)
{
LexerConfig config;
config.tokenStyle = TokenStyle.source;
auto f = File(args[1]);
auto tokens = (cast(ubyte[]) f.byLine(KeepTerminator.yes).join()).byToken(config);
for (int i = 0; !tokens.empty; i++)
{
auto rule = parseRuleDefinition(tokens);
auto o = File(rule.name ~ ".dot", "w");
o.write(q"[digraph grammar
{
rankdir=LR
fontsize=10
fontname="Liberation Mono"
node [fontsize=10, fontname="Liberation Mono", margin=0.05, height=0.3]
edge [arrowhead=none, arrowsize=0.5, length=0.2]
ranksep=0.2
]");
o.writeln("label=\"", rule.name, "\"");
rule.assignIds(format("rule%d", i));
rule.print(o);
o.writeln("}");
}
}
RuleDefinition createTestRule()
{
auto r = new RuleDefinition;
auto alt = new Alternatives;
auto seq = new Sequence;
auto term1 = new Terminal;
term1.terminal = `"abcde"`;
auto term2 = new Terminal;
term2.terminal = `"fghi"`;
auto oor = new OptionOrRepeat;
oor.node = term2;
oor.qualifier = Qualifier.star;
seq.items ~= term1;
seq.items ~= oor;
alt.alternatives ~= seq;
auto seq2 = new Sequence;
auto term3 = new Terminal;
term3.terminal = "xyz";
seq2.items ~= term3;
alt.alternatives ~= seq2;
r.alternatives = alt;
return r;
}
void printDotNode(File f, string nodeName)
{
f.writeln(nodeName, `[shape=point, label="", width=.01, height=.01, fixedsize=true]`);
}
void printHeavyArrow(File f, string src, string dst)
{
f.writefln("%s -> %s [weight=100]", src, dst);
}
void printLightArrow(File f, string src, string dst)
{
f.writefln("%s -> %s [weight=0]", src, dst);
}
void printOptionArrow(File f, string src, string dst)
{
f.writefln("%s -> %s [weight=0, constraint=false]", src, dst);
}
void printBackArrow(File f, string src, string dst)
{
f.writefln("%s -> %s [arrowstyle=normal, weight=0, constraint=false, dir=back]", src, dst);
}