public void testInvalidListTree()
        {
            ITreeAdaptor adaptor = new CommonTreeAdaptor();
            TreeWizard   wiz     = new TreeWizard(adaptor, tokens);
            CommonTree   t       = (CommonTree)wiz.Create("A B C");

            Assert.IsTrue(t == null);
        }
        public void testParseSingleNode()
        {
            ITreeAdaptor adaptor = new CommonTreeAdaptor();
            TreeWizard   wiz     = new TreeWizard(adaptor, tokens);
            CommonTree   t       = (CommonTree)wiz.Create("A");
            bool         valid   = wiz.Parse(t, "A");

            Assert.IsTrue(valid);
        }
        public void testWildcard()
        {
            ITreeAdaptor adaptor = new CommonTreeAdaptor();
            TreeWizard   wiz     = new TreeWizard(adaptor, tokens);
            CommonTree   t       = (CommonTree)wiz.Create("(A B C)");
            bool         valid   = wiz.Parse(t, "(A . .)");

            Assert.IsTrue(valid);
        }
        public void testParseWithTextFails()
        {
            ITreeAdaptor adaptor = new CommonTreeAdaptor();
            TreeWizard   wiz     = new TreeWizard(adaptor, tokens);
            CommonTree   t       = (CommonTree)wiz.Create("(A B C)");
            bool         valid   = wiz.Parse(t, "(A[foo] B C)");

            Assert.IsTrue(!valid);             // fails
        }
 public void testDoubleLevelTree()
 {
     ITreeAdaptor adaptor = new CommonTreeAdaptor();
     TreeWizard wiz = new TreeWizard(adaptor, tokens);
     CommonTree t = (CommonTree)wiz.Create("(A (B C) (B D) E)");
     string actual = t.ToStringTree();
     string expected = "(A (B C) (B D) E)";
     Assert.AreEqual(expected, actual);
 }
        public void testSingleNodeWithArg()
        {
            ITreeAdaptor adaptor  = new CommonTreeAdaptor();
            TreeWizard   wiz      = new TreeWizard(adaptor, tokens);
            CommonTree   t        = (CommonTree)wiz.Create("ID[foo]");
            string       actual   = t.ToStringTree();
            string       expected = "foo";

            Assert.AreEqual(expected, actual);
        }
        public void testListTree()
        {
            ITreeAdaptor adaptor  = new CommonTreeAdaptor();
            TreeWizard   wiz      = new TreeWizard(adaptor, tokens);
            CommonTree   t        = (CommonTree)wiz.Create("(nil A B C)");
            string       actual   = t.ToStringTree();
            string       expected = "A B C";

            Assert.AreEqual(expected, actual);
        }
        public void testDoubleLevelTree()
        {
            ITreeAdaptor adaptor  = new CommonTreeAdaptor();
            TreeWizard   wiz      = new TreeWizard(adaptor, tokens);
            CommonTree   t        = (CommonTree)wiz.Create("(A (B C) (B D) E)");
            string       actual   = t.ToStringTree();
            string       expected = "(A (B C) (B D) E)";

            Assert.AreEqual(expected, actual);
        }
        public void testEqualsWithMismatchedText()
        {
            ITreeAdaptor adaptor = new CommonTreeAdaptor();
            TreeWizard   wiz     = new TreeWizard(adaptor, tokens);
            CommonTree   t1      = (CommonTree)wiz.Create("(A B[foo] C)");
            CommonTree   t2      = (CommonTree)wiz.Create("(A B C)");
            bool         same    = TreeWizard.Equals(t1, t2, adaptor);

            Assert.IsTrue(!same);
        }
Example #10
0
        public void testParseWithText()
        {
            ITreeAdaptor adaptor = new CommonTreeAdaptor();
            TreeWizard   wiz     = new TreeWizard(adaptor, tokens);
            CommonTree   t       = (CommonTree)wiz.Create("(A B[foo] C[bar])");
            // C pattern has no text arg so despite [bar] in t, no need
            // to match text--check structure only.
            bool valid = wiz.Parse(t, "(A B[foo] C)");

            Assert.IsTrue(valid);
        }
