Search in sources :

Example 36 with GrammarAST

use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.

the class Tool method parse.

public GrammarRootAST parse(String fileName, CharStream in) {
    try {
        GrammarASTAdaptor adaptor = new GrammarASTAdaptor(in);
        ToolANTLRLexer lexer = new ToolANTLRLexer(in, this);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        lexer.tokens = tokens;
        ToolANTLRParser p = new ToolANTLRParser(tokens, this);
        p.setTreeAdaptor(adaptor);
        try {
            ParserRuleReturnScope r = p.grammarSpec();
            GrammarAST root = (GrammarAST) r.getTree();
            if (root instanceof GrammarRootAST) {
                ((GrammarRootAST) root).hasErrors = lexer.getNumberOfSyntaxErrors() > 0 || p.getNumberOfSyntaxErrors() > 0;
                assert ((GrammarRootAST) root).tokenStream == tokens;
                if (grammarOptions != null) {
                    ((GrammarRootAST) root).cmdLineOptions = grammarOptions;
                }
                return ((GrammarRootAST) root);
            }
        } catch (v3TreeGrammarException e) {
            errMgr.grammarError(ErrorType.V3_TREE_GRAMMAR, fileName, e.location);
        }
        return null;
    } catch (RecognitionException re) {
        // TODO: do we gen errors now?
        ErrorManager.internalError("can't generate this message at moment; antlr recovers");
    }
    return null;
}
Also used : CommonTokenStream(org.antlr.runtime.CommonTokenStream) GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) ToolANTLRLexer(org.antlr.v4.parse.ToolANTLRLexer) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) org.antlr.v4.parse.v3TreeGrammarException(org.antlr.v4.parse.v3TreeGrammarException) ParserRuleReturnScope(org.antlr.runtime.ParserRuleReturnScope) ToolANTLRParser(org.antlr.v4.parse.ToolANTLRParser) RecognitionException(org.antlr.runtime.RecognitionException)

Example 37 with GrammarAST

use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.

the class LexerATNFactory method range.

@Override
public Handle range(GrammarAST a, GrammarAST b) {
    ATNState left = newState(a);
    ATNState right = newState(b);
    int t1 = CharSupport.getCharValueFromGrammarCharLiteral(a.getText());
    int t2 = CharSupport.getCharValueFromGrammarCharLiteral(b.getText());
    checkRange(a, b, t1, t2);
    left.addTransition(CodePointTransitions.createWithCodePointRange(right, t1, t2));
    a.atnState = left;
    b.atnState = left;
    return new Handle(left, right);
}
Also used : ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 38 with GrammarAST

use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.

the class LexerATNFactory method action.

protected Handle action(GrammarAST node, LexerAction lexerAction) {
    ATNState left = newState(node);
    ATNState right = newState(node);
    boolean isCtxDependent = false;
    int lexerActionIndex = getLexerActionIndex(lexerAction);
    ActionTransition a = new ActionTransition(right, currentRule.index, lexerActionIndex, isCtxDependent);
    left.addTransition(a);
    node.atnState = left;
    Handle h = new Handle(left, right);
    return h;
}
Also used : ActionTransition(org.antlr.v4.runtime.atn.ActionTransition) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 39 with GrammarAST

use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.

the class LexerATNFactory method lexerCallCommand.

@Override
public Handle lexerCallCommand(GrammarAST ID, GrammarAST arg) {
    LexerAction lexerAction = createLexerAction(ID, arg);
    if (lexerAction != null) {
        return action(ID, lexerAction);
    }
    // fall back to standard action generation for the command
    ST cmdST = codegenTemplates.getInstanceOf("Lexer" + CharSupport.capitalize(ID.getText()) + "Command");
    if (cmdST == null) {
        g.tool.errMgr.grammarError(ErrorType.INVALID_LEXER_COMMAND, g.fileName, ID.token, ID.getText());
        return epsilon(ID);
    }
    if (cmdST.impl.formalArguments == null || !cmdST.impl.formalArguments.containsKey("arg")) {
        g.tool.errMgr.grammarError(ErrorType.UNWANTED_LEXER_COMMAND_ARGUMENT, g.fileName, ID.token, ID.getText());
        return epsilon(ID);
    }
    cmdST.add("arg", arg.getText());
    cmdST.add("grammar", arg.g);
    return action(cmdST.render());
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) ActionAST(org.antlr.v4.tool.ast.ActionAST) TerminalAST(org.antlr.v4.tool.ast.TerminalAST) RangeAST(org.antlr.v4.tool.ast.RangeAST) ST(org.stringtemplate.v4.ST) LexerAction(org.antlr.v4.runtime.atn.LexerAction)

