Search in sources :

Example 96 with LexerGrammar

use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.

the class TestParserInterpreter method testStarA.

@Test
public void testStarA() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "s : A* B ;\n", lg);
    testInterp(lg, g, "s", "b", "(s b)");
    testInterp(lg, g, "s", "ab", "(s a b)");
    testInterp(lg, g, "s", "aaaaaab", "(s a a a a a a b)");
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 97 with LexerGrammar

use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.

the class LexerATNFactory method createATN.

@Override
public ATN createATN() {
    // BUILD ALL START STATES (ONE PER MODE)
    Set<String> modes = ((LexerGrammar) g).modes.keySet();
    for (String modeName : modes) {
        // create s0, start state; implied Tokens rule node
        TokensStartState startState = newState(TokensStartState.class, null);
        atn.defineMode(modeName, startState);
    }
    // INIT ACTION, RULE->TOKEN_TYPE MAP
    atn.ruleToTokenType = new int[g.rules.size()];
    for (Rule r : g.rules.values()) {
        atn.ruleToTokenType[r.index] = g.getTokenType(r.name);
    }
    // CREATE ATN FOR EACH RULE
    _createATN(g.rules.values());
    atn.lexerActions = new LexerAction[indexToActionMap.size()];
    for (Map.Entry<Integer, LexerAction> entry : indexToActionMap.entrySet()) {
        atn.lexerActions[entry.getKey()] = entry.getValue();
    }
    // LINK MODE START STATE TO EACH TOKEN RULE
    for (String modeName : modes) {
        List<Rule> rules = ((LexerGrammar) g).modes.get(modeName);
        TokensStartState startState = atn.modeNameToStartState.get(modeName);
        for (Rule r : rules) {
            if (!r.isFragment()) {
                RuleStartState s = atn.ruleToStartState[r.index];
                epsilon(startState, s);
            }
        }
    }
    ATNOptimizer.optimize(g, atn);
    return atn;
}
Also used : TokensStartState(org.antlr.v4.runtime.atn.TokensStartState) LexerAction(org.antlr.v4.runtime.atn.LexerAction) RuleStartState(org.antlr.v4.runtime.atn.RuleStartState) Rule(org.antlr.v4.tool.Rule) HashMap(java.util.HashMap) Map(java.util.Map)

Example 98 with LexerGrammar

use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.

the class Tool method createGrammar.

/**
 * Given the raw AST of a grammar, create a grammar object
 *		associated with the AST. Once we have the grammar object, ensure
 *		that all nodes in tree referred to this grammar. Later, we will
 *		use it for error handling and generally knowing from where a rule
 *		comes from.
 */
public Grammar createGrammar(GrammarRootAST ast) {
    final Grammar g;
    if (ast.grammarType == ANTLRParser.LEXER)
        g = new LexerGrammar(this, ast);
    else
        g = new Grammar(this, ast);
    // ensure each node has pointer to surrounding grammar
    GrammarTransformPipeline.setGrammarPtr(g, ast);
    return g;
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar)

Example 99 with LexerGrammar

use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.

the class Tool method processNonCombinedGrammar.

public void processNonCombinedGrammar(Grammar g, boolean gencode) {
    if (g.ast == null || g.ast.hasErrors)
        return;
    if (internalOption_PrintGrammarTree)
        System.out.println(g.ast.toStringTree());
    boolean ruleFail = checkForRuleIssues(g);
    if (ruleFail)
        return;
    int prevErrors = errMgr.getNumErrors();
    // MAKE SURE GRAMMAR IS SEMANTICALLY CORRECT (FILL IN GRAMMAR OBJECT)
    SemanticPipeline sem = new SemanticPipeline(g);
    sem.process();
    if (errMgr.getNumErrors() > prevErrors)
        return;
    // BUILD ATN FROM AST
    ATNFactory factory;
    if (g.isLexer())
        factory = new LexerATNFactory((LexerGrammar) g);
    else
        factory = new ParserATNFactory(g);
    g.atn = factory.createATN();
    if (generate_ATN_dot)
        generateATNs(g);
    if (g.tool.getNumErrors() == 0)
        generateInterpreterData(g);
    // PERFORM GRAMMAR ANALYSIS ON ATN: BUILD DECISION DFAs
    AnalysisPipeline anal = new AnalysisPipeline(g);
    anal.process();
    if (g.tool.getNumErrors() > prevErrors)
        return;
    // GENERATE CODE
    if (gencode) {
        CodeGenPipeline gen = new CodeGenPipeline(g);
        gen.process();
    }
}
Also used : AnalysisPipeline(org.antlr.v4.analysis.AnalysisPipeline) SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) CodeGenPipeline(org.antlr.v4.codegen.CodeGenPipeline) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATNFactory(org.antlr.v4.automata.ATNFactory) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory)

Example 100 with LexerGrammar

use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.

the class SymbolChecks method checkForModeConflicts.

public void checkForModeConflicts(Grammar g) {
    if (g.isLexer()) {
        LexerGrammar lexerGrammar = (LexerGrammar) g;
        for (String modeName : lexerGrammar.modes.keySet()) {
            if (!modeName.equals("DEFAULT_MODE") && reservedNames.contains(modeName)) {
                Rule rule = lexerGrammar.modes.get(modeName).iterator().next();
                g.tool.errMgr.grammarError(ErrorType.MODE_CONFLICTS_WITH_COMMON_CONSTANTS, g.fileName, rule.ast.parent.getToken(), modeName);
            }
            if (g.getTokenType(modeName) != Token.INVALID_TYPE) {
                Rule rule = lexerGrammar.modes.get(modeName).iterator().next();
                g.tool.errMgr.grammarError(ErrorType.MODE_CONFLICTS_WITH_TOKEN, g.fileName, rule.ast.parent.getToken(), modeName);
            }
        }
    }
}
Also used : Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) LexerGrammar(org.antlr.v4.tool.LexerGrammar)

Aggregations

LexerGrammar (org.antlr.v4.tool.LexerGrammar)444 Test (org.junit.Test)410 Grammar (org.antlr.v4.tool.Grammar)140 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)120 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)100 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)86 ATN (org.antlr.v4.runtime.atn.ATN)72 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)58 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)43 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)26 ParseTree (org.antlr.v4.runtime.tree.ParseTree)24 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)22 CharStream (org.antlr.v4.runtime.CharStream)20 TokenStream (org.antlr.v4.runtime.TokenStream)17 LexerATNSimulator (org.antlr.v4.runtime.atn.LexerATNSimulator)14 STGroupString (org.stringtemplate.v4.STGroupString)14 ArrayList (java.util.ArrayList)13 ATNFactory (org.antlr.v4.automata.ATNFactory)12 DFA (org.antlr.v4.runtime.dfa.DFA)12 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)12