org.antlr.v4.runtime.misc.ParseCancellationException Java Examples

The following examples show how to use org.antlr.v4.runtime.misc.ParseCancellationException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BQLCompilerAnalyzer.java    From linden with Apache License 2.0 6 votes vote down vote up
@Override
public void exitType(BQLParser.TypeContext ctx) {
  String type;
  if (ctx.primitive_type() != null) {
    type = ctx.primitive_type().getText();
  } else if (ctx.boxed_type() != null) {
    type = ctx.boxed_type().getText();
  } else if (ctx.limited_type() != null) {
    type = ctx.limited_type().getText();
  } else if (ctx.map_type() != null) {
    return;
  } else {
    throw new UnsupportedOperationException("Not implemented yet.");
  }
  try {
    if (type.equalsIgnoreCase("int")) {
      valProperty.put(ctx, LindenType.INTEGER);
    } else {
      valProperty.put(ctx, LindenType.valueOf(type.toUpperCase()));
    }
  } catch (Exception e) {
    throw new ParseCancellationException(new SemanticException(ctx, "Type " + type + " not support."));
  }
}
 
Example #2
Source File: BQLCompilerAnalyzer.java    From linden with Apache License 2.0 6 votes vote down vote up
@Override
public void exitDelete_stmt(BQLParser.Delete_stmtContext ctx) {
  deleteRequest = new LindenDeleteRequest();
  if (ctx.dw != null) {
    LindenFilter filter = filterProperty.get(ctx.dw);
    if (filter == null) {
      throw new ParseCancellationException(new SemanticException(ctx, "Filter parse failed"));
    }
    LindenQuery query = LindenQueryBuilder.buildMatchAllQuery();
    query = LindenQueryBuilder.buildFilteredQuery(query, filter);
    deleteRequest.setQuery(query);
  } else {
    deleteRequest.setQuery(LindenQueryBuilder.buildMatchAllQuery());
  }
  if (ctx.route_param != null) {
    deleteRequest.setRouteParam((SearchRouteParam) valProperty.get(ctx.route_param));
  }
  if (ctx.indexes != null) {
    deleteRequest.setIndexNames((List<String>) valProperty.get(ctx.indexes));
  }
}
 
Example #3
Source File: BQLCompilerAnalyzer.java    From linden with Apache License 2.0 6 votes vote down vote up
@Override
public void exitSnippet_clause(BQLParser.Snippet_clauseContext ctx) {
  if (ctx.selection_list() != null) {
    List<String> selections = (List<String>) valProperty.get(ctx.selection_list());
    if (selections != null && !selections.isEmpty()) {
      SnippetParam snippet = new SnippetParam();
      for (String selection : selections) {
        Map.Entry<String, LindenType> fieldNameAndType = getFieldNameAndType(selection);
        LindenType type = fieldNameAndType.getValue();
        String col = fieldNameAndType.getKey();
        if (type == LindenType.STRING) {
          snippet.addToFields(new SnippetField(col));
        } else {
          throw new ParseCancellationException("Snippet doesn't support this type " + type);
        }
      }
      valProperty.put(ctx, snippet);
    }
  }
}
 
Example #4
Source File: BQLCompilerAnalyzer.java    From linden with Apache License 2.0 6 votes vote down vote up
@Override
public void exitPython_style_list(BQLParser.Python_style_listContext ctx) {
  List<String> values = new ArrayList<>();
  for (BQLParser.Python_style_valueContext subCtx : ctx.python_style_value()) {
    if (subCtx.value() != null) {
      values.add(subCtx.value().getText());
    } else if (subCtx.python_style_list() != null) {
      throw new ParseCancellationException(
          new SemanticException(subCtx.python_style_list(), "Nested list is not supported"));
    } else if (subCtx.python_style_dict() != null) {
      throw new ParseCancellationException(
          new SemanticException(subCtx.python_style_dict(), "Dict list is not supported"));
    }
  }
  valProperty.put(ctx, values);
}
 
