org.antlr.v4.runtime.tree.ParseTreeWalker Java Examples

The following examples show how to use org.antlr.v4.runtime.tree.ParseTreeWalker. 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: SQLToKuduPredicatesTranslator.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
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 + " \"");
    }
  }
}
 
Example #2
Source File: TestSQLListener.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testSQL() {
  plsqlLexer l = new plsqlLexer(new ANTLRInputStream(sql));
  CommonTokenStream str = new CommonTokenStream(l);
  plsqlParser parser = new plsqlParser(str);
  ParserRuleContext c;
  if (sql.startsWith("insert")) {
    c = parser.insert_statement();
  } else if (sql.startsWith("delete")) {
    c = parser.delete_statement();
  } else {
    c = parser.update_statement();
  }
  SQLListener sqlListener = new SQLListener();
  sqlListener.allowNulls();
  sqlListener.setColumns(new HashSet<>(Lists.newArrayList("ID", "NAME", "HIREDATE", "SALARY", "LASTLOGIN")));
  ParseTreeWalker parseTreeWalker = new ParseTreeWalker();
  // Walk it and attach our sqlListener
  parseTreeWalker.walk(sqlListener, c);
  Assert.assertEquals(expected, sqlListener.getColumns());
}
 
Example #3
Source File: Tool.java    From bookish with MIT License 6 votes vote down vote up
/** define all entities in all files */
public void defineAllEntities(Artifact artifact) {
	for (ChapDocInfo doc : artifact.docs) {
		DefEntitiesListener defPhase = new DefEntitiesListener(doc);
		ParseTreeWalker walker = new ParseTreeWalker();
		walker.walk(defPhase, doc.tree);

		// Copy all global entities to root artifact
		for (String label : defPhase.entities.keySet()) {
			EntityDef d = defPhase.entities.get(label);
			if ( d.isGloballyVisible() ) { // move to global space
				artifact.defGlobalEntity(label, d);
			}
			else {
				doc.defEntity(label, d); // define locally to this doc
			}
		}
	}
}
 
Example #4
Source File: CumulusConcatenatedControlPlaneExtractor.java    From batfish with Apache License 2.0 6 votes vote down vote up
private void parseInterfacesFile() {
  int end = _text.indexOf(START_OF_PORTS_FILE);
  String text = end > 0 ? _text.substring(0, end) : _text;

  CumulusInterfacesCombinedParser parser =
      new CumulusInterfacesCombinedParser(text, _grammarSettings, _line, _offset);
  Cumulus_interfaces_configurationContext ctxt = parser.parse();
  checkErrors(parser);
  ParseTreeWalker walker = new BatfishParseTreeWalker(parser);
  CumulusInterfacesConfigurationBuilder cb =
      new CumulusInterfacesConfigurationBuilder(_configuration, parser, _text, _w);
  walker.walk(cb, ctxt);
  mergeParseTree(ctxt, parser);

  Token startOfFrrFile = ctxt.getStop();
  _line = startOfFrrFile.getLine();
  _offset = startOfFrrFile.getStartIndex();
}
 
Example #5
Source File: BatfishLexerErrorListenerTest.java    From batfish with Apache License 2.0 6 votes vote down vote up
@Test
public void testNonRecoveryLexerErrorNode() {
  String recoveryText = readResource("org/batfish/grammar/non_recovery_lexer_error", UTF_8);
  GrammarSettings settings = MockGrammarSettings.builder().build();
  NonRecoveryCombinedParser cp = new NonRecoveryCombinedParser(recoveryText, settings);
  RecoveryContext ctx = cp.parse();
  RecoveryExtractor extractor = new RecoveryExtractor();
  ParseTreeWalker walker = new BatfishParseTreeWalker(cp);
  walker.walk(extractor, ctx);

  assertThat(extractor.getFirstErrorLine(), equalTo(2));
  assertThat(extractor.getNumSimpleStatements(), equalTo(1));
  /*
   * There should be 4 lexer errors for the word 'error':
   * 'er' (requires two chars since 'en...' is valid
   * 'r' (nothing starts with 'r')
   * 'o' (nothing starts with 'o')
   * 'r' (nothing starts with 'r')
   */
  assertThat(extractor.getNumErrorNodes(), equalTo(4));
}
 
