Search in sources :

Example 6 with ATEToken

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

the class ElementRule method getAlternatives.

public List<List<ATEToken>> getAlternatives() {
    List<List<ATEToken>> alts = new ArrayList<List<ATEToken>>();
    List<ATEToken> alt = null;
    boolean findColon = true;
    int level = 0;
    for (ATEToken token : getTokens()) {
        if (findColon) {
            if (token.getAttribute().equals(":")) {
                findColon = false;
                alt = new ArrayList<ATEToken>();
            }
        } else {
            if (token.getAttribute().equals("("))
                level++;
            else if (token.getAttribute().equals(")"))
                level--;
            else if (level == 0) {
                // removed token.type != GrammarSyntaxLexer.TOKEN_BLOCK &&
                if (token.getAttribute().equals("|")) {
                    alts.add(alt);
                    alt = new ArrayList<ATEToken>();
                    continue;
                }
            }
            alt.add(token);
        }
    }
    if (alt != null && !alt.isEmpty()) {
        alts.add(alt);
    }
    return alts;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ATEToken(org.antlr.works.ate.syntax.misc.ATEToken)

Example 7 with ATEToken

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

the class GrammarSyntaxParser method matchScopeUse.

/**
     * Matches a scope reference in a rule definition:
     *
     * scope name (',' name)* ';'
     *
     * @return true if a scope is matched
     */
private boolean matchScopeUse() {
    if (!isID(0, "scope"))
        return false;
    mark();
    // Must begin with the keyword 'scope'
    ATEToken start = T(0);
    if (!matchID(0, "scope"))
        return false;
    // Match the first name
    if (matchID(0)) {
        // Loop over additional scopes
        while (matchChar(0, ",")) {
            // match an ID
            if (!matchID(0)) {
                rewind();
                return false;
            }
        }
        if (matchSEMI(0))
            return true;
    }
    rewind();
    return false;
}
Also used : ATEToken(org.antlr.works.ate.syntax.misc.ATEToken)

Example 8 with ATEToken

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

the class GrammarSyntaxParser method matchBlock.

private boolean matchBlock(String label) {
    if (label == null && !isID(0))
        return false;
    if (label != null && !isID(0, label))
        return false;
    mark();
    ATEToken start = T(0);
    int startIndex = getPosition();
    if (label == null) {
        if (!matchID(0))
            return false;
    } else {
        if (!matchID(0, label))
            return false;
    }
    ElementBlock block = new ElementBlock(start.getAttribute().toLowerCase(), start);
    ATEToken beginBlock = T(0);
    if (matchBalancedToken(ATESyntaxLexer.TOKEN_LCURLY, ATESyntaxLexer.TOKEN_RCURLY, block, true)) {
        beginBlock.type = GrammarSyntaxLexer.TOKEN_BLOCK_LIMIT;
        T(-1).type = GrammarSyntaxLexer.TOKEN_BLOCK_LIMIT;
        start.type = GrammarSyntaxLexer.TOKEN_BLOCK_LABEL;
        blocks.add(block);
        block.end = T(-1);
        block.internalTokens = new ArrayList<ATEToken>(getTokens().subList(startIndex, getPosition()));
        block.parse();
        if (block.isTokenBlock) {
            List<ATEToken> tokens = block.getDeclaredTokens();
            for (ATEToken lexerToken : tokens) {
                lexerToken.type = GrammarSyntaxLexer.TOKEN_DECL;
                addDeclaration(lexerToken);
            }
        }
        return true;
    }
    rewind();
    return false;
}
Also used : ATEToken(org.antlr.works.ate.syntax.misc.ATEToken)

Example 9 with ATEToken

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

the class GrammarSyntaxParser method tryMatchName.

private boolean tryMatchName() {
    ATEToken start = T(0);
    // Check if the grammar has a type (e.g. lexer, parser, tree, etc)
    if (ElementGrammarName.isKnownType(start.getAttribute())) {
        if (!nextToken())
            return false;
    }
    if (!matchID(0, "grammar"))
        return false;
    // After the type comes the name of the grammar
    ATEToken name = T(0);
    if (!nextToken())
        return false;
    // The next token must be a semi colon
    if (!matchSEMI(0))
        return false;
    this.name = new ElementGrammarName(name, start, T(-1), start);
    return true;
}
Also used : ATEToken(org.antlr.works.ate.syntax.misc.ATEToken)

Example 10 with ATEToken

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

the class GrammarSyntaxParser method tryMatchRule.

private boolean tryMatchRule() {
    ATEToken start = T(0);
    if (start == null)
        return false;
    // Match any modifiers
    if (ruleModifiers.contains(start.getAttribute())) {
        // skip the modifier
        if (!nextToken())
            return false;
    }
    // Match the name (it has to be an ID)
    ElementToken tokenName = (ElementToken) T(0);
    String name = tokenName.getAttribute();
    if (!matchID(0))
        return false;
    // Match any optional argument
    matchArguments();
    // Match any comments
    while (true) {
        if (matchSingleComment(0))
            continue;
        if (matchComplexComment(0))
            continue;
        break;
    }
    // Match any returns
    if (matchID(0, "returns")) {
        matchArguments();
    }
    // Match any optional "!"
    matchChar(0, "!");
    // Match any comments, scopes and blocks
    while (true) {
        if (matchScopeUse())
            continue;
        if (matchBlock())
            continue;
        if (matchSingleComment(0))
            continue;
        if (matchComplexComment(0))
            continue;
        if (isCOLON(0)) {
            // When a colon is matched, we are at the beginning of the content of the rule
            nextToken();
            break;
        } else {
            // Invalid rule matching
            return false;
        }
    }
    // Parse the content of the rule (after the ':')
    final ATEToken colonToken = T(-1);
    final int oldRefsSize = references.size();
    final int oldBlocksSize = blocks.size();
    final int oldActionsSize = actions.size();
    currentRule = new ElementRule(this, name, start, colonToken, null);
    labels.clear();
    while (true) {
        // Match the end of the rule
        if (matchEndOfRule(tokenName, oldRefsSize, oldBlocksSize, oldActionsSize))
            return true;
        // Match any block
        if (matchBlock(OPTIONS_BLOCK_NAME))
            continue;
        // Match any ST rewrite template
        if (matchRewriteTemplate())
            continue;
        // Match any assignment
        if (matchAssignment(labels))
            continue;
        // Match any internal reference
        if (matchInternalRefInRule())
            continue;
        // Match any action
        if (matchAction())
            continue;
        // Match node token
        if (matchSingleQuoteString(0) && matchOptionalNodeToken())
            continue;
        // Nothing matched, go to the next token
        if (!nextToken())
            return false;
    }
}
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