Example #5
Source File: QueryDSL.java    From ameba with MIT License 6 votes vote down vote up
/**
 * <p>parse.</p>
 *
 * @param expression a {@link java.lang.String} object.
 * @return a {@link java.util.List} object.
 */
public static List<QueryExprMeta> parse(String expression) {
    QueryParser parser = parser(tokens(expression));
    try {
        return parse(parser);
    } catch (ParseCancellationException | RecognitionException e) {
        RecognitionException err;
        if (e instanceof ParseCancellationException) {
            err = (RecognitionException) e.getCause();
        } else {
            err = (RecognitionException) e;
        }
        throw new QuerySyntaxException(
                Messages.get("dsl.parse.err", err.getOffendingToken().getCharPositionInLine()),
                e
        );
    }
}
 
Example #6
Source File: BQLCompilerAnalyzer.java    From linden with Apache License 2.0 6 votes vote down vote up
@Override
public void exitAggregation_spec(BQLParser.Aggregation_specContext ctx) {
  String col = unescapeColumnName(ctx.column_name());
  Map.Entry<String, LindenType> fieldNameAndType = getFieldNameAndType(col);
  LindenType type = fieldNameAndType.getValue();
  if (type != LindenType.INTEGER && type != LindenType.LONG && type != LindenType.DOUBLE) {
    throw new ParseCancellationException(new SemanticException(ctx.column_name(),
                                                               "Aggregation doesn't support the type of the field \""
                                                               + col + "\"."));
  }
  col = fieldNameAndType.getKey();

  Aggregation aggregation = new Aggregation();
  aggregation.setField(col);
  aggregation.setType(type);
  for (BQLParser.Bucket_specContext specContext : ctx.bucket_spec()) {
    Bucket bucket = (Bucket) valProperty.get(specContext);
    if (bucket != null) {
      aggregation.addToBuckets(bucket);
    }
  }
  facetRequest.addToAggregations(aggregation);
}
 
Example #7
Source File: BQLCompiler.java    From linden with Apache License 2.0 6 votes vote down vote up
public String getErrorMessage(ParseCancellationException error) {
  if (error.getCause() != null) {
    String message = error.getCause().getMessage();
    if (error.getCause() instanceof SemanticException) {
      SemanticException semanticException = (SemanticException) error.getCause();
      if (semanticException.getNode() != null) {
        TerminalNode startNode = getStartNode(semanticException.getNode());
        if (startNode != null) {
          String prefix = String.format("[line:%d, col:%d] ", startNode.getSymbol().getLine(),
              startNode.getSymbol().getCharPositionInLine());
          message = prefix + message;
        }
      }
      return message;
    } else if (error.getCause() instanceof RecognitionException) {
      return getErrorMessage((RecognitionException) error.getCause());
    } else {
      return error.getCause().getMessage();
    }
  }

  return error.getMessage();
}
 
Example #8
Source File: BoaCompiler.java    From compiler with Apache License 2.0 6 votes vote down vote up
private static Start parse(final CommonTokenStream tokens, final BoaParser parser, final BoaErrorListener parserErrorListener) {
	parser.setBuildParseTree(false);
	parser.getInterpreter().setPredictionMode(PredictionMode.SLL);

	try {
		return parser.start().ast;
	} catch (final ParseCancellationException e) {
		// fall-back to LL mode parsing if SLL fails
		tokens.reset();
		parser.reset();

		parser.removeErrorListeners();
		parser.addErrorListener(parserErrorListener);
		parser.getInterpreter().setPredictionMode(PredictionMode.LL);

		return parser.start().ast;
	}
}
 
