public void TestMismatchedSetError()
        {
            Grammar pg = new Grammar(
                "parser grammar p;\n" +
                "prog : WHILE ID LCURLY (assign)* RCURLY;\n" +
                "assign : ID ASSIGN expr SEMI ;\n" +
                "expr : INT | FLOAT | ID ;\n");
            Grammar g = new Grammar();
            g.ImportTokenVocabulary(pg);
            g.FileName = "<string>";
            g.SetGrammarContent(
                "lexer grammar t;\n" +
                "WHILE : 'while';\n" +
                "LCURLY : '{';\n" +
                "RCURLY : '}';\n" +
                "ASSIGN : '=';\n" +
                "SEMI : ';';\n" +
                "ID : ('a'..'z')+ ;\n" +
                "INT : (DIGIT)+ ;\n" +
                "FLOAT : (DIGIT)+ '.' (DIGIT)* ;\n" +
                "fragment DIGIT : '0'..'9';\n" +
                "WS : (' ')+ ;\n");
            ICharStream input = new ANTLRStringStream("while x { i=; y=3.42; z=y; }");
            Interpreter lexEngine = new Interpreter(g, input);

            FilteringTokenStream tokens = new FilteringTokenStream(lexEngine);
            tokens.SetTokenTypeChannel(g.GetTokenType("WS"), 99);
            //System.out.println("tokens="+tokens.toString());
            Interpreter parseEngine = new Interpreter(pg, tokens);
            ParseTree t = parseEngine.Parse("prog");
            string result = t.ToStringTree();
            string expecting =
                "(<grammar p> (prog while x { (assign i = (expr MismatchedSetException(10!={5,6,7})))))";
            Assert.AreEqual(expecting, result);
        }
Example #2
0
        public void TestMismatchedTokenError()
        {
            Assert.Inconclusive("May be failing on just my port...");
            Grammar pg = new Grammar(
                "parser grammar p;\n" +
                "prog : WHILE ID LCURLY (assign)* RCURLY;\n" +
                "assign : ID ASSIGN expr SEMI ;\n" +
                "expr : INT | FLOAT | ID ;\n");
            Grammar g = new Grammar();

            g.FileName = Grammar.IGNORE_STRING_IN_GRAMMAR_FILE_NAME + "string";
            g.ImportTokenVocabulary(pg);
            g.SetGrammarContent(
                "lexer grammar t;\n" +
                "WHILE : 'while';\n" +
                "LCURLY : '{';\n" +
                "RCURLY : '}';\n" +
                "ASSIGN : '=';\n" +
                "SEMI : ';';\n" +
                "ID : ('a'..'z')+ ;\n" +
                "INT : (DIGIT)+ ;\n" +
                "FLOAT : (DIGIT)+ '.' (DIGIT)* ;\n" +
                "fragment DIGIT : '0'..'9';\n" +
                "WS : (' ')+ ;\n");
            ICharStream input     = new ANTLRStringStream("while x { i=1 y=3.42; z=y; }");
            Interpreter lexEngine = new Interpreter(g, input);

            FilteringTokenStream tokens = new FilteringTokenStream(lexEngine);

            tokens.SetTokenTypeChannel(g.GetTokenType("WS"), 99);
            //System.out.println("tokens="+tokens.toString());
            Interpreter parseEngine = new Interpreter(pg, tokens);
            ParseTree   t           = parseEngine.Parse("prog");
            string      result      = t.ToStringTree();
            string      expecting   =
                "(<grammar p> (prog while x { (assign i = (expr 1) MismatchedTokenException(5!=9))))";

            assertEquals(expecting, result);
        }
Example #3
0
        public void TestNoViableAltError()
        {
            Grammar pg = new Grammar(
                "parser grammar p;\n" +
                "prog : WHILE ID LCURLY (assign)* RCURLY;\n" +
                "assign : ID ASSIGN expr SEMI ;\n" +
                "expr : {;}INT | FLOAT | ID ;\n");
            Grammar g = new Grammar();

            g.ImportTokenVocabulary(pg);
            g.FileName = "<string>";
            g.SetGrammarContent(
                "lexer grammar t;\n" +
                "WHILE : 'while';\n" +
                "LCURLY : '{';\n" +
                "RCURLY : '}';\n" +
                "ASSIGN : '=';\n" +
                "SEMI : ';';\n" +
                "ID : ('a'..'z')+ ;\n" +
                "INT : (DIGIT)+ ;\n" +
                "FLOAT : (DIGIT)+ '.' (DIGIT)* ;\n" +
                "fragment DIGIT : '0'..'9';\n" +
                "WS : (' ')+ ;\n");
            ICharStream input     = new ANTLRStringStream("while x { i=; y=3.42; z=y; }");
            Interpreter lexEngine = new Interpreter(g, input);

            FilteringTokenStream tokens = new FilteringTokenStream(lexEngine);

            tokens.SetTokenTypeChannel(g.GetTokenType("WS"), 99);
            //System.out.println("tokens="+tokens.toString());
            Interpreter parseEngine = new Interpreter(pg, tokens);
            ParseTree   t           = parseEngine.Parse("prog");
            string      result      = t.ToStringTree();
            string      expecting   =
                "(<grammar p> (prog while x { (assign i = (expr NoViableAltException(10@[4:1: expr : ( INT | FLOAT | ID );])))))";

            Assert.AreEqual(expecting, result);
        }
        public void TestSimpleParse()
        {
            Grammar pg = new Grammar(
                "parser grammar p;\n" +
                "prog : WHILE ID LCURLY (assign)* RCURLY EOF;\n" +
                "assign : ID ASSIGN expr SEMI ;\n" +
                "expr : INT | FLOAT | ID ;\n");
            Grammar g = new Grammar();
            g.ImportTokenVocabulary(pg);
            g.FileName = Grammar.IGNORE_STRING_IN_GRAMMAR_FILE_NAME + "string";
            g.SetGrammarContent(
                "lexer grammar t;\n" +
                "WHILE : 'while';\n" +
                "LCURLY : '{';\n" +
                "RCURLY : '}';\n" +
                "ASSIGN : '=';\n" +
                "SEMI : ';';\n" +
                "ID : ('a'..'z')+ ;\n" +
                "INT : (DIGIT)+ ;\n" +
                "FLOAT : (DIGIT)+ '.' (DIGIT)* ;\n" +
                "fragment DIGIT : '0'..'9';\n" +
                "WS : (' ')+ ;\n");
            ICharStream input = new ANTLRStringStream("while x { i=1; y=3.42; z=y; }");
            Interpreter lexEngine = new Interpreter(g, input);

            FilteringTokenStream tokens = new FilteringTokenStream(lexEngine);
            tokens.SetTokenTypeChannel(g.GetTokenType("WS"), 99);
            //System.out.println("tokens="+tokens.toString());
            Interpreter parseEngine = new Interpreter(pg, tokens);
            ParseTree t = parseEngine.Parse("prog");
            string result = t.ToStringTree();
            string expecting =
                "(<grammar p> (prog while x { (assign i = (expr 1) ;) (assign y = (expr 3.42) ;) (assign z = (expr y) ;) } <EOF>))";
            Assert.AreEqual(expecting, result);
        }