Exemplo n.º 1
0
  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;
  }
Exemplo n.º 2
0
 @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
 }
Exemplo n.º 3
0
 @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);
 }
Exemplo n.º 4
0
 @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);
 }
Exemplo n.º 5
0
 @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());
 }
Exemplo n.º 6
0
 @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);
 }
Exemplo n.º 7
0
 @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());
 }
Exemplo n.º 8
0
 @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());
 }
Exemplo n.º 9
0
 @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());
 }
Exemplo n.º 10
0
 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;
 }