Пример #1
0
        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);
        }
Пример #2
0
        /// <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;
        }
Пример #3
0
 private string Preprocess(string unprocessedCode)
 {
     SymbolTable<string, IValue> symbolTable = new SymbolTable<string, IValue>();
     var stream = new AntlrInputStream(unprocessedCode);
     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(_vbaVersion));
     var tree = parser.compilationUnit();
     var expr = evaluator.Visit(tree);
     return expr.Evaluate().AsString;
 }
Пример #4
0
 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);
 }
Пример #5
0
        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 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());
 }
Пример #7
0
        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();
        }