| 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.enki; |
|---|
| 26 |
|
|---|
| 27 |
private import enki.EnkiBackend; |
|---|
| 28 |
private import enki.EnkiParser; |
|---|
| 29 |
private import enki.enki_bn; |
|---|
| 30 |
|
|---|
| 31 |
private import utils.ArgParser; |
|---|
| 32 |
|
|---|
| 33 |
private import tango.io.FilePath; |
|---|
| 34 |
private import tango.io.File; |
|---|
| 35 |
private import tango.io.Console; |
|---|
| 36 |
private import tango.io.Stdout; |
|---|
| 37 |
|
|---|
| 38 |
char[] helpText = |
|---|
| 39 |
`Enki - Frontend Parser Generator - V1.2 Build {} |
|---|
| 40 |
Copyright(c) 2006 Eric Anderton |
|---|
| 41 |
|
|---|
| 42 |
Creates a parser class based on annotated EBNF input. |
|---|
| 43 |
The default ouput filename is "parser.d". |
|---|
| 44 |
|
|---|
| 45 |
Usage: |
|---|
| 46 |
enki <source ebnf file> {{ -switch } |
|---|
| 47 |
|
|---|
| 48 |
-f<file> Specify output filename |
|---|
| 49 |
-b<file> Specify output ebnf filename |
|---|
| 50 |
-t Test Mode (output everything to console) |
|---|
| 51 |
|
|---|
| 52 |
The -b option is used primarily as a sanity check of |
|---|
| 53 |
enki's internal workings. The generated output is |
|---|
| 54 |
re-created from the internal parse tree after the input |
|---|
| 55 |
file is consumed. |
|---|
| 56 |
|
|---|
| 57 |
The <file> params to follow -f and -b may be omitted if |
|---|
| 58 |
-t is specified. |
|---|
| 59 |
`; |
|---|
| 60 |
|
|---|
| 61 |
void main(char[][] args){ |
|---|
| 62 |
if(args.length == 1){ |
|---|
| 63 |
Stdout.format(helpText,auto_build_number); |
|---|
| 64 |
return 0; |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
char[] inputFilename; |
|---|
| 68 |
char[] outputFilename; |
|---|
| 69 |
char[] ebnfFilename; |
|---|
| 70 |
bool testMode = false; |
|---|
| 71 |
|
|---|
| 72 |
outputFilename = "parser.d"; |
|---|
| 73 |
|
|---|
| 74 |
// configure the arg parser |
|---|
| 75 |
ArgParser argParser = new ArgParser(delegate uint(char[] value,uint ordinal){ |
|---|
| 76 |
if(ordinal > 0) throw new Exception("Invalid argument '" ~ value ~ "'"); |
|---|
| 77 |
inputFilename = value; |
|---|
| 78 |
return value.length; |
|---|
| 79 |
}); |
|---|
| 80 |
|
|---|
| 81 |
argParser.bind("-", "f",delegate uint(char[] value){ |
|---|
| 82 |
outputFilename = value; |
|---|
| 83 |
return value.length; |
|---|
| 84 |
}); |
|---|
| 85 |
|
|---|
| 86 |
argParser.bind("-", "b",delegate uint(char[] value){ |
|---|
| 87 |
ebnfFilename = value; |
|---|
| 88 |
return value.length; |
|---|
| 89 |
}); |
|---|
| 90 |
|
|---|
| 91 |
argParser.bind("-", "t",delegate uint(char[] value){ |
|---|
| 92 |
testMode = true; |
|---|
| 93 |
return value.length; |
|---|
| 94 |
}); |
|---|
| 95 |
|
|---|
| 96 |
// parse and resolve arguments |
|---|
| 97 |
argParser.parse(args[1..$]); |
|---|
| 98 |
|
|---|
| 99 |
if(!inputFilename){ |
|---|
| 100 |
throw new Exception("No filename specified."); |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
auto fp=FilePath(inputFilename); |
|---|
| 104 |
if(!fp.exists || fp.isFolder){ |
|---|
| 105 |
throw new Exception("File '" ~ inputFilename ~ "' doesn't exist."); |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
if(!testMode){ |
|---|
| 109 |
if(outputFilename && outputFilename == ""){ |
|---|
| 110 |
throw new Exception("Ouptut filename not specified - filename expected after '-f'."); |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
if(ebnfFilename && ebnfFilename == ""){ |
|---|
| 114 |
throw new Exception("Ouptut BNF filename not specified - filename expected after '-b'."); |
|---|
| 115 |
} |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
// run the Enki Parser |
|---|
| 119 |
auto parser = new EnkiParser(); |
|---|
| 120 |
parser.initalize(cast(char[])File(inputFilename).read); |
|---|
| 121 |
auto result = parser.parse_Syntax(); |
|---|
| 122 |
|
|---|
| 123 |
if(result.success){ |
|---|
| 124 |
parser.semanticPass(); |
|---|
| 125 |
|
|---|
| 126 |
if(testMode){ |
|---|
| 127 |
Cout("Parser:\n ")(parser.render()); |
|---|
| 128 |
} |
|---|
| 129 |
else{ |
|---|
| 130 |
File(outputFilename).write(parser.render()); |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
if(ebnfFilename){ |
|---|
| 134 |
if(testMode){ |
|---|
| 135 |
Cout("\nBNF:\n ")(parser.toBNF()); |
|---|
| 136 |
} |
|---|
| 137 |
else{ |
|---|
| 138 |
File(ebnfFilename).write(parser.toBNF()); |
|---|
| 139 |
} |
|---|
| 140 |
} |
|---|
| 141 |
} |
|---|
| 142 |
else{ |
|---|
| 143 |
Cout("\nErrors:\n")(parser.getErrorReport()); |
|---|
| 144 |
} |
|---|
| 145 |
} |
|---|