Example #11
0
        public void testSingleNodeIndex()
        {
            ITreeAdaptor adaptor  = new CommonTreeAdaptor();
            TreeWizard   wiz      = new TreeWizard(adaptor, tokens);
            CommonTree   t        = (CommonTree)wiz.Create("ID");
            IDictionary  m        = wiz.Index(t);
            string       actual   = CollectionUtils.DictionaryToString(m);
            string       expected = "{10=[ID]}";

            Assert.AreEqual(expected, actual);
        }
Example #12
0
        public void testFindPattern()
        {
            ITreeAdaptor adaptor  = new CommonTreeAdaptor();
            TreeWizard   wiz      = new TreeWizard(adaptor, tokens);
            CommonTree   t        = (CommonTree)wiz.Create("(A B C (A[foo] B[bar]) (D (A[big] B[dog])))");
            IList        subtrees = wiz.Find(t, "(A B)");
            IList        elements = subtrees;
            string       actual   = CollectionUtils.ListToString(elements);
            string       expected = "[foo, big]";

            Assert.AreEqual(expected, actual);
        }
Example #13
0
        public void testRepeatsIndex()
        {
            ITreeAdaptor adaptor = new CommonTreeAdaptor();
            TreeWizard   wiz     = new TreeWizard(adaptor, tokens);
            CommonTree   t       = (CommonTree)wiz.Create("(A B (A C B) B D D)");
            IDictionary  m       = wiz.Index(t);
            string       actual  = CollectionUtils.DictionaryToString(m);
            //string expected = "{8=[D, D], 6=[B, B, B], 7=[C], 5=[A, A]}";
            string expected = "{8=[D, D], 7=[C], 6=[B, B, B], 5=[A, A]}";

            Assert.AreEqual(expected, actual);
        }
Example #14
0
        public void testParseWithWildcardLabels()
        {
            ITreeAdaptor adaptor = new CommonTreeAdaptor();
            TreeWizard   wiz     = new TreeWizard(adaptor, tokens);
            CommonTree   t       = (CommonTree)wiz.Create("(A B C)");
            IDictionary  labels  = new Hashtable();
            bool         valid   = wiz.Parse(t, "(A %b:. %c:.)", labels);

            Assert.IsTrue(valid);
            Assert.AreEqual("B", labels["b"].ToString());
            Assert.AreEqual("C", labels["c"].ToString());
        }
Example #15
0
        public void testRepeatsVisit2()
        {
            ITreeAdaptor adaptor  = new CommonTreeAdaptor();
            TreeWizard   wiz      = new TreeWizard(adaptor, tokens);
            CommonTree   t        = (CommonTree)wiz.Create("(A B (A C B) B D D)");
            IList        elements = new ArrayList();

            wiz.Visit(t, wiz.GetTokenType("A"), new RecordAllElementsVisitor(elements));
            string actual   = CollectionUtils.ListToString(elements);
            string expected = "[A, A]";

            Assert.AreEqual(expected, actual);
        }
Example #16
0
        public void testRepeatsVisitWithNullParentAndContext()
        {
            ITreeAdaptor adaptor  = new CommonTreeAdaptor();
            TreeWizard   wiz      = new TreeWizard(adaptor, tokens);
            CommonTree   t        = (CommonTree)wiz.Create("(A B (A C B) B D D)");
            IList        elements = new ArrayList();

            wiz.Visit(t, wiz.GetTokenType("A"), new Test1ContextVisitor(adaptor, elements));
            string actual   = CollectionUtils.ListToString(elements);
            string expected = "[A@nil[0], A@A[1]]";

            Assert.AreEqual(expected, actual);
        }
Example #17
0
        public void testVisitPattern()
        {
            ITreeAdaptor adaptor  = new CommonTreeAdaptor();
            TreeWizard   wiz      = new TreeWizard(adaptor, tokens);
            CommonTree   t        = (CommonTree)wiz.Create("(A B C (A B) D)");
            IList        elements = new ArrayList();

            wiz.Visit(t, "(A B)", new RecordAllElementsVisitor(elements));
            string actual   = CollectionUtils.ListToString(elements);
            string expected = "[A]";             // shouldn't match overall root, just (A B)

            Assert.AreEqual(expected, actual);
        }