Example #9
Source File: CsvParametrizedImporter.java    From IrScrutinizer with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void load(Reader reader, String origin) throws IOException {
    prepareLoad(origin);
    boolean rejectNumbers = nameColumn < protocolColumn || nameColumn < dColumn
            || nameColumn < sColumn || nameColumn < fColumn;
    lineNo = 1;
    BufferedReader bufferedReader = new BufferedReader(reader);
    while (true) {
        String line = bufferedReader.readLine();
        if (line == null)
            break;
        String[] chunks = line.split(separator);
        try {
            Command command = scrutinizeParameters(chunks, "Line " + lineNo + ", " + origin, rejectNumbers);
            if (command != null)
                addCommand(command);
        } catch (GirrException | NameUnassignedException | ParseCancellationException ex) {
            if (isVerbose())
                System.err.println("Errors parsing line " + lineNo + ": \"" + line + "\": " + ex.getMessage());
        }
        lineNo++;
    }
    setupRemoteSet();
}
 
Example #10
Source File: DescriptiveErrorStrategy.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void recover(Parser recognizer, RecognitionException e) {
    for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) {
        context.exception = e;
    }

    if (PredictionMode.LL.equals(recognizer.getInterpreter().getPredictionMode())) {
        if (e instanceof NoViableAltException) {
            this.reportNoViableAlternative(recognizer, (NoViableAltException) e);
        } else if (e instanceof InputMismatchException) {
            this.reportInputMismatch(recognizer, (InputMismatchException) e);
        } else if (e instanceof FailedPredicateException) {
            this.reportFailedPredicate(recognizer, (FailedPredicateException) e);
        }
    }

    throw new ParseCancellationException(e);
}
 
Example #11
Source File: CoreQueryParser.java    From heroic with Apache License 2.0 5 votes vote down vote up
private QueryListener parse(Function<HeroicQueryParser, ParserRuleContext> op, String input) {
    final HeroicQueryLexer lexer = new HeroicQueryLexer(new ANTLRInputStream(input));

    final CommonTokenStream tokens = new CommonTokenStream(lexer);
    final HeroicQueryParser parser = new HeroicQueryParser(tokens);

    parser.removeErrorListeners();
    parser.setErrorHandler(new BailErrorStrategy());

    final ParserRuleContext context;

    try {
        context = op.apply(parser);
    } catch (final ParseCancellationException e) {
        if (!(e.getCause() instanceof RecognitionException)) {
            throw e;
        }

        throw toParseException((RecognitionException) e.getCause());
    }

    final QueryListener listener = new QueryListener();

    ParseTreeWalker.DEFAULT.walk(listener, context);

    final Token last = lexer.getToken();

    if (last.getType() != Token.EOF) {
        throw new ParseException(
            String.format("garbage at end of string: '%s'", last.getText()), null,
            last.getLine(), last.getCharPositionInLine());
    }

    return listener;
}
 
Example #12
Source File: BQLCompiler.java    From linden with Apache License 2.0 5 votes vote down vote up
public String getErrorMessage(Exception error) {
  if (error instanceof NoViableAltException) {
    return getErrorMessage((NoViableAltException) error);
  } else if (error instanceof ParseCancellationException) {
    return getErrorMessage((ParseCancellationException) error);
  } else {
    return error.getMessage();
  }
}
 
Example #13
Source File: SQLParserExecutor.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
private ParseASTNode twoPhaseParse() {
    SQLParser sqlParser = SQLParserFactory.newInstance(databaseTypeName, sql);
    try {
        ((Parser) sqlParser).setErrorHandler(new BailErrorStrategy());
        ((Parser) sqlParser).getInterpreter().setPredictionMode(PredictionMode.SLL);
        return (ParseASTNode) sqlParser.parse();
    } catch (final ParseCancellationException ex) {
        ((Parser) sqlParser).reset();
        ((Parser) sqlParser).setErrorHandler(new DefaultErrorStrategy());
        ((Parser) sqlParser).getInterpreter().setPredictionMode(PredictionMode.LL);
        return (ParseASTNode) sqlParser.parse();
    }
}
 
