Java 类org.antlr.v4.runtime.tree.ParseTreeWalker 实例源码

项目:dragoman    文件:BaseParser.java   
private void parse(
    SQLParserListener sqlParserListener,
    ErrorListener errorListener,
    ParserRuleContext entryPoint) {
  ParseTreeWalker walker = new ParseTreeWalker();

  walker.walk(sqlParserListener, entryPoint);

  // if the parser call raised any exceptions then lets throw
  errorListener
      .getException()
      .ifPresent(
          ex -> {
            throw ex;
          });
}
项目:SQLParser    文件:SQLParser.java   
public PlanNode callSql(String sqlstring, ArrayList list) throws FileNotFoundException, IOException {

    for (int i=0; i< list.size() ; i++) {
        sqlstring = sqlstring.replaceFirst("\\?", (String) list.get(i));
    }
    ANTLRInputStream input = new ANTLRInputStream( new   ByteArrayInputStream(sqlstring.getBytes()));

    PLSQLLexer lexer = new PLSQLLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    PLSQLParser parser = new PLSQLParser(tokens);
    parser.setBuildParseTree(true);
    ParseTree tree = parser.sql_statement();
       ParseTreeWalker walker  = new ParseTreeWalker();
       SQLModel model = new SQLModel();
       walker.walk(new MyListener(model), tree);



       return model.current;
}
项目:digraph-parser    文件:GraphParser.java   
public GraphParser(InputStream is) throws GraphParserException {
    DOTLexer lexer = null;
    DOTParser parser = null;
    try {
        lexer = new DOTLexer(new ANTLRInputStream(is));
        lexer.addErrorListener(new ErrorListener());

        parser = new DOTParser(new CommonTokenStream(lexer));
        parser.setErrorHandler(new ExceptionErrorStrategy());
        parser.addErrorListener(new ErrorListener());

        ParseTree tree = parser.graph();
        ParseTreeWalker.DEFAULT.walk(new NodeListener(), tree);
        ParseTreeWalker.DEFAULT.walk(new EdgeListener(), tree);
    }
    catch (Throwable t) {
        if (mErrMsg != null) throw new GraphParserException(mErrMsg, t);
        if (lexer != null) {
            mErrMsg = "at line " + lexer.getLine() + ":" + lexer.getCharPositionInLine();
            throw new GraphParserException(mErrMsg, t);
        }
        throw new GraphParserException(t);
    }

    if (mErrMsg != null) throw new GraphParserException(mErrMsg);
}
项目:pgcodekeeper    文件:AntlrParser.java   
public static void parseSqlStream(InputStream inputStream, String charsetName,
        String parsedObjectName, List<AntlrError> errors,IProgressMonitor mon, int monitoringLevel,
        Collection<SQLParserBaseListener> listeners) throws IOException, InterruptedException {
    SQLParser parser = makeBasicParser(SQLParser.class, inputStream, charsetName, parsedObjectName, errors);
    parser.addParseListener(new CustomParseTreeListener(
            monitoringLevel, mon == null ? new NullProgressMonitor() : mon));
    try {
        SqlContext ctx = parser.sql();
        for (SQLParserBaseListener listener : listeners) {
            ParseTreeWalker.DEFAULT.walk(listener, ctx);
        }
    } catch (MonitorCancelledRuntimeException mcre){
        throw new InterruptedException();
    } catch (UnresolvedReferenceException ex) {
        errors.add(CustomSQLParserListener.handleUnresolvedReference(ex));
    }
}
项目:gmstruct    文件:Function.java   
public Script getScript() {
    Script script =  new Script(struct.getGMLName() + "_" + name, "");

    List<String> args = new ArrayList<>();
    if (context.idList() != null) {
        for (IdContext id : context.idList().id()) {
            args.add(id.getText());
            script.addParameter(id.getText(), "");
        }
    }

    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(new StaticAnalyzer(), context.block());

    //walker.walk(new ScriptWriter(script), context.block());

    return script;
}
项目:CacheStore    文件:QueryListenerImpl.java   
public void walkTree() {
    try {
        init();
        QueryLexer lexer = new QueryLexer(new ANTLRInputStream(new StringReader(queryStr)));
        CommonTokenStream token = new CommonTokenStream(lexer);
        QueryParser parser = new QueryParser(token);
        parser.setBuildParseTree(true);
        QueryParser.ScriptContext tree = parser.script(); // parse
        ParseTreeWalker parseTreeWalker = new ParseTreeWalker();
        parseTreeWalker.walk( this, tree);
        //check for key# if size = 1
        checkPredicateStack();
    } catch (Exception ex) {
        logger.error(ex.getMessage());
        throw new QueryException( ex.getMessage(), ex );
    }
}
项目:CacheStore    文件:TestQueryObject.java   
public static void main(String[] args) throws Exception  {
    String[] opts = new String[] {"-query"};
    String[] defaults = new String[] {"replace Order set name = \"test\", campaign.id = 10 , booking.no =20.2 where not record# = 1  ; insert Campaign set cid=20, cname=\"go\""};
    String[] paras = Utils.getOpts(args, opts, defaults);
    //String query = paras[0] ;
    String query = "select id, name, bookings from order where id = 10";
    //CharStream input = new UnbufferedTokenStream(new ANTLRInputStream( new StringReader(query)));
    ObjectQueryLexer lexer = new ObjectQueryLexer( new ANTLRInputStream( new StringReader(query)));
    CommonTokenStream token = new CommonTokenStream( lexer);
    ObjectQueryParser objectQueryParser = new ObjectQueryParser( token);
    objectQueryParser.setBuildParseTree( true);
    ParserRuleContext tree = objectQueryParser.script();
    ParseTreeWalker walker  = new ParseTreeWalker();
    System.out.println(walker.getClass().getName()+ " simple "+ walker.getClass().getSimpleName());
    System.out.println( tree.toString()) ;
}
项目:MatrixC    文件:Interpreter.java   
public void interpret() {

    ProgramContext parseTree = matrixParser.program();

    ParseTreeWalker.DEFAULT.walk(semanticListener, parseTree);

    List<SemanticError> errorList = semanticListener.getSemanticErrors();

    if (matrixParser.getNumberOfSyntaxErrors() > 0) {
      System.err.println("Aborting...");
    } else if (!errorList.isEmpty()) {
      System.err.println("------------------- ERRORS ---------------------");
      errorList.forEach(System.err::println);
    } else {
      CodeExecutor codeExecutor = new CodeExecutor(semanticListener.getSymbolTable(), parseTree);

      try {
        codeExecutor.execute();
      } catch (RuntimeError e) {
        System.err.println("[" + e.getClass().getSimpleName() + "]: " + e.getMessage());
        System.err.println("Aborting...");
      }
    }

  }
