Example usage for org.antlr.v4.runtime CharStreams fromString

List of usage examples for org.antlr.v4.runtime CharStreams fromString

Introduction

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

Prototype

public static CodePointCharStream fromString(String s) 

Source Link

Document

Creates a CharStream given a String .

Usage

From source file:beast.util.TreeParser.java

License:Open Source License

/**
 * Parse a newick-ish string and generate the BEAST tree it describes.
 *
 * @param newick string to parse//from   ww  w.  j  ava 2s  .c  om
 * @return root node of tree
 */
public Node parseNewick(String newick) {
    CharStream charStream = CharStreams.fromString(newick);

    // Custom parse/lexer error listener
    BaseErrorListener errorListener = new BaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
                int charPositionInLine, String msg, RecognitionException e) {
            throw new TreeParsingException(msg, charPositionInLine, line);
        }
    };

    // Use lexer to produce token stream

    NewickLexer lexer = new NewickLexer(charStream);
    lexer.removeErrorListeners();
    lexer.addErrorListener(errorListener);

    CommonTokenStream tokens = new CommonTokenStream(lexer);

    // Parse token stream to produce parse tree

    NewickParser parser = new NewickParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(errorListener);

    ParseTree parseTree = parser.tree();

    // Traverse parse tree, constructing BEAST tree along the way

    NewickASTVisitor visitor = new NewickASTVisitor();

    return visitor.visit(parseTree);
}

From source file:com.abubusoft.kripton.android.sqlite.internals.MigrationSQLChecker.java

License:Apache License

/**
 * Prepare parser.//from ww w .j  a v  a  2  s  .c o  m
 *
 * @param jql the jql
 * @return the pair
 */
protected Pair<ParserRuleContext, CommonTokenStream> prepareParser(final String jql) {
    JqlLexer lexer = new JqlLexer(CharStreams.fromString(jql));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    JqlParser parser = new JqlParser(tokens);

    parser.removeErrorListeners();
    parser.addErrorListener(new JQLBaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
                int charPositionInLine, String msg, RecognitionException e) {
            KriptonAssert.assertTrue(false, "unespected char at pos %s of SQL '%s'", charPositionInLine, jql);
        }
    });

    ParserRuleContext context = parser.parse();
    return new Pair<>(context, tokens);
}

From source file:com.abubusoft.kripton.android.sqlite.SQLiteTestUtils.java

License:Apache License

/**
 * Extract commands.//from  w  w w.j  a  v a2  s .c om
 *
 * @param database
 *            the database
 * @param inputStream
 *            the input stream
 * @return the list
 */
static List<String> extractCommands(SQLiteDatabase database, InputStream inputStream) {
    final List<String> result = new ArrayList<>();
    final String input = IOUtils.readText(inputStream);
    JqlLexer lexer = new JqlLexer(CharStreams.fromString(input));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    JqlParser parser = new JqlParser(tokens);
    ParserRuleContext parseContext = parser.parse();

    ParseTreeWalker walk = new ParseTreeWalker();
    walk.walk(new JqlBaseListener() {

        @Override
        public void enterSql_stmt(Sql_stmtContext ctx) {
            int start = ctx.getStart().getStartIndex();
            int stop = ctx.getStop().getStopIndex() + 1;

            if (start == stop)
                return;

            result.add(input.substring(start, stop));
        }

    }, parseContext);

    return result;
}

From source file:com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker.java

License:Apache License

/**
 * Prepare parser./*  w ww .  j a  v a 2s  .  c  o  m*/
 *
 * @param jqlContext
 *            the jql context
 * @param jql
 *            the jql
 * @return the pair
 */
protected Pair<ParserRuleContext, CommonTokenStream> prepareParser(final JQLContext jqlContext,
        final String jql) {
    JqlLexer lexer = new JqlLexer(CharStreams.fromString(jql));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    JqlParser parser = new JqlParser(tokens);

    parser.removeErrorListeners();
    parser.addErrorListener(new JQLBaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
                int charPositionInLine, String msg, RecognitionException e) {
            AssertKripton.assertTrue(false,
                    jqlContext.getContextDescription() + ": unespected char at pos %s of SQL '%s'",
                    charPositionInLine, jql);
        }
    });

    ParserRuleContext context = parser.parse();
    return new Pair<>(context, tokens);
}

From source file:com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker.java

License:Apache License