Example #14
Source File: BailSyntaxErrorStrategy.java    From gdl with Apache License 2.0 5 votes vote down vote up
/**
 *  Instead of recovering from exception {@code e}, re-throw it wrapped
 *  in a {@link ParseCancellationException} so it is not caught by the
 *  rule function catches.  Use {@link Exception#getCause()} to get the
 *  original {@link RecognitionException}. To print the syntax error the
 *  {@link DefaultErrorStrategy#recover(Parser, RecognitionException)} method
 *  gets executed.
 */
@Override
public void recover(Parser recognizer, RecognitionException e) {
  super.recover(recognizer, e);
  for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) {
    context.exception = e;
  }
  throw new ParseCancellationException(e);
}
 
Example #15
Source File: BailSyntaxErrorStrategy.java    From gdl with Apache License 2.0 5 votes vote down vote up
/**
 *  Make sure we don't attempt to recover inline; if the parser
 *  successfully recovers, it won't throw an exception.
 *  Again, the {@link DefaultErrorStrategy#recoverInline(Parser)} gets executed
 *  to print the wrong syntax
 */
@Override
public Token recoverInline(Parser recognizer) throws RecognitionException {
  super.recoverInline(recognizer);
  InputMismatchException e = new InputMismatchException(recognizer);
  for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) {
    context.exception = e;
  }
  throw new ParseCancellationException(e);
}
 
Example #16
Source File: SqlParser.java    From crate with Apache License 2.0 5 votes vote down vote up
private Node invokeParser(String name, String sql, Function<SqlBaseParser, ParserRuleContext> parseFunction) {
    try {
        SqlBaseLexer lexer = new SqlBaseLexer(new CaseInsensitiveStream(CharStreams.fromString(sql, name)));
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        SqlBaseParser parser = new SqlBaseParser(tokenStream);

        parser.addParseListener(new PostProcessor());

        lexer.removeErrorListeners();
        lexer.addErrorListener(ERROR_LISTENER);

        parser.removeErrorListeners();
        parser.addErrorListener(ERROR_LISTENER);

        ParserRuleContext tree;
        try {
            // first, try parsing with potentially faster SLL mode
            parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
            tree = parseFunction.apply(parser);
        } catch (ParseCancellationException ex) {
            // if we fail, parse with LL mode
            tokenStream.seek(0); // rewind input stream
            parser.reset();

            parser.getInterpreter().setPredictionMode(PredictionMode.LL);
            tree = parseFunction.apply(parser);
        }

        return new AstBuilder().visit(tree);
    } catch (StackOverflowError e) {
        throw new ParsingException(name + " is too large (stack overflow while parsing)");
    }
}
 
Example #17
Source File: JsonPathTest.java    From JsonSurfer with MIT License 5 votes vote down vote up
@Test
public void testJsonPathFilterMatchRegexInputMismatch() throws Exception {
    try {
        JsonPathCompiler.compile("$.store.book[?(@.author=~ /abc)]"); // not a valid regular expression
    } catch (ParseCancellationException e) {
        assertTrue(e.getCause() instanceof InputMismatchException);
    }
}
 
Example #18
Source File: SqlParser.java    From macrobase with Apache License 2.0 5 votes vote down vote up
private Node invokeParser(String name, String sql,
    Function<SqlBaseParser, ParserRuleContext> parseFunction) {
    try {
        SqlBaseLexer lexer = new SqlBaseLexer(
            new CaseInsensitiveStream(new ANTLRInputStream(sql)));
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        SqlBaseParser parser = new SqlBaseParser(tokenStream);

        parser.addParseListener(new PostProcessor(Arrays.asList(parser.getRuleNames())));

        lexer.removeErrorListeners();
        lexer.addErrorListener(ERROR_LISTENER);

        parser.removeErrorListeners();
        parser.addErrorListener(ERROR_LISTENER);

        ParserRuleContext tree;
        try {
            // first, try parsing with potentially faster SLL mode
            parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
            tree = parseFunction.apply(parser);
        } catch (ParseCancellationException ex) {
            // if we fail, parse with LL mode
            tokenStream.seek(0); // rewind input stream
            parser.reset();

            parser.getInterpreter().setPredictionMode(PredictionMode.LL);
            tree = parseFunction.apply(parser);
        }

        return new AstBuilder().visit(tree);
    } catch (StackOverflowError e) {
        throw new ParsingException(name + " is too large (stack overflow while parsing)");
    }
}
 