项目:TriAL-QL-Engine    文件:AppImpala.java   
/**
 * Special case: STORE operator.
 * 
 * @param queryArray
 * @param i
 * @param file
 * @return
 * @throws Exception
 */
public static ResultSet storeOperator(String[] queryArray, int i, boolean file) throws Exception {
    ParseTree tree = TriALQLParseQuery.parse(queryArray[i] + ";", file);
    TriALQLClassListener extractor = new TriALQLClassListener();
    ParseTreeWalker.DEFAULT.walk(extractor, tree);

    theLastQuery = true;
    if (QueryStruct.oldTableName.get(i).equals(QueryStruct.newTableName.get(i))) {
        // results = ImpalaDaemon.main(QueryStruct.baseQuery.get(i - 1));
        notToDrop = QueryStruct.newTableName.get(i);
    } else {
        parseTree = false;
        queryExecutor(queryArray[i] + ";", file, theLastQuery, parseTree);

        results = ImpalaDaemon.main(QueryStruct.baseQuery.get(i));
    }
    return results;
}
项目:Simulizer    文件:Assembler.java   
/**
 * Performs the first stage of assembling a program. But stops once it
 * determines whether the program is valid or not. This is useful when only
 * the validity of the program needs to be known.
 * @param input the program string to assemble
 * @return any problems with the program (empty list if program valid)
 */
