-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.d
230 lines (203 loc) · 6.13 KB
/
grammar.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
/**
Copyright 2018 Mark Fisher
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**/
module dxx.util.grammar;
private import pegged.grammar;
private import dxx.util;
template parseGrammar(string g) {
enum parseGrammar = grammar(g);
}
template parseGrammarFile(string fname) {
enum parseGrammarFile = parseGrammar!(import(fname));
}
template parseData(alias Grammar,string data) {
enum parseData = Grammar(data);
}
template parseData(alias Grammar) {
auto parseData(string data) {
return Grammar(data);
}
}
//template parseData(string data,string g,alias Grammar) {
//auto g = parseGrammar!g;
// enum parseData = Grammar(data);
//}
template parseDataFile(alias Grammar,string grammarFile,string dataFile) {
mixin(parseGrammarFile!grammarFile);
enum parseDataFile = parseDataFile!(Grammar,dataFile);
}
template parseDataFile(alias Grammar,string dataFile) {
enum parseDataFile = Grammar(import(dataFile));
}
template outputData(Sink,alias data) {
auto outputData(Sink sink) {
return sink ~= data;
}
}
template iterateParseTree(alias Node,alias Fnc) {
string iterateParseTree() {
appender:string s;
static foreach(n;Node.children) {
s ~= iterateParseTree!n;
}
return Fnc!(Node.name,Node,s);
}
}
string renderData(Data)() {
// string buffer
outputData!Data(sb);
return sb.data;
}
template GrammarParser(string g,string name) {
mixin(parseGrammar!g);
auto GrammarParser(string d) {
mixin(name)(d);
}
}
unittest {
import std.stdio;
import std.conv;
import std.array;
import std.algorithm;
enum g=q{
MyGrammar:
test < "test" {myaction} test2
test2 < "test2" identifier
};
enum d=`test test2 testId`;
class MyGrammar {
static bool _assert = false;
static string[] names;
mixin(parseGrammar!g);
static PT myaction(PT)(PT p) {
writeln(text("_action ",p));
_assert = true;
names ~= p.name;
return p;
}
this() {
auto a = parseData!(MyGrammar)(d);
writeln("Grammar test " ~ a.toString);
assert(_assert);
}
}
auto a = new MyGrammar;
class MyGrammar2 {
enum g=q{
MyGrammar2:
test < "test" {myaction} test2
test2 <- "test2" identifier
};
enum d=`test test2 testId`;
mixin(parseGrammar!g);
static PT myaction(PT)(PT p) {
return p;
}
enum data = parseData!(MyGrammar2,d);
enum _dataString = dataString(data);
static string dataString(ParseTree Data) {
string n = "";
switch(Data.name) {
case "MyGrammar2":
n = "enum __a = \"";
n ~= "_MY:";
n ~= dataString(Data.children[0]);
n ~= "\";";
break;
case "MyGrammar2.test":
case "MyGrammar2.test2":
n = nodeToString(Data);
n ~= Data.children.map!(dataString).join(", ").text;
default:
break;
}
return n;
}
static string nodeToString(N)(N n) {
return "\" ~ impl_" ~ n.name ~ "!(\""~ n.name ~ "\") ~ \"";
}
}
auto b = new MyGrammar2;
class impl_MyGrammar2 {
static auto test(string n)() {
return "TEST";
}
static auto test2(string n)() {
return "TEST2";
}
mixin(MyGrammar2._dataString);
}
writeln("Grammar2 test " ~ impl_MyGrammar2.__a);
}
unittest {
import std.array;
import std.string;
import std.stdio;
import std.algorithm;
import std.regex;
enum GRAMMAR = q{
MYGRAMMAR(Template):
MyDoc <- MyLine+ :endOfInput
MyLine <- :LDelim ^Template :RDelim / Text
LDelim <- "{{"
RDelim <- "}}"
Text <- ~((!LDelim) Char )*
Char <- .
};
mixin(parseGrammar!GRAMMAR);
struct range {
enum varOne = "varOne";
static string opCall(string i)() {
return i;
}
}
template compile(string n)
{
enum compile = "range.opCall!(\"" ~ n ~ "\")()";
}
static string dataString(alias Data)() {
string n = "";
pragma(msg,"Data "~ Data.toString);
switch(Data.name) {
case "MYGRAMMAR":
case "MYGRAMMAR.MyDoc":
case "MYGRAMMAR.MyLine":
static foreach(x;Data.children) n~=dataString!x();
break;
case "MYGRAMMAR.Text":
n ~= Data.matches.join("");
break;
case "identifier":
n ~= compile!(Data.matches.join(""));
break;
default:
break;
}
return n;
}
enum d = "enum fred = {{varOne}};";
enum data = parseData!(MYGRAMMAR!identifier,d);
pragma(msg,data);
enum _dataString = dataString!(data);
pragma(msg,_dataString);
writeln("Grammar Test: " ~ _dataString);
mixin(_dataString);
static assert(__traits(compiles,"writeln(fred);"));
writeln("" ~ fred);
static assert("varOne" == fred);
}