@Test public void testParseSingleNode() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("A"); boolean valid = wiz.parse(t, "A"); assertTrue(valid); }
@Test public void testWildcard() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(A B C)"); boolean valid = wiz.parse(t, "(A . .)"); assertTrue(valid); }
@Test public void testVisitPatternMultipleWithLabels() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(A B C (A[foo] B[bar]) (D (A[big] B[dog])))"); final List<String> elements = new ArrayList<String>(); wiz.visit( t, "(%a:A %b:B)", new TreeWizard.ContextVisitor() { @Override public void visit(Object t, Object parent, int childIndex, Map<String, Object> labels) { elements.add( adaptor.getText(t) + "@" + (parent != null ? adaptor.getText(parent) : "nil") + "[" + childIndex + "]" + labels.get("a") + "&" + labels.get("b")); } }); String found = elements.toString(); String expecting = "[foo@A[2]foo&bar, big@D[0]big&dog]"; assertEquals(expecting, found); }
@Test public void testParseWithTextFails() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(A B C)"); boolean valid = wiz.parse(t, "(A[foo] B C)"); assertTrue(!valid); // fails }
public static Map<String, String> getStringLiteralAliasesFromLexerRules(GrammarRootAST ast) { GrammarASTAdaptor adaptor = new GrammarASTAdaptor(ast.token.getInputStream()); TreeWizard wiz = new TreeWizard(adaptor, ANTLRParser.tokenNames); Map<String, String> lexerRuleToStringLiteral = new HashMap<String, String>(); List<GrammarAST> ruleNodes = ast.getNodesWithType(ANTLRParser.RULE); if (ruleNodes == null || ruleNodes.size() == 0) return null; for (GrammarAST r : ruleNodes) { // tool.log("grammar", r.toStringTree()); Tree name = r.getChild(0); if (name.getType() == ANTLRParser.TOKEN_REF) { Map nodes = new HashMap(); boolean isLitRule = wiz.parse(r, "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL)))", nodes); if (isLitRule) { GrammarAST litNode = (GrammarAST) nodes.get("lit"); GrammarAST nameNode = (GrammarAST) nodes.get("name"); lexerRuleToStringLiteral.put(litNode.getText(), nameNode.getText()); continue; } // TODO: allow doc comment in there nodes = new HashMap(); // try with action in there isLitRule = wiz.parse(r, "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL ACTION)))", nodes); if (isLitRule) { GrammarAST litNode = (GrammarAST) nodes.get("lit"); GrammarAST nameNode = (GrammarAST) nodes.get("name"); lexerRuleToStringLiteral.put(litNode.getText(), nameNode.getText()); } } } return lexerRuleToStringLiteral; }
@Test public void testDoubleLevelTree() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(A (B C) (B D) E)"); String found = t.toStringTree(); String expecting = "(A (B C) (B D) E)"; assertEquals(expecting, found); }
@Test public void testEqualsWithMismatchedText() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t1 = (CommonTree) wiz.create("(A B[foo] C)"); CommonTree t2 = (CommonTree) wiz.create("(A B C)"); boolean same = TreeWizard.equals(t1, t2, adaptor); assertTrue(!same); }
@Test public void testSingleNodeWithArg() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("ID[foo]"); String found = t.toStringTree(); String expecting = "foo"; assertEquals(expecting, found); }
@Test public void testListTree() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(nil A B C)"); String found = t.toStringTree(); String expecting = "A B C"; assertEquals(expecting, found); }
@Test public void testParseWithText2() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(A B[T__32] (C (D E[a])))"); // C pattern has no text arg so despite [bar] in t, no need // to match text--check structure only. boolean valid = wiz.parse(t, "(A B[foo] C)"); assertEquals("(A T__32 (C (D a)))", t.toStringTree()); }
@Test public void testRepeatsIndex() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(A B (A C B) B D D)"); Map<Integer, List<Object>> m = wiz.index(t); String found = sortMapToString(m); String expecting = "{5=[A, A], 6=[B, B, B], 7=[C], 8=[D, D]}"; assertEquals(expecting, found); }
@Test public void testSingleNodeIndex() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("ID"); Map<Integer, List<Object>> m = wiz.index(t); String found = m.toString(); String expecting = "{10=[ID]}"; assertEquals(expecting, found); }
@Test public void testParseWithText() throws Exception { 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. boolean valid = wiz.parse(t, "(A B[foo] C)"); assertTrue(valid); }
@Test public void testParseWithWildcardLabels() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(A B C)"); Map<String, Object> labels = new HashMap<String, Object>(); boolean valid = wiz.parse(t, "(A %b:. %c:.)", labels); assertTrue(valid); assertEquals("B", labels.get("b").toString()); assertEquals("C", labels.get("c").toString()); }
@Test public void testFindPattern() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(A B C (A[foo] B[bar]) (D (A[big] B[dog])))"); final List<? extends Object> subtrees = wiz.find(t, "(A B)"); List<? extends Object> elements = subtrees; String found = elements.toString(); String expecting = "[foo, big]"; assertEquals(expecting, found); }
@Test public void testParseLabelsAndTestText() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(A B[foo] C)"); Map<String, Object> labels = new HashMap<String, Object>(); boolean valid = wiz.parse(t, "(%a:A %b:B[foo] %c:C)", labels); assertTrue(valid); assertEquals("A", labels.get("a").toString()); assertEquals("foo", labels.get("b").toString()); assertEquals("C", labels.get("c").toString()); }
@Test public void testParseLabelsInNestedTree() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(A (B C) (D E))"); Map<String, Object> labels = new HashMap<String, Object>(); boolean valid = wiz.parse(t, "(%a:A (%b:B %c:C) (%d:D %e:E) )", labels); assertTrue(valid); assertEquals("A", labels.get("a").toString()); assertEquals("B", labels.get("b").toString()); assertEquals("C", labels.get("c").toString()); assertEquals("D", labels.get("d").toString()); assertEquals("E", labels.get("e").toString()); }
@Test public void testVisitPattern() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(A B C (A B) D)"); final List<Object> elements = new ArrayList<Object>(); wiz.visit( t, "(A B)", new TreeWizard.Visitor() { @Override public void visit(Object t) { elements.add(t); } }); String found = elements.toString(); String expecting = "[A]"; // shouldn't match overall root, just (A B) assertEquals(expecting, found); }
@Test public void testRepeatsVisit2() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(A B (A C B) B D D)"); final List<Object> elements = new ArrayList<Object>(); wiz.visit( t, wiz.getTokenType("A"), new TreeWizard.Visitor() { @Override public void visit(Object t) { elements.add(t); } }); String found = elements.toString(); String expecting = "[A, A]"; assertEquals(expecting, found); }
@Test public void testRepeatsVisitWithNullParentAndContext() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(A B (A C B) B D D)"); final List<String> elements = new ArrayList<String>(); wiz.visit( t, wiz.getTokenType("A"), new TreeWizard.ContextVisitor() { @Override public void visit(Object t, Object parent, int childIndex, Map<String, Object> labels) { elements.add( adaptor.getText(t) + "@" + (parent != null ? adaptor.getText(parent) : "nil") + "[" + childIndex + "]"); } }); String found = elements.toString(); String expecting = "[A@nil[0], A@A[1]]"; assertEquals(expecting, found); }
protected static boolean defAlias( GrammarAST r, String pattern, org.antlr.runtime.tree.TreeWizard wiz, List<Pair<GrammarAST, GrammarAST>> lexerRuleToStringLiteral) { HashMap<String, Object> nodes = new HashMap<String, Object>(); if (wiz.parse(r, pattern, nodes)) { GrammarAST litNode = (GrammarAST) nodes.get("lit"); GrammarAST nameNode = (GrammarAST) nodes.get("name"); Pair<GrammarAST, GrammarAST> pair = new Pair<GrammarAST, GrammarAST>(nameNode, litNode); lexerRuleToStringLiteral.add(pair); return true; } return false; }
@Test public void testVisitPatternMultiple() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("(A B C (A B) (D (A B)))"); final List<String> elements = new ArrayList<String>(); wiz.visit( t, "(A B)", new TreeWizard.ContextVisitor() { @Override public void visit(Object t, Object parent, int childIndex, Map<String, Object> labels) { elements.add( adaptor.getText(t) + "@" + (parent != null ? adaptor.getText(parent) : "nil") + "[" + childIndex + "]"); } }); String found = elements.toString(); String expecting = "[A@A[2], A@D[0]]"; // shouldn't match overall root, just (A B) assertEquals(expecting, found); }
@Test public void testInvalidListTree() throws Exception { TreeWizard wiz = new TreeWizard(adaptor, tokens); CommonTree t = (CommonTree) wiz.create("A B C"); assertTrue(t == null); }