Example #18
0
        public void testVisitPatternMultiple()
        {
            ITreeAdaptor adaptor  = new CommonTreeAdaptor();
            TreeWizard   wiz      = new TreeWizard(adaptor, tokens);
            CommonTree   t        = (CommonTree)wiz.Create("(A B C (A B) (D (A B)))");
            IList        elements = new ArrayList();

            wiz.Visit(t, "(A B)", new Test1ContextVisitor(adaptor, elements));
            string actual   = CollectionUtils.ListToString(elements);
            string expected = "[A@A[2], A@D[0]]";             // shouldn't match overall root, just (A B)

            Assert.AreEqual(expected, actual);
        }
Example #19
0
        public void testVisitPatternMultipleWithLabels()
        {
            ITreeAdaptor adaptor  = new CommonTreeAdaptor();
            TreeWizard   wiz      = new TreeWizard(adaptor, tokens);
            CommonTree   t        = (CommonTree)wiz.Create("(A B C (A[foo] B[bar]) (D (A[big] B[dog])))");
            IList        elements = new ArrayList();

            wiz.Visit(t, "(%a:A %b:B)", new Test2ContextVisitor(adaptor, elements));
            string actual   = CollectionUtils.ListToString(elements);
            string expected = "[foo@A[2]foo&bar, big@D[0]big&dog]";

            Assert.AreEqual(expected, actual);
        }
Example #20
0
        public void testParseLabelsAndTestText()
        {
            ITreeAdaptor adaptor = new CommonTreeAdaptor();
            TreeWizard   wiz     = new TreeWizard(adaptor, tokens);
            CommonTree   t       = (CommonTree)wiz.Create("(A B[foo] C)");
            IDictionary  labels  = new Hashtable();
            bool         valid   = wiz.Parse(t, "(%a:A %b:B[foo] %c:C)", labels);

            Assert.IsTrue(valid);
            Assert.AreEqual("A", labels["a"].ToString());
            Assert.AreEqual("foo", labels["b"].ToString());
            Assert.AreEqual("C", labels["c"].ToString());
        }
Example #21
0
        public void testParseLabelsInNestedTree()
        {
            ITreeAdaptor adaptor = new CommonTreeAdaptor();
            TreeWizard   wiz     = new TreeWizard(adaptor, tokens);
            CommonTree   t       = (CommonTree)wiz.Create("(A (B C) (D E))");
            IDictionary  labels  = new Hashtable();
            bool         valid   = wiz.Parse(t, "(%a:A (%b:B %c:C) (%d:D %e:E) )", labels);

            Assert.IsTrue(valid);
            Assert.AreEqual("A", labels["a"].ToString());
            Assert.AreEqual("B", labels["b"].ToString());
            Assert.AreEqual("C", labels["c"].ToString());
            Assert.AreEqual("D", labels["d"].ToString());
            Assert.AreEqual("E", labels["e"].ToString());
        }
Example #22
0
 public void testListTree()
 {
     ITreeAdaptor adaptor = new CommonTreeAdaptor();
     TreeWizard wiz = new TreeWizard(adaptor, tokens);
     CommonTree t = (CommonTree)wiz.Create("(nil A B C)");
     string actual = t.ToStringTree();
     string expected = "A B C";
     Assert.AreEqual(expected, actual);
 }
Example #23
0
 public void testParseLabelsAndTestText()
 {
     ITreeAdaptor adaptor = new CommonTreeAdaptor();
     TreeWizard wiz = new TreeWizard(adaptor, tokens);
     CommonTree t = (CommonTree)wiz.Create("(A B[foo] C)");
     IDictionary labels = new Hashtable();
     bool valid = wiz.Parse(t, "(%a:A %b:B[foo] %c:C)", labels);
     Assert.IsTrue(valid);
     Assert.AreEqual("A", labels["a"].ToString());
     Assert.AreEqual("foo", labels["b"].ToString());
     Assert.AreEqual("C", labels["c"].ToString());
 }
Example #24
0
 public void testRepeatsVisitWithNullParentAndContext()
 {
     ITreeAdaptor adaptor = new CommonTreeAdaptor();
     TreeWizard wiz = new TreeWizard(adaptor, tokens);
     CommonTree t = (CommonTree)wiz.Create("(A B (A C B) B D D)");
     IList elements = new ArrayList();
     wiz.Visit(t, wiz.GetTokenType("A"), new Test1ContextVisitor(adaptor, elements));
     string actual = CollectionUtils.ListToString(elements);
     string expected = "[A@nil[0], A@A[1]]";
     Assert.AreEqual(expected, actual);
 }
