Search in sources :

Example 1 with ATEToken

use of org.antlr.works.ate.syntax.misc.ATEToken in project antlrworks by antlr.

the class ATERenderingView method findStartingTokenIndex.

/** This method finds the first token that is located in the line index p0
     *
     * @param p0
     * @param low
     * @param high
     * @param candidate
     * @return The index of the first token on line p0
     */
private int findStartingTokenIndex(int p0, int low, int high, int candidate) {
    if (Math.abs(high - low) <= 1)
        return Math.min(candidate, low);
    final int middle = low + (high - low) / 2;
    final ATEToken t = tokens.get(middle);
    if (p0 >= t.startLineIndex && p0 <= t.endLineIndex) {
        return findStartingTokenIndex(p0, low, middle, middle);
    } else {
        if (t.startLineIndex < p0)
            return findStartingTokenIndex(p0, middle, high, candidate);
        else
            return findStartingTokenIndex(p0, low, middle, candidate);
    }
}
Also used : ATEToken(org.antlr.works.ate.syntax.misc.ATEToken)

Example 2 with ATEToken

use of org.antlr.works.ate.syntax.misc.ATEToken in project antlrworks by antlr.

the class ATERenderingView method renderText.

/**
     * This method renders the text using the token information to set up the display attribute
     * of each token.
     *
     */
protected int renderText(TextOperation action, Graphics g, int x, int y, int p0, int p1) throws BadLocationException {
    if (p0 == p1)
        return x;
    if (!textEditor.isSyntaxColoring()) {
        return super.drawUnselectedText(g, x, y, p0, p1);
    }
    // Note: the tokens are not contiguous (e.g. white spaces are ignored)
    final Document doc = getDocument();
    final ATESyntaxEngine engine = textEditor.getParserEngine();
    tokens = engine.getTokens();
    int p = p0;
    final int start = findStartingTokenIndex(p0, 0, tokens.size(), 0);
    for (int i = start; i < tokens.size(); i++) {
        ATEToken t = tokens.get(i);
        AttributeSet attribute = engine.getAttributeForToken(t);
        if (t.start >= p0 && t.start <= p1) {
            // Fill any non-contiguous token with default color
            if (t.start > p) {
                x = action.renderTextPortion(g, x, y, p, t.start, p1, doc, null);
            }
            x = action.renderTextPortion(g, x, y, t.start, t.end, p1, doc, attribute);
            p = t.end;
        } else if (t.end >= p0 && t.start < p0) {
            x = action.renderTextPortion(g, x, y, p0, t.end, p1, doc, attribute);
            p = t.end;
        } else if (t.start > p1) {
            break;
        }
    }
    // Fill any remaining range with default color
    if (p < p1) {
        x = action.renderTextPortion(g, x, y, p, p1, p1, doc, null);
    }
    return x;
}
Also used : ATEToken(org.antlr.works.ate.syntax.misc.ATEToken) ATESyntaxEngine(org.antlr.works.ate.syntax.generic.ATESyntaxEngine)

Example 3 with ATEToken

use of org.antlr.works.ate.syntax.misc.ATEToken in project antlrworks by antlr.

the class RefactorEngine method renameToken.

public boolean renameToken(ATEToken t, String name) {
    String attr = t.getAttribute();
    boolean renameRefRule = t.type == GrammarSyntaxLexer.TOKEN_REFERENCE || t.type == GrammarSyntaxLexer.TOKEN_DECL;
    for (int index = tokens.size() - 1; index > 0; index--) {
        ATEToken token = tokens.get(index);
        if (!token.getAttribute().equals(attr))
            continue;
        if (token.type == t.type || renameRefRule && (token.type == GrammarSyntaxLexer.TOKEN_REFERENCE || token.type == GrammarSyntaxLexer.TOKEN_DECL)) {
            mutator.replace(token.getStartIndex(), token.getEndIndex(), name);
        }
    }
    return true;
}
Also used : ATEToken(org.antlr.works.ate.syntax.misc.ATEToken)

Example 4 with ATEToken

use of org.antlr.works.ate.syntax.misc.ATEToken in project antlrworks by antlr.

the class EditorInspector method discoverInvalidCharLiteralTokens.

protected void discoverInvalidCharLiteralTokens(List<EditorInspectorItem> items) {
    List<ATEToken> tokens = engine.getTokens();
    if (tokens == null)
        return;
    for (ATEToken t : tokens) {
        if (t.type == ATESyntaxLexer.TOKEN_DOUBLE_QUOTE_STRING) {
            if (RefactorEngine.ignoreScopeForDoubleQuoteLiteral(t.scope))
                continue;
            EditorInspectorItem item = new ItemInvalidCharLiteral();
            item.setAttributes(t, t.getStartIndex(), t.getEndIndex(), t.startLineNumber, Color.red, "Invalid character literal '" + t.getAttribute() + "' - must use single quote");
            items.add(item);
        }
    }
}
Also used : ATEToken(org.antlr.works.ate.syntax.misc.ATEToken)

Example 5 with ATEToken

use of org.antlr.works.ate.syntax.misc.ATEToken in project antlrworks by antlr.

the class ElementBlock method parseForTokenVocab.

/**
     * Parses for the tokenVocab key/value pair of a block of type 'options'
     */
private void parseForTokenVocab() {
    for (int index = 0; index < internalTokens.size(); index++) {
        ATEToken t = internalTokens.get(index);
        if (t.getAttribute().equals("tokenVocab") && index + 2 < internalTokens.size()) {
            t = internalTokens.get(index + 2);
            tokenVocab = t.getAttribute();
        }
    }
}
Also used : ATEToken(org.antlr.works.ate.syntax.misc.ATEToken)

Aggregations

ATEToken (org.antlr.works.ate.syntax.misc.ATEToken)42 ElementGroup (org.antlr.works.grammar.element.ElementGroup)2 ElementRule (org.antlr.works.grammar.element.ElementRule)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 BadLocationException (javax.swing.text.BadLocationException)1 ATESyntaxEngine (org.antlr.works.ate.syntax.generic.ATESyntaxEngine)1 ATESyntaxParser (org.antlr.works.ate.syntax.generic.ATESyntaxParser)1 Usages (org.antlr.works.find.Usages)1 ElementAction (org.antlr.works.grammar.element.ElementAction)1 ElementBlock (org.antlr.works.grammar.element.ElementBlock)1 GrammarSyntaxLexer (org.antlr.works.grammar.syntax.GrammarSyntaxLexer)1 ElementTemplateRule (org.antlr.works.stringtemplate.element.ElementTemplateRule)1