XSharpParserRuleContext buildTree(XSharpParser parser) { XSharpParserRuleContext tree; if (_isScript) { if (_isMacroScript) { tree = parser.macroScript(); } else { tree = parser.script(); } } else if (_options.Dialect == XSharpDialect.FoxPro) { tree = parser.foxsource(); } else { tree = parser.source(); } return(tree); }
static int AnalyzeCode(string code, bool showErrors) { var stream = new AntlrInputStream(code.ToString()); var lexer = new XSharpLexer(stream); lexer.Options = new CSharpParseOptions(); var tokens = new CommonTokenStream(lexer); var parser = new XSharpParser(tokens); parser.Options = lexer.Options; var errorListener = new XSharpErrorListener(showErrors); parser.AddErrorListener(errorListener); var tree = parser.source(); //Console.WriteLine(tree.ToStringTree()); return(errorListener.TotalErrors); }
List <MethodInfo> GetMethodInfos() { var lexer = XSharpLexer.Create(SourceCodeFile.SourceCode, SourceCodeFile.FileName); lexer.RemoveErrorListeners(); var tokenStream = new CommonTokenStream(lexer, 0); var parser = new XSharpParser(tokenStream); parser.Options = new XSharpParseOptions(); parser.Options.SetXSharpSpecificOptions(XSharpSpecificCompilationOptions.Default); parser.RemoveErrorListeners(); var source = parser.source(); var listener = new MethodListener(); new ParseTreeWalker().Walk(listener, source); return(listener.MethodList); }
private static void Parse(string fileName) { ITokenStream stream; IList <ParseErrorData> parseErrors = ParseErrorData.NewBag(); var filestream = new AntlrFileStream(fileName); var lexer = new XSharpLexer(filestream); lexer.TokenFactory = XSharpTokenFactory.Default; stream = new CommonTokenStream(lexer, Lexer.DefaultTokenChannel); var parser = new XSharpParser(stream); parser.IsScript = false; parser.AllowFunctionInsideClass = false; parser.AllowNamedArgs = false; parser.AllowXBaseVariables = false; parser.RemoveErrorListeners(); parser.Interpreter.PredictionMode = PredictionMode.Sll; parser.Interpreter.reportAmbiguities = true; parser.Interpreter.enable_global_context_dfa = true; // default false parser.Interpreter.optimize_tail_calls = true; parser.Interpreter.tail_call_preserves_sll = true; //parser.Interpreter.userWantsCtxSensitive = true; // default true parser.ErrorHandler = new XSharpErrorStrategy(); parser.AddErrorListener(new XSharpErrorListener(fileName, parseErrors, true)); XSharpParserRuleContext tree; try { tree = parser.source(); } catch (ParseCanceledException) { Console.WriteLine("Parse error, Errors from SLL mode"); showErrors(parseErrors); parseErrors.Clear(); parser.ErrorHandler = new XSharpErrorStrategy(); parser.AddErrorListener(new XSharpErrorListener(fileName, parseErrors, true)); parser.Interpreter.PredictionMode = PredictionMode.Ll; parser.Interpreter.force_global_context = true; parser.Interpreter.optimize_ll1 = false; parser.Interpreter.reportAmbiguities = true; parser.Reset(); try { tree = parser.source(); } catch (Exception e) { tree = null; Console.WriteLine(e.Message); } } // find parser errors (missing tokens etc) foreach (var e in lexer.LexErrors) { parseErrors.Add(e); } var walker = new ParseTreeWalker(); var errchecker = new XSharpParseErrorAnalysis(parser, parseErrors); if (tree != null) { walker.Walk(errchecker, tree); } Console.WriteLine("Parse error, Errors:"); showErrors(parseErrors); }
public static bool Parse(string sourceText, string fileName, CSharpParseOptions options, IErrorListener listener, out ITokenStream tokens, out XSharpParser.SourceContext tree) { tree = null; tokens = null; var parseErrors = ParseErrorData.NewBag(); try { var lexer = XSharpLexer.Create(sourceText, fileName, options); lexer.Options = options; BufferedTokenStream tokenStream = lexer.GetTokenStream(); tokenStream.Fill(); tokens = (ITokenStream)tokenStream; GetLexerErrors(lexer, tokenStream, parseErrors); // do we need to preprocess #region Determine if we really need the preprocessor bool mustPreprocess = true; if (lexer.HasPreprocessorTokens || !options.NoStdDef) { // no need to pre process in partial compilation // if lexer does not contain UDCs, Messages or Includes mustPreprocess = lexer.MustBeProcessed; } else { mustPreprocess = false; } #endregion XSharpPreprocessor pp = null; BufferedTokenStream ppStream = null; pp = new XSharpPreprocessor(lexer, tokenStream, options, fileName, Encoding.Unicode, SourceHashAlgorithm.None, parseErrors); if (mustPreprocess) { var ppTokens = pp.PreProcess(); ppStream = new CommonTokenStream(new XSharpListTokenSource(lexer, ppTokens)); } else { // No Standard Defs and no preprocessor tokens in the lexer // so we bypass the preprocessor and use the lexer token stream ppStream = new CommonTokenStream(new XSharpListTokenSource(lexer, tokenStream.GetTokens())); } ppStream.Fill(); var parser = new XSharpParser(ppStream); parser.Interpreter.tail_call_preserves_sll = false; // default = true Setting to FALSE will reduce memory used by parser parser.Options = options; tree = null; parser.RemoveErrorListeners(); parser.Interpreter.PredictionMode = PredictionMode.Sll; parser.ErrorHandler = new BailErrorStrategy(); try { tree = parser.source(); } catch (Exception) { var errorListener = new XSharpErrorListener(fileName, parseErrors); parser.AddErrorListener(errorListener); parser.ErrorHandler = new XSharpErrorStrategy(); parser.Interpreter.PredictionMode = PredictionMode.Ll; ppStream.Reset(); parser.Reset(); try { tree = parser.source(); } catch (Exception) { tree = null; } } } catch (Exception) { tree = null; } ReportErrors(parseErrors, listener); return(tree != null); }