Search in sources :

Example 1 with BeetlParserException

use of org.beetl.core.exception.BeetlParserException in project beetl2.0 by javamonkey.

the class BeetlAntlrErrorStrategy method reportNoViableAlternative.

protected void reportNoViableAlternative(@NotNull Parser recognizer, @NotNull NoViableAltException e) {
    TokenStream tokens = recognizer.getInputStream();
    String input;
    if (tokens instanceof TokenStream) {
        if (e.getStartToken().getType() == Token.EOF)
            input = "<文件尾>";
        else
            input = tokens.getText(e.getStartToken(), e.getOffendingToken());
    } else {
        input = "<未知输入>";
    }
    BeetlException exception = null;
    if (keys.contains(e.getOffendingToken().getText())) {
        exception = new BeetlParserException(BeetlException.PARSER_VIABLE_ERROR, "不允许" + e.getOffendingToken().getText() + "关键出现在这里" + ":" + escapeWSAndQuote(input), e);
    } else {
        exception = new BeetlParserException(BeetlException.PARSER_VIABLE_ERROR, escapeWSAndQuote(input), e);
    }
    // String msg = "no viable alternative at input " + escapeWSAndQuote(input);
    exception.pushToken(this.getGrammarToken(e.getOffendingToken()));
    throw exception;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) TokenStream(org.antlr.v4.runtime.TokenStream) BeetlParserException(org.beetl.core.exception.BeetlParserException)

Example 2 with BeetlParserException

use of org.beetl.core.exception.BeetlParserException in project beetl2.0 by javamonkey.

the class ForStatement method execute.

public final void execute(Context ctx) {
    // idNode 是其后设置的
    int varIndex = ((IVarIndex) idNode).getVarIndex();
    Object collection = exp.evaluate(ctx);
    IteratorStatus it = null;
    if (collection == null) {
        if (!this.hasSafe) {
            BeetlException ex = new BeetlException(BeetlException.NULL);
            ex.pushToken(exp.token);
            throw ex;
        } else {
            it = new IteratorStatus(Collections.EMPTY_LIST);
        }
    } else {
        it = IteratorStatus.getIteratorStatusByType(collection, itType);
        if (it == null) {
            BeetlParserException ex = new BeetlParserException(BeetlParserException.COLLECTION_EXPECTED_ERROR, "实际类型是:" + collection.getClass());
            ex.pushToken(exp.token);
            throw ex;
        }
    }
    ctx.vars[varIndex + 1] = it;
    // 
    if (this.hasGoto) {
        while (it.hasNext()) {
            ctx.vars[varIndex] = it.next();
            forPart.execute(ctx);
            switch(ctx.gotoFlag) {
                case IGoto.NORMAL:
                    break;
                case IGoto.CONTINUE:
                    ctx.gotoFlag = IGoto.NORMAL;
                    continue;
                case IGoto.RETURN:
                    return;
                case IGoto.BREAK:
                    ctx.gotoFlag = IGoto.NORMAL;
                    return;
            }
        }
        if (!it.hasData()) {
            if (elseforPart != null)
                elseforPart.execute(ctx);
        }
        return;
    } else {
        while (it.hasNext()) {
            ctx.vars[varIndex] = it.next();
            forPart.execute(ctx);
        }
        if (!it.hasData()) {
            if (elseforPart != null)
                elseforPart.execute(ctx);
        }
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) BeetlParserException(org.beetl.core.exception.BeetlParserException) IteratorStatus(org.beetl.core.IteratorStatus)

Example 3 with BeetlParserException

use of org.beetl.core.exception.BeetlParserException in project beetl2.0 by javamonkey.

the class BeetlAntlrErrorStrategy method reportUnwantedToken.

protected void reportUnwantedToken(@NotNull Parser recognizer) {
    if (inErrorRecoveryMode(recognizer)) {
        return;
    }
    beginErrorCondition(recognizer);
    Token t = recognizer.getCurrentToken();
    String tokenName = getTokenErrorDisplay(t);
    IntervalSet expecting = getExpectedTokens(recognizer);
    String msg = "多余输入 " + tokenName + " 期望 " + expecting.toString(recognizer.getTokenNames());
    BeetlException exception = new BeetlParserException(BeetlException.PARSER_MISS_ERROR, msg);
    // exception.token = this.getGrammarToken(t);
    exception.pushToken(this.getGrammarToken(t));
    throw exception;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) BeetlParserException(org.beetl.core.exception.BeetlParserException) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) GrammarToken(org.beetl.core.statement.GrammarToken) Token(org.antlr.v4.runtime.Token)

Example 4 with BeetlParserException

use of org.beetl.core.exception.BeetlParserException in project beetl2.0 by javamonkey.

the class BeetlAntlrErrorStrategy method reportFailedPredicate.

protected void reportFailedPredicate(@NotNull Parser recognizer, @NotNull FailedPredicateException e) {
    String ruleName = recognizer.getRuleNames()[recognizer.getContext().getRuleIndex()];
    BeetlException exception = new BeetlParserException(BeetlException.PARSER_PREDICATE_ERROR, ruleName, e);
    // exception.token = this.getGrammarToken(e.getOffendingToken());
    exception.pushToken(this.getGrammarToken(e.getOffendingToken()));
    throw exception;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) BeetlParserException(org.beetl.core.exception.BeetlParserException)

Example 5 with BeetlParserException

use of org.beetl.core.exception.BeetlParserException in project beetl2.0 by javamonkey.

the class BeetlAntlrErrorStrategy method reportInputMismatch.

protected void reportInputMismatch(@NotNull Parser recognizer, @NotNull InputMismatchException e) {
    Token t1 = recognizer.getInputStream().LT(-1);
    String msg = "缺少输入在 " + getTokenErrorDisplay(t1) + " 后面, 期望 " + e.getExpectedTokens().toString(recognizer.getTokenNames());
    BeetlException exception = new BeetlParserException(BeetlException.PARSER_MISS_ERROR, msg, e);
    // exception.token = this.getGrammarToken(e.getOffendingToken());
    exception.pushToken(this.getGrammarToken(t1));
    throw exception;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) BeetlParserException(org.beetl.core.exception.BeetlParserException) GrammarToken(org.beetl.core.statement.GrammarToken) Token(org.antlr.v4.runtime.Token)

Aggregations

BeetlException (org.beetl.core.exception.BeetlException)6 BeetlParserException (org.beetl.core.exception.BeetlParserException)6 Token (org.antlr.v4.runtime.Token)3 GrammarToken (org.beetl.core.statement.GrammarToken)3 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)2 TokenStream (org.antlr.v4.runtime.TokenStream)1 IteratorStatus (org.beetl.core.IteratorStatus)1