Example #6
Source File: PythonFileParser.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;
	}
       CharStream input = CharStreams.fromFileName(fileFullPath);
       Lexer lexer = new PythonLexer(input);
       CommonTokenStream tokens = new CommonTokenStream(lexer);
       
       
       PythonParser parser = new PythonParser(tokens);
       PythonCodeListener bridge = new PythonCodeListener(fileFullPath, entityRepo,inferer, includeFileLocator, processor);
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(bridge, parser.file_input());
	fileEntity = entityRepo.getEntity(fileFullPath);
	((FileEntity)fileEntity).cacheAllExpressions();
	bridge.done();
}
 
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: 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 #9
Source File: DistkvParser.java    From distkv with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public DistkvParsedResult parse(String command) {
  DistkvNewSqlListener dstNewSqlHandler = new DistkvNewSqlListener();
  DistkvNewSQLLexer lexer = new DistkvNewSQLLexer(CharStreams.fromString(command));

  //Add  DstErrorListener
  lexer.removeErrorListeners();
  lexer.addErrorListener(DistkvSqlErrorListener.INSTANCE);
  CommonTokenStream tokens = new CommonTokenStream(lexer);

  DistkvNewSQLParser parser = new DistkvNewSQLParser(tokens);
  parser.removeErrorListeners();
  parser.addErrorListener(DistkvSqlErrorListener.INSTANCE);

  DistkvNewSQLParser.StatementContext statement = parser.statement();
  ParseTreeWalker parseTreeWalker = new ParseTreeWalker();
  parseTreeWalker.walk(dstNewSqlHandler, statement);

  return dstNewSqlHandler.getParsedResult();
}
 
Example #10
Source File: DataModelExpressionParser.java    From wecube-platform with Apache License 2.0 6 votes vote down vote up
private Queue<DataModelExpressionDto> checkExpressionSyntax(String expression) {
    CharStream inputStream = CharStreams.fromString(expression);
    DataModelLexer dataModelLexer = new DataModelLexer(inputStream);
    CommonTokenStream tokens = new CommonTokenStream(dataModelLexer);
    DataModelParser parser = new DataModelParser(tokens);
    parser.addErrorListener(new BaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) throws WecubeCoreException {
            throw new WecubeCoreException(String.format("Expression syntax error: line %d:%d %s", line, charPositionInLine, msg));
        }
    });
    ParseTree tree = parser.route();
    ParseTreeWalker walker = new ParseTreeWalker();
    DataModelExpressionListener evalByListener = new DataModelExpressionListener();
    walker.walk(evalByListener, tree);
    return evalByListener.getExpressionQueue();
}
 
Example #11
Source File: Main.java    From ArduinoML-kernel with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static App buildModel(CharStream stream) {
    ArduinomlLexer    lexer   = new ArduinomlLexer(stream);
    lexer.removeErrorListeners();
    lexer.addErrorListener(new StopErrorListener());

    ArduinomlParser   parser  = new ArduinomlParser(new CommonTokenStream(lexer));
    parser.removeErrorListeners();
    parser.addErrorListener(new StopErrorListener());

    ParseTreeWalker   walker  = new ParseTreeWalker();
    ModelBuilder      builder = new ModelBuilder();

    walker.walk(builder, parser.root()); // parser.root() is the entry point of the grammar

    return builder.retrieve();
}
 
Example #12
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);

	for (String decl : listener.decls) {
		System.out.println(decl);
	}
}
 
Example #13
Source File: ExpressionTagQueryParser.java    From hawkular-alerts with Apache License 2.0 6 votes vote down vote up
public String parse(String expression) throws Exception {

        evalsPostfix = new ArrayList<>();
        stack = new Stack<>();
        error = false;
        errorMsg = null;

        ANTLRInputStream input = new ANTLRInputStream(expression);
        TagQueryLexer tql = new TagQueryLexer(input);
        tql.addErrorListener(this);
        CommonTokenStream tokens = new CommonTokenStream(tql);
        TagQueryParser parser = new TagQueryParser(tokens);
        ParseTree parseTree = parser.tagquery();
        ParseTreeWalker.DEFAULT.walk(this, parseTree);

        if (error) {
            throw new IllegalArgumentException("Expression [" + expression + "] is malformed. Msg: " + errorMsg);
        }
        if (log.isDebugEnabled()) {
            log.debugf("Expression [%s] evaluated as [%s]", expression, evalsPostfix);
        }
        return prefix(evalsPostfix);
    }
 
Example #14
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);

	System.out.println(listener.file.toString());
}
 
Example #15
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 #16
Source File: RouteQueryExpressionParser.java    From we-cmdb with Apache License 2.0 6 votes vote down vote up
public AdhocIntegrationQueryDto parse(String expression){
    RouteQueryExpressionListener routeQueryExpressListener = new RouteQueryExpressionListener(entityMap);
    CharStream inputStream = CharStreams.fromString(expression);
    RouteQueryLexer routeQueryLexer = new RouteQueryLexer(inputStream);
    CommonTokenStream tokens = new CommonTokenStream(routeQueryLexer);
    RouteQueryParser parser = new RouteQueryParser(tokens);
    parser.addErrorListener(new BaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e){
            throw new CmdbExpressException(String.format("Expression syntax error: line %d:%d %s", line, charPositionInLine, msg));
        }
    });

    ParseTree tree = parser.route();
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(routeQueryExpressListener, tree);
    return routeQueryExpressListener.getAdhocIntegrationQuery();
}
 