public static List<Problem> checkForProblems(String input) {
    StoreProblemLogger log = new StoreProblemLogger();

    input += '\n'; // to parse correctly, must end with a newline

    SimpLexer lexer = new SimpLexer(new ANTLRInputStream(input));
    SimpParser parser = new SimpParser(new CommonTokenStream(lexer));

    // prevent outputting to the console
    lexer.removeErrorListeners();
    parser.removeErrorListeners();

    // try to parse a program from the input
    SimpParser.ProgramContext tree = parser.program();

    ProgramExtractor extractor = new ProgramExtractor(log);
    ParseTreeWalker.DEFAULT.walk(extractor, tree);

    return log.getProblems();
}
项目:Reo    文件:Interpreter.java   
/**
 * Parses a source file using ANTLR4, and walks over the parse tree to
 * interpret this source file as a Java object.
 * 
 * @param c
 *            input character stream
 * @param path
 *            location of the file
 * @return an interpreted source file, or null in case of an error.
 */
@Nullable
private ReoFile<T> parse(CharStream c, String path) {
    ReoLexer lexer = new ReoLexer(c);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ReoParser parser = new ReoParser(tokens);

    ErrorListener errListener = new ErrorListener(m);
    parser.removeErrorListeners();
    parser.addErrorListener(errListener);
    ParseTree tree = parser.file();
    if (errListener.hasError)
        return null;
    ParseTreeWalker walker = new ParseTreeWalker();
    listener.setFileName(path);
    walker.walk(listener, tree);
    return listener.getMain();
}
项目:clarpse    文件:ClarpseGoCompiler.java   
private void compileFiles(List<RawFile> files, OOPSourceCodeModel srcModel, List<String> projectFileTypes) {
    for (RawFile file : files) {
        try {
            CharStream charStream = new ANTLRInputStream(file.content());
            GolangLexer lexer = new GolangLexer(charStream);
            TokenStream tokens = new CommonTokenStream(lexer);
            GolangParser parser = new GolangParser(tokens);
            SourceFileContext sourceFileContext = parser.sourceFile();
            parser.setErrorHandler(new BailErrorStrategy());
            parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
            ParseTreeWalker walker = new ParseTreeWalker();
            GolangBaseListener listener = new GoLangTreeListener(srcModel, projectFileTypes, file);
            walker.walk(listener, sourceFileContext);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
项目:georocket    文件:DefaultQueryCompiler.java   
/**
 * <p>Create an Elasticsearch query for the given search string but does
 * not apply the {@link ElasticsearchQueryOptimizer} to it.</p>
 * @param search the search string
 * @return the Elasticsearch query (may be null)
 */
protected JsonObject compileQueryNoOptimize(String search) {
  if (search == null || search.isEmpty()) {
    // match everything by default
    return matchAllQuery();
  }

  // parse query
  QueryLexer lexer = new QueryLexer(new ANTLRInputStream(search.trim()));
  CommonTokenStream tokens = new CommonTokenStream(lexer);
  QueryParser parser = new QueryParser(tokens);
  QueryContext ctx = parser.query();

  // compile query to QueryBuilder
  QueryCompilerListener listener = new QueryCompilerListener();
  ParseTreeWalker.DEFAULT.walk(listener, ctx);

  if (listener.result.isEmpty()) {
    return matchAllQuery();
  }
  return listener.result.pop();
}
项目:thrifty    文件:ThriftParser.java   
/**
 * Parse the given Thrift {@code text}, using the given {@code location}
 * to anchor parsed elements withing the file.
 * @param location the {@link Location} of the data being parsed.
 * @param text the text to be parsed.
 * @param reporter an {@link ErrorReporter} to collect warnings.
 * @return a representation of the parsed Thrift data.
 */
public static ThriftFileElement parse(Location location, String text, ErrorReporter reporter) {
    ANTLRInputStream charStream = new ANTLRInputStream(text);
    AntlrThriftLexer lexer = new AntlrThriftLexer(charStream);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    AntlrThriftParser antlrParser = new AntlrThriftParser(tokenStream);

    ThriftListener thriftListener = new ThriftListener(tokenStream, reporter, location);

    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(thriftListener, antlrParser.document());

    if (reporter.hasError()) {
        String errorReports = Joiner.on('\n').join(reporter.formattedReports());
        String message = String.format(Locale.US, "Syntax errors in %s:\n%s", location, errorReports);
        throw new IllegalStateException(message);
    }

    return thriftListener.buildFileElement();
}
项目:mdetect    文件:ParseUtils.java   
public static Document processFile(String filePath) {
    Pair<Parser, Lexer> pl = parsePHP(filePath);
    PHPParser parser = (PHPParser) pl.a;
    parser.setBuildParseTree(true);
    /* 
     * htmlDocument is the start rule (the top-level rule)
     * for the PHP grammar
     */
    ParserRuleContext tree =   parser.htmlDocument();
    List<String> ruleNames = Arrays.asList(parser.getRuleNames());
    Map<Integer, String> invTokenMap = getInvTokenMap(parser);
    TokenStream tokenStream = parser.getTokenStream();
    ParseTreeDOMSerializer ptSerializer = new ParseTreeDOMSerializer(ruleNames, invTokenMap, tokenStream);
    ParseTreeWalker.DEFAULT.walk(ptSerializer, tree);
    Document result= ptSerializer.getDOMDocument();
    return result;
}
项目:TPKB    文件:TPKB.java   
/**
 * @param args
 * @throws IOException 
 * @throws SQLException 
 */
public static void main(String[] args) throws IOException, SQLException {

       TPKBLexer lexer = new TPKBLexer(new ANTLRFileStream("test.kb"));
       TPKBParser parser = new TPKBParser(new CommonTokenStream(lexer));

       TPKBClassListener listener = new TPKBClassListener();

       ParseTree tree = parser.tpkb();
       ParseTreeWalker walker = new ParseTreeWalker();
       walker.walk(listener, tree);

       MySQLEngine engine = new MySQLEngine();
       engine.processTSubRaw();
       engine.processTIsRaw();

       double Z = engine.computeZ();
       System.out.println("Z = "+Z);

       Z = engine.computeZWithEvidence();
       System.out.println("Z(E) = "+Z);     
}
项目:MPL    文件:AutoCompletion.java   
public static @Nullable AutoCompletionContext getContext(int index, String text) {
  ANTLRInputStream input = new ANTLRInputStream(text);
  MplLexer lexer = new MplLexer(input);
  lexer.removeErrorListeners();
  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;
}
项目:gdl    文件:GDLHandler.java   
/**
 * Checks valid input and creates GDL Handler.
 *
 * @param antlrInputStream ANTLR input stream
 * @return GDL handler
 */
private GDLHandler build(ANTLRInputStream antlrInputStream) {
  if (graphLabel == null) {
    throw new IllegalArgumentException("Graph label must not be null.");
  }
  if (vertexLabel == null) {
    throw new IllegalArgumentException("Vertex label must not be null.");
  }
  if (edgeLabel == null) {
    throw new IllegalArgumentException("Edge label must not be null.");
  }
  if (errorStrategy == null) {
    throw new IllegalArgumentException("Error handler must not be null.");
  }

  GDLLexer lexer = new GDLLexer(antlrInputStream);
  GDLParser parser = new GDLParser(new CommonTokenStream(lexer));
  parser.setErrorHandler(errorStrategy);

  GDLLoader loader = new GDLLoader(graphLabel, vertexLabel, edgeLabel,
    useDefaultGraphLabel, useDefaultVertexLabel, useDefaultEdgeLabel);
  new ParseTreeWalker().walk(loader, parser.database());
  return new GDLHandler(loader);
}
项目:apex-malhar    文件:SQLToKuduPredicatesTranslator.java   
public void parseKuduExpression() throws Exception
{
  KuduSQLExpressionLexer lexer = new KuduSQLExpressionLexer(CharStreams.fromString(sqlExpresssion));
  CommonTokenStream tokens = new CommonTokenStream( lexer );
  parser = new KuduSQLParser( tokens );
  errorListener = parser.getKuduSQLExpressionErrorListener();
  ParseTree parserTree = parser.kudusqlexpression();
  if (!errorListener.isSyntaxError()) {
    ParseTreeWalker parseTreeWalker = new ParseTreeWalker();
    kuduSQLParseTreeListener = new KuduSQLParseTreeListener();
    kuduSQLParseTreeListener.setColumnSchemaList(allColumnsForThisTable);
    try {
      parseTreeWalker.walk(kuduSQLParseTreeListener, parserTree);
    } catch (Exception ex) {
      LOG.error(" The supplied SQL expression could not be parsed because " + ex.getMessage(),ex);
      errorListener.setSyntaxError(true);
    }
  } else {
    LOG.error(" Syntax error present in the Kudu SQL expression. Hence not processing");
    List<String> allRegisteredSyntaxErrors = errorListener.getListOfErrorMessages();
    for (String syntaxErrorMessage : allRegisteredSyntaxErrors) {
      LOG.error(" Error : " + syntaxErrorMessage  + " in SQL expression \"" + sqlExpresssion + " \"");
    }
  }
}
项目:JsInteropGenerator    文件:IdlListener.java   
@Override
public void enterCallbackRest(CallbackRestContext ctx) {
    String interfaceName = ctx.Identifier().toString();
    if(interfaces.containsKey(interfaceName)){
        current = interfaces.get(interfaceName);
    }else{
        current = new IdlInterface();
        current.setName(interfaceName);
        interfaces.put(interfaceName, current);
    }
    IdlOperation runOperation = new IdlOperation();
    runOperation.setName("run");
    IdlArgumentListener argList = new IdlArgumentListener(typeDef);
    if(ctx.argumentList() != null){
        ParseTreeWalker walker = new ParseTreeWalker();
        walker.walk(argList, ctx.argumentList());
    }
    runOperation.setArguments(argList.getArguments());
    runOperation.setType(ParserUtils.getType(ctx.returntype(),typeDef));
    current.getOperations().add(runOperation);
}
项目:Health    文件:Interpreter.java   
/**
 * Interprets the given script.
 *
 * @param script
 *            the script to interpret.
 * @param context
 *            the script context.
 * @param interpreter
 *            a statement listener for interpreting the statements in the
 *            script.
 * @throws IOException
 *             if any I/O-exceptions occur.
 */
public static void interpret(final String script, final Context context, final StatementListener interpreter)
        throws IOException {
    Objects.requireNonNull(script);
    Objects.requireNonNull(context);

    // Converts the script to an input stream for the lexer
    InputStream is = new ByteArrayInputStream(script.getBytes("UTF-8"));
    ANTLRInputStream input = new ANTLRInputStream(is);

    // Creates a lexer and parser to parse the script
    MyScriptLexer lexer = new MyScriptLexer(input);
    MyScriptParser parser = new MyScriptParser(new CommonTokenStream(lexer));
    parser.setBuildParseTree(true);

    ParseTree tree = parser.parse();

    // Create a walker to walk the parse tree for interpreting
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(interpreter, tree);
}
项目:viskell    文件:TypeBuilder.java   
/**
 * Parse a Haskell type declaration into a TypeT instance.
 *
 * @param hs The Haskell type declaration
 * @return Type
 */
public Type build(final String hs) {
    TypeLexer lexer = new TypeLexer(new ANTLRInputStream(hs));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    TypeParser parser = new TypeParser(tokens);

    ParserRuleContext tree = parser.type();
    ParseTreeWalker walker = new ParseTreeWalker();
    TypeBuilderListener extractor;

    extractor = new TypeBuilderListener(this.typeClasses);

    walker.walk(extractor, tree);

    return extractor.result();
}
项目:protostuff-compiler    文件:FileDescriptorLoaderImpl.java   
private ProtoContext parse(String filename, CharStream stream) {
    ProtoLexer lexer = new ProtoLexer(stream);
    lexer.removeErrorListeners();
    lexer.addErrorListener(errorListener);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    ProtoParser parser = new ProtoParser(tokenStream);
    parser.removeErrorListeners();
    parser.addErrorListener(errorListener);

    ProtoParser.ProtoContext tree = parser.proto();
    int numberOfSyntaxErrors = parser.getNumberOfSyntaxErrors();
    if (numberOfSyntaxErrors > 0) {
        String format = "Could not parse %s: %d syntax errors found";
        throw new ParserException(format, filename, numberOfSyntaxErrors);
    }
    ProtoContext context = new ProtoContext(filename);
    ProtoParserListener composite = CompositeParseTreeListener.create(ProtoParserListener.class,
            new ProtoParseListener(tokenStream, context),
            new MessageParseListener(tokenStream, context),
            new EnumParseListener(tokenStream, context),
            new OptionParseListener(tokenStream, context),
            new ServiceParseListener(tokenStream, context)
    );
    ParseTreeWalker.DEFAULT.walk(composite, tree);
    return context;
}
项目:transportlanguage    文件:OTLDListener.java   
/**
 * Parses the supplied input using the OTLDListener and returns it after walking it
 * @param reader input to parse
 * @return walked OTLDListener
 * @throws IOException
 */
public static OTLDListener parseFile(InputStream reader) throws IOException {
    OTLDErrorListener errorListener = new OTLDErrorListener();
    ANTLRInputStream stream = new ANTLRInputStream(reader);

    Lexer lexer = new otldLexer(stream);
    lexer.removeErrorListeners();
    lexer.addErrorListener(errorListener);
    TokenStream tokens = new CommonTokenStream(lexer);

    otldParser parser = new otldParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(errorListener);
    ParseTree tree = parser.program();

    OTLDListener railroad = new OTLDListener();

    if (errorListener.getErrors().isEmpty()) {
        ParseTreeWalker walker = new ParseTreeWalker();
        walker.walk(railroad, tree);
    } else {
        railroad.errors.addAll(errorListener.getErrors());
    }

    return railroad;
}
项目:monto-javascript-services    文件:ECMAScriptParser.java   
@Override
public ProductMessage onVersionMessage(List<Message> messages) throws IOException {
    VersionMessage version = Messages.getVersionMessage(messages);
    if (!version.getLanguage().equals(JAVASCRIPT)) {
        throw new IllegalArgumentException("wrong language in version message");
    }
    lexer.setInputStream(new ANTLRInputStream(version.getContent().getReader()));
    CommonTokenStream tokens = new CommonTokenStream(lexer);

    parser.setTokenStream(tokens);
    ParserRuleContext root = parser.program();
    ParseTreeWalker walker = new ParseTreeWalker();

    Converter converter = new Converter();
    walker.walk(converter, root);

    return new ProductMessage(
            version.getVersionId(),
            new LongKey(1),
            version.getSource(),
            AST,
            JAVASCRIPT,
            ASTs.encode(converter.getRoot()));
}
项目:cs652    文件:Test.java   
public static void main(String[] args) {
        String prop = "id=\"parrt\"\n";
        ANTLRInputStream input = new ANTLRInputStream(prop);
        PropertyFileLexer lexer = new PropertyFileLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        PropertyFileParser parser = new PropertyFileParser(tokens);
        ParseTree tree = parser.file();
        System.out.println(tree.toStringTree(parser));
//      Trees.inspect(tree, parser);

        ParseTreeWalker walker = new ParseTreeWalker();
        PropertyFileLoader loader = new PropertyFileLoader();
        walker.walk(loader, tree);

        System.out.println(loader.props);
    }
项目:cs652    文件:Load.java   
public static void main(String[] args) throws IOException {
    ANTLRInputStream input = new ANTLRFileStream("colleges.csv");
    CSVLexer lexer = new CSVLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    CSVParser parser = new CSVParser(tokens);
    ParseTree tree = parser.file();
    System.out.println(tree.toStringTree(parser));

    ParseTreeWalker walker = new ParseTreeWalker();
    CSVLoader loader = new CSVLoader();
    walker.walk(loader, tree);

    for (List<String> row : loader.data) {
        System.out.println(row);
    }
}
项目:cs652    文件:Trans.java   
public static void main(String[] args) {
    String code =
        "int x;\n" +
        "A b;\n";
    ANTLRInputStream input = new ANTLRInputStream(code);
    LangLexer lexer = new LangLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    LangParser parser = new LangParser(tokens);
    ParseTree tree = parser.file(); // start up

    System.out.println(tree.toStringTree(parser));

    ParseTreeWalker walker = new ParseTreeWalker();
    Gen listener = new Gen();
    walker.walk(listener, tree);

    ST output = listener.file.getTemplate();
    System.out.println(output.render());
}
项目:cs652    文件:Trans.java   
public static void main(String[] args) {
    String code =
        "int x;\n" +
        "A b;\n";
    ANTLRInputStream input = new ANTLRInputStream(code);
    LangLexer lexer = new LangLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    LangParser parser = new LangParser(tokens);
    ParseTree tree = parser.file(); // start up

    System.out.println(tree.toStringTree(parser));

    ParseTreeWalker walker = new ParseTreeWalker();
    Gen listener = new Gen();
    walker.walk(listener, tree);

    for (String decl : listener.decls) {
        System.out.println(decl);
    }
}
项目:cs652    文件:Trans.java   
public static void main(String[] args) {
    String code =
        "int x;\n" +
        "A b;\n";
    ANTLRInputStream input = new ANTLRInputStream(code);
    LangLexer lexer = new LangLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    LangParser parser = new LangParser(tokens);
    ParseTree tree = parser.file(); // start up

    System.out.println(tree.toStringTree(parser));

    ParseTreeWalker walker = new ParseTreeWalker();
    Gen listener = new Gen();
    walker.walk(listener, tree);

    ST output = listener.file.getTemplate();
    System.out.println(output.render());
}
项目:cs652    文件:Trans.java   
public static void main(String[] args) {
    String code =
        "int x;\n" +
        "A b;\n";
    ANTLRInputStream input = new ANTLRInputStream(code);
    LangLexer lexer = new LangLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    LangParser parser = new LangParser(tokens);
    ParseTree tree = parser.file(); // start up

    System.out.println(tree.toStringTree(parser));

    ParseTreeWalker walker = new ParseTreeWalker();
    Gen listener = new Gen();
    walker.walk(listener, tree);

    System.out.println(listener.file.toString());
}
项目:Candle    文件:Candle.java   
/**
 * Processes an {@link org.antlr.v4.runtime.ANTLRInputStream}.
 *
 * @param inputStream The input stream.
 * @return The document.
 *
 * @throws com.torchmind.candle.api.error.CandleException when lexing or parsing fails.
 */
@Nonnull
protected Candle read (@Nonnull ANTLRInputStream inputStream) throws CandleException {
        try {
                CandleLexer lexer = new CandleLexer (inputStream);
                lexer.addErrorListener (new LexerErrorListener ());

                CommonTokenStream tokenStream = new CommonTokenStream (lexer);
                CandleParser parser = new CandleParser (tokenStream);
                parser.setErrorHandler (new ParserErrorStrategy ());

                ParseTreeWalker walker = new ParseTreeWalker ();
                CandleListener listener = new CandleListener (this);

                this.clear ();
                walker.walk (listener, parser.candle ());
                return this;
        } catch (RuntimeException ex) {
                if (ex.getCause () instanceof CandleException) { throw ((CandleException) ex.getCause ()); }
                throw ex;
        }
}
项目:srclib-basic    文件:LanguageImpl.java   
@Override
protected void parse(File sourceFile) throws ParseException {
    try {
        GrammarConfiguration configuration = LanguageBase.createGrammarConfiguration(this,
                sourceFile,
                CPP14Lexer.class,
                CPP14Parser.class,
                new DefaultErrorListener(sourceFile));
        ((CPP14Lexer) configuration.lexer).setSupport(this);
        ParseTree tree = ((CPP14Parser) configuration.parser).translationunit();
        ParseTreeWalker walker = new ParseTreeWalker();
        walker.walk(new CPPParseTreeListener(this), tree);
    } catch (Exception e) {
        throw new ParseException(e);
    }

}
项目:srclib-basic    文件:LanguageImpl.java   
@Override
protected void parse(File sourceFile) throws ParseException {
    try {
        GrammarConfiguration configuration = LanguageBase.createGrammarConfiguration(this,
                sourceFile,
                ObjCLexer.class,
                ObjCParser.class,
                new DefaultErrorListener(sourceFile));
        ParseTree tree = ((ObjCParser) configuration.parser).translation_unit();
        ParseTreeWalker walker = new ParseTreeWalker();
        walker.walk(new ObjCParseTreeListener(this), tree);
    } catch (Exception e) {
        throw new ParseException(e);
    }

}
项目:srclib-basic    文件:LanguageImpl.java   
@Override
public void graph() {
    // first pass to extract defs
    super.graph();

    // second pass to extract refs
    firstPass = false;
    for (Map.Entry<File, ParseTree> entry : trees.entrySet()) {
        processingPath.push(PathUtil.relativizeCwd(entry.getKey().toPath()));
        LOGGER.info("Extracting refs from {}", getCurrentFile());
        try {
            ParseTreeWalker walker = new ParseTreeWalker();
            walker.walk(new SwiftParseTreeListener(this), entry.getValue());
        } catch (Exception e) {
            LOGGER.error("Failed to process {} - unexpected error", getCurrentFile(), e);
        } finally {
            processingPath.pop();
        }
    }
}
项目:srclib-basic    文件:LanguageImpl.java   
@Override
protected void parse(File sourceFile) throws ParseException {
    try {
        GrammarConfiguration configuration = LanguageBase.createGrammarConfiguration(
                this,
                sourceFile,
                SwiftLexer.class,
                SwiftParser.class,
                new DefaultErrorListener(sourceFile));
        ParseTree tree = ((SwiftParser) configuration.parser).top_level();
        ParseTreeWalker walker = new ParseTreeWalker();
        walker.walk(new SwiftParseTreeListener(this), tree);
        trees.put(sourceFile, tree);
    } catch (Exception e) {
        throw new ParseException(e);
    }
}
项目:isetools    文件:DOMBuilder.java   
/**
 * Fire off the full parse of the input and return the resulting DOM object.
 * Any errors encountered during the parse will be logged in the Log class.
 * <p>
 * @see Log
 * <p>
 * @return the DOM built by parsing the string or file.
 */
@Override
public DOM build() {
  ParserErrorListener parserListener = new ParserErrorListener(dom);
  ISELexer lexer = new ISELexer(ais);
  lexer.removeErrorListeners();
  lexer.addErrorListener(parserListener);

  CommonTokenStream tokenStream = new CommonTokenStream(lexer);

  ISEParser parser = new ISEParser(tokenStream);
  parser.removeErrorListeners();
  parser.addErrorListener(parserListener);

  ParseTreeWalker ptw = new ParseTreeWalker();
  tokens = parser.getTokenStream();
  ParseTree pt = parser.document();
  ptw.walk(this, pt);
  dom.index();
  return dom;
}
项目:perspective-backend    文件:QueryParserImpl.java   
@Override
public void parse(String sql) throws SQLSyntaxErrorException {
    try {
        CharStream input = new CaseInsensitiveInputStream(sql);
        ANTLRErrorListener errorListener = new InternalErrorListener();
        SQLLexer sqlLexer = new SQLLexer(input);
        sqlLexer.removeErrorListeners();
        sqlLexer.addErrorListener(errorListener);
        CommonTokenStream commonTokenStream = new CommonTokenStream(sqlLexer);
        SQLParser sqlParser = new SQLParser(commonTokenStream);
        sqlParser.removeErrorListeners();
        sqlParser.addErrorListener(errorListener);
        ParseTree parseTree = sqlParser.query();
        ParseTreeWalker walker = new ParseTreeWalker();
        walker.walk(this, parseTree);
    } catch (ParseException e) {
        errors.add(e.getMessage());
    }
    if (!errors.isEmpty()) {
        String message = errors.stream().collect(Collectors.joining("; "));
        throw new SQLSyntaxErrorException(message);
    }
}
项目:perspective-backend    文件:PlaceholderConfigurer.java   
public List<String> getQueries() throws SQLDataException {
    CharStream input = new ANTLRInputStream(sqlWithPlaceholders);
    ANTLRErrorListener errorListener = new InternalErrorListener();
    ParametersLexer parametersLexer = new ParametersLexer(input);
    parametersLexer.removeErrorListeners();
    parametersLexer.addErrorListener(errorListener);
    CommonTokenStream commonTokenStream = new CommonTokenStream(parametersLexer);
    ParametersParser parametersParser = new ParametersParser(commonTokenStream);
    parametersParser.removeErrorListeners();
    parametersParser.addErrorListener(errorListener);
    ParseTree parseTree = parametersParser.queries();
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(this, parseTree);
    if (exception.isPresent()) {
        throw exception.get();
    }
    return preparedQueries;
}
项目:archie    文件:ADLParser.java   
public Archetype parse(CharStream stream) {
    errors = new ADLParserErrors();
    errorListener = new ADLErrorListener(errors);

    lexer = new AdlLexer(stream);
    lexer.addErrorListener(errorListener);
    parser = new AdlParser(new CommonTokenStream(lexer));
    parser.addErrorListener(errorListener);
    tree = parser.adl(); // parse
    //System.out.println(tree.toStringTree(parser));

    ADLListener listener = new ADLListener(errors);
    walker= new ParseTreeWalker();
    walker.walk(listener, tree);
    Archetype result = listener.getArchetype();
    if(modelConstraintImposer != null && result.getDefinition() != null) {
        modelConstraintImposer.imposeConstraints(result.getDefinition());
    }
    return result;

}