root/trunk/enki/bootstrap.d

Revision 289, 32.1 kB (checked in by h3r3tic, 4 months ago)

ported Enki to Tango; bootstraps, no other tests done yet

Line 
1 /+
2     Copyright (c) 2006 Eric Anderton
3
4     Permission is hereby granted, free of charge, to any person
5     obtaining a copy of this software and associated documentation
6     files (the "Software"), to deal in the Software without
7     restriction, including without limitation the rights to use,
8     copy, modify, merge, publish, distribute, sublicense, and/or
9     sell copies of the Software, and to permit persons to whom the
10     Software is furnished to do so, subject to the following
11     conditions:
12
13     The above copyright notice and this permission notice shall be
14     included in all copies or substantial portions of the Software.
15
16     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18     OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20     HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21     WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23     OTHER DEALINGS IN THE SOFTWARE.
24 +/
25 module enki.bootstrap;
26
27 /**
28     Bootstrap module for Enki.
29     
30     Generates EnkiParser.d and enki.bnf from parse-tree declared in this module.
31     
32     Build using "-version=Bootstrap".
33 */
34
35 private import enki.types;
36 private import enki.EnkiBackend;
37 private import enki.CodeGenerator;
38 private import enki.Expression;
39 private import enki.Directive;
40 private import enki.Rule;
41
42 import tango.text.Util;
43 import tango.text.Unicode;
44 import tango.io.Stdout;
45 import tango.io.File;
46
47 // helper functions
48 public SubExpression[] makeTerm(SubExpression[] factors...){
49     return factors.dup;
50 }
51
52 public SubExpression[][] orExpr(SubExpression[][] terms...){
53     return terms.dup;
54 }
55
56 class EmptyLine : SyntaxLine{
57     public void semanticPass(BaseEnkiParser root){
58     }
59    
60     public String toBNF(){
61         return "\n";
62     }
63     public String toString(){
64         return "";
65     }
66 }
67
68 static EmptyLine empty;
69 static this(){ empty = new EmptyLine(); }
70
71 void main(){
72     SyntaxLine[] lines;
73            
74     lines ~= new BoilerplateDirective(`/+
75     Copyright (c) 2006 Eric Anderton
76
77     Permission is hereby granted, free of charge, to any person
78     obtaining a copy of this software and associated documentation
79     files (the "Software"), to deal in the Software without
80     restriction, including without limitation the rights to use,
81     copy, modify, merge, publish, distribute, sublicense, and/or
82     sell copies of the Software, and to permit persons to whom the
83     Software is furnished to do so, subject to the following
84     conditions:
85
86     The above copyright notice and this permission notice shall be
87     included in all copies or substantial portions of the Software.
88
89     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
90     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
91     OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
92     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
93     HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
94     WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
95     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
96     OTHER DEALINGS IN THE SOFTWARE.
97 +/`);
98
99     lines ~= empty;
100     lines ~= new Comment(" Enki Grammar Definition (self-hosting frontend)");
101     lines ~= new ModuleDirective("enki.EnkiParser");
102     lines ~= empty;
103     lines ~= new ImportDirective("enki.EnkiBackend");
104     lines ~= new ImportDirective("enki.Rule");
105     lines ~= new ImportDirective("enki.Expression");
106     lines ~= new ImportDirective("enki.Directive");
107    
108     lines ~= empty;
109     lines ~= new DefineDirective("bool","eoi",true,"End of Input");
110     lines ~= new DefineDirective("bool","eol",true,"End of Line");
111     lines ~= new DefineDirective("bool","err",true,"Error");
112     lines ~= new DefineDirective("String","letter",true,"Letter");
113     lines ~= new DefineDirective("String","digit",true,"Digit");
114     lines ~= new DefineDirective("String","hexdigit",true,"Hexdigit");
115     lines ~= new DefineDirective("String","any",true,"any");
116     lines ~= new DefineDirective("String","newline",true,"Newline");
117     lines ~= new DefineDirective("String","sp",true,"Space(s)");
118     lines ~= new DefineDirective("String","ws",false,"Whitespace");
119    
120     lines ~= empty;             
121     lines ~= new ParseTypeDirective("String");
122     lines ~= new UTFDirective("8");
123    
124     lines ~= empty;             
125     lines ~= new BaseClassDirective("BaseEnkiParser");
126     lines ~= new ClassnameDirective("EnkiParser");
127    
128     // SUPPORT
129     lines ~= empty;
130     lines ~= new Rule("WS",
131         new DefaultPredicate(),
132         new Expression(
133             new Production("ws",null),
134             new OptionalExpr(
135                 new Expression(
136                     new GroupExpr(
137                         new Expression(
138                             orExpr(
139                                 makeTerm(new Production("SlashSlashComment",null)),
140                                 makeTerm(new Production("SlashStarComment",null))
141                             )
142                         ),
143                         null
144                     ),
145                     new Production("WS",null)
146                 ),
147                 null
148             )
149         )
150     ); 
151    
152     // SYNTAX
153     lines ~= empty;
154     lines ~= new Rule("Syntax",
155         new FunctionPredicate(
156             new Param(false,"void","createSyntax"),
157             new Param(true,"SyntaxLine","lines")
158         ),
159         new Expression(
160             new Production("WS",null),
161             new ZeroOrMoreExpr(
162                 new Expression(
163                     new GroupExpr(
164                         new Expression(
165                             orExpr(
166                                 makeTerm(new Production("Rule",new Binding(true,"lines"))),
167                                 makeTerm(new Production("Comment",new Binding(true,"lines"))),
168                                 makeTerm(new Production("Directive",new Binding(true,"lines")))
169                             )
170                         ),
171                         null
172                     ),
173                     new Production("WS",null)
174                 ),
175                 null,
176                 new Production("eoi",null)
177             )
178         )
179     );
180    
181     lines ~= empty;
182     lines ~= new Comment("");
183     lines ~= new Comment(" Rule and Predicate");
184     lines ~= new Comment("");
185
186     //RULE
187     lines ~= new Rule("Rule",
188         new ClassPredicate(
189             "Rule",
190             new Param("name"),
191             new Param("pred"),
192             new Param("expr"),
193             new Param("decl")
194         ),
195         new Expression(
196             new Production("Identifier",new Binding(false,"name")),
197             new Production("WS",null),
198             new OptionalExpr(
199                 new Expression(
200                     new Production("RuleDecl",new Binding(false,"decl")),
201                     new Production("WS",null)
202                 ),
203                 null
204             ),
205             new OptionalExpr(
206                 new Expression(
207                     new Production("RulePredicate",new Binding(false,"pred"))
208                 ),
209                 null
210             ),
211             new Production("WS",null),
212             new Terminal("::=",null),
213             new Production("WS",null),
214             new Production("Expression",new Binding(false,"expr")),
215             new Production("WS",null),
216             new Terminal(";",null)
217         )
218     );
219    
220     lines ~= new Rule("RuleDecl",
221         new ClassPredicate("RuleDecl",new Param(true,"Param","params")),
222         new Expression(
223             new Production("ParamsExpr",new Binding(false,"params"))
224         )
225     );
226
227     lines ~= new Rule("RulePredicate",
228         new BindingPredicate(new Param(false,"RulePredicate","pred")),
229         new Expression(
230             new Terminal("=",null),
231             new Production("WS",null),
232             new GroupExpr(
233                 new Expression(
234                     orExpr(
235                         makeTerm(new Production("ClassPredicate",new Binding(false,"pred"))),
236                         makeTerm(new Production("FunctionPredicate",new Binding(false,"pred"))),
237                         makeTerm(new Production("BindingPredicate",new Binding(false,"pred")))
238                     )
239                 ),
240                 null
241             )
242         )
243     );
244
245     lines ~= new Rule("ClassPredicate",
246         new ClassPredicate("ClassPredicate",
247             new Param("name"),
248             new Param("params")
249         ),
250         new Expression(
251             new Terminal("new",null),
252             new Production("WS",null),
253             new Production("Identifier",new Binding(false,"name")),
254             new Production("WS",null),
255             new Production("ParamsExpr",new Binding(false,"params"))
256         )
257     );
258
259     lines ~= new Rule("FunctionPredicate",
260         new ClassPredicate("FunctionPredicate",
261             new Param("decl"),
262             new Param("params")
263         ),
264         new Expression(
265             new Production("ExplicitParam",new Binding(false,"decl")),
266             new Production("WS",null),
267             new Production("ParamsExpr",new Binding(false,"params"))
268         )
269     );
270
271     lines ~= new Rule("BindingPredicate",
272         new ClassPredicate("BindingPredicate",
273             new Param("param")
274         ),
275         new Expression(
276             new Production("Param",new Binding(false,"param"))
277         )
278     );
279
280     lines ~= new Rule("ParamsExpr",
281         new BindingPredicate(new Param(true,"Param","params")),
282         new Expression(
283             new Terminal("(",null),
284             new Production("WS",null),
285             new OptionalExpr(
286                 new Expression(
287                     new Production("Param",new Binding(true,"params")),
288                     new Production("WS",null),
289                     new ZeroOrMoreExpr(
290                         new Expression(
291                             new Terminal(",",null),
292                             new Production("WS",null),
293                             new Production("Param",new Binding(true,"params")),
294                             new Production("WS",null)
295                         ),
296                         null,
297                         null
298                     )
299                 ),
300                 null
301             ),
302             new Terminal(")",null)
303         )
304     );
305
306     lines ~= new Rule("Param",
307         new BindingPredicate(new Param("param")),
308         new Expression(
309             orExpr(
310                 makeTerm(new Production("ExplicitParam",new Binding(false,"param"))),
311                 makeTerm(new Production("WeakParam",new Binding(false,"param")))
312             )
313         )
314     );
315    
316     lines ~= new Rule("WeakParam",
317         new ClassPredicate("Param",
318             new Param("name")
319         ),
320         new Expression(
321             new Production("Identifier",new Binding(false,"name"))
322         )
323     );
324    
325     lines ~= new Rule("ExplicitParam",
326         new ClassPredicate("Param",
327             new Param(false,"bool","isArray"),
328             new Param("type"),
329             new Param("name")
330         ),
331         new Expression(
332             new Production("Identifier",new Binding(false,"type")),
333             new Production("WS",null),
334             new OptionalExpr(
335                 new Expression(
336                     new Terminal("[]",new Binding(false,"isArray")),
337                     new Production("Brackets",null),
338                     new Production("WS",null)
339                 ),
340                 null
341             ),
342             new Production("Identifier",new Binding(false,"name"))
343         )
344     );
345    
346     lines ~= new Rule("Brackets",
347         new DefaultPredicate(),
348         new Expression(
349             new OptionalExpr(
350                 new Expression(
351                     new Terminal("[]",null),
352                     new Production("Brackets",null)
353                 ),
354                 null
355             )
356         )
357     );
358    
359     lines ~= new Comment("");
360     lines ~= new Comment(" Expressions");
361     lines ~= new Comment("");
362    
363     lines ~= new Rule("Expression",     
364         new ClassPredicate("Expression",
365             new Param(true,"Term","terms")
366         ),
367         new Expression(
368             new Production("Term",new Binding(true,"terms")),
369             new Production("WS",null),
370             new ZeroOrMoreExpr(
371                 new Expression(
372                     new Terminal("|",null),
373                     new Production("WS",null),
374                     new Production("Term",new Binding(true,"terms")),
375                     new Production("WS",null)
376                 ),
377                 null,
378                 null
379             )
380         )
381     );
382    
383    
384     lines ~= new Rule("Term",       
385         new BindingPredicate(new Param(true,"SubExpression","factors")),
386         new Expression(
387             new Production("SubExpression",new Binding(true,"factors")),
388             new Production("WS",null),
389             new ZeroOrMoreExpr(
390                 new Expression(
391                     new Production("SubExpression",new Binding(true,"factors")),
392                     new Production("WS",null)
393                 ),
394                 null,
395                 null
396             )
397         )
398     );
399        
400     lines ~= new Rule("SubExpression",
401         new BindingPredicate(new Param(false,"SubExpression","expr")),
402         new Expression(
403             orExpr(
404                 makeTerm(new Production("Production",new Binding(false,"expr"))),
405                 makeTerm(new Production("Substitution",new Binding(false,"expr"))),
406                 makeTerm(new Production("Terminal",new Binding(false,"expr"))),
407                 makeTerm(new Production("Range",new Binding(false,"expr"))),
408                 makeTerm(new Production("Regexp",new Binding(false,"expr"))),
409                 makeTerm(new Production("GroupExpr",new Binding(false,"expr"))),
410                 makeTerm(new Production("OptionalExpr",new Binding(false,"expr"))),
411                 makeTerm(new Production("ZeroOrMoreExpr",new Binding(false,"expr"))),
412                 makeTerm(new Production("NegateExpr",new Binding(false,"expr"))),
413                 makeTerm(new Production("TestExpr",new Binding(false,"expr"))),
414                 makeTerm(new Production("LiteralExpr",new Binding(false,"expr"))),
415                 makeTerm(new Production("CustomTerminal",new Binding(false,"expr")))
416             )
417         )
418     ); 
419
420     lines ~= new Rule("Production",
421         new ClassPredicate("Production",
422             new Param("name"),
423             new Param("binding"),
424             new Param(true,"ProductionArg","args")
425         ),
426         new Expression(
427             new Production("Identifier",new Binding(false,"name")),
428             new Production("WS",null),
429             new OptionalExpr(
430                 new Expression(
431                     new Terminal("!(",null),
432                     new Production("WS",null),
433                     new Production("ProductionArg",new Binding(true,"args")),
434                     new ZeroOrMoreExpr(
435                         new Expression(
436                             new Production("WS",null),
437                             new Terminal(",",null),
438                             new Production("WS",null),
439                             new Production("ProductionArg",new Binding(true,"args"))
440                         ),
441                         null,
442                         new Terminal(")",null)
443                     )
444                 ),
445                 null
446             ),
447             new OptionalExpr(
448                 new Expression(
449                     new Production("Binding",new Binding(false,"binding"))
450                 ),
451                 null
452             )
453         )
454     );
455
456     lines ~= new Rule("ProductionArg",
457         new BindingPredicate(new Param(false,"ProductionArg","arg")),
458         new Expression(
459             orExpr(
460                 makeTerm(new Production("StringProductionArg",new Binding(false,"arg"))),
461                 makeTerm(new Production("BindingProductionArg",new Binding(false,"arg")))
462             )
463         )
464     );
465    
466     lines ~= new Rule("StringProductionArg",
467         new ClassPredicate("StringProductionArg",new Param(false,"String","value")),
468         new Expression(
469             new Production("String",new Binding(false,"value"))
470         )
471     );
472    
473
474     lines ~= new Rule("BindingProductionArg",
475         new ClassPredicate("BindingProductionArg",new Param(false,"String","value")),
476         new Expression(
477             new Production("Identifier",new Binding(false,"value"))
478         )
479     );
480
481     lines ~= new Rule("Substitution",
482         new ClassPredicate("Substitution",
483             new Param(false,"String","name"),
484             new Param(false,"Binding","binding")
485         ),
486         new Expression(
487             new Terminal(".",null),
488             new Production("Identifier",new Binding(false,"name")),
489             new Production("WS",null),
490             new OptionalExpr(
491                 new Expression(
492                     new Production("Binding",new Binding(false,"binding"))
493                 ),
494                 null
495             )
496         )
497     );
498    
499     lines ~= new Rule("GroupExpr",
500         new ClassPredicate("GroupExpr",
501             new Param("expr"),
502             new Param("binding")
503         ),
504         new Expression(
505             new Terminal("(",null),
506             new Production("WS",null),
507             new Production("Expression",new Binding(false,"expr")),
508             new Production("WS",null),
509             new Terminal(")",null),
510             new Production("WS",null),     
511             new OptionalExpr(
512                 new Expression(
513                     new Production("Binding",new Binding(false,"binding"))
514                 ),
515                 null
516             )
517         )
518     );   
519    
520     lines ~= new Rule("OptionalExpr",
521         new ClassPredicate("OptionalExpr",
522             new Param("expr"),
523             new Param("binding")
524         ),
525         new Expression(
526             new Terminal("[",null),
527             new Production("WS",null),
528             new Production("Expression",new Binding(false,"expr")),
529             new Production("WS",null),
530             new Terminal("]",null),
531             new Production("WS",null),     
532             new OptionalExpr(
533                 new Expression(
534                     new Production("Binding",new Binding(false,"binding"))
535                 ),
536                 null
537             )
538         )
539     );     
540        
541     lines ~= new Rule("ZeroOrMoreExpr",
542         new ClassPredicate("ZeroOrMoreExpr",
543             new Param("expr"),
544             new Param("binding"),
545             new Param("term")
546         ),
547         new Expression(
548             new Terminal("{",null),
549             new Production("WS",null),
550             new Production("Expression",