Example usage for org.antlr.v4.tool Grammar getTokenNames

List of usage examples for org.antlr.v4.tool Grammar getTokenNames

Introduction

In this page you can find the example usage for org.antlr.v4.tool Grammar getTokenNames.

Prototype

public String[] getTokenNames() 

Source Link

Document

Gets an array of token names for tokens defined or imported by the grammar.

Usage

From source file:net.certiv.json.test.base.AbstractBase.java

License:Open Source License

protected void checkSymbols(Grammar g, String rulesStr, String allValidTokensStr) throws Exception {
    String[] typeToTokenName = g.getTokenNames();
    Set<String> tokens = new HashSet<String>();
    for (int i = 0; i < typeToTokenName.length; i++) {
        String t = typeToTokenName[i];
        if (t != null) {
            if (t.startsWith(Grammar.AUTO_GENERATED_TOKEN_NAME_PREFIX)) {
                tokens.add(g.getTokenDisplayName(i));
            } else {
                tokens.add(t);/*from   w w  w .  j  a v a 2 s. c om*/
            }
        }
    }

    // make sure expected tokens are there
    StringTokenizer st = new StringTokenizer(allValidTokensStr, ", ");
    while (st.hasMoreTokens()) {
        String tokenName = st.nextToken();
        assertTrue("token " + tokenName + " expected, but was undefined",
                g.getTokenType(tokenName) != Token.INVALID_TYPE);
        tokens.remove(tokenName);
    }
    // make sure there are not any others (other than <EOF> etc...)
    for (String tokenName : tokens) {
        assertTrue("unexpected token name " + tokenName, g.getTokenType(tokenName) < Token.MIN_USER_TOKEN_TYPE);
    }

    // make sure all expected rules are there
    st = new StringTokenizer(rulesStr, ", ");
    int n = 0;
    while (st.hasMoreTokens()) {
        String ruleName = st.nextToken();
        assertNotNull("rule " + ruleName + " expected", g.getRule(ruleName));
        n++;
    }
    Assert.assertEquals("number of rules mismatch; expecting " + n + "; found " + g.rules.size(), n,
            g.rules.size());
}