Example #25
0
 public void testVisitPattern()
 {
     ITreeAdaptor adaptor = new CommonTreeAdaptor();
     TreeWizard wiz = new TreeWizard(adaptor, tokens);
     CommonTree t = (CommonTree)wiz.Create("(A B C (A B) D)");
     IList elements = new ArrayList();
     wiz.Visit(t, "(A B)", new RecordAllElementsVisitor(elements));
     string actual = CollectionUtils.ListToString(elements);
     string expected = "[A]"; // shouldn't match overall root, just (A B)
     Assert.AreEqual(expected, actual);
 }
Example #26
0
 public void testParseLabelsInNestedTree()
 {
     ITreeAdaptor adaptor = new CommonTreeAdaptor();
     TreeWizard wiz = new TreeWizard(adaptor, tokens);
     CommonTree t = (CommonTree)wiz.Create("(A (B C) (D E))");
     IDictionary labels = new Hashtable();
     bool valid = wiz.Parse(t, "(%a:A (%b:B %c:C) (%d:D %e:E) )", labels);
     Assert.IsTrue(valid);
     Assert.AreEqual("A", labels["a"].ToString());
     Assert.AreEqual("B", labels["b"].ToString());
     Assert.AreEqual("C", labels["c"].ToString());
     Assert.AreEqual("D", labels["d"].ToString());
     Assert.AreEqual("E", labels["e"].ToString());
 }
Example #27
0
 public void testVisitPatternMultipleWithLabels()
 {
     ITreeAdaptor adaptor = new CommonTreeAdaptor();
     TreeWizard wiz = new TreeWizard(adaptor, tokens);
     CommonTree t = (CommonTree)wiz.Create("(A B C (A[foo] B[bar]) (D (A[big] B[dog])))");
     IList elements = new ArrayList();
     wiz.Visit(t, "(%a:A %b:B)", new Test2ContextVisitor(adaptor, elements));
     string actual = CollectionUtils.ListToString(elements);
     string expected = "[foo@A[2]foo&bar, big@D[0]big&dog]";
     Assert.AreEqual(expected, actual);
 }
Example #28
0
 public void testVisitPatternMultiple()
 {
     ITreeAdaptor adaptor = new CommonTreeAdaptor();
     TreeWizard wiz = new TreeWizard(adaptor, tokens);
     CommonTree t = (CommonTree)wiz.Create("(A B C (A B) (D (A B)))");
     IList elements = new ArrayList();
     wiz.Visit(t, "(A B)", new Test1ContextVisitor(adaptor, elements));
     string actual = CollectionUtils.ListToString(elements);
     string expected = "[A@A[2], A@D[0]]"; // shouldn't match overall root, just (A B)
     Assert.AreEqual(expected, actual);
 }
Example #29
0
 public void testParseWithText()
 {
     ITreeAdaptor adaptor = new CommonTreeAdaptor();
     TreeWizard wiz = new TreeWizard(adaptor, tokens);
     CommonTree t = (CommonTree)wiz.Create("(A B[foo] C[bar])");
     // C pattern has no text arg so despite [bar] in t, no need
     // to match text--check structure only.
     bool valid = wiz.Parse(t, "(A B[foo] C)");
     Assert.IsTrue(valid);
 }
Example #30
0
 public void testWildcard()
 {
     ITreeAdaptor adaptor = new CommonTreeAdaptor();
     TreeWizard wiz = new TreeWizard(adaptor, tokens);
     CommonTree t = (CommonTree)wiz.Create("(A B C)");
     bool valid = wiz.Parse(t, "(A . .)");
     Assert.IsTrue(valid);
 }
Example #31
0
 public void testEquals()
 {
     ITreeAdaptor adaptor = new CommonTreeAdaptor();
     TreeWizard wiz = new TreeWizard(adaptor, tokens);
     CommonTree t1 = (CommonTree)wiz.Create("(A B C)");
     CommonTree t2 = (CommonTree)wiz.Create("(A B C)");
     bool same = TreeWizard.Equals(t1, t2, adaptor);
     Assert.IsTrue(same);
 }