/**
 * <p>//from   w w w  .j a v  a 2s.c o  m
 * Parse the variable parts of a SQL:
 * </p>
 * 
 * <ul>
 * <li>where_stmt</li>
 * <li>group_stmt</li>
 * <li>having_stmt</li>
 * <li>order_stmt</li>
 * <li>limit_stmt</li>
 * <li>offset_stmt</li>
 * </ul>
 * .
 *
 * @param jqlContext
 *            the jql context
 * @param jql
 *            the jql
 * @return the pair
 */
protected Pair<ParserRuleContext, CommonTokenStream> prepareVariableStatement(final JQLContext jqlContext,
        final String jql) {
    JqlLexer lexer = new JqlLexer(CharStreams.fromString(jql));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    JqlParser parser = new JqlParser(tokens);

    parser.removeErrorListeners();
    parser.addErrorListener(new JQLBaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
                int charPositionInLine, String msg, RecognitionException e) {
            AssertKripton.assertTrue(false,
                    jqlContext.getContextDescription() + ": unespected char at pos %s of JQL '%s'",
                    charPositionInLine, jql);
        }
    });

    ParserRuleContext context = parser.parse_variable();
    return new Pair<>(context, tokens);
}

From source file:com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriChecker.java

License:Apache License

/**
 * Prepare path./* www.j av a2s  .c om*/
 *
 * @param input the input
 * @return the pair
 */
private Pair<ParserRuleContext, CommonTokenStream> preparePath(final String input) {
    UriLexer lexer = new UriLexer(CharStreams.fromString(input));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    UriParser parser = new UriParser(tokens);

    parser.removeErrorListeners();
    parser.addErrorListener(new ContentUriBaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
                int charPositionInLine, String msg, RecognitionException e) {
            AssertKripton.assertTrue(false, "unespected char at pos %s of URI '%s'", charPositionInLine, input);
        }
    });

    ParserRuleContext context = parser.path();
    return new Pair<>(context, tokens);
}

From source file:com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriChecker.java

License:Apache License

/**
 * Prepare uri.//from  ww w.j av  a 2 s  . com
 *
 * @param input the input
 * @return the pair
 */
private Pair<ParserRuleContext, CommonTokenStream> prepareUri(final String input) {
    UriLexer lexer = new UriLexer(CharStreams.fromString(input));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    UriParser parser = new UriParser(tokens);

    parser.removeErrorListeners();
    parser.addErrorListener(new ContentUriBaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
                int charPositionInLine, String msg, RecognitionException e) {
            AssertKripton.assertTrue(false, "unespected char at pos %s of URI '%s'", charPositionInLine, input);
        }

        @Override
        public void reportAmbiguity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact,
                BitSet ambigAlts, ATNConfigSet configs) {
            AssertKripton.assertTrue(false, "ambiguity syntax at pos %s of URI '%s'", startIndex, input);
        }

        @Override
        public void reportAttemptingFullContext(Parser recognizer, DFA dfa, int startIndex, int stopIndex,
                BitSet conflictingAlts, ATNConfigSet configs) {
            AssertKripton.assertTrue(false, "error at pos %s of URI '%s'", startIndex, input);
        }

        @Override
        public void reportContextSensitivity(Parser recognizer, DFA dfa, int startIndex, int stopIndex,
                int prediction, ATNConfigSet configs) {
            AssertKripton.assertTrue(false, "context eror at pos %s of URI '%s'", startIndex, input);
        }

    });

    ParserRuleContext context = parser.uri();
    return new Pair<>(context, tokens);
}

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)}.
 *//* w  w  w .j  a  v a2 s  . c  om*/
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;
}

From source file:com.github.robozonky.strategy.natural.GeneratedStrategyVerifier.java

License:Apache License

public static ParsedStrategy parseWithAntlr(final String strategy) {
    return NaturalLanguageStrategyService.parseWithAntlr(CharStreams.fromString(strategy));
}

From source file:com.github.robozonky.strategy.natural.NaturalLanguageStrategyService.java

License:Apache License

private static synchronized ParsedStrategy parseOrCached(final String strategy) {
    return getCached(strategy).orElseGet(() -> {
        LOGGER.trace("Parsing started.");
        final ParsedStrategy parsed = parseWithAntlr(CharStreams.fromString(strategy));
        LOGGER.trace("Parsing finished.");
        setCached(strategy, parsed);/*from   w  w w .  j a va2s.c  om*/
        return parseOrCached(strategy); // call itself again, making use of the cache
    });
}