public IParseTree Parse(string moduleName, string moduleCode, IParseTreeListener[] listeners, out ITokenStream outStream) { var stream = new AntlrInputStream(moduleCode); var lexer = new VBALexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new VBAParser(tokens); parser.AddErrorListener(new ExceptionErrorListener()); ParserRuleContext tree = null; try { parser.Interpreter.PredictionMode = PredictionMode.Sll; tree = parser.startRule(); } catch (Exception ex) { Logger.Warn(ex, "SLL mode failed in module {0}. Retrying using LL.", moduleName); tokens.Reset(); parser.Reset(); parser.Interpreter.PredictionMode = PredictionMode.Ll; tree = parser.startRule(); } foreach (var listener in listeners) { ParseTreeWalker.Default.Walk(listener, tree); } outStream = tokens; return(tree); }
public IParseTree Parse(string moduleName, CommonTokenStream moduleTokens, IParseTreeListener[] listeners, BaseErrorListener errorListener, out ITokenStream outStream) { moduleTokens.Reset(); var parser = new VBAParser(moduleTokens); parser.AddErrorListener(errorListener); ParserRuleContext tree; try { parser.Interpreter.PredictionMode = PredictionMode.Sll; tree = parser.startRule(); } catch (Exception ex) { Logger.Warn(ex, "SLL mode failed in module {0}. Retrying using LL.", moduleName); moduleTokens.Reset(); parser.Reset(); parser.Interpreter.PredictionMode = PredictionMode.Ll; tree = parser.startRule(); } foreach (var listener in listeners) { ParseTreeWalker.Default.Walk(listener, tree); } outStream = moduleTokens; return(tree); }
public void ParsesEmptyForm() { var code = @" VERSION 5.00 Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} Form1 Caption = ""Form1"" ClientHeight = 2640 ClientLeft = 45 ClientTop = 375 ClientWidth = 4710 OleObjectBlob = ""Form1.frx"":0000 StartUpPosition = 1 'CenterOwner End Attribute VB_Name = ""Form1"" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False "; var stream = new AntlrInputStream(code); var lexer = new VBALexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new VBAParser(tokens); parser.ErrorListeners.Clear(); parser.ErrorListeners.Add(new ExceptionErrorListener()); var tree = parser.startRule(); Assert.IsNotNull(tree); }
/// <summary> /// Exports the specified component to a temporary file, loads, and then parses the exported file. /// </summary> /// <param name="component"></param> public IDictionary<Tuple<string, DeclarationType>, Attributes> Parse(VBComponent component) { var path = _exporter.Export(component); if (!File.Exists(path)) { // a document component without any code wouldn't be exported (file would be empty anyway). return new Dictionary<Tuple<string, DeclarationType>, Attributes>(); } var code = File.ReadAllText(path); File.Delete(path); var type = component.Type == vbext_ComponentType.vbext_ct_StdModule ? DeclarationType.Module : DeclarationType.Class; var listener = new AttributeListener(Tuple.Create(component.Name, type)); var stream = new AntlrInputStream(code); var lexer = new VBALexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new VBAParser(tokens); // parse tree isn't usable for declarations because // line numbers are offset due to module header and attributes // (these don't show up in the VBE, that's why we're parsing an exported file) var tree = parser.startRule(); ParseTreeWalker.Default.Walk(listener, tree); return listener.Attributes; }
/// <summary> /// Exports the specified component to a temporary file, loads, and then parses the exported file. /// </summary> /// <param name="component"></param> public IDictionary <Tuple <string, DeclarationType>, Attributes> Parse(VBComponent component) { var path = _exporter.Export(component); if (!File.Exists(path)) { // a document component without any code wouldn't be exported (file would be empty anyway). return(new Dictionary <Tuple <string, DeclarationType>, Attributes>()); } var code = File.ReadAllText(path); File.Delete(path); var type = component.Type == vbext_ComponentType.vbext_ct_StdModule ? DeclarationType.ProceduralModule : DeclarationType.ClassModule; var listener = new AttributeListener(Tuple.Create(component.Name, type)); var stream = new AntlrInputStream(code); var lexer = new VBALexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new VBAParser(tokens); // parse tree isn't usable for declarations because // line numbers are offset due to module header and attributes // (these don't show up in the VBE, that's why we're parsing an exported file) var tree = parser.startRule(); ParseTreeWalker.Default.Walk(listener, tree); return(listener.Attributes); }
protected override IParseTree Parse(ITokenStream tokenStream, PredictionMode predictionMode, IParserErrorListener errorListener) { var parser = new VBAParser(tokenStream); parser.Interpreter.PredictionMode = predictionMode; parser.AddErrorListener(errorListener); return(parser.startRule()); }
public (IParseTree tree, ITokenStream tokenStream) Parse(string moduleName, CommonTokenStream moduleTokens, IParseTreeListener[] listeners, BaseErrorListener errorListener) { moduleTokens.Reset(); var parser = new VBAParser(moduleTokens); parser.AddErrorListener(errorListener); ParserRuleContext tree; try { parser.Interpreter.PredictionMode = PredictionMode.Sll; tree = parser.startRule(); } catch (ParsePassSyntaxErrorException syntaxErrorException) { var parsePassText = syntaxErrorException.ParsePass == ParsePass.CodePanePass ? "code pane" : "exported"; Logger.Warn($"SLL mode failed while parsing the {parsePassText} version of module {moduleName} at symbol {syntaxErrorException.OffendingSymbol.Text} at L{syntaxErrorException.LineNumber}C{syntaxErrorException.Position}. Retrying using LL."); Logger.Debug(syntaxErrorException, "SLL mode exception"); moduleTokens.Reset(); parser.Reset(); parser.Interpreter.PredictionMode = PredictionMode.Ll; tree = parser.startRule(); } catch (Exception exception) { Logger.Warn($"SLL mode failed while parsing module {moduleName}. Retrying using LL."); Logger.Debug(exception, "SLL mode exception"); moduleTokens.Reset(); parser.Reset(); parser.Interpreter.PredictionMode = PredictionMode.Ll; tree = parser.startRule(); } foreach (var listener in listeners) { ParseTreeWalker.Default.Walk(listener, tree); } return(tree, moduleTokens); }
private Tuple <VBAParser, ParserRuleContext> Parse(string code) { var stream = new AntlrInputStream(code); var lexer = new VBALexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new VBAParser(tokens); //parser.AddErrorListener(new ExceptionErrorListener()); var root = parser.startRule(); // Useful for figuring out what XPath to use for querying the parse tree. var str = root.ToStringTree(parser); return(Tuple.Create <VBAParser, ParserRuleContext>(parser, root)); }
private IParseTree Parse(string code, out TokenStreamRewriter outRewriter) { var input = new AntlrInputStream(code); var lexer = new VBALexer(input); var tokens = new CommonTokenStream(lexer); var parser = new VBAParser(tokens); parser.AddErrorListener(new ExceptionErrorListener()); outRewriter = new TokenStreamRewriter(tokens); var result = parser.startRule(); return(result); }
private Tuple <VBAParser, ParserRuleContext> Parse(string code) { var stream = new AntlrInputStream(code); var lexer = new VBALexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new VBAParser(tokens); // Don't remove this line otherwise we won't get notified of parser failures. parser.AddErrorListener(new ExceptionErrorListener()); // If SLL fails we want to get notified ASAP so we can fix it, that's why we don't retry using LL. parser.Interpreter.PredictionMode = PredictionMode.Sll; var tree = parser.startRule(); return(Tuple.Create <VBAParser, ParserRuleContext>(parser, tree)); }
private static IParseTree ParseInternal(string code, IParseTreeListener[] listeners, out ITokenStream outStream) { var stream = new AntlrInputStream(code); var lexer = new VBALexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new VBAParser(tokens); parser.AddErrorListener(new ExceptionErrorListener()); foreach (var l in listeners) { parser.AddParseListener(l); } outStream = tokens; return(parser.startRule()); }
public string Format(string code) { var indenter = new Indenter(null, () => new IndenterSettings(true)); var indentedCode = indenter.Indent(code.Split('\n').Select(line => line.Replace("\r", string.Empty))).ToArray(); var builder = new StringBuilder(); var tokens = Tokenize(string.Join("\n", indentedCode)); var parser = new VBAParser(tokens) { Interpreter = { PredictionMode = PredictionMode.Ll } }; var listeners = new IntervalListener[] { new CommentIntervalsListener(), new AnnotationIntervalsListener(), new AttributeIntervalsListener(), new AttributeValueIntervalsListener(), }; foreach (var listener in listeners) { parser.AddParseListener(listener); } parser.startRule(); for (var i = 0; i < tokens.Size; i++) { var token = tokens.Get(i); var listener = listeners .Select(e => new { IsValidInterval = e.IsValidInterval(token, out var interval), Interval = interval, Class = e.Class })
private Tuple<VBAParser, ParserRuleContext> Parse(string code) { var stream = new AntlrInputStream(code); var lexer = new VBALexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new VBAParser(tokens); //parser.AddErrorListener(new ExceptionErrorListener()); var root = parser.startRule(); // Useful for figuring out what XPath to use for querying the parse tree. var str = root.ToStringTree(parser); return Tuple.Create<VBAParser, ParserRuleContext>(parser, root); }
public IParseTree Parse(string code, out ITokenStream outStream) { var input = new AntlrInputStream(code); var lexer = new VBALexer(input); var tokens = new CommonTokenStream(lexer); var parser = new VBAParser(tokens); parser.AddErrorListener(new ExceptionErrorListener()); outStream = tokens; var result = parser.startRule(); return result; }
private static IParseTree ParseInternal(string code, IParseTreeListener[] listeners, out ITokenStream outStream) { var stream = new AntlrInputStream(code); var lexer = new VBALexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new VBAParser(tokens); parser.AddErrorListener(new ExceptionErrorListener()); foreach (var l in listeners) { parser.AddParseListener(l); } outStream = tokens; return parser.startRule(); }