1 module ruleTranslator; 2 3 import RuleTranslatorLexer; 4 import RuleTranslatorParser : RuleTranslatorParser; 5 import RuleTranslatorBaseListener : RuleTranslatorBaseListener; 6 import RuleWriter: RuleWriter; 7 import IBAListener; 8 import TTSListener; 9 import antlr.v4.runtime.ANTLRInputStream; 10 import antlr.v4.runtime.CommonToken; 11 import antlr.v4.runtime.CommonTokenStream; 12 import antlr.v4.runtime.LexerNoViableAltException; 13 import antlr.v4.runtime.tree.ParseTreeWalker; 14 import std.getopt; 15 import std.path; 16 import std.stdio; 17 import std.string; 18 19 string outputDir; 20 bool verbose; 21 string withPropertyName = "this"; 22 string arguments; 23 enum Version {tts, iba}; 24 Version grammar_version; 25 26 int main(string[] argv) { 27 auto helpInformation = getopt( 28 argv, 29 "dsl-type|d", "DSL source type: tts (default) or iba", &grammar_version, 30 "with|w", "the with (main) property", &withPropertyName, 31 "arguments|a", "string of rule arguments", &arguments, 32 "od", "Output directory name, otherwise print to standard output", &outputDir, 33 "verbose|v", &verbose 34 ); 35 36 if (helpInformation.helpWanted) 37 { 38 defaultGetoptPrinter("Combined Announcement-DSL to IBA-XML/Text-to-speech translator.\n", 39 helpInformation.options); 40 return 0; 41 } 42 if (argv.length > 1) { 43 bool parserFailed = false; 44 45 foreach(filename; argv[1..$]) 46 { 47 if(verbose) 48 { 49 writefln("\nProcessing: %s as %s", filename, grammar_version); 50 } 51 auto antlrInput = new ANTLRInputStream(File(filename, "r")); 52 auto lexer = new RuleTranslatorLexer(antlrInput); 53 auto cts = new CommonTokenStream(lexer); 54 55 // Pass the tokens to the parser 56 auto parser = new RuleTranslatorParser(cts); 57 if(verbose) { 58 writefln("\tNumber of on channel tokens \t= %s.\n", cts.getNumberOfOnChannelTokens); 59 writefln("\ttokens:"); 60 foreach(t; cts.getTokens) 61 writefln("\t\t%s", t); 62 } 63 64 // Specify our entry point 65 auto rootContext = parser.file_input; 66 67 if(!parser.numberOfSyntaxErrors) 68 { 69 // No syntax errors 70 auto base = baseName(filename); 71 auto outputName = base[0..lastIndexOf(base, '.')+1] ~ "d"; 72 if (grammar_version == Version.iba) { 73 auto baseListener = new IBAListener; 74 auto writer = new RuleWriter; 75 if(outputDir) 76 { 77 writer.setOutputPath(outputDir); 78 } 79 writer.setOutputFilename(outputName); 80 baseListener.writer = writer; 81 82 auto walker = new ParseTreeWalker; 83 walker.walk(baseListener, rootContext); 84 } 85 else { 86 auto baseListener = new TTSListener; 87 auto writer = new RuleWriter; 88 if(outputDir) 89 { 90 writer.setOutputPath(outputDir); 91 } 92 writer.setOutputFilename(outputName); 93 baseListener.writer = writer; 94 baseListener.withPropertyName = withPropertyName; 95 baseListener.arguments = arguments; 96 97 auto walker = new ParseTreeWalker; 98 walker.walk(baseListener, rootContext); 99 } 100 } 101 else 102 { 103 writefln("\t%s syntax errors", parser.numberOfSyntaxErrors); 104 parserFailed = true; 105 } 106 } 107 return parserFailed ? 1 : 0; 108 } 109 else { 110 stderr.writeln("Error: missing DSL source"); 111 defaultGetoptPrinter("\nCommand line options", helpInformation.options); 112 return 1; 113 } 114 }