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

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

Introduction

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

Prototype

public ANTLRInputStream(InputStream input) throws IOException 

Source Link

Usage

From source file:App.java

License:Apache License

public static void main(String[] args) {
    try {/*from   w w  w  . ja va  2s.  co  m*/
        //      ANTLRInputStream input = new ANTLRInputStream("doc(doc)/PERSONAE");
        ANTLRInputStream input = new ANTLRInputStream("doc(doc)/PERSONAE/TITLE");

        xPathLexer lexer = new xPathLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);

        xPathParser parser = new xPathParser(tokens);
        parser.removeErrorListeners();
        ParseTree tree = parser.ap();
        EvalXpath evalpath = new EvalXpath();
        evalpath.visit(tree);

    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("Error: " + e.getMessage());
    }
}

From source file:HSMgen.java

License:Open Source License

public static void main(String[] args) throws Exception {
    String inputFile = null;/*from www . j  av  a2s .co  m*/

    // Push global environment.
    pushEnv(new SymEnv());

    if (args.length > 0)
        inputFile = args[0];
    InputStream is = System.in;
    if (inputFile != null)
        is = new FileInputStream(inputFile);
    HSMgenLexer lexer = new HSMgenLexer(new ANTLRInputStream(is));
    HSMgenParser parser = new HSMgenParser(new CommonTokenStream(lexer));
    parser.setBuildParseTree(true); // tell ANTLR to build a parse tree
    ParseTree tree = parser.init();

    // Needed for 'NULL' definition.
    System.out.println("#include <cstddef>");

    new ParseTreeWalker().walk(new HSMgen(), tree);
}

From source file:ComplexityListenerAggregator.java

License:Open Source License

private static ParseTree GetFileParseTree(String filePath) throws Exception {
    BufferedReader fs = new BufferedReader(new FileReader(filePath));
    System.out.println("Parsing file: " + filePath);

    // create a CharStream that reads from standard input
    ANTLRInputStream input = new ANTLRInputStream(fs);

    // create a lexer that feeds off of input CharStream
    VisualBasic6Lexer lexer = new VisualBasic6Lexer(input);

    // create a buffer of tokens pulled from the lexer
    CommonTokenStream tokens = new CommonTokenStream(lexer);

    // parse the file
    VisualBasic6Parser parser = new VisualBasic6Parser(tokens);
    ParseTree tree = parser.file();//from w  w  w .j a  va  2s .  c  om

    return tree;
}

From source file:Extended$parser_class.java

License:BSD License

