Package org.antlr.v4.tool

Examples of org.antlr.v4.tool.Grammar


    String tokenNames = "A, C, D";
    checkSymbols(g, rules, tokenNames);
  }

  @Test public void testCombinedGrammarLiterals() throws Exception {
    Grammar g = new Grammar(
        "grammar t;\n"+
        "a : 'begin' b 'end';\n" +
        "b : C ';' ;\n" +
        "ID : 'a' ;\n" +
        "FOO : 'foo' ;\n" // "foo" is not a token name
View Full Code Here


    checkSymbols(g, rules, tokenNames);
  }

  @Test public void testLiteralInParserAndLexer() throws Exception {
    // 'x' is token and char in lexer rule
    Grammar g = new Grammar(
        "grammar t;\n" +
        "a : 'x' E ; \n" +
        "E: 'x' '0' ;\n");

    String literals = "['x']";
    String foundLiterals = g.stringLiteralToTypeMap.keySet().toString();
    assertEquals(literals, foundLiterals);

    foundLiterals = g.implicitLexer.stringLiteralToTypeMap.keySet().toString();
    assertEquals("['x']", foundLiterals); // pushed in lexer from parser

    String[] typeToTokenName = g.getTokenDisplayNames();
    Set<String> tokens = new LinkedHashSet<String>();
    for (String t : typeToTokenName) if ( t!=null ) tokens.add(t);
    assertEquals("[<INVALID>, 'x', E]", tokens.toString());
  }
View Full Code Here

    assertEquals("[<INVALID>, 'x', E]", tokens.toString());
  }

  @Test public void testPredDoesNotHideNameToLiteralMapInLexer() throws Exception {
    // 'x' is token and char in lexer rule
    Grammar g = new Grammar(
        "grammar t;\n" +
        "a : 'x' X ; \n" +
        "X: 'x' {true}?;\n"); // must match as alias even with pred

    assertEquals("{'x'=1}", g.stringLiteralToTypeMap.toString());
View Full Code Here

    assertEquals("{'x'=1}", g.implicitLexer.stringLiteralToTypeMap.toString());
    assertEquals("{EOF=-1, X=1}", g.implicitLexer.tokenNameToTypeMap.toString());
  }

  @Test public void testCombinedGrammarWithRefToLiteralButNoTokenIDRef() throws Exception {
    Grammar g = new Grammar(
        "grammar t;\n"+
        "a : 'a' ;\n" +
        "A : 'a' ;\n");
    String rules = "a";
    String tokenNames = "A, 'a'";
View Full Code Here

    String tokenNames = "A, 'a'";
    checkSymbols(g, rules, tokenNames);
  }

  @Test public void testSetDoesNotMissTokenAliases() throws Exception {
    Grammar g = new Grammar(
        "grammar t;\n"+
        "a : 'a'|'b' ;\n" +
        "A : 'a' ;\n" +
        "B : 'b' ;\n");
    String rules = "a";
View Full Code Here

  }

  // T E S T  L I T E R A L  E S C A P E S

  @Test public void testParserCharLiteralWithEscape() throws Exception {
    Grammar g = new Grammar(
        "grammar t;\n"+
        "a : '\\n';\n");
    Set<?> literals = g.stringLiteralToTypeMap.keySet();
    // must store literals how they appear in the antlr grammar
    assertEquals("'\\n'", literals.toArray()[0]);
View Full Code Here

    }
    return null;
  }

  RulePropertyRef getRulePropertyRef(Token x, Token prop) {
    Grammar g = factory.getGrammar();
    try {
      Class<? extends RulePropertyRef> c = rulePropToModelMap.get(prop.getText());
      Constructor<? extends RulePropertyRef> ctor = c.getConstructor(StructDecl.class, String.class);
      RulePropertyRef ref =
        ctor.newInstance(nodeContext, getRuleLabel(x.getText()));
View Full Code Here

  public int ttype;
  public List<Decl> labels = new ArrayList<Decl>();

  public MatchToken(OutputModelFactory factory, TerminalAST ast) {
    super(factory, ast);
    Grammar g = factory.getGrammar();
    CodeGenerator gen = factory.getGenerator();
    ttype = g.getTokenType(ast.getText());
    name = gen.getTarget().getTokenTypeAsTargetLabel(g, ttype);
  }
View Full Code Here

  public Boolean genVisitor = false;
  public String grammarName;
 
  public ParserFile(OutputModelFactory factory, String fileName) {
    super(factory, fileName);
    Grammar g = factory.getGrammar();
    namedActions = new HashMap<String, Action>();
    for (String name : g.namedActions.keySet()) {
      ActionAST ast = g.namedActions.get(name);
      namedActions.put(name, new Action(factory, ast));
    }
View Full Code Here

  @ModelElement public Action header;

  public VisitorFile(OutputModelFactory factory, String fileName) {
    super(factory, fileName);
    Grammar g = factory.getGrammar();
    parserName = g.getRecognizerName();
    grammarName = g.name;
    for (Rule r : g.rules.values()) {
      Map<String, List<Pair<Integer, AltAST>>> labels = r.getAltLabels();
      if ( labels!=null ) {
        for (Map.Entry<String, List<Pair<Integer, AltAST>>> pair : labels.entrySet()) {
View Full Code Here

TOP

Related Classes of org.antlr.v4.tool.Grammar

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.