Search in sources :

Example 21 with ElementRule

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();
        }
    }
}
Also used : ElementRule(org.antlr.works.grammar.element.ElementRule)

Example 22 with ElementRule

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();
}
Also used : ElementRule(org.antlr.works.grammar.element.ElementRule)

Example 23 with ElementRule

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();
}
Also used : ElementReference(org.antlr.works.grammar.element.ElementReference) ElementRule(org.antlr.works.grammar.element.ElementRule)

Example 24 with ElementRule

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);
}
Also used : ElementRule(org.antlr.works.grammar.element.ElementRule)

Example 25 with ElementRule

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;
    }
}
Also used : ATEGutterItem(org.antlr.works.ate.gutter.ATEGutterItem) ElementRule(org.antlr.works.grammar.element.ElementRule)

Aggregations

ElementRule (org.antlr.works.grammar.element.ElementRule)30 ElementReference (org.antlr.works.grammar.element.ElementReference)3 ATEToken (org.antlr.works.ate.syntax.misc.ATEToken)2 IOException (java.io.IOException)1 Set (java.util.Set)1 ATEGutterItem (org.antlr.works.ate.gutter.ATEGutterItem)1 Usages (org.antlr.works.find.Usages)1 ElementAction (org.antlr.works.grammar.element.ElementAction)1 ElementGroup (org.antlr.works.grammar.element.ElementGroup)1 SDGenerator (org.antlr.works.visualization.SDGenerator)1