-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddpg.d
100 lines (97 loc) · 3.27 KB
/
ddpg.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
#!/usr/bin/rdmd
// ddpg.d
//
// Copyright Peter Williams 2013 <[email protected]>.
//
// 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)
import std.stdio;
import std.getopt;
import std.file;
import std.utf;
version (bootstrap) {
import bootstrap;
} else {
import dunnart;
}
import grammar;
import cli;
import errors;
int main(string[] args)
{
if (!process_command_line(args)) {
return 1;
}
// Read the text to be parsed
string input_text;
try {
input_text = readText(input_file_path);
} catch (FileException e) {
auto msg = extract_file_exception_msg(e);
writeln(msg);
return 2;
} catch (UTFException e) {
writefln("%s: not a valid text file: %s", input_file_path, e);
return 3;
}
// Parse the text and generate the grammar specification
auto grammar_specification = parse_specification_text(input_text, input_file_path);
if (grammar_specification is null) return 4;
if (verbose) {
writeln("Grammar Specification\n");
foreach (text_line; grammar_specification.get_description()) {
writeln(text_line);
}
}
// warn about unused symbols
foreach (unused_symbol; grammar_specification.symbol_table.get_unused_symbols()) {
warning(unused_symbol.defined_at, "Symbol \"%s\" is not used", unused_symbol);
}
// undefined symbols are fatal errors
foreach (undefined_symbol; grammar_specification.symbol_table.get_undefined_symbols()) {
foreach (locn; undefined_symbol.used_at) {
error(locn, "Symbol \"%s\" is not defined", undefined_symbol);
}
}
if (error_count > 0) {
stderr.writefln("Too many (%s) errors aborting", error_count);
return 5;
}
// Generate the grammar from the specification
auto grammar = new Grammar(grammar_specification);
if (grammar is null) return 6;
if (state_file_path) {
try {
auto state_file = File(state_file_path, "w");
state_file.write(grammar.get_parser_states_description());
} catch (Exception e) {
writeln(e);
}
} else if (verbose) {
writeln("\nGrammar");
writeln(grammar.get_parser_states_description());
}
if (grammar.total_unresolved_conflicts > 0) {
foreach (parser_state; grammar.parser_states) {
foreach (src; parser_state.shift_reduce_conflicts) {
writefln("State<%s>: shift/reduce conflict on token: %s", parser_state.id, src.shift_symbol);
}
foreach (rrc; parser_state.reduce_reduce_conflicts) {
writefln("State<%s>: reduce/reduce conflict on token(s): %s", parser_state.id, rrc.look_ahead_set_intersection);
}
}
if (grammar.total_unresolved_conflicts != expected_number_of_conflicts) {
stderr.writefln("Unexpected conflicts (%s) aborting", grammar.total_unresolved_conflicts);
return 7;
}
}
try {
auto output_file = File(output_file_path, "w");
grammar.write_parser_code(output_file, module_name);
} catch (Exception e) {
writeln(e);
return 8;
}
return 0;
}