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:dbms.ANTGui.java

public void compilar() {
    try {/*from  ww  w  . j  a  va 2  s.c  om*/

        jTextArea3.setText("");
        String in = "";

        if (!this.runSelected) {
            in = this.jTextArea2.getText();
        } else {
            if (this.jTextArea2.getStringSelected().isEmpty()) {
                DBMS.throwMessage("No hay nada seleccionado");
            } else {
                in = this.jTextArea2.getStringSelected();
            }
            this.jTextArea2.setStringSelected("");
        }

        CharStream cs = new ANTLRInputStream(in);

        sqlLexer lexer = new sqlLexer(cs);
        lexer.removeErrorListeners();
        lexer.addErrorListener(DescriptiveErrorListener.INSTANCE);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        parser = new sqlParser(tokens);
        parser.removeErrorListeners();
        parser.addErrorListener(DescriptiveErrorListener.INSTANCE);
        contexto = parser.sql2003Parser();
        ParseTree tree = contexto;

        // Specify our entry point

        ruleNames = parser.getRuleNames();

        int errorsCount = parser.getNumberOfSyntaxErrors();
        System.out.println(errorsCount);
        if (errorsCount == 0) {
            System.out.println("Parseo Exitoso");

            JOptionPane.showMessageDialog(this, "Parseo Exitoso!");
            Visitor vistor = new Visitor();
            VisitorAdolfo vistorAdolfo = new VisitorAdolfo();
            VisitorChuso vistorChuso = new VisitorChuso();
            vistor.visit(tree);
            vistorAdolfo.visit(tree);
            vistorChuso.visit(tree);

        } else {
            JOptionPane.showMessageDialog(this, "Parseo Fallido!");
        }

    } catch (RecognitionException e) {
        System.out.println("ERROR");

    }

}

From source file:de.adrodoc55.minecraft.mpl.ide.autocompletion.AutoCompletion.java

License:Open Source License

public static AutoCompletionContext getContext(int index, String text) {
    ANTLRInputStream input = new ANTLRInputStream(text);
    MplLexer lexer = new MplLexer(input);
    lexer.removeErrorListeners();/*w w  w .j  av  a 2  s . c o m*/
    TokenStream tokens = new CommonTokenStream(lexer);
    MplParser parser = new MplParser(tokens);
    parser.removeErrorListeners();
    FileContext ctx = parser.file();

    AutoCompletionListener listener = new AutoCompletionListener(index);
    try {
        new ParseTreeWalker().walk(listener, ctx);
    } catch (ResultException earlyExit) {
        return earlyExit.getResult();
    }
    return null;
}

From source file:de.adrodoc55.minecraft.mpl.interpretation.MplInterpreter.java

License:Open Source License

private FileContext parse(File programFile) throws IOException {
    byte[] bytes = Files.readAllBytes(programFile.toPath());
    ANTLRInputStream input = new ANTLRInputStream(FileUtils.toUnixLineEnding(new String(bytes)));
    MplLexer lexer = new MplLexer(input);
    TokenStream tokens = new CommonTokenStream(lexer);
    MplParser parser = new MplParser(tokens);
    parser.removeErrorListeners();/*from   w w  w. j  av  a 2s .  c om*/
    parser.addErrorListener(new BaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object token, int line, int charPositionInLine,
                String message, RecognitionException cause) {
            MplSource source = toSource((Token) token);
            addException(new CompilerException(source, message));
        }
    });
    FileContext context = parser.file();
    // Trees.inspect(context, parser);
    return context;
}

From source file:de.bioviz.parser.BioParser.java

License:Open Source License

/**
 * Parses a String in BioGram format./* w w w. j a  v a2  s .  c o m*/
 *
 * @param inputString
 *       String containing a BioGram description.
 * @return Biochip as described in the String, null if not parsable
 */
public static Biochip parse(final String inputString) {

    logger.trace("Parsing file of length {}", inputString.length());

    try {
        ANTLRInputStream input = new ANTLRInputStream(inputString);
        BioLexerGrammar lexer = new BioLexerGrammar(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        Bio parser = new Bio(tokens);
        parser.removeErrorListeners();
        BioErrorListener errorListener = new BioErrorListener();
        parser.addErrorListener(errorListener);
        ParseTree tree = parser.bio(); // parse everything

        if (errorListener.hasErrors()) {
            Biochip chip = new Biochip();
            for (final String msg : errorListener.getErrors()) {
                logger.error(msg);
                chip.hardErrors.add(msg);
            }
            return chip;
        } else {
            ParseTreeWalker walker = new ParseTreeWalker();
            // Walk the tree created during the parse, trigger callbacks
            BioParserListener listener = new BioParserListener();
            walker.walk(listener, tree);
            Biochip biochip = listener.getBiochip();
            List<String> annotations = parseChannel(input, BioLexerGrammar.ANNOTATION);
            biochip.addAnnotations(annotations);
            return biochip;
        }
    } catch (final Exception e) {
        logger.error("Failed to parse file");
        e.printStackTrace();
        return null;
    }
}

From source file:de.bioviz.parser.BioParser.java

License:Open Source License

/**
 * Parses the annotations in a file.//w ww . jav a  2 s. co  m
 * @param input an ANTLRInputStream
 * @param channel the channel to parse
 * @return A List of Strings containing the annotations.
 */
private static List<String> parseChannel(final ANTLRInputStream input, final int channel) {
    BioLexerGrammar lexer = new BioLexerGrammar(input);

    lexer.reset();
    CommonTokenStream cts = new CommonTokenStream(lexer);
    List<String> channelTokens = new ArrayList<>();

    // this one gets everything that is in the stream.
    cts.getText();
    // now we can use size() to run over the tokens
    for (int i = 0; i < cts.size(); i++) {
        Token token = cts.get(i);
        // and check here if the token is on the right channel
        if (token.getChannel() == channel) {
            logger.trace("Parsing Comment: " + token.getText());
            channelTokens.add(token.getText());
        }
    }

    return channelTokens;
}

From source file:de.codesourcery.luaparser.LuaToJSON.java

License:Apache License

public String getJSON(File luaFile) throws IOException {

    this.luaInputFile = luaFile;

    final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();

    jsonWriter = new BufferedWriter(new PrintWriter(byteOut));

    final String input = readFile(luaInputFile);

    final LuaLexer lexer = new LuaLexer(new ANTLRInputStream(input));

    final CommonTokenStream tokens = new CommonTokenStream(lexer);

    final LuaParser parser = new LuaParser(tokens);

    final LuaParser.ChunkContext context = parser.chunk();

    final NodeSearcher funcSearcher = new NodeSearcher(tree -> {
        if (tree instanceof FunctioncallContext) {
            if (isMonitoredMethod((FunctioncallContext) tree)) {
                return true;
            }/*  w  w w . j av  a 2  s .com*/
        }
        return false;
    });
    visitPreOrder(context, funcSearcher);

    jsonWriter.append("{");

    funcSearcher.forEach((funcContext, lastItem) -> {
        final FunctioncallContext match = (FunctioncallContext) funcContext;

        final TableconstructorContext child = (TableconstructorContext) match.getChild(1) // NameAndArgsContext
                .getChild(2) // ArgsContext
                .getChild(1) // ExpListContext
                .getChild(0) // ExpContext
                .getChild(0); // TableconstructorContext

        topLevelTable = child;

        String text = "";
        text += printValue(child, true);

        try {
            jsonWriter.append(text);
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    });
    jsonWriter.append(" }");

    jsonWriter.close();

    return byteOut.toString();
}

From source file:de.dfki.kiara.impl.ConnectionImpl.java

License:Open Source License

private void loadIDL(ANTLRInputStream input, String fileName) throws IOException {
    // create a lexer that feeds off of input CharStream
    KiaraLexer lexer = new KiaraLexer(input);
    // create a buffer of tokens pulled from the lexer
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // create a parser that feeds off the tokens buffer
    KiaraParser parser = new KiaraParser(tokens);
    ParseTree tree = parser.program(); // begin parsing at program rule

    ParseTreeWalker walker = new ParseTreeWalker();

    KiaraKTDConstructor ktdConstructor = new KiaraKTDConstructor(module, fileName);
    walker.walk(ktdConstructor, tree);//w  w  w.  ja va  2 s.com

    if (!ktdConstructor.getParserErrors().isEmpty()) {
        StringBuilder b = new StringBuilder("IDL parser errors:");
        b.append(System.lineSeparator());
        for (String error : ktdConstructor.getParserErrors()) {
            b.append(error);
            b.append(System.lineSeparator());
        }
        throw new IOException(b.toString());
    }
}

From source file:de.dfki.kiara.tool.KiaraIDLParser.java

License:Open Source License

public static void main(String[] args) throws Exception {
    System.out.println("Parsing file: " + args[0]);
    // create a CharStream that reads from standard input
    ANTLRInputStream input = new ANTLRInputStream(new FileInputStream(args[0]));
    // create a lexer that feeds off of input CharStream
    KiaraLexer lexer = new KiaraLexer(input);
    // create a buffer of tokens pulled from the lexer
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // create a parser that feeds off the tokens buffer
    KiaraParser parser = new KiaraParser(tokens);
    ParseTree tree = parser.program(); // begin parsing at program rule

    ParseTreeWalker walker = new ParseTreeWalker();

    World world = new World();
    Module module = new Module(world, "kiara");
    KiaraKTDConstructor ktdConstructor = new KiaraKTDConstructor(module, args[0]);
    walker.walk(ktdConstructor, tree);//from  w  w  w.j  a va  2 s .  c om
    //System.out.println(tree.toStringTree(parser)); // print LISP-style tree

    if (!ktdConstructor.getParserErrors().isEmpty()) {
        System.err.println("Parser errors:");
        for (String error : ktdConstructor.getParserErrors()) {
            System.err.print("ERROR: ");
            System.err.println(error);
        }
    }

    IDLWriter idlWriter = new IDLWriter(module);
    idlWriter.write(System.out);
}

From source file:de.dfki.kiara.tool.ThriftIDLParser.java

License:Open Source License

public static void main(String[] args) throws Exception {
    System.out.println("Parsing file: " + args[0]);
    // create a CharStream that reads from standard input
    ANTLRInputStream input = new ANTLRInputStream(new FileInputStream(args[0]));
    // create a lexer that feeds off of input CharStream
    ThriftLexer lexer = new ThriftLexer(input);
    // create a buffer of tokens pulled from the lexer
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // create a parser that feeds off the tokens buffer
    ThriftParser parser = new ThriftParser(tokens);
    ParseTree tree = parser.program(); // begin parsing at program rule
    System.out.println(tree.toStringTree(parser)); // print LISP-style tree
}

From source file:de.dfki.resc28.ole.bootstrap.App.java

License:Open Source License

private static void parseFile(File file) throws IOException {
    if (file.isFile()) {

        // parse the .DAT file and create RDF models for asset and its .DAT distribution
        InputStream fis = new FileInputStream(file);
        LDrawLexer lexer = new LDrawLexer(new ANTLRInputStream(fis));
        LDrawParser parser = new LDrawParser(new CommonTokenStream(lexer));
        ParseTreeWalker walker = new ParseTreeWalker();
        ParseTree tree = parser.file();//  w  w  w . j a  va2 s  .  c  om

        walker.walk(new AssetListener(file.getName(), fGraphStore), tree);
        walker.walk(new LdrawDistributionListener(file.getName(), fGraphStore), tree);
        //         // FIXME: create users
        //         walker.walk(new UserListener(file.getName(), fGraphStore), tree);

        // add asset to repo
        Resource asset = fRepoModel
                .createResource(Util.joinPath(fAssetBaseUri, FilenameUtils.getBaseName(file.getName())));
        fRepoModel.add(fRepo, DCAT.dataset, asset);

        // close InputStream
        fis.close();
    }
}