use of org.antlr.works.grammar.element.ElementRule in project antlrworks by antlr.
the class GrammarMenu method group.
public void group() {
StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_RULE_GROUP);
String s = (String) JOptionPane.showInputDialog(window.getJavaContainer(), "Group Name:", "Group", JOptionPane.QUESTION_MESSAGE, null, null, "Group");
if (s != null && s.length() > 0) {
List<ElementRule> rules = window.editorRules.getSelectedRules();
if (!rules.isEmpty()) {
window.beginGroupChange("Group");
ElementRule firstRule = rules.get(0);
ElementRule lastRule = rules.get(rules.size() - 1);
int end = lastRule.getEndIndex();
window.getTextEditor().insertText(end + 1, "\n" + GrammarSyntaxParser.END_GROUP + "\n");
int start = firstRule.getStartIndex();
window.getTextEditor().insertText(start - 1, "\n" + GrammarSyntaxParser.BEGIN_GROUP + s + "\n");
window.endGroupChange();
}
}
}
use of org.antlr.works.grammar.element.ElementRule in project antlrworks by antlr.
the class GrammarRefactorMenu method removeAllLeftRecursion.
public void removeAllLeftRecursion() {
StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_REMOVE_ALL_LEFT_RECURSION);
beginRefactor("Remove All Left Recursion");
List<ElementRule> rules = window.editorRules.getRules();
for (int index = rules.size() - 1; index >= 0; index--) {
ElementRule rule = rules.get(index);
if (rule.hasLeftRecursion()) {
String ruleText = rule.getTextRuleAfterRemovingLeftRecursion();
mutator.replace(rule.getInternalTokensStartIndex(), rule.getInternalTokensEndIndex(), ruleText);
}
}
endRefactor();
}
use of org.antlr.works.grammar.element.ElementRule in project antlrworks by antlr.
the class GrammarRefactorMenu method inlineRule.
protected void inlineRule(ElementRule rule) {
String oldContent = window.getText();
beginRefactor("Inline");
String ruleName = rule.name;
String ruleContent = Utils.trimString(oldContent.substring(rule.colon.getEndIndex(), rule.end.getStartIndex()));
List<ElementRule> rules = window.editorRules.getRules();
if (rule.end.index - rule.colon.index > 2) {
// More than one token, append ()
ruleContent = "(" + ruleContent + ")";
}
for (int r = rules.size() - 1; r >= 0; r--) {
ElementRule candidate = rules.get(r);
if (candidate == rule) {
mutator.delete(rule.getStartIndex(), rule.getEndIndex() + 1);
} else {
List<ElementReference> references = candidate.getReferences();
if (references == null)
continue;
for (int index = references.size() - 1; index >= 0; index--) {
ElementReference ref = references.get(index);
if (ref.token.getAttribute().equals(ruleName)) {
mutator.replace(ref.token.getStartIndex(), ref.token.getEndIndex(), ruleContent);
}
}
}
}
endRefactor();
}
use of org.antlr.works.grammar.element.ElementRule in project antlrworks by antlr.
the class TestParser method testIgnoreRules.
public void testIgnoreRules() throws Exception {
parseFile(TestConstants.PREFIX + "ignore_rules.g");
EditorRules.findTokensToIgnore(getEngine().getRules(), true);
int ignored = 0;
for (ElementRule r : getEngine().getRules()) {
if (r.ignored)
ignored++;
}
assertEquals("ignored rules", 3, ignored);
}
use of org.antlr.works.grammar.element.ElementRule in project antlrworks by antlr.
the class EditorGutterColumnManager method getGutterItems.
public List<ATEGutterItem> getGutterItems(String column) {
if (column.equals(RULES)) {
List<ATEGutterItem> items = new ArrayList<ATEGutterItem>();
List<ElementRule> rules = window.getGrammarEngine().getRules();
if (rules != null) {
for (ElementRule r : rules) {
items.add(r);
}
}
return items;
} else {
List<Integer> sortedKeys = new ArrayList<Integer>(breakpoints.keySet());
Collections.sort(sortedKeys);
List<ATEGutterItem> sortedItems = new ArrayList<ATEGutterItem>();
for (Integer k : sortedKeys) {
sortedItems.add(breakpoints.get(k));
}
return sortedItems;
}
}
Aggregations