Example #19
Source File: TargetVisitor.java    From disthene-reader with MIT License 5 votes vote down vote up
@Override
public Target visitExpressionCall(GraphiteParser.ExpressionCallContext ctx) {
    GraphiteParser.CallContext call = ctx.call();
    try {
        DistheneFunction function = FunctionRegistry.getFunction(context, call.FunctionName().getText(), from, to);
        function.setText(ctx.getText());

        for(GraphiteParser.ArgContext arg : call.args().arg()) {
            if (arg instanceof GraphiteParser.ArgExpressionContext) {
                function.addArg(visit(arg));
            } else if (arg instanceof GraphiteParser.ArgBooleanContext) {
                function.addArg(Boolean.parseBoolean(arg.getText()));
            } else if (arg instanceof GraphiteParser.ArgNumberContext) {
                function.addArg(Double.parseDouble(arg.getText()));
            } else if (arg instanceof GraphiteParser.ArgStringContext) {
                function.addArg(arg.getText().replaceAll("^[\"\']|[\"\']$", ""));
            }
        }

        function.checkArguments();

        return function;
    } catch (InvalidFunctionException | InvalidArgumentException e) {
        e.printStackTrace();
        throw new ParseCancellationException(e.getMessage(), e);
    }


}
 
Example #20
Source File: DoFailOnErrorHandler.java    From JsoupXpath with Apache License 2.0 5 votes vote down vote up
@Override
public void recover(Parser recognizer, RecognitionException e) {
    for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) {
        context.exception = e;
    }
    throw new ParseCancellationException(e);
}
 
Example #21
Source File: DoFailOnErrorHandler.java    From JsoupXpath with Apache License 2.0 5 votes vote down vote up
@Override
public Token recoverInline(Parser recognizer) throws RecognitionException {
    InputMismatchException e = new InputMismatchException(recognizer);
    for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) {
        context.exception = e;
    }
    throw new ParseCancellationException(e);
}
 
Example #22
Source File: FormulaParserTest.java    From pretty-formula with MIT License 5 votes vote down vote up
@Test(expected = ParseCancellationException.class)
public void testMismatchedInput() {
   this.formula = "1,1";
   this.expResult = "the exception is thrown";
   this.doTest();
   this.formula = "1.1.1";
   this.expResult = "the exception is thrown";
   this.doTest();
}
 
Example #23
Source File: ShowAmbigTreesDialog.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void popupAmbigTreesDialog(PreviewState previewState, AmbiguityInfo ambigInfo) {
	// pop up subtrees for ambig intrepretation
	ShowAmbigTreesDialog dialog = new ShowAmbigTreesDialog();
	Parser parser = previewState.parsingResult.parser;
	int startRuleIndex = parser.getRuleIndex(previewState.startRuleName);
	List<ParserRuleContext> ambiguousParseTrees = null;
	try {
		ambiguousParseTrees =
			GrammarParserInterpreter.getAllPossibleParseTrees(previewState.g,
			                                                  parser,
			                                                  parser.getTokenStream(),
			                                                  ambigInfo.decision,
			                                                  ambigInfo.ambigAlts,
			                                                  ambigInfo.startIndex,
			                                                  ambigInfo.stopIndex,
			                                                  startRuleIndex);
	} catch (ParseCancellationException pce) {
		// should be no errors for ambiguities, unless original
		// input itself has errors. Just display error in this case.
		JBPanel errPanel = new JBPanel(new BorderLayout());
		errPanel.add(new JBLabel("Cannot display ambiguous trees while there are syntax errors in your input."));
		dialog.treeScrollPane.setViewportView(errPanel);
	}

	if ( ambiguousParseTrees!=null ) {
		TokenStream tokens = previewState.parsingResult.parser.getInputStream();
		String phrase = tokens.getText(Interval.of(ambigInfo.startIndex, ambigInfo.stopIndex));
		if ( phrase.length()>MAX_PHRASE_WIDTH ) {
			phrase = phrase.substring(0, MAX_PHRASE_WIDTH)+"...";
		}
		String title = ambiguousParseTrees.size()+
			" Interpretations of Ambiguous Input Phrase: "+
			phrase;
		dialog.ambigPhraseLabel.setText(title);
		dialog.setTrees(previewState, ambiguousParseTrees, title, 0, true);
	}

	dialog.pack();
	dialog.setVisible(true);
}
 
