Java Code Examples for org.antlr.v4.runtime.tree.ParseTreeWalker#walk()

The following examples show how to use org.antlr.v4.runtime.tree.ParseTreeWalker#walk() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Trans.java    From cs652 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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());
}
 
Example 2
Source File: FeatureParser.java    From karate with MIT License 6 votes vote down vote up
private FeatureParser(Feature feature, InputStream is) {
    this.feature = feature;
    CharStream stream;
    try {
        stream = CharStreams.fromStream(is, StandardCharsets.UTF_8);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    KarateLexer lexer = new KarateLexer(stream);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    KarateParser parser = new KarateParser(tokens);
    parser.addErrorListener(errorListener);
    RuleContext tree = parser.feature();
    if (logger.isTraceEnabled()) {
        logger.debug(tree.toStringTree(parser));
    }
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(this, tree);
    if (errorListener.isFail()) {
        String errorMessage = errorListener.getMessage();
        logger.error("not a valid feature file: {} - {}", feature.getResource().getRelativePath(), errorMessage);
        throw new RuntimeException(errorMessage);
    }
}
 
Example 3
Source File: Trans.java    From cs652 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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());
}
 
Example 4
Source File: BNFProcessor.java    From openCypher with Apache License 2.0 6 votes vote down vote up
private Grammar processAntrlStream(CharStream inStream)
    {
        BNFLexer lexer = new BNFLexer(inStream);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        BNFParser parser = new BNFParser(tokens);
        // leaving the old listeners in gives a nice error messsage
        // parser.removeErrorListeners();
        // lexer.removeErrorListeners();
        lexer.addErrorListener(new FailingErrorListener());
        parser.addErrorListener(new FailingErrorListener());

        ParseTree tree = parser.rulelist();

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

        GrammarTop itemTree = listener.getTreeTop();
//        LOGGER.warn("bnf gave {}", itemTree.getStructure(""));
        // convert to openCypher grammar
        GrammarConverter converter = new GrammarConverter(itemTree);
        return converter.convert();
    }
 
Example 5
Source File: ADLParser.java    From archie with Apache License 2.0 6 votes vote down vote up
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;

}
 
Example 6
Source File: PomFileParser.java    From depends with MIT License 6 votes vote down vote up
@Override
public void parse() throws IOException {
	/* If file already exist, skip it */
	Entity fileEntity = entityRepo.getEntity(fileFullPath);
	if (fileEntity!=null && fileEntity instanceof FileEntity) {
		return;
	}
	/*parse file*/
       CharStream input = CharStreams.fromFileName(fileFullPath);
       Lexer lexer = new XMLLexer(input);
       CommonTokenStream tokens = new CommonTokenStream(lexer);
       XMLParser parser = new XMLParser(tokens);
       PomListener bridge = new PomListener(fileFullPath, entityRepo, includePaths,parseCreator,inferer);
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(bridge, parser.document());
	fileEntity = entityRepo.getEntity(fileFullPath);
	bridge.done();
	((FileEntity)fileEntity).cacheAllExpressions();
}
 
Example 7
Source File: JavaFileParser.java    From depends with MIT License 6 votes vote down vote up
@Override
public void parse() throws IOException {
       CharStream input = CharStreams.fromFileName(fileFullPath);
       Lexer lexer = new JavaLexer(input);
       lexer.setInterpreter(new LexerATNSimulator(lexer, lexer.getATN(), lexer.getInterpreter().decisionToDFA, new PredictionContextCache()));
       CommonTokenStream tokens = new CommonTokenStream(lexer);
       JavaParser parser = new JavaParser(tokens);
       ParserATNSimulator interpreter = new ParserATNSimulator(parser, parser.getATN(), parser.getInterpreter().decisionToDFA, new PredictionContextCache());
       parser.setInterpreter(interpreter);
       JavaListener bridge = new JavaListener(fileFullPath, entityRepo,inferer);
    ParseTreeWalker walker = new ParseTreeWalker();
    try {
    	walker.walk(bridge, parser.compilationUnit());
		Entity fileEntity = entityRepo.getEntity(fileFullPath);
		((FileEntity)fileEntity).cacheAllExpressions();
		interpreter.clearDFA();
		bridge.done();
    	
    }catch (Exception e) {
    	System.err.println("error encountered during parse..." );
    	e.printStackTrace();
    }
    
   }
 
