Search in sources :

Example 1 with ElementGroup

use of org.antlr.works.grammar.element.ElementGroup in project antlrworks by antlr.

the class EditorRules method rebuildTree.

public void rebuildTree() {
    saveExpandedNodes();
    rememberSelectedTreeItem();
    rulesTreeRootNode.removeAllChildren();
    List<ElementRule> rules = getGrammarEngine().getRules();
    List<ElementGroup> groups = getGrammarEngine().getGroups();
    if (rules == null || groups == null)
        return;
    if (groups.isEmpty()) {
        buildTree(rulesTreeRootNode, rules, 0, rules.size() - 1);
    } else {
        Stack<DefaultMutableTreeNode> parentStack = new Stack<DefaultMutableTreeNode>();
        parentStack.add(rulesTreeRootNode);
        int ruleIndex = 0;
        for (ElementGroup group : groups) {
            DefaultMutableTreeNode parentNode = parentStack.peek();
            if (group.ruleIndex >= 0) {
                buildTree(parentNode, rules, ruleIndex, group.ruleIndex);
                ruleIndex = group.ruleIndex + 1;
            }
            if (group.openGroup) {
                DefaultMutableTreeNode node = new DefaultMutableTreeNode(new RuleTreeUserObject(group));
                parentNode.add(node);
                parentStack.push(node);
            } else {
                if (parentStack.size() > 1)
                    parentStack.pop();
            }
        }
        if (ruleIndex < rules.size()) {
            DefaultMutableTreeNode parentNode = parentStack.peek();
            buildTree(parentNode, rules, ruleIndex, rules.size() - 1);
        }
    }
    rulesTreeModel.reload();
    restoreSelectedTreeItem();
    restoreExpandedNodes();
}
Also used : ElementRule(org.antlr.works.grammar.element.ElementRule) ElementGroup(org.antlr.works.grammar.element.ElementGroup)

Example 2 with ElementGroup

use of org.antlr.works.grammar.element.ElementGroup in project antlrworks by antlr.

the class GrammarMenu method ungroup.

public void ungroup() {
    StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_RULE_UNGROUP);
    ElementGroup openGroup = window.editorRules.getSelectedGroup();
    if (openGroup == null) {
        // No open group selected in the tree. Try to find the closest open group
        // by moving backward
        openGroup = window.editorRules.findOpenGroupClosestToLocation(window.getTextPane().getSelectionStart());
        if (openGroup == null) {
            // Still no open group ? Give up
            XJAlert.display(window.getJavaContainer(), "Ungroup", "Cannot ungroup because no enclosing group has been found.");
            return;
        }
    }
    ElementGroup closingGroup = window.editorRules.findClosingGroupForGroup(openGroup);
    window.beginGroupChange("Ungroup");
    if (closingGroup != null) {
        // End of file is considered as a closing group but no group really exists
        // for that purpose
        ATEToken t = closingGroup.token;
        window.replaceText(t.getStartIndex() - 1, t.getEndIndex(), "");
    }
    ATEToken t = openGroup.token;
    window.replaceText(t.getStartIndex() - 1, t.getEndIndex(), "");
    window.endGroupChange();
}
Also used : ElementGroup(org.antlr.works.grammar.element.ElementGroup) ATEToken(org.antlr.works.ate.syntax.misc.ATEToken)

Example 3 with ElementGroup

use of org.antlr.works.grammar.element.ElementGroup in project antlrworks by antlr.

the class EditorRules method findOpenGroupClosestToLocation.

public ElementGroup findOpenGroupClosestToLocation(int location) {
    // Look backward into the list of groups
    List<ElementGroup> groups = getGrammarEngine().getGroups();
    if (groups == null || groups.isEmpty())
        return null;
    ElementGroup previous = null;
    for (ElementGroup group : groups) {
        if (!group.openGroup)
            continue;
        ATEToken t = group.token;
        if (t.getStartIndex() > location)
            break;
        previous = group;
    }
    return previous;
}
Also used : ElementGroup(org.antlr.works.grammar.element.ElementGroup) ATEToken(org.antlr.works.ate.syntax.misc.ATEToken)

Example 4 with ElementGroup

use of org.antlr.works.grammar.element.ElementGroup in project antlrworks by antlr.

the class EditorRules method findClosingGroupForGroup.

public ElementGroup findClosingGroupForGroup(ElementGroup group) {
    List<ElementGroup> groups = getGrammarEngine().getGroups();
    if (groups == null || groups.isEmpty())
        return null;
    int index = groups.indexOf(group) + 1;
    if (index == -1)
        return null;
    int open = 0;
    while (index < groups.size()) {
        ElementGroup g = groups.get(index);
        if (g.openGroup)
            open++;
        else if (open == 0)
            return g;
        else
            open--;
        index++;
    }
    return null;
}
Also used : ElementGroup(org.antlr.works.grammar.element.ElementGroup)

Aggregations

ElementGroup (org.antlr.works.grammar.element.ElementGroup)4 ATEToken (org.antlr.works.ate.syntax.misc.ATEToken)2 ElementRule (org.antlr.works.grammar.element.ElementRule)1