public static void main(String[] args) {
    try {/*w ww  . j av a  2  s.  c  o  m*/
        ExtendedTargetLexer lexer = new ExtendedTargetLexer(
                new ANTLRInputStream(new DataInputStream(System.in)));
        lexer.addErrorListener(new ExtendedErrorListener());
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        Extended$parser_class parser = new Extended$parser_class(tokens);
        ExtendedTargetListener listener = new ExtendedTargetListener(parser);

        parser.addParseListener(listener);
        Extended$parser_class.class.getMethod(args[0]).invoke(parser);
        parser.syntaxErrorWarning();
        try (JsonWriter w = Json.createWriter(System.out)) {
            w.write(listener.root.createJsonObjectBuilder().build());
        }
    } catch (Exception e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
}

From source file:tns2toad.java

License:Open Source License

public static void main(String[] args) throws Exception {

    // A bit of "sign on and blow my own trumpet stuff" ;-)
    String thisVersion = "0.1"; // Version of this utility.

    // Assume tnsnames.ora will be piped via stdin, unless we get a parameter passed.
    String tnsnamesFilename = null;
    InputStream iStream = System.in;

    // How many positional options are we expecting?
    int expectedPositionalArgs = 1;

    // These are collected from the command line.
    File inputFile = null;/*from   w  ww  .j  a v a 2 s .  c o  m*/
    String oracleHome = "";
    String userName = "";

    // These are used to process the command line.
    int i = 0;
    String thisArg;

    //---------------------------------------------------------
    // Let's scan the command line and see what needs doing ...
    //---------------------------------------------------------

    // Scan along the args array, looking at all the options. These
    // are all  prefixed by "--" and must all be before any of the
    // positional arguments.
    // When we find one, we zap it!
    // Each option takes a parameter - they get zapped also.
    // ThisArg holds the argument, i points at the parameter for it.
    while (i < args.length && args[i].startsWith("--")) {
        thisArg = args[i].toLowerCase();
        args[i++] = "";

        // Oracle Home...
        if (thisArg.equals("--oracle_home")) {
            if (i < args.length) {
                oracleHome = args[i];
                args[i++] = "";
            } else {
                usage("ERROR: --oracle_home requires a path name");
            }
        }
        // User name...
        else if (thisArg.equals("--user")) {
            if (i < args.length) {
                userName = args[i];
                args[i++] = "";
            } else {
                usage("ERROR: --user requires a username");
            }
        }
        // Something else? Not permitted.
        else {
            usage("Invalid option '" + thisArg + "'");
        }

    }

    // At this point we should be sitting with i pointing at the first
    // positional argument. Scan those next. All the options have been
    // extracted now, and zapped.

    // However, just exactly how many positional args do we want? This will
    // also catch any --options mingling within the positional args.
    if (i != args.length - expectedPositionalArgs) {
        usage("Unexpected or insufficient positional parameter(s) supplied.");
    }

    // We should only have a single parameter here, the tnsnames.ora file.
    tnsnamesFilename = args[i];

    //---------------------------------------------------------
    // Well, we got here, args on the command line must be ok
    // Check if we can open and/or read the tnsnames.ora file.
    //---------------------------------------------------------
    inputFile = new File(tnsnamesFilename);
    if (inputFile.isFile()) {
        iStream = new FileInputStream(tnsnamesFilename);
        //tnsnamesFilename = inputFile.getCanonicalPath();
    } else {
        System.out.println("\nERROR 1: '" + tnsnamesFilename + "' is not a valid filename.\n");
        System.exit(1); // Error exit.
    }

    //---------------------------------------------------------
    // Everything is fine, let's JFDI! :-)
    //---------------------------------------------------------

    // Feed the tnsnames.ora file into the lexer and get a
    // token stream from the lexer...
    ANTLRInputStream input = new ANTLRInputStream(iStream);
    tnsnamesLexer lexer = new tnsnamesLexer(input);

    // Feed the lexer's token stream to the parser and get
    // a parse tree out in return...
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    tnsnamesParser parser = new tnsnamesParser(tokens);
    ParseTree tree = parser.tnsnames();

    // Feed the parse tree to the tree walker & the listener
    // and get a load of text on stdout as a final result.
    // That is your import file, redirect it to a file and
    // let Toad import it for you.
    ParseTreeWalker tnsWalker = new ParseTreeWalker();
    tns2toadListener tnsListener = new tns2toadListener(parser, userName, oracleHome);
    tnsWalker.walk(tnsListener, tree);
}

From source file:SExpr.java

License:Open Source License

public MusicboxParser parse(String query) {

    // create a lexer that feeds off of input CharStream
    MusicboxLexer lexer = new MusicboxLexer(new ANTLRInputStream(query));
    // create a buffer of tokens pulled from the lexer
    CommonTokenStream tokens = new CommonTokenStream(lexer); // create a parser that feeds off the tokens buffer
    MusicboxParser parser = new MusicboxParser(tokens);
    return parser;

}

From source file:Cymbol.java

/**
 * @param args the command line arguments
 *///from   w ww.  j  a  va 2s .  com
public static void main(String[] args) {

    if (args.length == 1) {
        try {
            // parsing
            // create file input stream
            FileInputStream source = new FileInputStream(args[0]);
            // create a CharStream that reads from standard input
            ANTLRInputStream input = new ANTLRInputStream(source);
            // create a lexer that feeds off of input CharStream
            CymbolLexer lexer = new CymbolLexer(input);
            // create a buffer of tokens pulled from the lexer
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            // create a parser that feeds off the tokens buffer
            CymbolParser parser = new CymbolParser(tokens);
            ParseTree tree = parser.file(); // begin parsing at init rule

            // dump ast
            System.out.println(tree.toStringTree(parser)); // print LISP-style tree

            // build call graph
            ParseTreeWalker walker = new ParseTreeWalker();
            FunctionListener collector = new FunctionListener();
            walker.walk(collector, tree);
            System.out.println(collector.graph.toString());
            System.out.println(collector.graph.toDOT());

        } catch (IOException e) {
            System.out.print("error: " + e.getMessage());
        }
    } else {
        System.out.print("error: syntax is Cymbol <file path> !");
    }
}

From source file:Ecmascript.java

/**
 * @param args the command line arguments
 *//*from w w  w  .  j ava 2  s.  c  o m*/
public static void main(String[] args) {
    if (args.length == 1) {
        try {
            // parsing
            // create file input stream
            FileInputStream source = new FileInputStream(args[0]);
            // create a CharStream that reads from standard input
            ANTLRInputStream input = new ANTLRInputStream(source);
            // create a lexer that feeds off of input CharStream
            ECMAScriptLexer lexer = new ECMAScriptLexer(input);
            // create a buffer of tokens pulled from the lexer
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            // create a parser that feeds off the tokens buffer
            ECMAScriptParser parser = new ECMAScriptParser(tokens);
            ParseTree tree = parser.program(); // begin parsing at init rule

            // dump ast
            System.out.println("AST is : " + tree.toStringTree(parser)); // print LISP-style tree

            // build call graph
            ParseTreeWalker walker = new ParseTreeWalker();
            FunctionListener collector = new FunctionListener();
            walker.walk(collector, tree);
            System.out.println(collector.graph.toString());
            System.out.println(collector.graph.toDOT());

        } catch (IOException e) {
            System.out.print("error: " + e.getMessage());
        }
    } else {
        System.out.print("error: syntax is Cymbol <file path> !");
    }
}

From source file:ai.grakn.graql.Autocomplete.java

License:Open Source License

/**
 * @param query a graql query/*from  w  w  w  . j a va 2  s.  co m*/
 * @return a list of tokens from running the lexer on the query
 */
private static List<? extends Token> getTokens(String query) {
    ANTLRInputStream input = new ANTLRInputStream(query);
    GraqlLexer lexer = new GraqlLexer(input);

    // Ignore syntax errors
    lexer.removeErrorListeners();
    lexer.addErrorListener(new BaseErrorListener());

    return lexer.getAllTokens();
}

From source file:ai.grakn.graql.internal.parser.GremlinVisitor.java

License:Open Source License

public static void main(String[] args) throws IOException {
    System.out.println(prettify(new ANTLRInputStream(System.in)));
}