Example 8
Source File: Pql2Compiler.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
public static AstNode buildAst(String expression) {
  CharStream charStream = new ANTLRInputStream(expression);
  PQL2Lexer lexer = new PQL2Lexer(charStream);
  lexer.setTokenFactory(new CommonTokenFactory(true));
  lexer.removeErrorListeners();
  lexer.addErrorListener(ERROR_LISTENER);
  TokenStream tokenStream = new UnbufferedTokenStream<CommonToken>(lexer);
  PQL2Parser parser = new PQL2Parser(tokenStream);
  parser.setErrorHandler(new BailErrorStrategy());
  parser.removeErrorListeners();
  parser.addErrorListener(ERROR_LISTENER);

  // Parse
  ParseTree parseTree = parser.root();

  ParseTreeWalker walker = new ParseTreeWalker();
  Pql2AstListener listener = new Pql2AstListener(expression);
  walker.walk(listener, parseTree);

  AstNode rootNode = listener.getRootNode();
  return rootNode;
}
 
Example 9
Source File: Trans.java    From cs652 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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());
}
 
Example 10
Source File: TypedValue.java    From istio-java-api with Apache License 2.0 6 votes vote down vote up
public static TypedValue from(String value) {
    // Get our lexer
    CEXLLexer lexer = new CEXLLexer(CharStreams.fromString(value));

    // Get a list of matched tokens
    CommonTokenStream tokens = new CommonTokenStream(lexer);

    // Pass the tokens to the parser
    CEXLParser parser = new CEXLParser(tokens);

    // Specify our entry point
    final CEXLParser.ExpressionContext context = parser.expression();

    // Walk it and attach our listener
    ParseTreeWalker walker = new ParseTreeWalker();
    final CEXLTypeResolver resolver = new CEXLTypeResolver();
    walker.walk(resolver, context);

    return new TypedValue(resolver.getExpressionType(), value);
}
 
Example 11
Source File: Test.java    From cs652 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args) throws IOException {
		ANTLRInputStream input = new ANTLRFileStream(args[0]);
		SimpleLexer l = new SimpleLexer(input);
		TokenStream tokens = new CommonTokenStream(l);

		SimpleParser parser = new SimpleParser(tokens);
		ParserRuleContext tree = parser.file();
		System.out.println(tree.toStringTree(parser));
//		tree.inspect(parser);

		DefSymbolsAndScopes def = new DefSymbolsAndScopes();
		ParseTreeWalker walker = new ParseTreeWalker();
		walker.walk(def, tree);
	}
 
Example 12
Source File: StackTracesParsing.java    From botsing with Apache License 2.0 5 votes vote down vote up
/**
 * Parses the given input and returns a list of stack traces found in that input.
 * @param input The input to parse.
 * @return A list with the stack traces parsed from the given input.
 */
public static List<CrashStackTrace> parseStackTraces(String input){
    CharStream text = CharStreams.fromString(input);
    StackTracesLexer lexer = new StackTracesLexer(text);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    StackTracesParser parser = new StackTracesParser(tokens);
    StackTracesParser.StackTracesContext tree = parser.stackTraces();
    // Walk the three to build the stack traces
    StackTracesCollectorListener listener = new StackTracesCollectorListener();
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(listener, tree);
    // Return the built list
    return listener.getStackTraces();
}
 
Example 13
Source File: AntlrParser.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void checkSyntax(String expression) throws ExpressionException {
	ParseTree tree = parseExpression(expression);
	ParseTreeWalker walker = new ParseTreeWalker();
	FunctionListener listener = new FunctionListener(lookup);
	try {
		walker.walk(listener, tree);
	} catch (ExpressionParsingException e) {
		throw new ExpressionException(e);
	}
}
 
Example 14
Source File: TestFaLaLa.java    From cs652 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	ANTLRInputStream input = new ANTLRFileStream("test.falala");
	FaLaLaLexer lexer = new FaLaLaLexer(input);
	CommonTokenStream tokens = new CommonTokenStream(lexer);
	FaLaLaParser parser = new FaLaLaParser(tokens);
	ParseTree tree = parser.prog();
	System.out.println(tree.toStringTree(parser));

	ParseTreeWalker walker = new ParseTreeWalker();
	DefSymbols def = new DefSymbols();
	walker.walk(def, tree);
}
 
Example 15
Source File: GenCallGraph.java    From cs652 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args) throws IOException {
	LangLexer lexer = new LangLexer(new ANTLRInputStream(input));
	CommonTokenStream tokens = new CommonTokenStream(lexer);
	LangParser parser = new LangParser(tokens);
	ParserRuleContext tree = parser.file();
	LinkViz graph = new LinkViz();
	ParseTreeWalker walker = new ParseTreeWalker();
	CollectCalls calls = new CollectCalls(graph);
	walker.walk(calls, tree);
	System.out.println(GenDOT.gen(graph));
}
 
Example 16
Source File: KotlinFileParser.java    From depends with MIT License 5 votes vote down vote up
@Override
public void parse() throws IOException {
       CharStream input = CharStreams.fromFileName(fileFullPath);
        Lexer lexer = new KotlinLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        KotlinParser parser = new KotlinParser(tokens);
        KotlinListener bridge = new KotlinListener(fileFullPath, entityRepo,inferer);
	    ParseTreeWalker walker = new ParseTreeWalker();
	    walker.walk(bridge, parser.kotlinFile());
}
 