Example #24
Source File: CompileErrorStrategy.java    From BigDataScript with Apache License 2.0 5 votes vote down vote up
/** Instead of recovering from pendingException {@code e}, re-throw it wrapped
 *  in a {@link ParseCancellationException} so it is not caught by the
 *  rule function catches.  Use {@link Exception#getCause()} to get the
 *  original {@link RecognitionException}.
 */
@Override
public void recover(Parser recognizer, RecognitionException e) {
	// Add a compiler error message
	String message = "Cannot parse input, near '" + e.getOffendingToken().getText() + "'";
	CompilerMessage cm = new CompilerMessage(e.getOffendingToken().getInputStream().getSourceName(), e.getOffendingToken().getLine(), -1, message, MessageType.ERROR);
	CompilerMessages.get().add(cm);

	// Add pendingException to all contexts
	for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent())
		context.exception = e;

	throw new ParseCancellationException(e);
}
 
Example #25
Source File: CompileErrorStrategy.java    From BigDataScript with Apache License 2.0 5 votes vote down vote up
/** Make sure we don't attempt to recover inline; if the parser
 *  successfully recovers, it won't throw an pendingException.
 */
@Override
public Token recoverInline(Parser recognizer) throws RecognitionException {
	InputMismatchException e = new InputMismatchException(recognizer);

	String message = "Cannot parse input, near '" + e.getOffendingToken().getText() + "'";
	CompilerMessage cm = new CompilerMessage(e.getOffendingToken().getInputStream().getSourceName(), e.getOffendingToken().getLine(), -1, message, MessageType.ERROR);
	CompilerMessages.get().add(cm);

	// Add pendingException to all contexts
	for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent())
		context.exception = e;

	throw new ParseCancellationException(e);
}
 
Example #26
Source File: BQLCompilerAnalyzer.java    From linden with Apache License 2.0 5 votes vote down vote up
@Override
public void exitLike_predicate(BQLParser.Like_predicateContext ctx) {
  if (ctx.PLACEHOLDER() != null) {
    return;
  }

  String col = unescapeColumnName(ctx.column_name());
  Map.Entry<String, LindenType> fieldNameAndType = getFieldNameAndType(col);
  LindenType type = fieldNameAndType.getValue();
  if (type != LindenType.STRING && type != LindenType.FACET) {
    throw new ParseCancellationException(new SemanticException(ctx.column_name(),
                                                               "Non-string type column \"" + col
                                                               + "\" can not be used in LIKE predicates."));
  }
  col = fieldNameAndType.getKey();

  String likeString = unescapeStringLiteral(ctx.STRING_LITERAL());
  LindenWildcardQuery wildcardQuery = new LindenWildcardQuery().setField(col)
      .setQuery(likeString);
  if (inQueryWhere) {
    if (ctx.NOT() != null) {
      LindenBooleanQueryBuilder builder = new LindenBooleanQueryBuilder();
      builder.addQuery(LindenRangeQueryBuilder.buildMatchAllQuery(), LindenBooleanClause.MUST);
      builder.addQuery(new LindenQuery().setWildcardQuery(wildcardQuery), LindenBooleanClause.MUST_NOT);
      queryProperty.put(ctx, builder.build());
    } else {
      queryProperty.put(ctx, new LindenQuery().setWildcardQuery(wildcardQuery));
    }
  } else {
    LindenFilter filter = new LindenFilter().setQueryFilter(
        new LindenQueryFilter().setQuery(new LindenQuery().setWildcardQuery(wildcardQuery)));
    if (ctx.NOT() != null) {
      LindenBooleanFilter booleanFilter = new LindenBooleanFilter();
      booleanFilter.addToFilters(new LindenBooleanSubFilter().setFilter(filter)
                                     .setClause(LindenBooleanClause.MUST_NOT));
      filter = new LindenFilter().setBooleanFilter(booleanFilter);
    }
    filterProperty.put(ctx, filter);
  }
}
 
