static void Try(string input) { if (show_input) { System.Console.WriteLine("Input:"); System.Console.WriteLine(input); } var str = new AntlrInputStream(input); var lexer = new ANTLRv3Lexer(str); var tokens = new CommonTokenStream(lexer); var parser = new ANTLRv3Parser(tokens); var listener = new ErrorListener <IToken>(parser, lexer, tokens); parser.AddErrorListener(listener); lexer.AddErrorListener(new ErrorListener <int>(parser, lexer, tokens)); var tree = parser.grammarDef(); if (listener.had_error) { System.Console.WriteLine("error in parse."); } else { System.Console.WriteLine("parse completed."); } if (show_tokens) { System.Console.WriteLine(tokens.OutputTokens(lexer)); } if (show_tree) { System.Console.WriteLine(tree.OutputTree(tokens)); } }
public void Try(string ffn, string input, ref Dictionary <string, string> results) { var now = DateTime.Now.ToString(); var errors = new StringBuilder(); var str = new AntlrInputStream(input); var lexer = new ANTLRv3Lexer(str); var tokens = new CommonTokenStream(lexer); var parser = new ANTLRv3Parser(tokens); var elistener = new ErrorListener <IToken>(parser, lexer, 0); parser.AddErrorListener(elistener); var tree = parser.grammarDef(); var error_file_name = ffn; error_file_name = error_file_name.EndsWith(".g3") ? (error_file_name.Substring(0, error_file_name.Length - 3) + ".txt") : error_file_name; error_file_name = error_file_name.EndsWith(".g") ? (error_file_name.Substring(0, error_file_name.Length - 2) + ".txt") : error_file_name; var new_ffn = ffn; new_ffn = new_ffn.EndsWith(".g3") ? (new_ffn.Substring(0, new_ffn.Length - 3) + ".g4") : new_ffn; new_ffn = new_ffn.EndsWith(".g") ? (new_ffn.Substring(0, new_ffn.Length - 2) + ".g4") : new_ffn; if (elistener.had_error) { results.Add(error_file_name, errors.ToString()); return; } else { errors.AppendLine("File " + ffn + " parsed successfully."); errors.AppendLine("Date: " + now); } // Transforms derived from two sources: // https://github.com/senseidb/sensei/pull/23 var(text_before, other) = TreeEdits.TextToLeftOfLeaves(tokens, tree); // Remove unused options at top of grammar def. // This specifically looks at the options at the top of the file, // not rule-based options. That will be handled separately below. using (AntlrTreeEditing.AntlrDOM.AntlrDynamicContext dynamicContext = new AntlrTreeEditing.AntlrDOM.ConvertToDOM().Try(tree, parser)) { org.eclipse.wst.xml.xpath2.processor.Engine engine = new org.eclipse.wst.xml.xpath2.processor.Engine(); // Allow language, tokenVocab, TokenLabelType, superClass var nodes = engine.parseExpression( @"//grammarDef/optionsSpec /option [id /(TOKEN_REF | RULE_REF) [text() = 'output' or text() = 'backtrack' or text() = 'memoize' or text() = 'ASTLabelType' or text() = 'rewrite' ]]", new StaticContextBuilder()).evaluate( dynamicContext, new object[] { dynamicContext.Document }) .Select(x => (x.NativeValue as AntlrTreeEditing.AntlrDOM.AntlrElement).AntlrIParseTree); TreeEdits.Delete(nodes); var options = engine.parseExpression( @"//grammarDef/optionsSpec", new StaticContextBuilder()).evaluate( dynamicContext, new object[] { dynamicContext.Document }) .Select(x => (x.NativeValue as AntlrTreeEditing.AntlrDOM.AntlrElement).AntlrIParseTree); foreach (var os in options) { if (os.ChildCount == 3) { TreeEdits.Delete(os); } } } // Fix options in the beginning of rules. // See https://theantlrguy.atlassian.net/wiki/spaces/ANTLR3/pages/2687029/Rule+and+subrule+options using (AntlrTreeEditing.AntlrDOM.AntlrDynamicContext dynamicContext = new AntlrTreeEditing.AntlrDOM.ConvertToDOM().Try(tree, parser)) { org.eclipse.wst.xml.xpath2.processor.Engine engine = new org.eclipse.wst.xml.xpath2.processor.Engine(); // Allow language, tokenVocab, TokenLabelType, superClass var nodes = engine.parseExpression( @"//rule_/optionsSpec /option [id /(TOKEN_REF | RULE_REF) [text() = 'output' or text() = 'backtrack' or text() = 'memoize' or text() = 'ASTLabelType' or text() = 'rewrite' ]]", new StaticContextBuilder()).evaluate( dynamicContext, new object[] { dynamicContext.Document }) .Select(x => (x.NativeValue as AntlrTreeEditing.AntlrDOM.AntlrElement).AntlrIParseTree); TreeEdits.Delete(nodes); var options = engine.parseExpression( @"//rule_/optionsSpec", new StaticContextBuilder()).evaluate( dynamicContext, new object[] { dynamicContext.Document }) .Select(x => (x.NativeValue as AntlrTreeEditing.AntlrDOM.AntlrElement).AntlrIParseTree); foreach (var os in options) { if (os.ChildCount == 3) { TreeEdits.Delete(os); } } } // Use new tokens{} syntax using (AntlrTreeEditing.AntlrDOM.AntlrDynamicContext dynamicContext = new AntlrTreeEditing.AntlrDOM.ConvertToDOM().Try(tree, parser)) { org.eclipse.wst.xml.xpath2.processor.Engine engine = new org.eclipse.wst.xml.xpath2.processor.Engine(); var nodes = engine.parseExpression( @"//tokensSpec /tokenSpec /SEMI", new StaticContextBuilder()).evaluate( dynamicContext, new object[] { dynamicContext.Document }) .Select(x => (x.NativeValue as AntlrTreeEditing.AntlrDOM.AntlrElement).AntlrIParseTree); if (nodes.Any()) { // Delete tha last ";" in tokens list--change in syntax. var last = nodes.Last(); TreeEdits.Delete(last); // Replace all remaining ";" with ",". TreeEdits.Replace(tree, (in IParseTree n, out bool c) => { c = true; if (!nodes.Contains(n) && n != last) { return(null); } var t = n as TerminalNodeImpl; var new_sym = new TerminalNodeImpl(new CommonToken(ANTLRv4Lexer.COMMA) { Line = -1, Column = -1, Text = "," }); text_before.TryGetValue(t, out string v); if (v != null) { text_before.Add(new_sym, v); } return(new_sym); });