-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbols.d
540 lines (484 loc) · 16.7 KB
/
symbols.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// symbols.d
//
// Copyright Peter Williams 2013 <pwil3058@bigpond.net.au>.
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
module symbols;
import std.string;
import std.regex;
import sets;
import idnumber;
import workarounds;
import workarounds: wa_sort;
import ddlib.lexan;
alias uint SymbolId;
enum SpecialSymbols : SymbolId { start, end, invalid_token, parse_error };
enum SymbolType {token, tag, non_terminal};
enum Associativity {nonassoc, left, right};
alias uint Precedence;
struct AssociativePrecedence {
Associativity associativity;
Precedence precedence;
@property
bool is_explicitly_set()
{
return precedence != 0;
}
}
class FirstsData {
Set!TokenSymbol tokenset;
bool transparent;
this(Set!TokenSymbol tokenset, bool transparent)
{
this.tokenset = tokenset;
this.transparent = transparent;
}
override string toString()
{
return format("Firsts: %s; Transparent: %s", tokenset, transparent);
}
}
bool is_allowable_name(string name)
{
return name.length < 2 || (name[0..2] != "dd" && name[0..2] != "DD");
}
class Symbol {
mixin IdNumber!(SymbolId);
const SymbolType type;
const string name;
AssociativePrecedence associative_precedence;
CharLocation defined_at;
CharLocation[] used_at;
string field_name;
string pattern;
FirstsData firsts_data;
this(SymbolId id, string sname, SymbolType stype, CharLocation location, bool is_definition=true)
{
this.id = id;
name = sname;
type = stype;
if (type == SymbolType.token) {
// FIRST() for a token is trivial
firsts_data = new FirstsData(Set!TokenSymbol(this), false);
} else {
firsts_data = null;
}
if (is_definition) {
defined_at = location;
} else {
used_at ~= location;
}
}
@property
Associativity associativity()
{
return associative_precedence.associativity;
}
@property
Precedence precedence()
{
return associative_precedence.precedence;
}
@property
bool is_defined()
{
return defined_at != CharLocation(0, 0);
}
@property
bool is_used()
{
return used_at.length > 0;
}
override string toString()
{
if (type == SymbolType.token && pattern.length > 0 && pattern[0] == '"') {
return pattern;
}
return name;
}
}
alias Symbol TokenSymbol;
alias Symbol TagSymbol;
alias Symbol NonTerminalSymbol;
struct FieldDefinition {
string field_name;
string field_type;
string conversion_function_name;
CharLocation defined_at;
}
import std.stdio;
struct SymbolTable {
private SymbolId next_symbol_id = SpecialSymbols.max + 1;
private static Symbol[SpecialSymbols.max + 1] special_symbols;
private TokenSymbol[string] tokens; // indexed by token name
private TokenSymbol[string] literal_tokens; // indexed by literal string
private TagSymbol[string] tags; // indexed by name
private NonTerminalSymbol[string] non_terminals; // indexed by name
private FieldDefinition[string] field_definitions; // indexed by name
private string[] skip_rule_list;
private auto current_precedence = Precedence.max;
invariant () {
import std.stdio;
auto check_sum = 0; // Sum of arithmetic series
for (auto i = SpecialSymbols.min; i <= SpecialSymbols.max; i++) {
assert(special_symbols[i].id == i);
check_sum += i;
}
assert(next_symbol_id == (SpecialSymbols.max + tokens.length + tags.length + non_terminals.length + 1));
foreach (literal; literal_tokens.keys) {
auto literal_token = literal_tokens[literal];
assert(literal_token.pattern == literal);
assert(tokens[literal_token.name] is literal_token);
}
foreach (key; tokens.byKey()) {
auto token = tokens[key];
assert(token.name == key && token.type == SymbolType.token);
assert(token.pattern[0] == '"' ? literal_tokens[token.pattern] is token : token.pattern !in literal_tokens);
assert(token.id > SpecialSymbols.max && token.id < next_symbol_id);
check_sum += token.id;
}
foreach (key; tags.byKey()) {
auto tag = tags[key];
assert(tag.name == key && tag.type == SymbolType.tag);
assert(tag.id > SpecialSymbols.max && tag.id < next_symbol_id);
check_sum += tag.id;
}
foreach (key; non_terminals.byKey()) {
auto non_terminal = non_terminals[key];
assert(non_terminal.name == key && non_terminal.type == SymbolType.non_terminal);
assert(non_terminal.id > SpecialSymbols.max && non_terminal.id < next_symbol_id);
check_sum += non_terminal.id;
}
assert(check_sum == ((next_symbol_id * (next_symbol_id - 1)) / 2));
}
static this()
{
special_symbols[SpecialSymbols.start] = new Symbol(SpecialSymbols.start, "ddSTART", SymbolType.non_terminal, CharLocation(0, 0));
special_symbols[SpecialSymbols.end] = new Symbol(SpecialSymbols.end, "ddEND", SymbolType.token, CharLocation(0, 0));
special_symbols[SpecialSymbols.invalid_token] = new Symbol(SpecialSymbols.invalid_token, "ddINVALID_TOKEN", SymbolType.token, CharLocation(0, 0));
special_symbols[SpecialSymbols.parse_error] = new Symbol(SpecialSymbols.parse_error, "ddERROR", SymbolType.non_terminal, CharLocation(0, 0));
special_symbols[SpecialSymbols.parse_error].firsts_data = new FirstsData(Set!Symbol(), true);
}
TokenSymbol new_token(string new_token_name, string pattern, CharLocation location, string field_name = "")
in {
assert(!is_known_symbol(new_token_name));
}
body {
auto token = cast(TokenSymbol) new Symbol(next_symbol_id++, new_token_name, SymbolType.token, location);
token.pattern = pattern;
token.field_name = field_name;
tokens[new_token_name] = token;
if (pattern[0] == '"') {
literal_tokens[pattern] = token;
}
return token;
}
TagSymbol new_tag(string new_tag_name, CharLocation location)
in {
assert(!is_known_symbol(new_tag_name));
}
body {
auto tag = cast(TagSymbol) new Symbol(next_symbol_id++, new_tag_name, SymbolType.tag, location);
tags[new_tag_name] = tag;
return tag;
}
@property
size_t token_count()
{
return tokens.length;
}
@property
size_t non_terminal_count()
{
return non_terminals.length;
}
bool is_known_token(string symbol_name)
{
return (symbol_name in tokens) !is null;
}
bool is_known_literal(string literal)
{
return (literal in literal_tokens) !is null;
}
bool is_known_tag(string symbol_name)
{
return (symbol_name in tags) !is null;
}
bool
is_known_non_terminal(string symbol_name)
{
return (symbol_name in non_terminals) !is null;
}
bool is_known_symbol(string symbol_name)
{
return symbol_name in tokens || symbol_name in tags || symbol_name in non_terminals;
}
Symbol get_symbol(string symbol_name)
{
if (symbol_name in tokens) {
return tokens[symbol_name];
} else if (symbol_name in non_terminals) {
return non_terminals[symbol_name];
} else if (symbol_name in tags) {
return tags[symbol_name];
}
return null;
}
Symbol get_special_symbol(SymbolId symbol_id)
{
return special_symbols[symbol_id];
}
Symbol get_symbol(string symbol_name, CharLocation location, bool auto_create=false)
{
auto symbol = get_symbol(symbol_name);
if (symbol !is null) {
symbol.used_at ~= location;
} else if (auto_create) {
// if it's referenced without being defined it's a non terminal
symbol = cast(NonTerminalSymbol) new Symbol(next_symbol_id++, symbol_name, SymbolType.non_terminal, location, false);
non_terminals[symbol_name] = symbol;
}
return symbol;
}
TokenSymbol get_literal_token(string literal, CharLocation location)
{
auto token_symbol = literal_tokens.get(literal, null);
if (token_symbol !is null) {
token_symbol.used_at ~= location;
}
return token_symbol;
}
CharLocation get_declaration_point(string symbol_name)
in {
assert(is_known_symbol(symbol_name));
}
body {
if (symbol_name in tokens) {
return tokens[symbol_name].defined_at;
} else if (symbol_name in non_terminals) {
return non_terminals[symbol_name].defined_at;
} else if (symbol_name in tags) {
return tags[symbol_name].defined_at;
}
assert(0);
}
void set_precedences(Associativity assoc, string[] symbol_names, CharLocation location)
in {
foreach (symbol_name; symbol_names) {
assert(!is_known_non_terminal(symbol_name));
assert(!is_known_tag(symbol_name));
}
}
body {
foreach (symbol_name; symbol_names) {
auto symbol = tokens.get(symbol_name, null);
if (symbol is null) {
symbol = new_tag(symbol_name, location);
}
symbol.associative_precedence = AssociativePrecedence(assoc, current_precedence);
}
current_precedence--;
}
void set_precedences(Associativity assoc, Symbol[] symbols)
in {
foreach (symbol; symbols) {
assert(symbol.type != SymbolType.non_terminal);
}
}
body {
foreach (symbol; symbols) {
symbol.associative_precedence = AssociativePrecedence(assoc, current_precedence);
}
current_precedence--;
}
void new_field(string field_name, string field_type, string conv_func_name, CharLocation defined_at)
in {
assert(!is_known_field(field_name));
}
body {
field_definitions[field_name] = FieldDefinition(field_name, field_type, conv_func_name);
}
bool is_known_field(string field_name)
{
return (field_name in field_definitions) !is null;
}
void add_skip_rule(string new_rule)
in {
assert(new_rule.length > 3);
}
body {
skip_rule_list ~= new_rule;
}
NonTerminalSymbol define_non_terminal(string symbol_name, CharLocation location)
in {
assert(!is_known_token(symbol_name) && !is_known_tag(symbol_name));
assert(!is_known_non_terminal(symbol_name) || !non_terminals[symbol_name].is_defined());
}
body {
auto symbol = non_terminals.get(symbol_name, null);
if (symbol !is null) {
symbol.defined_at = location;
} else {
symbol = cast(NonTerminalSymbol) new Symbol(next_symbol_id++, symbol_name, SymbolType.non_terminal, location, true);
non_terminals[symbol_name] = symbol;
}
return symbol;
}
NonTerminalSymbol[] get_undefined_symbols()
{
NonTerminalSymbol[] undefined_symbols;
foreach (nts; non_terminals) {
if (!nts.is_defined) {
undefined_symbols ~= nts;
}
}
return undefined_symbols;
}
Symbol[] get_unused_symbols()
{
Symbol[] unused_symbols;
foreach (symbols; [tokens, tags, non_terminals]) {
foreach (symbol; symbols) {
if (!symbol.is_used) {
unused_symbols ~= symbol;
}
}
}
return unused_symbols;
}
TokenSymbol[] get_tokens_ordered()
out (result) {
mixin WorkAroundClassCmpLimitations!TokenSymbol;
for (auto i = 0; i < result.length; i++) {
if (i > 0) assert(WA_CMP_ECAST(result[i -1]) < WA_CMP_ECAST(result[i]));
assert(result[i].type == SymbolType.token);
}
assert(result.length == tokens.length);
}
body {
return tokens.values.wa_sort;
}
TokenSymbol[] get_special_tokens_ordered()
out (result) {
mixin WorkAroundClassCmpLimitations!TokenSymbol;
for (auto i = 0; i < result.length; i++) {
if (i > 0) assert(WA_CMP_ECAST(result[i -1]) < WA_CMP_ECAST(result[i]));
assert(result[i].type == SymbolType.token);
assert(result[i].id >= SpecialSymbols.min);
assert(result[i].id <= SpecialSymbols.max);
}
}
body {
// special_symbols is ordered so just pick the tokens
TokenSymbol[] special_tokens_ordered;
foreach (symbol; special_symbols) {
if (symbol.type == SymbolType.token) {
special_tokens_ordered ~= symbol;
}
}
return special_tokens_ordered;
}
NonTerminalSymbol[] get_non_terminals_ordered()
out (result) {
mixin WorkAroundClassCmpLimitations!TokenSymbol;
for (auto i = 0; i < result.length; i++) {
if (i > 0) assert(WA_CMP_ECAST(result[i -1]) < WA_CMP_ECAST(result[i]));
assert(result[i].type == SymbolType.non_terminal);
}
assert(result.length == non_terminals.length);
}
body {
return non_terminals.values.wa_sort;
}
NonTerminalSymbol[] get_special_non_terminals_ordered()
out (result) {
mixin WorkAroundClassCmpLimitations!TokenSymbol;
for (auto i = 0; i < result.length; i++) {
if (i > 0) assert(WA_CMP_ECAST(result[i -1]) < WA_CMP_ECAST(result[i]));
assert(result[i].type == SymbolType.non_terminal);
assert(result[i].id >= SpecialSymbols.min);
assert(result[i].id <= SpecialSymbols.max);
}
}
body {
NonTerminalSymbol[] special_non_terminals;
foreach (symbol; special_symbols) {
if (symbol.type == SymbolType.non_terminal) {
special_non_terminals ~= symbol;
}
}
return special_non_terminals;
}
CharLocation get_field_defined_at(string field_name)
{
return field_definitions[field_name].defined_at;
}
FieldDefinition[] get_field_definitions()
{
return field_definitions.values;
}
string[] get_skip_rules()
{
return skip_rule_list.dup;
}
string[] get_description()
{
auto text_lines = ["Fields:"];
if (field_definitions.length == 0) {
text_lines ~= " <none>";
} else {
foreach (key; field_definitions.keys.wa_sort) {
with (field_definitions[key]) {
if (conversion_function_name.length == 0) {
text_lines ~= format(" %s: %s: %s to!(%s)(string str)", field_name, field_type, field_type, field_type);
} else {
text_lines ~= format(" %s: %s: %s %s(string str)", field_name, field_type, field_type, conversion_function_name);
}
}
}
}
text_lines ~= "Tokens:";
if (tokens.length == 0) {
text_lines ~= " <none>";
} else {
foreach (token; get_tokens_ordered()) {
with (token) {
text_lines ~= format(" %s: %s: %s: %s: %s: %s", id, name, pattern, field_name, associativity, precedence);
text_lines ~= format(" Defined At: %s", defined_at);
text_lines ~= format(" Used At: %s", used_at);
}
}
}
text_lines ~= "Precedence Tags:";
if (tags.length == 0) {
text_lines ~= " <none>";
} else {
foreach (tagKey; tags.keys.wa_sort) {
with (tags[tagKey]) {
text_lines ~= format(" %s: %s: %s: %s", id, name, associativity, precedence);
text_lines ~= format(" Defined At: %s", defined_at);
text_lines ~= format(" Used At: %s", used_at);
}
}
}
text_lines ~= "Non Terminal Symbols:";
if (non_terminals.length == 0) {
text_lines ~= " <none>";
} else {
foreach (non_terminal; get_non_terminals_ordered()) {
with (non_terminal) {
text_lines ~= format(" %s: %s:", id, name);
text_lines ~= format(" Defined At: %s", defined_at);
text_lines ~= format(" Used At: %s", used_at);
}
}
}
return text_lines;
}
}
unittest {
auto st = SymbolTable();
assert(st.get_special_symbol(SpecialSymbols.start).name == "ddSTART");
}