public virtual RuleAST ParseArtificialRule(Grammar g, string ruleText) { ANTLRLexer lexer = new ANTLRLexer(new ANTLRStringStream(ruleText)); GrammarASTAdaptor adaptor = new GrammarASTAdaptor(lexer.CharStream); CommonTokenStream tokens = new CommonTokenStream(lexer); lexer.tokens = tokens; ToolANTLRParser p = new ToolANTLRParser(tokens, tool); p.TreeAdaptor = adaptor; IToken ruleStart = null; try { Antlr.Runtime.AstParserRuleReturnScope <GrammarAST, IToken> r = p.rule(); RuleAST tree = (RuleAST)r.Tree; ruleStart = r.Start; GrammarTransformPipeline.SetGrammarPtr(g, tree); GrammarTransformPipeline.AugmentTokensWithOriginalPosition(g, tree); return(tree); } catch (Exception e) { tool.errMgr.ToolError(ErrorType.INTERNAL_ERROR, e, ruleStart, "error parsing rule created during left-recursion detection: " + ruleText); } return(null); }
public static Level GetLevel(string id) { LKLevelLexer lexer = new LKLevelLexer(new Antlr.Runtime.ANTLRFileStream(id + ".lk")); Antlr.Runtime.CommonTokenStream tokens = new Antlr.Runtime.CommonTokenStream(lexer); LKLevelParser parser = new LKLevelParser(tokens); return parser.level_def(); }
public static List <BekProgram> DefsFromString(string programs) { var res = new List <BekProgram>(); try { var input = new Antlr.Runtime.ANTLRStringStream(programs); var lexer = new bekLexer(input); var tokens = new Antlr.Runtime.CommonTokenStream(lexer); var parser = new bekParser(tokens); var resp = parser.BekPgms(); foreach (BekPgm cur in resp) { var stab = new Symtab(cur); //TypeChecker.TypeCheck(cur, stab); var p = new BekProgram(cur, stab); //Library.PerformExpansions(p); res.Add(p); } } catch (Antlr.Runtime.MismatchedTokenException e) { string tok = (e.Token != null ? "'" + e.Token.Text + "'" : (e.Character >= 0 ? Microsoft.Automata.StringUtility.Escape((char)e.Character) : "")); string msg = "unexpected token " + tok; if (tok != "" && 0 <= e.Expecting && e.Expecting < ParserImpl.bekParser.tokenNames.Length) { msg += string.Format(" expecting {0}", ParserImpl.bekParser.tokenNames[e.Expecting]); } throw new BekParseException(e.Line, e.CharPositionInLine, msg); } catch (Antlr.Runtime.FailedPredicateException e) { string msg = string.Format("unexpected '{0}' failed {1}", (e.Token != null ? e.Token.Text : ((char)e.Character).ToString()), e.PredicateText); throw new BekParseException(e.Line, e.CharPositionInLine, msg); } catch (Antlr.Runtime.NoViableAltException e) { string msg = string.Format("unexpected '{0}' no alternatives", (e.Token != null ? e.Token.Text : ((char)e.Character).ToString())); throw new BekParseException(e.Line, e.CharPositionInLine, msg); } catch (Antlr.Runtime.RecognitionException e) { string msg = string.Format("unexpected '{0}'", (e.Token != null ? e.Token.Text : ((char)e.Character).ToString())); throw new BekParseException(e.Line, e.CharPositionInLine, msg); } catch (BekParseException e) { throw e; } catch (Exception e) { throw new BekParseException(1, 1, e.Message); } return(res); }
private List <IR.Table> ParseERDTables(System.IO.TextWriter errorWriter) { SqlDdlReaderLexer lex = new SqlDdlReaderLexer(new Antlr.Runtime.ANTLRFileStream(InputFile)); Antlr.Runtime.CommonTokenStream tokens = new Antlr.Runtime.CommonTokenStream(lex); SqlDdlReaderParser parser = new SqlDdlReaderParser(tokens); parser.TraceDestination = errorWriter; return(parser.Process()); }
public static List<BekProgram> DefsFromString(string programs) { var res = new List<BekProgram>(); try { var input = new Antlr.Runtime.ANTLRStringStream(programs); var lexer = new bekLexer(input); var tokens = new Antlr.Runtime.CommonTokenStream(lexer); var parser = new bekParser(tokens); var resp = parser.BekPgms(); foreach (BekPgm cur in resp) { var stab = new Symtab(cur); //TypeChecker.TypeCheck(cur, stab); var p = new BekProgram(cur, stab); //Library.PerformExpansions(p); res.Add(p); } } catch (Antlr.Runtime.MismatchedTokenException e) { string tok = (e.Token != null ? "'" + e.Token.Text + "'" : (e.Character >= 0 ? Microsoft.Automata.StringUtility.Escape((char)e.Character) : "")); string msg = "unexpected token " + tok; if (tok != "" && 0 <= e.Expecting && e.Expecting < ParserImpl.bekParser.tokenNames.Length) msg += string.Format(" expecting {0}", ParserImpl.bekParser.tokenNames[e.Expecting]); throw new BekParseException(e.Line, e.CharPositionInLine, msg); } catch (Antlr.Runtime.FailedPredicateException e) { string msg = string.Format("unexpected '{0}' failed {1}", (e.Token != null ? e.Token.Text : ((char)e.Character).ToString()), e.PredicateText); throw new BekParseException(e.Line, e.CharPositionInLine, msg); } catch (Antlr.Runtime.NoViableAltException e) { string msg = string.Format("unexpected '{0}' no alternatives", (e.Token != null ? e.Token.Text : ((char)e.Character).ToString())); throw new BekParseException(e.Line, e.CharPositionInLine, msg); } catch (Antlr.Runtime.RecognitionException e) { string msg = string.Format("unexpected '{0}'", (e.Token != null ? e.Token.Text : ((char)e.Character).ToString())); throw new BekParseException(e.Line, e.CharPositionInLine, msg); } catch (BekParseException e) { throw e; } catch (Exception e) { throw new BekParseException(1, 1, e.Message); } return res; }
protected override void ReParseImpl() { var outputWindow = OutputWindowService.TryGetPane(PredefinedOutputWindowPanes.TvlIntellisense); Stopwatch stopwatch = Stopwatch.StartNew(); var snapshot = TextBuffer.CurrentSnapshot; SnapshotCharStream input = new SnapshotCharStream(snapshot, new Span(0, snapshot.Length)); AlloyLexer lexer = new AlloyLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); AlloyParser parser = new AlloyParser(tokens); List <ParseErrorEventArgs> errors = new List <ParseErrorEventArgs>(); parser.ParseError += (sender, e) => { errors.Add(e); string message = e.Message; ITextDocument document; if (TextBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document) && document != null) { string fileName = document.FilePath; var line = snapshot.GetLineFromPosition(e.Span.Start); message = string.Format("{0}({1},{2}): {3}", fileName, line.LineNumber + 1, e.Span.Start - line.Start.Position + 1, message); } if (message.Length > 100) { message = message.Substring(0, 100) + " ..."; } if (outputWindow != null) { outputWindow.WriteLine(message); } if (errors.Count > 100) { throw new OperationCanceledException(); } }; var result = parser.compilationUnit(); OnParseComplete(new AntlrParseResultEventArgs(snapshot, errors, stopwatch.Elapsed, tokens.GetTokens(), result)); }
private string ValidateAntlr(string anltlr) { String errString = string.Empty; CalcETreeParser parser = null; try { TypeCastImpl.DataTypeMisMatchList = new List <DataMisMatchedException>(); CalcETreeParser.Counter = 0; String antlrResultText = string.Empty; CalcETreeLexer lexer = new CalcETreeLexer(new Antlr.Runtime.ANTLRStringStream(anltlr)); Antlr.Runtime.CommonTokenStream tokens = new Antlr.Runtime.CommonTokenStream(lexer); parser = new CalcETreeParser(tokens); parser.block(); if (TypeCastImpl.DataTypeMisMatchList.Count > 0) { foreach (DataMisMatchedException dataMisMatch in TypeCastImpl.DataTypeMisMatchList) { errString += dataMisMatch.ErrorCause + " " + dataMisMatch.ErrorMessage + ".\n"; } } } catch (Antlr.Runtime.NoViableAltException nve) { errString = "Invalid expression at step# " + CalcETreeParser.Counter + ". Please verify the syntax of the expression"; } catch (Antlr.Runtime.MismatchedTokenException mte) { //if (!string.IsNullOrEmpty(parser.TokenNames[mte.Expecting].ToString())) // errString = parser.TokenNames[mte.Expecting].ToString() + " Expecting in the Expression in row " + CalcETreeParser.Counter; //else errString = TypeCastImpl.AntlrException.ErrorMessage + " at step# " + CalcETreeParser.Counter + ". Please verify the syntax of the expression"; //_astTA.text += "\n" + er.message; } catch (Antlr.Runtime.RecognitionException re) { errString = "Invalid expression at step# " + CalcETreeParser.Counter + ". Please verify the syntax of the expression"; //_astTA.text += "\n" + er.message; } catch (Exception er) { errString = "An Error occured in row " + CalcETreeParser.Counter + " .\nDetails:" + er.Message; } return(errString); }
private static string ToOriginalString(Antlr.Runtime.CommonTokenStream tokenStream, int start, int end) { StringBuilder buf = new StringBuilder(); for (int i = start; i >= 0 && i <= end && i < tokenStream.Count; i++) { string s = tokenStream.Get(i).Text; if (tokenStream.Get(i).Type == ANTLRParser.BLOCK) { s = "("; } buf.Append(s); } return(buf.ToString()); }
/// <summary> /// Parse a sequence of queries. /// Eeach query in the sequence must end with a ';'. /// Output also the individual query strings. /// </summary> public static List <Expression> ParseQueries(string query_sequence, out List <string> query_strings) { try { var input = new Antlr.Runtime.ANTLRStringStream(query_sequence); var lexer = new queryLexer(input); var tokens = new Antlr.Runtime.CommonTokenStream(lexer); var parser = new queryParser(tokens); var res = parser.Queries(); query_strings = res.Item2; return(res.Item1); } catch (Antlr.Runtime.MismatchedTokenException e) { string tok = (e.Token != null ? "'" + e.Token.Text + "'" : (e.Character >= 0 ? StringUtility.Escape((char)e.Character) : "")); string msg = "unexpected token " + tok; if (tok != "" && 0 <= e.Expecting && e.Expecting < queryParser.tokenNames.Length) { msg += string.Format(" expecting {0}", queryParser.tokenNames[e.Expecting]); } throw new QueryParseException(e.Line, e.CharPositionInLine + 1, msg); } catch (Antlr.Runtime.FailedPredicateException e) { string msg = string.Format("unexpected '{0}' failed {1}", (e.Token != null ? e.Token.Text : ((char)e.Character).ToString()), e.PredicateText); throw new QueryParseException(e.Line, e.CharPositionInLine + 1, msg); } catch (Antlr.Runtime.NoViableAltException e) { string msg = string.Format("unexpected '{0}' no alternatives", (e.Token != null ? e.Token.Text : ((char)e.Character).ToString())); throw new QueryParseException(e.Line, e.CharPositionInLine + 1, msg); } catch (Antlr.Runtime.RecognitionException e) { string msg = string.Format("unexpected '{0}'", (e.Token != null ? e.Token.Text : ((char)e.Character).ToString())); throw new QueryParseException(e.Line, e.CharPositionInLine + 1, msg); } catch (QueryParseException e) { throw e; } catch (Exception e) { throw new QueryParseException(1, 1, e.Message); } }
internal static expr ParseExprFromString(string str) { try { var input = new Antlr.Runtime.ANTLRStringStream(str); var lexer = new bekLexer(input); var tokens = new Antlr.Runtime.CommonTokenStream(lexer); var parser = new bekParser(tokens); return(parser.Comp_expr()); } catch (Antlr.Runtime.MismatchedTokenException e) { string tok = (e.Token != null ? "'" + e.Token.Text + "'" : (e.Character >= 0 ? Microsoft.Automata.StringUtility.Escape((char)e.Character) : "")); string msg = "unexpected token " + tok; if (tok != "" && 0 <= e.Expecting && e.Expecting < ParserImpl.bekParser.tokenNames.Length) { msg += string.Format(" expecting {0}", ParserImpl.bekParser.tokenNames[e.Expecting]); } throw new BekParseException(e.Line, e.CharPositionInLine, msg); } catch (Antlr.Runtime.FailedPredicateException e) { string msg = string.Format("unexpected '{0}' failed {1}", (e.Token != null ? e.Token.Text : ((char)e.Character).ToString()), e.PredicateText); throw new BekParseException(e.Line, e.CharPositionInLine, msg); } catch (Antlr.Runtime.NoViableAltException e) { string msg = string.Format("unexpected '{0}' no alternatives", (e.Token != null ? e.Token.Text : ((char)e.Character).ToString())); throw new BekParseException(e.Line, e.CharPositionInLine, msg); } catch (Antlr.Runtime.RecognitionException e) { string msg = string.Format("unexpected '{0}'", (e.Token != null ? e.Token.Text : ((char)e.Character).ToString())); throw new BekParseException(e.Line, e.CharPositionInLine, msg); } catch (BekParseException e) { throw e; } catch (Exception e) { throw new BekParseException(1, 1, e.Message); } }
protected override void ReParseImpl() { var outputWindow = OutputWindowService.TryGetPane(PredefinedOutputWindowPanes.TvlIntellisense); Stopwatch stopwatch = Stopwatch.StartNew(); var snapshot = TextBuffer.CurrentSnapshot; SnapshotCharStream input = new SnapshotCharStream(snapshot, new Span(0, snapshot.Length)); AlloyLexer lexer = new AlloyLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); AlloyParser parser = new AlloyParser(tokens); List<ParseErrorEventArgs> errors = new List<ParseErrorEventArgs>(); parser.ParseError += (sender, e) => { errors.Add(e); string message = e.Message; ITextDocument document; if (TextBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document) && document != null) { string fileName = document.FilePath; var line = snapshot.GetLineFromPosition(e.Span.Start); message = string.Format("{0}({1},{2}): {3}", fileName, line.LineNumber + 1, e.Span.Start - line.Start.Position + 1, message); } if (message.Length > 100) message = message.Substring(0, 100) + " ..."; if (outputWindow != null) outputWindow.WriteLine(message); if (errors.Count > 100) throw new OperationCanceledException(); }; var result = parser.compilationUnit(); OnParseComplete(new AntlrParseResultEventArgs(snapshot, errors, stopwatch.Elapsed, tokens.GetTokens(), result)); }
public virtual GrammarRootAST Parse(string fileName, ICharStream @in) { try { GrammarASTAdaptor adaptor = new GrammarASTAdaptor(@in); ToolANTLRLexer lexer = new ToolANTLRLexer(@in, this); CommonTokenStream tokens = new CommonTokenStream(lexer); lexer.tokens = tokens; ToolANTLRParser p = new ToolANTLRParser(tokens, this); p.TreeAdaptor = adaptor; try { var r = p.grammarSpec(); GrammarAST root = (GrammarAST)r.Tree; if (root is GrammarRootAST) { ((GrammarRootAST)root).hasErrors = lexer.NumberOfSyntaxErrors > 0 || p.NumberOfSyntaxErrors > 0; Debug.Assert(((GrammarRootAST)root).tokenStream == tokens); if (grammarOptions != null) { ((GrammarRootAST)root).cmdLineOptions = grammarOptions; } return((GrammarRootAST)root); } } catch (v3TreeGrammarException e) { errMgr.GrammarError(ErrorType.V3_TREE_GRAMMAR, fileName, e.location); } return(null); } catch (RecognitionException) { // TODO: do we gen errors now? ErrorManager.InternalError("can't generate this message at moment; antlr recovers"); } return(null); }
private void btnParser_Click(object sender, RoutedEventArgs e) { string programa = txbCod.Text; //programa = programa.Replace("\r\n", " "); Antlr.Runtime.ANTLRStringStream ss = new Antlr.Runtime.ANTLRStringStream(programa); minipascalLexer lexer = new minipascalLexer(ss); Antlr.Runtime.CommonTokenStream tokens = new Antlr.Runtime.CommonTokenStream(lexer); minipascalParser parser = new minipascalParser(tokens); try { parser.start(); MessageBox.Show("Resultado OK!"); } catch (Antlr.Runtime.RecognitionException ex) { MessageBox.Show(("Erro no código!\n" + ex.Message), "Erro de Sintaxe", MessageBoxButton.OK, MessageBoxImage.Error); } }
/// <summary> /// Parse a sequence of queries. /// Eeach query in the sequence must end with a ';'. /// Output also the individual query strings. /// </summary> public static List<Expression> ParseQueries(string query_sequence, out List<string> query_strings) { try { var input = new Antlr.Runtime.ANTLRStringStream(query_sequence); var lexer = new queryLexer(input); var tokens = new Antlr.Runtime.CommonTokenStream(lexer); var parser = new queryParser(tokens); var res = parser.Queries(); query_strings = res.Item2; return res.Item1; } catch (Antlr.Runtime.MismatchedTokenException e) { string tok = (e.Token != null ? "'" + e.Token.Text + "'" : (e.Character >= 0 ? StringUtility.Escape((char)e.Character) : "")); string msg = "unexpected token " + tok; if (tok != "" && 0 <= e.Expecting && e.Expecting < queryParser.tokenNames.Length) msg += string.Format(" expecting {0}", queryParser.tokenNames[e.Expecting]); throw new QueryParseException(e.Line, e.CharPositionInLine + 1, msg); } catch (Antlr.Runtime.FailedPredicateException e) { string msg = string.Format("unexpected '{0}' failed {1}", (e.Token != null ? e.Token.Text : ((char)e.Character).ToString()), e.PredicateText); throw new QueryParseException(e.Line, e.CharPositionInLine + 1, msg); } catch (Antlr.Runtime.NoViableAltException e) { string msg = string.Format("unexpected '{0}' no alternatives", (e.Token != null ? e.Token.Text : ((char)e.Character).ToString())); throw new QueryParseException(e.Line, e.CharPositionInLine + 1, msg); } catch (Antlr.Runtime.RecognitionException e) { string msg = string.Format("unexpected '{0}'", (e.Token != null ? e.Token.Text : ((char)e.Character).ToString())); throw new QueryParseException(e.Line, e.CharPositionInLine + 1, msg); } catch (QueryParseException e) { throw e; } catch (Exception e) { throw new QueryParseException(1, 1, e.Message); } }
public virtual RuleAST ParseArtificialRule(Grammar g, string ruleText) { ANTLRLexer lexer = new ANTLRLexer(new ANTLRStringStream(ruleText)); GrammarASTAdaptor adaptor = new GrammarASTAdaptor(lexer.CharStream); CommonTokenStream tokens = new CommonTokenStream(lexer); lexer.tokens = tokens; ToolANTLRParser p = new ToolANTLRParser(tokens, tool); p.TreeAdaptor = adaptor; IToken ruleStart = null; try { Antlr.Runtime.AstParserRuleReturnScope<GrammarAST, IToken> r = p.rule(); RuleAST tree = (RuleAST)r.Tree; ruleStart = r.Start; GrammarTransformPipeline.SetGrammarPtr(g, tree); GrammarTransformPipeline.AugmentTokensWithOriginalPosition(g, tree); return tree; } catch (Exception e) { tool.errMgr.ToolError(ErrorType.INTERNAL_ERROR, e, ruleStart, "error parsing rule created during left-recursion detection: " + ruleText); } return null; }
public virtual GrammarRootAST Parse(string fileName, ICharStream @in) { try { GrammarASTAdaptor adaptor = new GrammarASTAdaptor(@in); ToolANTLRLexer lexer = new ToolANTLRLexer(@in, this); CommonTokenStream tokens = new CommonTokenStream(lexer); lexer.tokens = tokens; ToolANTLRParser p = new ToolANTLRParser(tokens, this); p.TreeAdaptor = adaptor; try { var r = p.grammarSpec(); GrammarAST root = (GrammarAST)r.Tree; if (root is GrammarRootAST) { ((GrammarRootAST)root).hasErrors = lexer.NumberOfSyntaxErrors > 0 || p.NumberOfSyntaxErrors > 0; Debug.Assert(((GrammarRootAST)root).tokenStream == tokens); if (grammarOptions != null) { ((GrammarRootAST)root).cmdLineOptions = grammarOptions; } return ((GrammarRootAST)root); } } catch (v3TreeGrammarException e) { errMgr.GrammarError(ErrorType.V3_TREE_GRAMMAR, fileName, e.location); } return null; } catch (RecognitionException) { // TODO: do we gen errors now? ErrorManager.InternalError("can't generate this message at moment; antlr recovers"); } return null; }
public virtual void ParseAndBuildAST( TextReader r ) { // BUILD AST FROM GRAMMAR ANTLRLexer lexer = new ANTLRLexer( new Antlr.Runtime.ANTLRReaderStream( r ) ); lexer.Filename = this.FileName; // use the rewrite engine because we want to buffer up all tokens // in case they have a merged lexer/parser, send lexer rules to // new grammar. //lexer.setTokenObjectClass( "antlr.TokenWithIndex" ); tokenBuffer = new Antlr.Runtime.CommonTokenStream( lexer ); //tokenBuffer = new TokenStreamRewriteEngine( lexer ); //tokenBuffer.Discard( ANTLRParser.WS, ANTLRParser.ML_COMMENT, ANTLRParser.COMMENT, ANTLRParser.SL_COMMENT ); //tokenBuffer.discard( ANTLRParser.WS ); //tokenBuffer.discard( ANTLRParser.ML_COMMENT ); //tokenBuffer.discard( ANTLRParser.COMMENT ); //tokenBuffer.discard( ANTLRParser.SL_COMMENT ); ANTLRParser parser = new ANTLRParser( tokenBuffer ); parser.FileName = this.FileName; ANTLRParser.grammar__return result = null; try { result = parser.grammar_( this ); } //catch ( TokenStreamException tse ) //{ // ErrorManager.internalError( "unexpected stream error from parsing " + fileName, tse ); //} catch ( RecognitionException re ) { ErrorManager.InternalError( "unexpected parser recognition error from " + fileName, re ); } DealWithTreeFilterMode(); // tree grammar and filter=true? if ( lexer.hasASTOperator && !BuildAST ) { object value = GetOption( "output" ); if ( value == null ) { ErrorManager.GrammarWarning( ErrorManager.MSG_REWRITE_OR_OP_WITH_NO_OUTPUT_OPTION, this, null ); SetOption( "output", "AST", null ); } else { ErrorManager.GrammarError( ErrorManager.MSG_AST_OP_WITH_NON_AST_OUTPUT_OPTION, this, null, value ); } } //grammarTree = (GrammarAST)parser.getAST(); grammarTree = (GrammarAST)result.tree; FileName = lexer.Filename; // the lexer #src might change name if ( grammarTree == null || grammarTree.FindFirstType( ANTLRParser.RULE ) == null ) { ErrorManager.Error( ErrorManager.MSG_NO_RULES, FileName ); return; } // Get syn pred rules and add to existing tree IList<GrammarAST> synpredRules = GetArtificialRulesForSyntacticPredicates( parser, nameToSynpredASTMap ); for ( int i = 0; i < synpredRules.Count; i++ ) { GrammarAST rAST = (GrammarAST)synpredRules[i]; grammarTree.AddChild( rAST ); } }
internal static expr ParseExprFromString(string str) { try { var input = new Antlr.Runtime.ANTLRStringStream(str); var lexer = new bekLexer(input); var tokens = new Antlr.Runtime.CommonTokenStream(lexer); var parser = new bekParser(tokens); return parser.Comp_expr(); } catch (Antlr.Runtime.MismatchedTokenException e) { string tok = (e.Token != null ? "'" + e.Token.Text + "'" : (e.Character >= 0 ? Microsoft.Automata.StringUtility.Escape((char)e.Character) : "")); string msg = "unexpected token " + tok; if (tok != "" && 0 <= e.Expecting && e.Expecting < ParserImpl.bekParser.tokenNames.Length) msg += string.Format(" expecting {0}", ParserImpl.bekParser.tokenNames[e.Expecting]); throw new BekParseException(e.Line, e.CharPositionInLine, msg); } catch (Antlr.Runtime.FailedPredicateException e) { string msg = string.Format("unexpected '{0}' failed {1}", (e.Token != null ? e.Token.Text : ((char)e.Character).ToString()), e.PredicateText); throw new BekParseException(e.Line, e.CharPositionInLine, msg); } catch (Antlr.Runtime.NoViableAltException e) { string msg = string.Format("unexpected '{0}' no alternatives", (e.Token != null ? e.Token.Text : ((char)e.Character).ToString())); throw new BekParseException(e.Line, e.CharPositionInLine, msg); } catch (Antlr.Runtime.RecognitionException e) { string msg = string.Format("unexpected '{0}'", (e.Token != null ? e.Token.Text : ((char)e.Character).ToString())); throw new BekParseException(e.Line, e.CharPositionInLine, msg); } catch (BekParseException e) { throw e; } catch (Exception e) { throw new BekParseException(1, 1, e.Message); } }
public SAN ParseFile(string filename) { SAN san = new SAN(); SANLexer lexer = new SANLexer(new Antlr.Runtime.ANTLRFileStream(filename)); Antlr.Runtime.CommonTokenStream tokens = new Antlr.Runtime.CommonTokenStream(lexer); SANParser parser = new SANParser(tokens); parser.start(san); return san; }
public virtual void ParseAndBuildAST( TextReader r ) { // BUILD AST FROM GRAMMAR ANTLRLexer lexer = new ANTLRLexer( new Antlr.Runtime.ANTLRReaderStream( r ) ); lexer.Filename = this.FileName; // use the rewrite engine because we want to buffer up all tokens // in case they have a merged lexer/parser, send lexer rules to // new grammar. //lexer.setTokenObjectClass( "antlr.TokenWithIndex" ); tokenBuffer = new Antlr.Runtime.CommonTokenStream( lexer ); //tokenBuffer = new TokenStreamRewriteEngine( lexer ); //tokenBuffer.Discard( ANTLRParser.WS, ANTLRParser.ML_COMMENT, ANTLRParser.COMMENT, ANTLRParser.SL_COMMENT ); //tokenBuffer.discard( ANTLRParser.WS ); //tokenBuffer.discard( ANTLRParser.ML_COMMENT ); //tokenBuffer.discard( ANTLRParser.COMMENT ); //tokenBuffer.discard( ANTLRParser.SL_COMMENT ); ANTLRParser parser = new ANTLRParser( tokenBuffer ); parser.FileName = this.FileName; Antlr.Runtime.IAstRuleReturnScope<GrammarAST> result = null; try { result = parser.grammar_( this ); } //catch ( TokenStreamException tse ) //{ // ErrorManager.internalError( "unexpected stream error from parsing " + fileName, tse ); //} catch ( RecognitionException re ) { ErrorManager.InternalError( "unexpected parser recognition error from " + fileName, re ); } DealWithTreeFilterMode(); // tree grammar and filter=true? if ( lexer.hasASTOperator && !BuildAST ) { object value = GetOption( "output" ); if ( value == null ) { ErrorManager.GrammarWarning( ErrorManager.MSG_REWRITE_OR_OP_WITH_NO_OUTPUT_OPTION, this, null ); SetOption( "output", "AST", null ); } else { ErrorManager.GrammarError( ErrorManager.MSG_AST_OP_WITH_NON_AST_OUTPUT_OPTION, this, null, value ); } } grammarTree = result.Tree; if (grammarTree != null && AntlrTool.internalOption_PrintGrammarTree) Console.WriteLine("grammar tree: " + grammarTree.ToStringTree()); grammarTree.SetUnknownTokenBoundaries(); FileName = lexer.Filename; // the lexer #src might change name if ( grammarTree.FindFirstType( ANTLRParser.RULE ) == null ) { ErrorManager.Error( ErrorManager.MSG_NO_RULES, FileName ); } }