Search in sources :

Example 11 with ElementRule

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

the class EditorRules method findTokensToIgnore.

public static void findTokensToIgnore(List<ElementRule> rules, boolean reset) {
    for (ElementRule rule : rules) {
        if (reset)
            rule.ignored = false;
        List<ElementAction> actions = rule.getActions();
        if (actions == null || actions.isEmpty())
            continue;
        for (ElementAction action : actions) {
            List<ATEToken> tokens = action.getTokens();
            for (int t = 0; t < tokens.size(); t++) {
                ATEToken token = tokens.get(t);
                /* the 'channel' token can be either an ID or a reference if a rule in the grammar has the name
                   'channel' */
                if ((token.type == ATESyntaxLexer.TOKEN_ID || token.type == GrammarSyntaxLexer.TOKEN_REFERENCE) && token.getAttribute().equals("channel") && t + 3 < tokens.size()) {
                    ATEToken t1 = tokens.get(t + 1);
                    ATEToken t2 = tokens.get(t + 2);
                    if (t1.type != ATESyntaxLexer.TOKEN_CHAR || !t1.getAttribute().equals("="))
                        continue;
                    if (t2.type != ATESyntaxLexer.TOKEN_ID || !t2.getAttribute().equals("HIDDEN"))
                        continue;
                    rule.ignored = true;
                    break;
                }
                if (token.type == GrammarSyntaxLexer.TOKEN_ID && token.getAttribute().equals("skip")) {
                    // Take skip() into account only if it is the only token in the block
                    if (tokens.size() == 5 && t == 1) {
                        rule.ignored = true;
                        break;
                    }
                }
            }
        }
    }
}
Also used : ElementAction(org.antlr.works.grammar.element.ElementAction) ElementRule(org.antlr.works.grammar.element.ElementRule) ATEToken(org.antlr.works.ate.syntax.misc.ATEToken)

Example 12 with ElementRule

use of org.antlr.works.grammar.element.ElementRule 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 13 with ElementRule

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

the class EditorRules method getRulesStartingWith.

public List<String> getRulesStartingWith(String match) {
    List<String> matches = new ArrayList<String>();
    if (getGrammarEngine().getRules() == null)
        return matches;
    List<ElementRule> rules = getGrammarEngine().getRules();
    for (ElementRule r : rules) {
        String rname = r.name.toLowerCase();
        if (rname.startsWith(match) && !matches.contains(r.name))
            matches.add(r.name);
    }
    return matches;
}
Also used : ElementRule(org.antlr.works.grammar.element.ElementRule)

Example 14 with ElementRule

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

the class InterpreterTab method updateRulesCombo.

public void updateRulesCombo(List<ElementRule> rules) {
    Object selectedItem = rulesCombo.getSelectedItem();
    rulesCombo.removeAllItems();
    if (rules != null) {
        for (ElementRule rule : rules) {
            rulesCombo.addItem(rule.toString());
        }
    }
    if (selectedItem != null)
        rulesCombo.setSelectedItem(selectedItem);
}
Also used : ElementRule(org.antlr.works.grammar.element.ElementRule)

Example 15 with ElementRule

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

the class ExportMenu method exportAllRules.

public void exportAllRules(boolean asImage) {
    List<String> extensions = null;
    if (asImage) {
        extensions = lookupAvailableImageFormat();
    }
    if (!XJFileChooser.shared().displayChooseDirectory(window.getJavaContainer(), extensions, extensions, !asImage)) {
        return;
    }
    String directory = XJFileChooser.shared().getSelectedFilePath();
    String extension = XJFileChooser.shared().getSelectedFileExtension();
    SDGenerator sd = new SDGenerator(window.getGrammarEngine());
    for (ElementRule rule : window.getRules()) {
        try {
            if (asImage) {
                sd.renderRuleToBitmapFile(rule.name, extension, XJUtils.concatPath(directory, rule.name + "." + extension));
            } else {
                sd.renderRuleToEPSFile(rule.name, XJUtils.concatPath(directory, rule.name + ".eps"));
            }
        } catch (Exception e) {
            XJAlert.display(window.getJavaContainer(), "Error", "Images cannot be saved because:\n" + e);
        }
    }
}
Also used : SDGenerator(org.antlr.works.visualization.SDGenerator) ElementRule(org.antlr.works.grammar.element.ElementRule) IOException(java.io.IOException)

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