Example #32
0
 public void testSingleNodeWithArg()
 {
     ITreeAdaptor adaptor = new CommonTreeAdaptor();
     TreeWizard wiz = new TreeWizard(adaptor, tokens);
     CommonTree t = (CommonTree)wiz.Create("ID[foo]");
     string actual = t.ToStringTree();
     string expected = "foo";
     Assert.AreEqual(expected, actual);
 }
Example #33
0
        /** Return list of (TOKEN_NAME node, 'literal' node) pairs */
        public static IList<System.Tuple<GrammarAST, GrammarAST>> GetStringLiteralAliasesFromLexerRules(GrammarRootAST ast)
        {
            string[] patterns = {
            "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL)))",
            "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL ACTION)))",
            "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL SEMPRED)))",
            "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) .)))",
            "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) . .)))",
            "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) (LEXER_ACTION_CALL . .))))",
            "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) . (LEXER_ACTION_CALL . .))))",
            "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) (LEXER_ACTION_CALL . .) .)))",
			// TODO: allow doc comment in there
		};
            GrammarASTAdaptor adaptor = new GrammarASTAdaptor(ast.Token.InputStream);
            Antlr.Runtime.Tree.TreeWizard wiz = new Antlr.Runtime.Tree.TreeWizard(adaptor, ANTLRParser.tokenNames);
            IList<System.Tuple<GrammarAST, GrammarAST>> lexerRuleToStringLiteral =
                new List<System.Tuple<GrammarAST, GrammarAST>>();

            IList<GrammarAST> ruleNodes = ast.GetNodesWithType(ANTLRParser.RULE);
            if (ruleNodes == null || ruleNodes.Count == 0)
                return null;

            foreach (GrammarAST r in ruleNodes)
            {
                //tool.log("grammar", r.toStringTree());
                //			System.out.println("chk: "+r.toStringTree());
                Antlr.Runtime.Tree.ITree name = r.GetChild(0);
                if (name.Type == ANTLRParser.TOKEN_REF)
                {
                    // check rule against patterns
                    bool isLitRule;
                    foreach (string pattern in patterns)
                    {
                        isLitRule =
                            DefAlias(r, pattern, wiz, lexerRuleToStringLiteral);
                        if (isLitRule)
                            break;
                    }
                    //				if ( !isLitRule ) System.out.println("no pattern matched");
                }
            }
            return lexerRuleToStringLiteral;
        }
Example #34
0
 public void testParseSingleNode()
 {
     ITreeAdaptor adaptor = new CommonTreeAdaptor();
     TreeWizard wiz = new TreeWizard(adaptor, tokens);
     CommonTree t = (CommonTree)wiz.Create("A");
     bool valid = wiz.Parse(t, "A");
     Assert.IsTrue(valid);
 }
Example #35
0
 public void testInvalidListTree()
 {
     ITreeAdaptor adaptor = new CommonTreeAdaptor();
     TreeWizard wiz = new TreeWizard(adaptor, tokens);
     CommonTree t = (CommonTree)wiz.Create("A B C");
     Assert.IsTrue(t == null);
 }
Example #36
0
 public void testRepeatsVisit2()
 {
     ITreeAdaptor adaptor = new CommonTreeAdaptor();
     TreeWizard wiz = new TreeWizard(adaptor, tokens);
     CommonTree t = (CommonTree)wiz.Create("(A B (A C B) B D D)");
     IList elements = new ArrayList();
     wiz.Visit(t, wiz.GetTokenType("A"), new RecordAllElementsVisitor(elements));
     string actual = CollectionUtils.ListToString(elements);
     string expected = "[A, A]";
     Assert.AreEqual(expected, actual);
 }
Example #37
0
 public void testSingleNodeIndex()
 {
     ITreeAdaptor adaptor = new CommonTreeAdaptor();
     TreeWizard wiz = new TreeWizard(adaptor, tokens);
     CommonTree t = (CommonTree)wiz.Create("ID");
     IDictionary m = wiz.Index(t);
     string actual = CollectionUtils.DictionaryToString(m);
     string expected = "{10=[ID]}";
     Assert.AreEqual(expected, actual);
 }
