Created
October 1, 2022 20:21
-
-
Save dk949/00d86f693fdf89df76c396ef7ea3cd62 to your computer and use it in GitHub Desktop.
read PEG file and generate a D parser for it with pegged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/+dub.json: | |
{ | |
"authors": ["dk949" ], | |
"copyright": "Copyright © 2022, dk949", | |
"dependencies": { "pegged": "~>0.4.6" }, | |
"description": "compiler for standalone pegged grammars", | |
"license": "BSL-1.0", | |
"name": "pegd" | |
} | |
+/ | |
import pegged.grammar; | |
import std.file; | |
import std.path; | |
import std.stdio; | |
int main(string[] args) { | |
string input; | |
string output = "parser"; | |
switch (args.length) { | |
case 3: | |
output = args[2]; | |
goto case; | |
case 2: | |
input = args[1]; | |
break; | |
case 0: | |
args ~= "[APPLICATION_NAME]"; | |
goto default; | |
default: | |
stderr.writeln("Usage: ", baseName(args[0]), " INPUT_FILE [OUTPUT_FILE]"); | |
return 1; | |
} | |
if (output.length >= 2 && output[$ - 2 .. $] == ".d") | |
output.length -= 2; | |
if (output == "") { | |
stderr.writeln( | |
"output file name has to not be enmpty (and cannot contain just the extension)"); | |
return 1; | |
} | |
try | |
asModule(baseName(output), output, readText(input)); | |
catch (Exception e) { | |
stderr.writefln("Error: ", e.message); | |
return 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment