Search in sources :

Example 76 with RecognitionException

use of org.antlr.runtime.RecognitionException in project Palladio-Editors-Sirius by PalladioSimulator.

the class PCMServices method validExpression.

/**
 * Parses an stochastic expression to determine whether it is valid.
 *
 * @param the
 *            expressionString
 * @return the validity
 */
private boolean validExpression(final String expressionString) {
    final MyPCMStoExLexer lexer = new MyPCMStoExLexer(new ANTLRStringStream(expressionString));
    final MyPCMStoExParser parser = new MyPCMStoExParser(new CommonTokenStream(lexer));
    try {
        parser.expression();
    } catch (final RecognitionException e1) {
        return false;
    }
    if (lexer.hasErrors() || parser.hasErrors()) {
        return false;
    }
    return true;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) MyPCMStoExParser(org.palladiosimulator.pcm.stochasticexpressions.parser.MyPCMStoExParser) CommonTokenStream(org.antlr.runtime.CommonTokenStream) MyPCMStoExLexer(org.palladiosimulator.pcm.stochasticexpressions.parser.MyPCMStoExLexer) RecognitionException(org.antlr.runtime.RecognitionException)

Example 77 with RecognitionException

use of org.antlr.runtime.RecognitionException in project hive by apache.

the class ParseDriver method parseTriggerExpression.

public ASTNode parseTriggerExpression(String command) throws ParseException {
    HiveLexerX lexer = new HiveLexerX(new ANTLRNoCaseStringStream(command));
    TokenRewriteStream tokens = new TokenRewriteStream(lexer);
    HiveParser parser = new HiveParser(tokens);
    parser.setTreeAdaptor(adaptor);
    HiveParser_ResourcePlanParser.triggerExpressionStandalone_return r = null;
    try {
        r = parser.gResourcePlanParser.triggerExpressionStandalone();
    } catch (RecognitionException e) {
        e.printStackTrace();
        throw new ParseException(parser.errors);
    }
    if (lexer.getErrors().size() != 0) {
        throw new ParseException(lexer.getErrors());
    } else if (parser.errors.size() != 0) {
        throw new ParseException(parser.errors);
    }
    return r.getTree();
}
Also used : TokenRewriteStream(org.antlr.runtime.TokenRewriteStream) RecognitionException(org.antlr.runtime.RecognitionException)

Example 78 with RecognitionException

use of org.antlr.runtime.RecognitionException in project hive by apache.

the class ParseDriver method parse.

/**
 * Parses a command, optionally assigning the parser's token stream to the
 * given context.
 *
 * @param command
 *          command to parse
 *
 * @param ctx
 *          context with which to associate this parser's token stream, or
 *          null if either no context is available or the context already has
 *          an existing stream
 *
 * @return parsed AST
 */
public ASTNode parse(String command, Context ctx, String viewFullyQualifiedName) throws ParseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Parsing command: " + command);
    }
    HiveLexerX lexer = new HiveLexerX(new ANTLRNoCaseStringStream(command));
    TokenRewriteStream tokens = new TokenRewriteStream(lexer);
    if (ctx != null) {
        if (viewFullyQualifiedName == null) {
            // Top level query
            ctx.setTokenRewriteStream(tokens);
        } else {
            // It is a view
            ctx.addViewTokenRewriteStream(viewFullyQualifiedName, tokens);
        }
        lexer.setHiveConf(ctx.getConf());
    }
    HiveParser parser = new HiveParser(tokens);
    if (ctx != null) {
        parser.setHiveConf(ctx.getConf());
    }
    parser.setTreeAdaptor(adaptor);
    HiveParser.statement_return r = null;
    try {
        r = parser.statement();
    } catch (RecognitionException e) {
        e.printStackTrace();
        throw new ParseException(parser.errors);
    }
    if (lexer.getErrors().size() == 0 && parser.errors.size() == 0) {
        LOG.debug("Parse Completed");
    } else if (lexer.getErrors().size() != 0) {
        throw new ParseException(lexer.getErrors());
    } else {
        throw new ParseException(parser.errors);
    }
    ASTNode tree = (ASTNode) r.getTree();
    tree.setUnknownTokenBoundaries();
    return tree;
}
Also used : TokenRewriteStream(org.antlr.runtime.TokenRewriteStream) RecognitionException(org.antlr.runtime.RecognitionException)

Example 79 with RecognitionException