Example #38
0
 public void testParseWithWildcardLabels()
 {
     ITreeAdaptor adaptor = new CommonTreeAdaptor();
     TreeWizard wiz = new TreeWizard(adaptor, tokens);
     CommonTree t = (CommonTree)wiz.Create("(A B C)");
     IDictionary labels = new Hashtable();
     bool valid = wiz.Parse(t, "(A %b:. %c:.)", labels);
     Assert.IsTrue(valid);
     Assert.AreEqual("B", labels["b"].ToString());
     Assert.AreEqual("C", labels["c"].ToString());
 }
Example #39
0
 public void testRepeatsIndex()
 {
     ITreeAdaptor adaptor = new CommonTreeAdaptor();
     TreeWizard wiz = new TreeWizard(adaptor, tokens);
     CommonTree t = (CommonTree)wiz.Create("(A B (A C B) B D D)");
     IDictionary m = wiz.Index(t);
     string actual = CollectionUtils.DictionaryToString(m);
     //string expected = "{8=[D, D], 6=[B, B, B], 7=[C], 5=[A, A]}";
     string expected = "{8=[D, D], 7=[C], 6=[B, B, B], 5=[A, A]}";
     Assert.AreEqual(expected, actual);
 }
Example #40
0
        public virtual void Rewrite( ITreeAdaptor adaptor, CommonTree t, string[] tokenNames )
        {
            TreeWizard wiz = new TreeWizard( adaptor, tokenNames );

            // ACTIONS STUFF
            wiz.Visit( t, ANTLRParser.ACTION, ( tree ) =>
            {
                ACTION( tokens, (CommonTree)tree );
            } );

            // ^('@' id ACTION) rule actions
            wiz.Visit( t, ANTLRParser.AMPERSAND, ( tree ) =>
            {
                CommonTree a = (CommonTree)t;
                CommonTree action = null;
                if (a.ChildCount == 2)
                {
                    action = (CommonTree)a.GetChild(1);
                }
                else if (a.ChildCount == 3)
                {
                    action = (CommonTree)a.GetChild(2);
                }

                if ( action.Type == ANTLRParser.ACTION )
                {
                    tokens.Delete( a.TokenStartIndex, a.TokenStopIndex );
                    KillTrailingNewline( tokens, action.TokenStopIndex );
                }
            } );

            wiz.Visit( t, ANTLRParser.ACTION, ( tree ) =>
            {
            } );

            // wipe rule arguments
            wiz.Visit( t, ANTLRParser.ARG, ( tree ) =>
            {
                CommonTree a = (CommonTree)t;
                a = (CommonTree)a.GetChild( 0 );
                tokens.Delete( a.Token.TokenIndex );
                KillTrailingNewline( tokens, a.Token.TokenIndex );
            } );

            // wipe rule return declarations
            wiz.Visit( t, ANTLRParser.RET, ( tree ) =>
            {
                CommonTree a = (CommonTree)t;
                CommonTree ret = (CommonTree)a.GetChild( 0 );
                tokens.Delete( a.Token.TokenIndex, ret.Token.TokenIndex );
            } );

            // comment out semantic predicates
            wiz.Visit( t, ANTLRParser.SEMPRED, ( tree ) =>
            {
                CommonTree a = (CommonTree)t;
                tokens.Replace( a.Token.TokenIndex, "/*" + a.Text + "*/" );
            } );

            // comment out semantic predicates
            wiz.Visit( t, ANTLRParser.GATED_SEMPRED, ( tree ) =>
            {
                CommonTree a = (CommonTree)t;
                string text = tokens.ToString( a.TokenStartIndex, a.TokenStopIndex );
                tokens.Replace( a.TokenStartIndex, a.TokenStopIndex, "/*" + text + "*/" );
            } );

            // comment scope specs
            wiz.Visit( t, ANTLRParser.SCOPE, ( tree ) =>
            {
                CommonTree a = (CommonTree)t;
                tokens.Delete( a.TokenStartIndex, a.TokenStopIndex );
                KillTrailingNewline( tokens, a.TokenStopIndex );
            } );

            // args r[x,y] -> ^(r [x,y])
            wiz.Visit( t, ANTLRParser.ARG_ACTION, ( tree ) =>
            {
                CommonTree a = (CommonTree)t;
                if ( a.Parent.Type == ANTLRParser.RULE_REF )
                    tokens.Delete( a.TokenStartIndex, a.TokenStopIndex );
            } );

            #if false // what is this token type in the C# ported version of the V3 grammar?
            // ^('=' id ^(RULE_REF [arg])), ...
            wiz.Visit( t, ANTLRParser.LABEL_ASSIGN, ( tree ) =>
            {
                CommonTree a = (CommonTree)t;
                if ( !a.HasAncestor( ANTLRParser.OPTIONS ) )
                {
                    // avoid options
                    CommonTree child = (CommonTree)a.GetChild( 0 );
                    // kill "id="
                    tokens.Delete( a.token.TokenIndex );
                    tokens.Delete( child.token.TokenIndex );
                }
            } );
            #endif

            #if false // what is this token type in the C# ported version of the V3 grammar?
            // ^('+=' id ^(RULE_REF [arg])), ...
            wiz.Visit( t, ANTLRParser.LIST_LABEL_ASSIGN, ( tree ) =>
            {
                CommonTree a = (CommonTree)t;
                CommonTree child = (CommonTree)a.GetChild( 0 );
                // kill "id+="
                tokens.Delete( a.token.TokenIndex );
                tokens.Delete( child.token.TokenIndex );
            } );
            #endif

            // AST STUFF

            wiz.Visit( t, ANTLRParser.REWRITE, ( tree ) =>
            {
                CommonTree a = (CommonTree)t;
                CommonTree child = (CommonTree)a.GetChild( 0 );
                int stop = child.TokenStopIndex;
                if ( child.Type == ANTLRParser.SEMPRED )
                {
                    CommonTree rew = (CommonTree)a.GetChild( 1 );
                    stop = rew.TokenStopIndex;
                }
                tokens.Delete( a.Token.TokenIndex, stop );
                KillTrailingNewline( tokens, stop );
            } );

            wiz.Visit( t, ANTLRParser.ROOT, ( tree ) =>
            {
                tokens.Delete( ( (CommonTree)t ).Token.TokenIndex );
            } );

            wiz.Visit( t, ANTLRParser.BANG, ( tree ) =>
            {
                tokens.Delete( ( (CommonTree)t ).Token.TokenIndex );
            } );
        }
