use of org.antlr.tool.Grammar in project antlrworks by antlr.
the class TokensDFA method getDOTString.
@Override
public String getDOTString() throws Exception {
ANTLRGrammarEngine eg = window.getGrammarEngine().getANTLRGrammarEngine();
eg.analyze();
Grammar g = eg.getLexerGrammar();
if (g == null) {
throw new Exception("Cannot show tokens DFA because there is no lexer grammar");
}
Rule r = g.getRule(Grammar.ARTIFICIAL_TOKENS_RULENAME);
NFAState s = (NFAState) r.startState.transition(0).target;
DFA dfa = g.getLookaheadDFA(s.getDecisionNumber());
DOTGenerator dg = new DOTGenerator(g);
dg.setArrowheadType("none");
// Left-to-right
dg.setRankdir("LR");
return dg.getDOT(dfa.startState);
}
use of org.antlr.tool.Grammar in project antlrworks by antlr.
the class DecisionDFAEngine method getDecisionDFAItems.
public List<DecisionDFAItem> getDecisionDFAItems() {
List<DecisionDFAItem> items = new ArrayList<DecisionDFAItem>();
for (int lineIndex : decisionDFA.keySet()) {
for (int columnIndex : decisionDFA.get(lineIndex)) {
DFA dfa = getDFAAtPosition(lineIndex, columnIndex);
if (dfa == null) {
System.err.println("DFA is null for line " + lineIndex + " and column " + columnIndex);
continue;
}
Grammar g = discoveredLexerGrammar;
if (g != null) {
Rule r = g.getRule(Grammar.ARTIFICIAL_TOKENS_RULENAME);
NFAState s = (NFAState) r.startState.transition(0).target;
if (s == null) {
System.err.println("NFAState s is null for rule " + r.name);
continue;
}
// Ignore tokens DFA
if (dfa.getDecisionNumber() == s.getDecisionNumber())
continue;
}
Color c = new Color(0, 128, 64);
String title = "DFA decision " + dfa.getDecisionNumber();
String info = "";
if (usesSemPreds.contains(dfa.getDecisionNumber())) {
info += "uses semantic predicate";
c = new Color(255, 220, 0);
} else if (usesSynPreds.contains(dfa.getDecisionNumber())) {
info += "uses syntactic predicate";
c = new Color(255, 220, 0);
}
if (dfa.isCyclic()) {
if (info.length() > 0)
info += ", ";
info += "cyclic";
}
if (info.length() > 0)
info += ", ";
if (dfa.getNumberOfStates() != 0) {
info += dfa.getNumberOfStates() + " states";
} else {
info += "<=" + dfa.getMaxStateNumber() + " states";
}
Point p = window.textEditor.getLineTextPositionsAtLineIndex(lineIndex - 1);
if (p != null) {
DecisionDFAItem item = new DecisionDFAItem(window);
item.setAttributes(null, p.x + columnIndex - 1, p.x + columnIndex, lineIndex - 1, c, title + " (" + info + ")");
item.shape = ATEOverlayManager.SHAPE_RECT;
items.add(item);
}
}
}
return items;
}
use of org.antlr.tool.Grammar in project antlrworks by antlr.
the class GrammarEngineImpl method getGeneratedClassName.
public String getGeneratedClassName(int type) throws Exception {
String name = null;
antlrEngine.createGrammars();
if (type == ElementGrammarName.LEXER) {
Grammar g = antlrEngine.getLexerGrammar();
if (g == null)
return null;
name = g.name + getSuffix(type);
} else if (type == ElementGrammarName.PARSER) {
Grammar g = antlrEngine.getParserGrammar();
if (g == null)
return null;
name = g.name + getSuffix(type);
} else if (type == ElementGrammarName.TREEPARSER) {
Grammar g = antlrEngine.getParserGrammar();
if (g == null)
return null;
if (!isTreeParserGrammar())
return null;
name = g.name + getSuffix(type);
}
return name;
}
use of org.antlr.tool.Grammar in project antlrworks by antlr.
the class InterpreterTab method process.
protected void process() {
progress.setInfo("Interpreting...");
window.consoleTab.println("Interpreting...");
CharStream input = new ANTLRStringStream(Utils.convertRawTextWithEOL(textPane.getText(), eolCombo));
ANTLRGrammarEngine eg = window.getGrammarEngine().getANTLRGrammarEngine();
try {
eg.createGrammars();
} catch (Exception e) {
window.consoleTab.println(e);
return;
}
Grammar parser = eg.getParserGrammar();
Grammar lexer = eg.getLexerGrammar();
if (lexer == null) {
throw new RuntimeException("Lexer is null. Check the grammar before running the interpreterTab.");
}
Interpreter lexEngine = new CustomInterpreter(lexer, input);
FilteringTokenStream tokens = new FilteringTokenStream(lexEngine);
StringTokenizer tk = new StringTokenizer(tokensToIgnoreLabel.getText(), " ");
while (tk.hasMoreTokens()) {
String tokenName = tk.nextToken();
tokens.setTokenTypeChannel(lexer.getTokenType(tokenName), Token.HIDDEN_CHANNEL);
}
Interpreter parseEngine = new CustomInterpreter(parser, tokens);
ParseTree t = null;
try {
if (ATEToken.isLexerName(startSymbol)) {
t = lexEngine.parse(startSymbol);
} else {
t = parseEngine.parse(startSymbol);
}
} catch (Exception e) {
window.consoleTab.println(e);
}
if (parser != null && t != null) {
SwingUtilities.invokeLater(new Refresh(parser, t));
}
}
use of org.antlr.tool.Grammar in project antlrworks by antlr.
the class GrammarPropertiesImpl method getAllGeneratedNames.
public List<String> getAllGeneratedNames() throws Exception {
List<String> names = new ArrayList<String>();
Grammar g = antlrEngine.getDefaultGrammar();
if (g != null) {
names.add(g.getRecognizerName());
for (Grammar gd : g.getDelegates()) {
names.add(gd.getRecognizerName());
}
}
Grammar lexer = antlrEngine.getLexerGrammar();
if (lexer != null) {
names.add(lexer.getRecognizerName());
for (Grammar gd : lexer.getDelegates()) {
names.add(gd.getRecognizerName());
}
}
return names;
}
Aggregations