Example usage for org.antlr.v4.runtime CommonTokenStream CommonTokenStream

List of usage examples for org.antlr.v4.runtime CommonTokenStream CommonTokenStream

Introduction

In this page you can find the example usage for org.antlr.v4.runtime CommonTokenStream CommonTokenStream.

Prototype

public CommonTokenStream(TokenSource tokenSource) 

Source Link

Document

Constructs a new CommonTokenStream using the specified token source and the default token channel ( Token#DEFAULT_CHANNEL ).

Usage

From source file:com.antsdb.saltedfish.sql.mysql.ExprGenerator.java

License:Open Source License

public static Operator gen(GeneratorContext ctx, Planner cursorMeta, String expr) {
    CharStream cs = new ANTLRInputStream(expr);
    MysqlLexer lexer = new MysqlLexer(cs);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    tokens.setTokenSource(lexer);//w  w  w. j av a  2s. co m
    MysqlParser parser = new MysqlParser(tokens);
    parser.setErrorHandler(new BailErrorStrategy());
    MysqlParser.ExprContext rule = parser.expr();
    return gen(ctx, cursorMeta, rule);
}

From source file:com.antsdb.saltedfish.sql.mysql.MysqlParserFactory.java

License:Open Source License

static MysqlParser.ScriptContext parse(CharStream cs) {
    if (isCommtedStatement(cs)) {
        String s = cs.toString();
        s = s.substring(9);/*  w w  w  .  j a  va  2 s.com*/
        s = s.substring(0, s.length() - 3);
        cs = new ANTLRInputStream(s);
    }
    MysqlLexer lexer = new MysqlLexer(cs);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    tokens.setTokenSource(lexer);
    MysqlParser parser = new MysqlParser(tokens);
    parser.setErrorHandler(new BailErrorStrategy());
    boolean success = false;
    try {
        MysqlParser.ScriptContext script = parser.script();
        success = true;
        return script;
    } finally {
        if (!success && (parser.lastStatement != null)) {
            _log.debug("last passed statement: {}", ((ParseTree) parser.lastStatement).getText());
        }
    }
}

From source file:com.awesheet.managers.FunctionManager.java

License:Open Source License

/**
 * Parses a function string into an actual function.
 * @param value the function string./*  w w  w .  j a va 2s . com*/
 * @param sheet the containing sheet.
 * @return the parsed function or null on failure
 */
public DataFunction parseFunction(String value, Sheet sheet) {
    try {
        ANTLRInputStream inputStream = new ANTLRInputStream(value);

        AweFuncLexer lexer = new AweFuncLexer(inputStream);
        CommonTokenStream tokens = new CommonTokenStream(lexer);

        AweFuncParser parser = new AweFuncParser(tokens);
        AweFuncParser.AweFunctionContext tree = parser.aweFunction();

        AweVisitor visitor = new AweVisitor(sheet);
        visitor.visit(tree);

        DataFunction function = visitor.getBaseFunction();

        if (!function.parse()) {
            return null;
        }

        return function;
    } catch (RuntimeException e) {
        return null;
    } catch (Exception e) {
        return null;
    }
}

From source file:com.axway.jmb.JMBCompiler.java

License:Open Source License

public JMBCompiler(InputStream is) throws IOException {
    input = new ANTLRInputStream(is);
    lexer = new JMessageBuilderLexer(input);
    tokens = new CommonTokenStream(lexer);
    parser = new JMessageBuilderParser(tokens);
}

From source file:com.basho.contact.ContactConsole.java

License:Apache License

private static void processInput(String line, List<ContactBaseListener> walkers, RuntimeContext runtimeCtx) {
    ANTLRInputStream input = new ANTLRInputStream(line);
    ContactLexer lexer = new ContactLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ContactParser parser = new ContactParser(tokens);

    // combine these two into one
    parser.addErrorListener(new ContactErrorListener(runtimeCtx));

    ParseTreeWalker walker = new ParseTreeWalker();
    try {/*from ww w .  j  a v  a 2  s.  com*/
        ContactParser.ProgContext prog = parser.prog();
        for (ContactBaseListener w : walkers) {
            walker.walk(w, prog);
        }
    } catch (Throwable t) {
        // catch parse errors. ANTLR will display a message for me.
    }

}

From source file:com.blazebit.persistence.impl.expression.AbstractExpressionFactory.java

License:Apache License

private Expression createExpression(RuleInvoker ruleInvoker, String expression, boolean allowCaseWhen) {
    if (expression == null) {
        throw new NullPointerException("expression");
    }/* w  ww.  j  a  va  2  s.  co m*/
    if (expression.isEmpty()) {
        throw new IllegalArgumentException("expression");
    }
    JPQLSelectExpressionLexer l = new JPQLSelectExpressionLexer(new ANTLRInputStream(expression));
    configureLexer(l);
    CommonTokenStream tokens = new CommonTokenStream(l);
    JPQLSelectExpressionParser p = new JPQLSelectExpressionParser(tokens, allowCaseWhen);
    configureParser(p);
    ParserRuleContext ctx = ruleInvoker.invokeRule(p);

    if (LOG.isLoggable(Level.FINEST)) {
        LOG.finest(ctx.toStringTree());
    }

    JPQLSelectExpressionVisitorImpl visitor = new JPQLSelectExpressionVisitorImpl(tokens);
    return visitor.visit(ctx);
}

From source file:com.blazebit.persistence.parser.expression.AbstractExpressionFactory.java

License:Apache License

private Expression createExpression(RuleInvoker ruleInvoker, String expression, boolean allowCaseWhen,
        boolean allowQuantifiedPredicates, boolean allowTreatJoinExtension,
        MacroConfiguration macroConfiguration, Set<String> usedMacros) {
    if (expression == null) {
        throw new NullPointerException("expression");
    }//from w w  w  .j  a va 2  s .co m
    if (expression.isEmpty()) {
        throw new IllegalArgumentException("expression");
    }
    JPQLSelectExpressionLexer l = new JPQLSelectExpressionLexer(new ANTLRInputStream(expression));
    configureLexer(l);
    CommonTokenStream tokens = new CommonTokenStream(l);
    JPQLSelectExpressionParser p = new JPQLSelectExpressionParser(tokens, allowCaseWhen,
            allowQuantifiedPredicates, allowTreatJoinExtension);
    configureParser(p);
    ParserRuleContext ctx;
    try {
        ctx = ruleInvoker.invokeRule(p);
    } catch (SyntaxErrorException ex) {
        throw new SyntaxErrorException("Could not parse expression '" + expression + "', " + ex.getMessage(),
                ex);
    }

    if (LOG.isLoggable(Level.FINEST)) {
        LOG.finest(ctx.toStringTree());
    }

    JPQLSelectExpressionVisitorImpl visitor = new JPQLSelectExpressionVisitorImpl(aggregateFunctions, enumTypes,
            entityTypes, minEnumSegmentCount, minEntitySegmentCount,
            macroConfiguration == null ? Collections.EMPTY_MAP : macroConfiguration.macros, usedMacros);
    Expression parsedExpression = visitor.visit(ctx);
    if (optimize) {
        parsedExpression = parsedExpression.accept(optimizer);
    }
    return parsedExpression;
}

From source file:com.bosch.example.rsql.suggestion.RsqlSuggestionHelper.java

License:Open Source License

public static SuggestionContext parse(final String rsql, final int cursorPosition) {
    CharStream inputCharStream;/*ww  w.  j a  va  2s .c o m*/
    try {
        inputCharStream = new ANTLRInputStream(new StringReader(rsql));
    } catch (final IOException e) {
        throw Throwables.propagate(e);
    }
    final TokenSource tokenSource = new RsqlLexer(inputCharStream);
    final TokenStream inputTokenStream = new CommonTokenStream(tokenSource);
    final RsqlParser parser = new RsqlParser(inputTokenStream);
    final SuggestionErrorListener errorListener = new SuggestionErrorListener();
    parser.addErrorListener(errorListener);
    final SuggestionParseTreeListener parserListener = new SuggestionParseTreeListener();
    parser.addParseListener(parserListener);

    parser.expr();

    final SuggestionContext suggestionContext = new SuggestionContext();

    suggestionContext.syntaxError = errorListener.isErrorOccurred();
    if (errorListener.isErrorOccurred()) {
        final SyntaxErrorContext errorContext = new SyntaxErrorContext();
        final Token errorToken = errorListener.getErrorToken().getToken();
        errorContext.tokenStart = errorToken.getStartIndex();
        errorContext.tokenEnd = errorToken.getStopIndex();

        final IntervalSet expectedTokenType = errorListener.getErrorToken().getExpectedTokenType();
        expectedTokenType.getIntervals().forEach(interval -> {
            final int start = interval.a;
            final int end = interval.b;
            for (int index = start; index <= end; index++) {
                final String symbolicName = parser.getVocabulary().getSymbolicName(index);
                errorContext.suggestions.addAll(SuggestionMap.getSuggestions(symbolicName));
            }
        });
        suggestionContext.syntaxErrorContext = errorContext;
    }

    final Optional<Token> tokenAtCursorPosition = parserListener.getTokenAtCursorPosition(cursorPosition);
    if (tokenAtCursorPosition.isPresent()) {
        final CursorPositionSuggestionContext cursorPositionSuggestionContext = new CursorPositionSuggestionContext();
        cursorPositionSuggestionContext.currentCursor = cursorPosition;
        cursorPositionSuggestionContext.tokenStart = tokenAtCursorPosition.get().getStartIndex();
        cursorPositionSuggestionContext.tokenEnd = tokenAtCursorPosition.get().getStopIndex();
        cursorPositionSuggestionContext.suggestions = SuggestionMap
                .getSuggestions(parser.getVocabulary().getSymbolicName(tokenAtCursorPosition.get().getType()));
        suggestionContext.cursorPositionContext = cursorPositionSuggestionContext;
    }

    return suggestionContext;
}

From source file:com.boylesoftware.web.impl.routes.RoutesRouterConfiguration.java

License:Apache License

@Override
protected void buildRoutes(final ServletContext sc, final RoutesBuilder routes) throws UnavailableException {

    try (final InputStream in = sc.getResourceAsStream(ROUTES_PATH)) {

        if (in == null)
            throw new UnavailableException("No " + ROUTES_PATH + " found in the web-application.");

        final RoutesParser parser = new RoutesParser(
                new CommonTokenStream(new RoutesLexer(new ANTLRInputStream(in))));
        parser.setErrorHandler(new BailErrorStrategy());
        parser.setRoutesBuilder(routes);
        try {/*from   www .j ava 2 s  . co m*/
            parser.config();
        } catch (final Exception e) {
            this.error(e);
        }

    } catch (final IOException e) {
        this.error(e);
    }
}

From source file:com.castlemock.core.basis.utility.parser.expression.ExpressionInputParser.java

License:Apache License

/**
 * The parse method provides the functionality to parse and convert
 * a String input into an {@link ExpressionInput}.
 * @param input The input that will be converted into an {@link ExpressionInput}.
 * @return an {@link ExpressionInput} based on the provided <code>input</code>.
 * @see {@link ExpressionInputParser#parseArgument(ExpressionParser.ArgumentValueContext)}.
 *///from   w  w  w  .  j a  va  2  s  . c  o m
public static ExpressionInput parse(final String input) {
    // Parse the input
    final CodePointCharStream stream = CharStreams.fromString(input);
    final ExpressionLexer lexer = new ExpressionLexer(stream);
    final CommonTokenStream tokens = new CommonTokenStream(lexer);
    final ExpressionParser parser = new com.castlemock.core.expression.ExpressionParser(tokens);
    final ExpressionParser.ExpressionContext expression = parser.expression();

    // Create expression details
    final String expressionName = expression.type.getText();
    final ExpressionInput expressionInput = new ExpressionInput(expressionName);
    for (ExpressionParser.ArgumentContext argumentContext : expression.argument()) {
        String argumentName = argumentContext.argumentName().getText();
        ExpressionParser.ArgumentValueContext argumentValueContext = argumentContext.argumentValue();
        ExpressionArgument expressionArgument = parseArgument(argumentValueContext);
        expressionInput.addArgument(argumentName, expressionArgument);
    }

    return expressionInput;
}