Example #41
0
 public void testParseWithTextFails()
 {
     ITreeAdaptor adaptor = new CommonTreeAdaptor();
     TreeWizard wiz = new TreeWizard(adaptor, tokens);
     CommonTree t = (CommonTree)wiz.Create("(A B C)");
     bool valid = wiz.Parse(t, "(A[foo] B C)");
     Assert.IsTrue(!valid); // fails
 }
Example #42
0
        public virtual void Rewrite(ITreeAdaptor adaptor, CommonTree t, string[] tokenNames)
        {
            TreeWizard wiz = new TreeWizard(adaptor, tokenNames);

            // ACTIONS STUFF
            wiz.Visit(t, ANTLRParser.ACTION, (tree) =>
            {
                ACTION(tokens, (CommonTree)tree);
            });

            // ^('@' id ACTION) rule actions
            wiz.Visit(t, ANTLRParser.AMPERSAND, (tree) =>
            {
                CommonTree a      = (CommonTree)t;
                CommonTree action = null;
                if (a.ChildCount == 2)
                {
                    action = (CommonTree)a.GetChild(1);
                }
                else if (a.ChildCount == 3)
                {
                    action = (CommonTree)a.GetChild(2);
                }

                if (action.Type == ANTLRParser.ACTION)
                {
                    tokens.Delete(a.TokenStartIndex, a.TokenStopIndex);
                    KillTrailingNewline(tokens, action.TokenStopIndex);
                }
            });

            wiz.Visit(t, ANTLRParser.ACTION, (tree) =>
            {
            });

            // wipe rule arguments
            wiz.Visit(t, ANTLRParser.ARG, (tree) =>
            {
                CommonTree a = (CommonTree)t;
                a            = (CommonTree)a.GetChild(0);
                tokens.Delete(a.token.TokenIndex);
                KillTrailingNewline(tokens, a.token.TokenIndex);
            });

            // wipe rule return declarations
            wiz.Visit(t, ANTLRParser.RET, (tree) =>
            {
                CommonTree a   = (CommonTree)t;
                CommonTree ret = (CommonTree)a.GetChild(0);
                tokens.Delete(a.token.TokenIndex, ret.token.TokenIndex);
            });

            // comment out semantic predicates
            wiz.Visit(t, ANTLRParser.SEMPRED, (tree) =>
            {
                CommonTree a = (CommonTree)t;
                tokens.Replace(a.token.TokenIndex, "/*" + a.Text + "*/");
            });

            // comment out semantic predicates
            wiz.Visit(t, ANTLRParser.GATED_SEMPRED, (tree) =>
            {
                CommonTree a = (CommonTree)t;
                string text  = tokens.ToString(a.TokenStartIndex, a.TokenStopIndex);
                tokens.Replace(a.TokenStartIndex, a.TokenStopIndex, "/*" + text + "*/");
            });

            // comment scope specs
            wiz.Visit(t, ANTLRParser.SCOPE, (tree) =>
            {
                CommonTree a = (CommonTree)t;
                tokens.Delete(a.TokenStartIndex, a.TokenStopIndex);
                KillTrailingNewline(tokens, a.TokenStopIndex);
            });

            // args r[x,y] -> ^(r [x,y])
            wiz.Visit(t, ANTLRParser.ARG_ACTION, (tree) =>
            {
                CommonTree a = (CommonTree)t;
                if (a.Parent.Type == ANTLRParser.RULE_REF)
                {
                    tokens.Delete(a.TokenStartIndex, a.TokenStopIndex);
                }
            });

#if false // what is this token type in the C# ported version of the V3 grammar?
            // ^('=' id ^(RULE_REF [arg])), ...
            wiz.Visit(t, ANTLRParser.LABEL_ASSIGN, (tree) =>
            {
                CommonTree a = (CommonTree)t;
                if (!a.HasAncestor(ANTLRParser.OPTIONS))
                {
                    // avoid options
                    CommonTree child = (CommonTree)a.GetChild(0);
                    // kill "id="
                    tokens.Delete(a.token.TokenIndex);
                    tokens.Delete(child.token.TokenIndex);
                }
            });
#endif

#if false // what is this token type in the C# ported version of the V3 grammar?
            // ^('+=' id ^(RULE_REF [arg])), ...
            wiz.Visit(t, ANTLRParser.LIST_LABEL_ASSIGN, (tree) =>
            {
                CommonTree a     = (CommonTree)t;
                CommonTree child = (CommonTree)a.GetChild(0);
                // kill "id+="
                tokens.Delete(a.token.TokenIndex);
                tokens.Delete(child.token.TokenIndex);
            });
#endif

            // AST STUFF

            wiz.Visit(t, ANTLRParser.REWRITE, (tree) =>
            {
                CommonTree a     = (CommonTree)t;
                CommonTree child = (CommonTree)a.GetChild(0);
                int stop         = child.TokenStopIndex;
                if (child.Type == ANTLRParser.SEMPRED)
                {
                    CommonTree rew = (CommonTree)a.GetChild(1);
                    stop           = rew.TokenStopIndex;
                }
                tokens.Delete(a.token.TokenIndex, stop);
                KillTrailingNewline(tokens, stop);
            });

            wiz.Visit(t, ANTLRParser.ROOT, (tree) =>
            {
                tokens.Delete(((CommonTree)t).token.TokenIndex);
            });

            wiz.Visit(t, ANTLRParser.BANG, (tree) =>
            {
                tokens.Delete(((CommonTree)t).token.TokenIndex);
            });
        }
Example #43
0
 public void testFindPattern()
 {
     ITreeAdaptor adaptor = new CommonTreeAdaptor();
     TreeWizard wiz = new TreeWizard(adaptor, tokens);
     CommonTree t = (CommonTree)wiz.Create("(A B C (A[foo] B[bar]) (D (A[big] B[dog])))");
     IList subtrees = wiz.Find(t, "(A B)");
     IList elements = subtrees;
     string actual = CollectionUtils.ListToString(elements);
     string expected = "[foo, big]";
     Assert.AreEqual(expected, actual);
 }