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); }
/// <summary> /// Parses the given VBA expression. /// </summary> /// <param name="expression">The expression to parse. NOTE: Call statements are not supported.</param> /// <returns>The root of the parse tree.</returns> public ParserRuleContext Parse(string expression) { var stream = new AntlrInputStream(expression); 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.expression(); } catch (Exception ex) { Logger.Warn(ex, "SLL mode failed for {0}. Retrying using LL.", expression); tokens.Reset(); parser.Reset(); parser.Interpreter.PredictionMode = PredictionMode.Ll; tree = parser.expression(); } 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); }
public CommonTokenStream Tokens(string code) { var stream = new AntlrInputStream(code); var lexer = new VBALexer(stream); return(new CommonTokenStream(lexer)); }
/// <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); }
private VBAExpressionParser.ExpressionContext Parse(string expression) { var stream = new AntlrInputStream(expression); var lexer = new VBALexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new VBAExpressionParser(tokens); parser.AddErrorListener(new ExceptionErrorListener()); var tree = parser.startRule(); return(tree.expression()); }
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 Tuple <SymbolTable <string, IValue>, IValue> Preprocess(string code) { SymbolTable <string, IValue> symbolTable = new SymbolTable <string, IValue>(); var stream = new AntlrInputStream(code); var lexer = new VBALexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new VBAConditionalCompilationParser(tokens); parser.AddErrorListener(new ExceptionErrorListener()); var evaluator = new VBAPreprocessorVisitor(symbolTable, new VBAPredefinedCompilationConstants(7.01)); var tree = parser.compilationUnit(); var expr = evaluator.Visit(tree); return(Tuple.Create(symbolTable, expr.Evaluate())); }
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()); }
private Tuple <SymbolTable <string, IValue>, IValue> Preprocess(string code) { SymbolTable <string, IValue> symbolTable = new SymbolTable <string, IValue>(); var stream = new AntlrInputStream(code); var lexer = new VBALexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new VBAConditionalCompilationParser(tokens); parser.ErrorHandler = new BailErrorStrategy(); //parser.AddErrorListener(new ExceptionErrorListener()); var tree = parser.compilationUnit(); var evaluator = new VBAPreprocessorVisitor(symbolTable, new VBAPredefinedCompilationConstants(7.01), tree.start.InputStream, tokens); var expr = evaluator.Visit(tree); var resultValue = expr.Evaluate(); Debug.Assert(parser.NumberOfSyntaxErrors == 0); return(Tuple.Create(symbolTable, resultValue)); }
static void Main(string[] args) { var code = @" Public Sub DoSomething() Debug.Print ""hi from path 1"" If True Then MsgBox ""hello from path 2"" End If Debug.Print ""still in path 1"" End Sub".Trim(); var stream = new AntlrInputStream(code); var lexer = new VBALexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new VBAParser(tokens); parser.Interpreter.PredictionMode = PredictionMode.Sll; var tree = parser.subStmt(); Console.ReadKey(true); }
static void Main(string[] args) { var code = @"Public Sub DoSomething() Debug.Print ""hi from path 1"" If True Then MsgBox ""hello from path 2"" End If Debug.Print ""still in path 1"" End Sub"; var stream = new AntlrInputStream(code); var lexer = new VBALexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new VBAParser(tokens); parser.Interpreter.PredictionMode = PredictionMode.Sll; var tree = parser.subStmt(); var visualizerHost = new VisualizerDevelopmentHost(tree, typeof(Visualizer), typeof(ObjectSource)); visualizerHost.ShowVisualizer(); }
public VBAConditionalCompilationParser.CompilationUnitContext Parse(string moduleName, string unprocessedCode) { var stream = new AntlrInputStream(unprocessedCode); var lexer = new VBALexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new VBAConditionalCompilationParser(tokens); parser.AddErrorListener(new ExceptionErrorListener()); VBAConditionalCompilationParser.CompilationUnitContext tree = null; try { parser.Interpreter.PredictionMode = PredictionMode.Sll; tree = parser.compilationUnit(); } 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.compilationUnit(); } return(tree); }