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)");
}
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;
}
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;
}
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();
}
}
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);
}
}
}
}
Aggregations