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:es.us.isa.sedl.marshaller.execution.ExecutionsParser.java

@Override
public List<Execution> parse(String s, SEDL4PeopleExtendedListener listener) {
    List<Execution> result = null;
    try {//from  w  ww.j a  v a  2 s.  com
        ByteArrayInputStream stream = new ByteArrayInputStream(s.getBytes());
        SEDL4PeopleLexer lexer = new SEDL4PeopleLexer(new ANTLRInputStream(stream));
        //lexer.addErrorListener(errorListener);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        SEDL4PeopleParser parser = new SEDL4PeopleParser(tokens);
        //parser.addErrorListener(errorListener);                       
        result = parse(parser.execution(), listener);
    } catch (IOException ex) {
        java.util.logging.Logger.getLogger(DatasetSpecificationParser.class.getName())
                .log(java.util.logging.Level.SEVERE, null, ex);
    }
    return result;
}

From source file:eu.ensure.ipqet.eqel.EqelLoader.java

License:Open Source License

public /* aggregation tree */ void load(InputStream is) throws IOException, RecognitionException {
    ANTLRInputStream input = new ANTLRInputStream(is);

    EqelLexer lexer = new EqelLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    EqelParser parser = new EqelParser(tokens);
    parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    ParserRuleContext tree = parser.statements(); // parse

    ParseTreeWalker walker = new ParseTreeWalker(); // a standard walker
    Listener listener = new Listener(parser, domainSpecifications, validationSpecifications, this);
    walker.walk(listener, tree); // initiate walk to tree with listener
}

From source file:eu.eubrazilcc.lvl.storage.ql.QueryLoader.java

License:EUPL

public static void load(final String query) throws IOException {
    try (final InputStream is = new ByteArrayInputStream(query.getBytes())) {
        final ANTLRInputStream input = new ANTLRInputStream(is);
        final LvLQLLexer lexer = new LvLQLLexer(input);
        final CommonTokenStream tokens = new CommonTokenStream(lexer);
        final LvLQLParser parser = new LvLQLParser(tokens);
        final ParseTree tree = parser.query();

        final ParseTreeWalker walker = new ParseTreeWalker();
        final QueryLoader loader = new QueryLoader();
        walker.walk(loader, tree);//from  w ww . j  ava 2s. c  o m

        // TODO
    }
}

From source file:eu.himeros.dictparser.Main.java

License:Open Source License

/**
 * @param args the command line arguments
 *///  w w  w  .  j  ava2 s  . c om
public static void main(String[] args) {
    // TODO code application logic here
    try {
        FileInputStream fis = new FileInputStream(args[0]);
        ANTLRInputStream input = new ANTLRInputStream(fis);
        DictParserLexer lexer = new DictParserLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        DictParserParser parser = new DictParserParser(tokens);

        parser.main(args);
    } catch (Exception ex) {
    }
}

From source file:eu.mihosoft.vrl.licenseheaderutil.LicenseHeaderUtil.java

License:Open Source License

/**
 * Changes the license header of the specified .java source file. Everything
 * between the beginning of the file and the package declaration wil be
 * replaced with the specified string.//w  w  w .j  a v  a  2 s.c o  m
 *
 * @param srcFile the source file
 * @param destFile the destination file (can be equal to src file)
 * @param licenseComment license comment (must be a valid Java comment)
 * @return <code>true</code> if the file could be processed;
 * <code>false</code> otherwise
 * @throws RecognitionException
 * @throws IOException
 */
public static boolean changeLicenseHeaderOfFile(Path srcFile, Path destFile, String licenseComment)
        throws RecognitionException, IOException {

    Date now = new Date();
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    DateFormat yearFormat = new SimpleDateFormat("yyyy");
    DateFormat monthFormat = new SimpleDateFormat("MM");
    DateFormat dayFormat = new SimpleDateFormat("dd");

    licenseComment = licenseComment.replace("${VRL-LICENSE-HEADER-FILE-NAME}",
            destFile.getFileName().toString());

    licenseComment = licenseComment.replace("${VRL-LICENSE-HEADER-YEAR}", "" + yearFormat.format(now));

    licenseComment = licenseComment.replace("${VRL-LICENSE-HEADER-MONTH}", "" + monthFormat.format(now));

    licenseComment = licenseComment.replace("${VRL-LICENSE-HEADER-DAY}", "" + dayFormat.format(now));

    licenseComment = licenseComment.replace("${VRL-LICENSE-HEADER-DATE}", "" + dateFormat.format(now));

    InputStream is = null;

    if (srcFile != null && Files.isRegularFile(srcFile)) {
        is = Files.newInputStream(srcFile);
    }

    ANTLRInputStream input = new ANTLRInputStream(is);

    JavaLexer lexer = new JavaLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    JavaParser parser = new JavaParser(tokens);
    ParserRuleContext tree = parser.compilationUnit();

    ParseTreeWalker walker = new ParseTreeWalker();
    ChangeLicenseHeaderListener extractor = new ChangeLicenseHeaderListener(parser);
    walker.walk(extractor, tree);

    String fullCode = licenseComment + "\n" + extractor.getCode();
    if (extractor.hasPackage()) {
        Files.write(destFile, fullCode.getBytes("UTF-8"));
    } else {
        System.out.println(" -> skipping: " + srcFile);
        return false;
    }

    return true;
}