Example 40 with GrammarAST

use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.

the class LexerATNFactory method getSetFromCharSetLiteral.

public IntervalSet getSetFromCharSetLiteral(GrammarAST charSetAST) {
    String chars = charSetAST.getText();
    chars = chars.substring(1, chars.length() - 1);
    IntervalSet set = new IntervalSet();
    if (chars.length() == 0) {
        g.tool.errMgr.grammarError(ErrorType.EMPTY_STRINGS_AND_SETS_NOT_ALLOWED, g.fileName, charSetAST.getToken(), "[]");
        return set;
    }
    CharSetParseState state = CharSetParseState.NONE;
    int n = chars.length();
    for (int i = 0; i < n; ) {
        if (state.mode == CharSetParseState.Mode.ERROR) {
            return new IntervalSet();
        }
        int c = chars.codePointAt(i);
        int offset = Character.charCount(c);
        if (c == '\\') {
            EscapeSequenceParsing.Result escapeParseResult = EscapeSequenceParsing.parseEscape(chars, i);
            switch(escapeParseResult.type) {
                case INVALID:
                    g.tool.errMgr.grammarError(ErrorType.INVALID_ESCAPE_SEQUENCE, g.fileName, charSetAST.getToken(), charSetAST.getText());
                    state = CharSetParseState.ERROR;
                    break;
                case CODE_POINT:
                    state = applyPrevStateAndMoveToCodePoint(charSetAST, set, state, escapeParseResult.codePoint);
                    break;
                case PROPERTY:
                    state = applyPrevStateAndMoveToProperty(charSetAST, set, state, escapeParseResult.propertyIntervalSet);
                    break;
            }
            offset = escapeParseResult.parseLength;
        } else if (c == '-' && !state.inRange && i != 0 && i != n - 1) {
            if (state.mode == CharSetParseState.Mode.PREV_PROPERTY) {
                g.tool.errMgr.grammarError(ErrorType.UNICODE_PROPERTY_NOT_ALLOWED_IN_RANGE, g.fileName, charSetAST.getToken(), charSetAST.getText());
                state = CharSetParseState.ERROR;
            } else {
                state = new CharSetParseState(state.mode, true, state.prevCodePoint, state.prevProperty);
            }
        } else {
            state = applyPrevStateAndMoveToCodePoint(charSetAST, set, state, c);
        }
        i += offset;
    }
    if (state.mode == CharSetParseState.Mode.ERROR) {
        return new IntervalSet();
    }
    // Whether or not we were in a range, we'll add the last code point found to the set.
    applyPrevState(charSetAST, set, state);
    return set;
}
Also used : EscapeSequenceParsing(org.antlr.v4.misc.EscapeSequenceParsing) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet)

Aggregations

GrammarAST (org.antlr.v4.tool.ast.GrammarAST)55 Rule (org.antlr.v4.tool.Rule)20 ATNState (org.antlr.v4.runtime.atn.ATNState)15 ArrayList (java.util.ArrayList)12 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)12 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)12 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)12 Grammar (org.antlr.v4.tool.Grammar)8 Decl (org.antlr.v4.codegen.model.decl.Decl)7 ActionAST (org.antlr.v4.tool.ast.ActionAST)7 AltAST (org.antlr.v4.tool.ast.AltAST)7 TerminalAST (org.antlr.v4.tool.ast.TerminalAST)7 LinkedHashMap (java.util.LinkedHashMap)6 Token (org.antlr.runtime.Token)6 RuleAST (org.antlr.v4.tool.ast.RuleAST)6 HashMap (java.util.HashMap)5 AddToLabelList (org.antlr.v4.codegen.model.AddToLabelList)5 Pair (org.antlr.v4.runtime.misc.Pair)5 LexerGrammar (org.antlr.v4.tool.LexerGrammar)5 GrammarASTWithOptions (org.antlr.v4.tool.ast.GrammarASTWithOptions)5