Ejemplo n.º 1
0
        private CppParser BuildParser(string sourceCode)
        {
            // Parse the file
            var inputStream = new AntlrInputStream(sourceCode);
            var lexer       = new CppLexer(inputStream);
            var tokenStream = new CommonTokenStream(lexer);
            var parser      = new CppParser(tokenStream);

            // Setup error handling
            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(new LexerExceptionErrorListener());
            parser.RemoveErrorListeners();
            parser.AddErrorListener(new ParserExceptionErrorListener());

            return(parser);
        }
Ejemplo n.º 2
0
        private void RunTest(string sourceCode, int expectedToken)
        {
            // Parse the file
            var inputStream = new AntlrInputStream(sourceCode);
            var lexer       = new CppLexer(inputStream);

            // Setup error handling
            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(new LexerExceptionErrorListener());

            // Match all tokens
            var tokens = lexer.GetAllTokens();

            // Verify we got the single expected token
            Assert.Equal(1, tokens.Count);
            Assert.Equal(expectedToken, tokens[0].Type);
        }
Ejemplo n.º 3
0
        public static TranslationUnit GenerateAST(AntlrInputStream inputStream)
        {
            // Parse the file
            var lexer       = new CppLexer(inputStream);
            var tokenStream = new CommonTokenStream(lexer);
            var parser      = new CppParser(tokenStream);

            // var tokens = lexer.GetAllTokens().Select(token => lexer.Vocabulary.GetSymbolicName(token.Type)).ToList();

            // Setup error handling
            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(new LexerExceptionErrorListener());
            parser.RemoveErrorListeners();
            parser.AddErrorListener(new ParserExceptionErrorListener());

            // Parse the concrete syntax tree
            var translationUnit = parser.translationUnit();

            // Convert the the abstract syntax tree
            var visitor = new ASTVisitor();
            var ast     = (TranslationUnit)visitor.Visit(translationUnit);

            return(ast);
        }