Example 17
Source File: NxosControlPlaneExtractor.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Override
public void processParseTree(NetworkSnapshot snapshot, ParserRuleContext tree) {
  ParseTreeWalker walker = new BatfishParseTreeWalker(_parser);
  // extract metadata and set defaults
  walker.walk(new CiscoNxosPreprocessor(_text, _parser, _w, _configuration), tree);
  // build the configuration
  walker.walk(new CiscoNxosControlPlaneExtractor(_text, _parser, _w, _configuration), tree);
}
 
Example 18
Source File: ParseTreePrettyPrinter.java    From batfish with Apache License 2.0 5 votes vote down vote up
public static ParseTreeSentences getParseTreeSentences(
    ParserRuleContext ctx, BatfishCombinedParser<?, ?> combinedParser, boolean printLineNumbers) {
  ParseTreeWalker walker = new BatfishParseTreeWalker(combinedParser);
  ParseTreePrettyPrinter printer =
      new ParseTreePrettyPrinter(ctx, combinedParser, printLineNumbers);
  walker.walk(printer, ctx);
  return printer._ptSentences;
}
 
Example 19
Source File: Python335FileAnalyzer.java    From steady with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Getter for the field <code>constructs</code>.</p>
 *
 * @param m a {@link java.io.InputStream} object.
 * @return a {@link java.util.Map} object.
 * @throws com.sap.psr.vulas.FileAnalysisException if any.
 * @throws java.io.IOException if any.
 * @throws org.antlr.v4.runtime.RecognitionException if any.
 */
public Map<ConstructId, Construct> getConstructs(InputStream m) throws FileAnalysisException, IOException, RecognitionException {
	final ANTLRInputStream input = new ANTLRInputStream(m);
	final Python335Lexer lexer = new Python335Lexer(input);
	final CommonTokenStream tokens = new CommonTokenStream(lexer);
	final Python335Parser parser = new Python335Parser(tokens);
	final ParseTree root = parser.file_input();
	final ParseTreeWalker walker = new ParseTreeWalker();
	
	try {
		walker.walk(this, root);
	}
	catch(IllegalStateException ise) {
		throw new FileAnalysisException("Parser error", ise);
	}

	// Update module body after the parsing of the entire file
	if(this.stmts!=null && this.stmts.size()>0) {
		final StringBuffer b = new StringBuffer();
		for(String stmt: this.stmts)
			if(!stmt.trim().equals(""))
				b.append(stmt);
		this.constructs.get(this.module).setContent(b.toString());
	}
	
	return this.constructs;
}
 
Example 20
Source File: F5BigipStructuredControlPlaneExtractor.java    From batfish with Apache License 2.0 4 votes vote down vote up
@Override
public void processParseTree(NetworkSnapshot snapshot, ParserRuleContext tree) {
  F5BigipStructuredConfigurationBuilder cb =
      new F5BigipStructuredConfigurationBuilder(_parser, _text, _w);
  ParseTreeWalker walker = new BatfishParseTreeWalker(_parser);
  walker.walk(cb, tree);
  _configuration = cb.getConfiguration();

  /*
   * Everything below is a dirty stinking hack that works around current lack of support for
   * multi-file configs. All of it will be removed after that feature is added, since
   * at that point F5 BIG-IP imish configs will not appear in same file as F5 BIG-IP structured
   * configs.
   */
  Integer imishConfigurationOffset = cb.getImishConfigurationOffset();
  if (imishConfigurationOffset == null) {
    return;
  }
  F5BigipImishCombinedParser imishParser =
      new F5BigipImishCombinedParser(
          _text, _parser.getSettings(), imishConfigurationOffset, cb.getImishConfigurationLine());

  // parse imish content
  ParserRuleContext imishCtx = imishParser.parse();
  if (!imishParser.getErrors().isEmpty()) {
    throw new BatfishException(
        String.format(
            "Configuration file: '%s' contains unrecognized lines:\n%s",
            _filename, String.join("\n", imishParser.getErrors())));
  }

  ParseTreeWalker imishWalker = new BatfishParseTreeWalker(imishParser);
  F5BigipImishConfigurationBuilder icb =
      new F5BigipImishConfigurationBuilder(imishParser, _text, _w, _configuration);

  // merge in imish configuration
  imishWalker.walk(icb, imishCtx);

  // merge in imish parse tree if desired
  if (_ptSentences != null) {
    _ptSentences
        .get()
        .getSentences()
        .addAll(
            ParseTreePrettyPrinter.getParseTreeSentences(
                    imishCtx, imishParser, _printParseTreeLineNums)
                .getSentences());
  }
}