Search in sources :

Example 1 with ElementReference

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

the class GrammarWindow method autoCompletionMenuGetMatchingWordsForPartialWord.

/** AutoCompletionMenuDelegate method: return the list of corresponding words
     *  given a partial word
     */
public List<String> autoCompletionMenuGetMatchingWordsForPartialWord(String partialWord) {
    if (grammarEngine.getNumberOfRules() == 0) {
        return null;
    }
    partialWord = partialWord.toLowerCase();
    List<String> matchingRules = new ArrayList<String>();
    if (editorRules.isRuleAtIndex(getCaretPosition())) {
        // Inside a rule - show all rules in alphabetical order
        List<ElementRule> sortedRules = Collections.list(Collections.enumeration(grammarEngine.getRules()));
        Collections.sort(sortedRules, new Comparator<ElementRule>() {

            public int compare(ElementRule o1, ElementRule o2) {
                return o1.name.compareToIgnoreCase(o2.name);
            }
        });
        for (ElementRule rule : sortedRules) {
            if (rule.name.toLowerCase().startsWith(partialWord) && !matchingRules.contains(rule.name))
                matchingRules.add(rule.name);
        }
    } else {
        // Not inside rule - show only undefined rules
        List<ElementReference> sortedUndefinedReferences = Collections.list(Collections.enumeration(grammarEngine.getUndefinedReferences()));
        Collections.sort(sortedUndefinedReferences, new Comparator<ElementReference>() {

            public int compare(ElementReference o1, ElementReference o2) {
                return o1.rule.name.compareToIgnoreCase(o2.rule.name);
            }
        });
        for (ElementReference ref : sortedUndefinedReferences) {
            String attr = ref.token.getAttribute();
            if (attr.toLowerCase().startsWith(partialWord) && !attr.equals(partialWord) && !matchingRules.contains(attr)) {
                matchingRules.add(attr);
            }
        }
    }
    return matchingRules;
}
Also used : ElementReference(org.antlr.works.grammar.element.ElementReference) ElementRule(org.antlr.works.grammar.element.ElementRule)

Example 2 with ElementReference

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

the class RulesDependency method willLaunch.

@Override
protected boolean willLaunch() {
    if (!checkForCurrentRule())
        return false;
    ElementRule rule = window.getCurrentRule();
    List<ElementReference> refs = window.editorRules.getReferencesInRule(rule);
    if (refs == null || refs.isEmpty()) {
        XJAlert.display(window.getJavaContainer(), "Error", "The selected rule doesn't contain any references");
        return false;
    }
    includeLexerRefs = true;
    if (!rule.lexer && window.getGrammarEngine().isCombinedGrammar()) {
        includeLexerRefs = XJAlert.displayAlertYESNO(window.getJavaContainer(), "Rule Dependency Graph", "Do you want to include lexer references ?") == XJAlert.YES;
    }
    return true;
}
Also used : ElementReference(org.antlr.works.grammar.element.ElementReference) ElementRule(org.antlr.works.grammar.element.ElementRule)

Example 3 with ElementReference

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

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

the class RulesDependency method buildGraph.

protected void buildGraph(ElementRule rule) {
    if (rule == null)
        return;
    visitedRules.add(rule.name);
    List<ElementReference> refs = window.editorRules.getReferencesInRule(rule);
    if (refs == null || refs.isEmpty())
        return;
    for (ElementReference reference : refs) {
        String refRuleName = reference.token.getAttribute();
        String visitedRef = rule.name + " -> " + refRuleName;
        if (visitedRefs.contains(visitedRef))
            continue;
        if (ATEToken.isLexerName(reference.token.getAttribute()) && !includeLexerRefs)
            continue;
        visitedRefs.add(visitedRef);
        dependency.append(visitedRef);
        dependency.append(";\n");
        if (!visitedRules.contains(refRuleName))
            buildGraph(window.getGrammarEngine().getRuleWithName(refRuleName));
    }
}
Also used : ElementReference(org.antlr.works.grammar.element.ElementReference)

Aggregations

ElementReference (org.antlr.works.grammar.element.ElementReference)4 ElementRule (org.antlr.works.grammar.element.ElementRule)3