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:citrus.Interpreter.java

License:Apache License

public SchemeObject read(String inputStr) throws IOException {
    ANTLRInputStream input = new ANTLRInputStream(inputStr);
    SchemeLexer lexer = new SchemeLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SchemeParser parser = new SchemeParser(tokens);
    ParseTree tree = parser.datum();//from   ww  w .j  ava  2s  .c  om

    SchemeReadVisitor visitor = new SchemeReadVisitor();
    return visitor.visit(tree);
}

From source file:cloudlens.parser.ASTBuilder.java

License:Apache License

public static List<ASTElement> parse(ANTLRInputStream script) {
    final CloudLensLexer clLexer = new CloudLensLexer(script);
    clLexer.removeErrorListeners();/*from ww w.j  ava 2 s .c o  m*/
    clLexer.addErrorListener(ASTError.INSTANCE);
    final CommonTokenStream token = new CommonTokenStream(clLexer);
    final CloudLensParser clParser = new CloudLensParser(token);
    clParser.removeErrorListeners();
    clParser.addErrorListener(ASTError.INSTANCE);
    final ASTBuilder clAST = new ASTBuilder();
    clAST.visit(clParser.top());
    return clAST.astElements;
}

From source file:codemate.Fortran.FortranProcessor.java

License:Open Source License

/**
 * callParser/*from w ww .  j  a  v a 2 s. c o m*/
 *
 * This method parses the given input stream from root 'file' rule.
 *
 * @param       entity           The code entity to be parsed
 *
 * @author      Li Dong <dongli@lasg.iap.ac.cn>
 */
public static void callParser(CodeEntity entity) throws Exception {
    InputStream inputStream = new FileInputStream(entity.getPath());
    ANTLRInputStream input = new ANTLRInputStream(inputStream);
    FortranLexer lexer = new FortranLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    FortranParser parser = new FortranParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(new FortranErrorListener());
    entity.setParseTree(parser.file());
}

From source file:codemate.Fortran.FortranProcessor.java

License:Open Source License

/**
 * callParser//from w  w w . j  a  va 2s.  c  o m
 *
 * This method parses pieces of Fortran code. It is called by
 * FortranTemplater.
 *
 * @param       type           The starting parser rule.
 * @param       source          A input string for parsing.
 * @return                      The parse tree.
 *
 * @author      Li Dong <dongli@lasg.iap.ac.cn>
 */
public static ParseTree callParser(int ruleIndex, String source) throws Exception {
    ANTLRInputStream input = new ANTLRInputStream(source);
    FortranLexer lexer = new FortranLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    FortranParser parser = new FortranParser(tokens);
    ParseTree tree = null;
    // NOTE: We set the rules that can be used.
    if (ruleIndex == FortranParser.RULE_declarationStatements) {
        tree = parser.declarationStatements();
    } else if (ruleIndex == FortranParser.RULE_executableStatements) {
        tree = parser.executableStatements();
    } else if (ruleIndex == FortranParser.RULE_typeDeclarationStatement) {
        tree = parser.typeDeclarationStatement();
    } else if (ruleIndex == FortranParser.RULE_containedProcedures) {
        tree = parser.containedProcedures();
    } else if (ruleIndex == FortranParser.RULE_derivedTypeName) {
        tree = parser.derivedTypeName();
    } else if (ruleIndex == FortranParser.RULE_expression) {
        tree = parser.expression_();
    } else if (ruleIndex == FortranParser.RULE_accessibilityStatements) {
        tree = parser.accessibilityStatements();
    } else {
        UI.error("codemate", "Unimplemented parser rule!");
    }
    return tree;
}

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

License:Apache License

/**
 * Prepare parser./*from www  .  j av  a  2  s.com*/
 *
 * @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.//  w  w w  .  j  a v a2s . 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.//from  w  w  w .j  a  v a  2  s  .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 a2s.  c  om*/
 * 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./*from  w w  w .j a va2s.  c o  m*/
 *
 * @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.//www  .j  av  a2 s  . c o m
 *
 * @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);
}