From source file:feast.expressions.ExpCalculator.java

License:Open Source License

@Override
public void initAndValidate() throws Exception {

    // Assemble name->param map
    Map<String, Function> functionsMap = new HashMap<>();
    for (Function func : functionsInput.get()) {
        BEASTObject obj = (BEASTObject) func;
        functionsMap.put(obj.getID(), func);
    }/*w ww .  j av a  2  s.c o m*/

    // Build AST from expression string
    ANTLRInputStream input = new ANTLRInputStream(expressionInput.get());
    ExpressionLexer lexer = new ExpressionLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ExpressionParser parser = new ExpressionParser(tokens);
    parseTree = parser.expression();

    // Create new visitor for calculating expression values:
    visitor = new ExpCalculatorVisitor(functionsMap);

    update();
}

From source file:feast.expressions.ExpCalculatorDistribution.java

@Override
public void initAndValidate() throws Exception {

    // Assemble name->param map
    Map<String, Function> functionsMap = new HashMap<>();
    for (Function func : functionsInput.get()) {
        BEASTObject obj = (BEASTObject) func;
        functionsMap.put(obj.getID(), func);
    }/*from  w ww.ja v a2  s.  co m*/

    // Build AST from expression string
    ANTLRInputStream input = new ANTLRInputStream(expressionInput.get());
    ExpressionLexer lexer = new ExpressionLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ExpressionParser parser = new ExpressionParser(tokens);
    parseTree = parser.expression();

    // Create new visitor for calculating expression values:
    visitor = new ExpCalculatorVisitor(functionsMap);

    update();

    if (res.length != 1)
        throw new IllegalArgumentException("ExpCalculatorDistribution " + "expressions must be single-valued.");
}

From source file:feast.expressions.ExpCalculatorParametricDistribution.java

License:Open Source License

@Override
public void initAndValidate() throws Exception {

    // Parameter to house real value
    param = new LightParameter(1.0);

    // Assemble name->param map
    Map<String, Function> functionsMap = new HashMap<>();
    functionsMap.put("x", param);

    // Build AST from expression string
    ANTLRInputStream input = new ANTLRInputStream(expressionInput.get());
    ExpressionLexer lexer = new ExpressionLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ExpressionParser parser = new ExpressionParser(tokens);
    parseTree = parser.expression();/*w ww  .j av a2s.  co m*/

    // Create new visitor for calculating expression values:
    visitor = new ExpCalculatorVisitor(functionsMap);

    if (visitor.visit(parseTree).length != 1)
        throw new IllegalArgumentException("ExpCalculatorDistribution " + "expressions must be single-valued.");
}

From source file:fi.hsl.parkandride.core.domain.Spatial.java

License:EUPL

private static WKTParser newParser(String input) {
    WKTLexer lexer = new WKTLexer(new ANTLRInputStream(input));
    lexer.removeErrorListeners();//  www  .  ja v a2 s.c  o  m
    lexer.addErrorListener(ERROR_LISTENER);
    WKTParser parser = new WKTParser(new CommonTokenStream(lexer));
    parser.removeErrorListeners();
    parser.addErrorListener(ERROR_LISTENER);
    return parser;
}

From source file:fr.inria.oak.paxquery.xparser.client.XClient.java

License:Apache License

private LogicalPlan parseQuery(InputStream inputStream, String outputpath, boolean drawTrees, String graphsPath)
        throws Exception {
    // VISITOR VERSION
    // create a CharStream that reads from standard input
    ANTLRInputStream input = new ANTLRInputStream(inputStream);
    // create a lexer that feeds off of input CharStream
    XQueryLexer lexer = new XQueryLexer(input);

    // create a buffer of tokens pulled from the lexer
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // create a parser that feeds off the tokens buffer
    XQueryParser parser = new XQueryParser(tokens);

    ParseTree tree = parser.xquery();/* w  w w. jav a  2  s  .  com*/
    XQueryVisitorImplementation loader = new XQueryVisitorImplementation(outputpath);
    loader.visit(tree);

    // print output
    System.out.println("Creating algebraic plan.");
    System.out.println("Original algebraic plan: ");
    printParseDetails(tree, parser, loader);
    // draw Logical Plan
    if (drawTrees) {
        NavigationTreePattern.setGraphicsPath(graphsPath);
        NavigationTreePattern.resetPrintCardinal();
        NavigationTreePattern.setPrintGraphics(true);
        drawLogicalPlan(loader.logicalPlan, graphsPath, false);
    }

    // optimized algebraic plan
    System.out.println("Optimizing algebraic plan.");
    Optimizer.INSTANCE.optimize(loader.logicalPlan);
    System.out.println("Optimized algebraic plan: ");
    printParseDetails(tree, parser, loader);
    // draw Logical Plan
    if (drawTrees) {
        XMLScan.resetColorCounter();
        NavigationTreePattern.setPrintGraphics(false);
        drawLogicalPlan(loader.logicalPlan, graphsPath, true);
    }

    return loader.logicalPlan;
}