use of org.antlr.runtime.RecognitionException in project hive by apache.

the class PartFilterExprUtil method getFilterParser.

public static FilterParser getFilterParser(String filter) throws MetaException {
    FilterLexer lexer = new FilterLexer(new ANTLRNoCaseStringStream(filter));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    FilterParser parser = new FilterParser(tokens);
    try {
        parser.filter();
    } catch (RecognitionException re) {
        throw new MetaException("Error parsing partition filter; lexer error: " + lexer.errorMsg + "; exception " + re);
    }
    if (lexer.errorMsg != null) {
        throw new MetaException("Error parsing partition filter : " + lexer.errorMsg);
    }
    return parser;
}
Also used : CommonTokenStream(org.antlr.runtime.CommonTokenStream) FilterParser(org.apache.hadoop.hive.metastore.parser.FilterParser) RecognitionException(org.antlr.runtime.RecognitionException) FilterLexer(org.apache.hadoop.hive.metastore.parser.FilterLexer) ANTLRNoCaseStringStream(org.apache.hadoop.hive.metastore.parser.ExpressionTree.ANTLRNoCaseStringStream) MetaException(org.apache.hadoop.hive.metastore.api.MetaException)

Example 80 with RecognitionException

use of org.antlr.runtime.RecognitionException in project xtext-core by eclipse.

the class AbstractInternalAntlrParser method recoverFromMismatchedToken.

@Override
protected Object recoverFromMismatchedToken(IntStream input, int ttype, BitSet follow) throws RecognitionException {
    RecognitionException e = null;
    // if next token is what we are looking for then "delete" this token
    if (mismatchIsUnwantedToken(input, ttype)) {
        e = new UnwantedTokenException(ttype, input);
        /*
			System.err.println("recoverFromMismatchedToken deleting "+
							   ((TokenStream)input).LT(1)+
							   " since "+((TokenStream)input).LT(2)+" is what we want");
			 */
        beginResync();
        // simply delete extra token
        input.consume();
        endResync();
        // report after consuming so AW sees the token in the exception
        reportError(e);
        // we want to return the token we're actually matching
        Object matchedSymbol = getCurrentInputSymbol(input);
        // move past ttype token as if all were ok
        input.consume();
        return matchedSymbol;
    }
    // can't recover with single token deletion, try insertion
    if (mismatchIsMissingToken(input, follow)) {
        Object inserted = getMissingSymbol(input, e, ttype, follow);
        e = new MissingTokenException(ttype, input, inserted);
        // report after inserting so AW sees the token in the exception
        reportError(e);
        return null;
    // throw e;
    }
    // even that didn't work; must throw the exception
    e = new MismatchedTokenException(ttype, input);
    throw e;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) MismatchedTokenException(org.antlr.runtime.MismatchedTokenException) UnwantedTokenException(org.antlr.runtime.UnwantedTokenException) MissingTokenException(org.antlr.runtime.MissingTokenException) RecognitionException(org.antlr.runtime.RecognitionException)

Aggregations

RecognitionException (org.antlr.runtime.RecognitionException)257 Token (org.antlr.runtime.Token)169 CommonTree (org.antlr.runtime.tree.CommonTree)132 RewriteRuleSubtreeStream (org.antlr.runtime.tree.RewriteRuleSubtreeStream)98 RewriteRuleTokenStream (org.antlr.runtime.tree.RewriteRuleTokenStream)74 CommonToken (org.antlr.runtime.CommonToken)48 NoViableAltException (org.antlr.runtime.NoViableAltException)43 RewriteEarlyExitException (org.antlr.runtime.tree.RewriteEarlyExitException)21 ParserRuleReturnScope (org.antlr.runtime.ParserRuleReturnScope)14 CommonTokenStream (org.antlr.runtime.CommonTokenStream)11 EarlyExitException (org.antlr.runtime.EarlyExitException)10 MismatchedSetException (org.antlr.runtime.MismatchedSetException)10 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)8 MismatchedTokenException (org.antlr.runtime.MismatchedTokenException)7 TokenRewriteStream (org.antlr.runtime.TokenRewriteStream)7 FailedPredicateException (org.antlr.runtime.FailedPredicateException)6 MissingTokenException (org.antlr.runtime.MissingTokenException)6 UnwantedTokenException (org.antlr.runtime.UnwantedTokenException)6 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)5 HashMap (java.util.HashMap)4