Example #27
Source File: TypeCalculation.java    From presto with Apache License 2.0 5 votes vote down vote up
private static ParserRuleContext parseTypeCalculation(String calculation)
{
    TypeCalculationLexer lexer = new TypeCalculationLexer(new CaseInsensitiveStream(CharStreams.fromString(calculation)));
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    TypeCalculationParser parser = new TypeCalculationParser(tokenStream);

    lexer.removeErrorListeners();
    lexer.addErrorListener(ERROR_LISTENER);

    parser.removeErrorListeners();
    parser.addErrorListener(ERROR_LISTENER);

    ParserRuleContext tree;
    try {
        // first, try parsing with potentially faster SLL mode
        parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
        tree = parser.typeCalculation();
    }
    catch (ParseCancellationException ex) {
        // if we fail, parse with LL mode
        tokenStream.seek(0); // rewind input stream
        parser.reset();

        parser.getInterpreter().setPredictionMode(PredictionMode.LL);
        tree = parser.typeCalculation();
    }
    return tree;
}
 
Example #28
Source File: LexerWrapper.java    From antlr4-autosuggest with Apache License 2.0 5 votes vote down vote up
private TokenizationResult tokenize(String input) {
    Lexer lexer = this.createLexer(input);
    lexer.removeErrorListeners();
    final TokenizationResult result = new TokenizationResult();
    ANTLRErrorListener newErrorListener = new BaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
                int charPositionInLine, String msg, RecognitionException e) throws ParseCancellationException {
            result.untokenizedText = input.substring(charPositionInLine); // intended side effect
        }
    };
    lexer.addErrorListener(newErrorListener);
    result.tokens = lexer.getAllTokens();
    return result;
}
 
Example #29
Source File: TypeCalculation.java    From rainbow with Apache License 2.0 5 votes vote down vote up
private static ParserRuleContext parseTypeCalculation(String calculation)
{
    TypeCalculationLexer lexer = new TypeCalculationLexer(new CaseInsensitiveStream(new ANTLRInputStream(calculation)));
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    TypeCalculationParser parser = new TypeCalculationParser(tokenStream);

    lexer.removeErrorListeners();
    lexer.addErrorListener(ERROR_LISTENER);

    parser.removeErrorListeners();
    parser.addErrorListener(ERROR_LISTENER);

    ParserRuleContext tree;
    try {
        // first, try parsing with potentially faster SLL mode
        parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
        tree = parser.typeCalculation();
    }
    catch (ParseCancellationException ex) {
        // if we fail, parse with LL mode
        tokenStream.reset(); // rewind input stream
        parser.reset();

        parser.getInterpreter().setPredictionMode(PredictionMode.LL);
        tree = parser.typeCalculation();
    }
    return tree;
}
 
Example #30
Source File: LogicalPlanSmallTest.java    From incubator-iotdb with Apache License 2.0 5 votes vote down vote up
@Test(expected = ParseCancellationException.class)
public void testOffsetNotPositive() {
  String sqlStr = "select * from root.vehicle.d1 where s1 < 20 and time <= now() limit 1 offset -1";
  RootOperator operator = (RootOperator) parseDriver
      .parse(sqlStr, IoTDBDescriptor.getInstance().getConfig().getZoneID());
  // expected to throw SQLParserException: OFFSET <OFFSETValue>: OFFSETValue should >= 0.
}