Example #17
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 #18
Source File: CumulusConcatenatedControlPlaneExtractor.java    From batfish with Apache License 2.0 6 votes vote down vote up
private void parsePortsFile() {
  int end = _text.indexOf(START_OF_FRR_FILE);
  String text = end > 0 ? _text.substring(0, end) : _text;

  CumulusPortsCombinedParser parser =
      new CumulusPortsCombinedParser(text, _grammarSettings, _line, _offset);
  Cumulus_ports_configurationContext ctxt = parser.parse();
  checkErrors(parser);
  ParseTreeWalker walker = new BatfishParseTreeWalker(parser);
  CumulusPortsConfigurationBuilder cb =
      new CumulusPortsConfigurationBuilder(_configuration, parser, _w);
  walker.walk(cb, ctxt);
  mergeParseTree(ctxt, parser);

  Token startOfInterfacesFile = ctxt.getStop();
  _line = startOfInterfacesFile.getLine();
  _offset = startOfInterfacesFile.getStartIndex();
}
 
Example #19
Source File: UserAgentTreeFlattener.java    From yauaa with Apache License 2.0 6 votes vote down vote up
/**
 * Parse the useragent and return every part that was found.
 *
 * @param userAgent The useragent instance that needs to be parsed
 * @return If the parse was valid (i.e. were there any parser errors: true=valid; false=has errors
 */
private MutableUserAgent parseIntoCleanUserAgent(MutableUserAgent userAgent) {
    if (userAgent.getUserAgentString() == null) {
        userAgent.set(SYNTAX_ERROR, "true", 1);
        return userAgent; // Cannot parse this
    }

    // Parse the userAgent into tree
    UserAgentContext userAgentContext = parseUserAgent(userAgent);

    // Walk the tree an inform the calling analyzer about all the nodes found
    state = new ParseTreeProperty<>();

    State rootState = new State(AGENT);
    rootState.calculatePath(PathType.CHILD, false);
    state.put(userAgentContext, rootState);

    if (userAgent.hasSyntaxError()) {
        inform(null, SYNTAX_ERROR, "true");
    } else {
        inform(null, SYNTAX_ERROR, "false");
    }

    ParseTreeWalker.DEFAULT.walk(this, userAgentContext);
    return userAgent;
}
 
Example #20
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);

	System.out.println(listener.file.toString());
}
 
Example #21
Source File: QueryTest.java    From mobi with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void replaceDatasetClause() throws Exception {
    InputStream query = getClass().getResourceAsStream("/example2.rq");
    Sparql11Parser parser = Query.getParser(streamToString(query));
    parser.addErrorListener(new BaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
            throw new IllegalStateException("failed to parse at line " + line + " due to " + msg, e);
        }
    });

    Sparql11Parser.QueryContext queryContext = parser.query();

    // Rewrite the dataset clause
    TokenStreamRewriter rewriter = new TokenStreamRewriter(parser.getTokenStream());
    ParseTreeWalker walker = new ParseTreeWalker();
    DatasetListener listener = new DatasetListener(rewriter);
    walker.walk(listener, queryContext);

    // Test result
    String newText = rewriter.getText();
    parser = Query.getParser(newText);
    String datasetText = parser.query().selectQuery().datasetClause().get(0).getText();
    assertEquals(DATASET_REPLACEMENT, datasetText);
}
 
Example #22
Source File: SimpleDatasetRepositoryConnection.java    From mobi with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Rewrites a SPARQL query replacing the dataset clauses with appropriate dataset clauses for this dataset.
 *
 * @param query The query to rewrite.
 * @return A String representing the query rewritten with dataset clauses appropriate for this dataset.
 */
private String rewriteQuery(String query, Resource... contexts) {
    Sparql11Parser parser = Query.getParser(query);
    Sparql11Parser.QueryContext queryContext = parser.query();
    TokenStreamRewriter rewriter = new TokenStreamRewriter(parser.getTokenStream());
    ParseTreeWalker walker = new ParseTreeWalker();

    DatasetListener listener;
    if (varargsPresent(contexts)) {
        listener = new DatasetListener(rewriter, Arrays.asList(contexts));
    } else {
        listener = new DatasetListener(rewriter);
    }

    walker.walk(listener, queryContext);

    String processedQuery = rewriter.getText();
    log.debug("Dataset Processed Query: \n" + processedQuery);

    return processedQuery;
}
 
Example #23
Source File: Test.java    From cs652 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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);
	}
 
Example #24
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);

	for (String decl : listener.decls) {
		System.out.println(decl);
	}
}
 
Example #25
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 #26
Source File: G4Processor.java    From openCypher with Apache License 2.0 6 votes vote down vote up
private Grammar processAntrlStream(CharStream inStream)
    {
        Gee4Lexer lexer = new Gee4Lexer(inStream);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        Gee4Parser parser = new Gee4Parser(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.wholegrammar();

        ParseTreeWalker walker = new ParseTreeWalker();
        G4Listener listener = new G4Listener(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 #27
Source File: Load.java    From cs652 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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);
	}
}
 
Example #28
Source File: GDLHandler.java    From gdl with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #29
Source File: Test.java    From cs652 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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);
	}
 
Example #30
Source File: FileDescriptorLoaderImpl.java    From protostuff-compiler with Apache License 2.0 6 votes vote down vote up
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;
}