Java 类org.antlr.v4.runtime.CommonTokenStream 实例源码

项目:bitbox    文件:PainterLogic.java   
@Override
public SortedMap<Integer, String> parseString(String fieldString) {
    CharStream is = CharStreams.fromString(fieldString);
    DrawGrammarLexer lex = new DrawGrammarLexer(is);
    CommonTokenStream tokens = new CommonTokenStream(lex);
    DrawGrammarParser parser = new DrawGrammarParser(tokens);
    palette = Palette.makeDefaultPalette("DarkSpectrum");
    final SortedMap<Integer, String> resultMap;
    try {
        resultMap = parser.root().map;
    } catch (RecognitionException | NullPointerException | StringIndexOutOfBoundsException | RasterFormatException re) {
        //Something wrong with the parsing do not update.
        return null;
    }
    return resultMap;
}
项目:CTalk    文件:App.java   
/**
 * @param args the command line arguments
 * @throws java.io.IOException
 * @throws java.net.URISyntaxException
 */
public static void main(String[] args) throws IOException, URISyntaxException {
    final String entryPoint;
    final URL res;
    switch (args.length) {
    case 2:
        res = Paths.get(args[0]).toUri().toURL();
        entryPoint = args[1];
        break;
    default:
        System.err.println("Supply two parameters in the following order:\n- file name of the main function\n- name of the main function\n\nFor example: hello.ct main:argc:argv");
        return;
    }
    final CharStream inp = CharStreams.fromStream(res.openStream());
    final GrammarLexer lex = new GrammarLexer(inp);
    final TokenStream toks = new CommonTokenStream(lex);
    final GrammarParser parser = new GrammarParser(toks);
    System.out.println(new Translator(res.toURI()).generate(parser.program(), entryPoint));
}
项目:coherence-sql    文件:SqlParser.java   
/**
 * @throws InvalidQueryException if the given query contains invalid characters or is incomplete
 */
public SqlGrammarParser.SelectStatementContext parse(String query) {
    log.trace("About to parse [{}]", query);
    long now = currentTimeMillis();

    CharStream input = CharStreams.fromString(query);
    SqlGrammarLexer lexer = new SqlGrammarLexer(input);
    TokenStream tokenStream = new CommonTokenStream(lexer);
    SqlGrammarParser parser = new SqlGrammarParser(tokenStream);

    parser.removeErrorListeners();
    parser.addErrorListener(new RaiseExceptionErrorListener());

    SqlGrammarParser.SelectStatementContext result = parser.selectStatement();

    log.trace("Successfully parsed [{}] into [{}] in [{}ms]",
            query, result.toStringTree(parser), currentTimeMillis() - now);

    return result;
}
项目:BEAST    文件:CBMCCodeGenerator.java   
private BooleanExpListNode generateAST(String code) {
    FormalPropertyDescriptionLexer l = new FormalPropertyDescriptionLexer(new ANTLRInputStream(code));
    CommonTokenStream ts = new CommonTokenStream(l);
    FormalPropertyDescriptionParser p = new FormalPropertyDescriptionParser(ts);

    BooleanExpScope declaredVars = new BooleanExpScope();

    preAndPostConditionsDescription.getSymbolicVariableList().forEach((v) -> {
        declaredVars.addTypeForId(v.getId(), v.getInternalTypeContainer());
    });

    return translator.generateFromSyntaxTree(
            p.booleanExpList(),
            electionDescription.getInputType().getType(),
            electionDescription.getOutputType().getType(),
            declaredVars);
}
项目:ETUmulator    文件:Linker.java   
/**
 * Returns an ExecutableCode that can then be run by the processor. If the given code contains
 * data section, linker will only generate memory addresses. Without loading process, referenced
 * data will be unpredictable.
 *
 * @param code The code that will be linked.
 *
 * @return The executable code.
 *
 * @throws LabelError If an undefined label used or duplicate labels exist.
 * @see Loader#load(Linker.ExecutableCode)
 */
public ExecutableCode link(String code) throws LabelError {
    definedBranches.clear();
    definedData.clear();
    addressBook.clear();
    secondPass = false;
    this.code = parseCode(code);
    AssemblerLexer lexer = new AssemblerLexer(CharStreams.fromString(code));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    AssemblerParser parser = new AssemblerParser(tokens);
    AssemblerParser.ProgContext program = parser.prog();
    visit(program);
    secondPass = true;
    visit(program);
    List<Data> temp = new ArrayList<>(definedData.size());
    definedData.forEach((label, data) -> {
        temp.add(data);
    });
    return new ExecutableCode(this.code, temp);
}
项目:elasticsearch_my    文件:Walker.java   
private SourceContext buildAntlrTree(String source) {
    ANTLRInputStream stream = new ANTLRInputStream(source);
    PainlessLexer lexer = new EnhancedPainlessLexer(stream, sourceName);
    PainlessParser parser = new PainlessParser(new CommonTokenStream(lexer));
    ParserErrorStrategy strategy = new ParserErrorStrategy(sourceName);

    lexer.removeErrorListeners();
    parser.removeErrorListeners();

    if (settings.isPicky()) {
        setupPicky(parser);
    }

    parser.setErrorHandler(strategy);

    return parser.source();
}
项目: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);
}
项目:ontolib    文件:OboParser.java   
private void parseInputStream(CharStream inputStream, OboParseResultListener listener) {
  final OboLexer l = new OboLexer(inputStream);
  final Antlr4OboParser p = new Antlr4OboParser(new CommonTokenStream(l));

  p.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);
    }
  });

  if (debug) {
    p.addErrorListener(new DiagnosticErrorListener());
  }

  p.addParseListener(new OboParserListener(listener));

  p.oboFile();
}
项目:rest-modeling-framework    文件:TypeExpressionConstructor.java   
public EObject parse(final String typeExpression, final Scope scope) {
    final CharStream charStream = CharStreams.fromString(typeExpression);
    final TypeExpressionLexer lexer = new TypeExpressionLexer(charStream);
    final TokenStream tokenStream = new CommonTokenStream(lexer);
    final TypeExpressionParser typeExpressionParser = new TypeExpressionParser(tokenStream);

    lexer.removeErrorListeners();
    typeExpressionParser.removeErrorListeners();

    final ParserErrorCollector errorCollector = new ParserErrorCollector();
    lexer.addErrorListener(errorCollector);
    typeExpressionParser.addErrorListener(errorCollector);

    final TypeExpressionParser.Type_exprContext typeExpr = typeExpressionParser.type_expr();

    final EObject anyType = new TypeExpressionBuilder(scope, ARRAY_TYPE).visit(typeExpr);
    scope.getResource().getErrors().addAll(errorCollector.getErrors());

    return anyType;
}
项目:rest-modeling-framework    文件:RamlResource.java   
@Override
protected void doLoad(final InputStream inputStream, final Map<?, ?> options) throws IOException {
    final BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
    final Optional<AbstractConstructor> optionalRootConstructor = getRootConstructor(bufferedInputStream);

    if (optionalRootConstructor.isPresent()) {
        final AbstractConstructor rootConstructor = optionalRootConstructor.get();
        final RAMLCustomLexer lexer = new RAMLCustomLexer(uri, getURIConverter());
        final TokenStream tokenStream = new CommonTokenStream(lexer);
        final RAMLParser parser = new RAMLParser(tokenStream);

        parser.removeErrorListeners();
        final ParserErrorCollector errorCollector = new ParserErrorCollector();
        parser.addErrorListener(errorCollector);

        try {
            rootConstructor.construct(parser, resourceScope);
            validate();
        } catch (final Exception e) {
            getErrors().addAll(errorCollector.getErrors());
            throw e;
        }
    }
}
项目:rest-modeling-framework    文件:TypeDeclarationResolverTest.java   
@Ignore
@Test
public void api() {
    final File apiFile = new File("/Users/mkoester/Development/commercetools-api-reference/api.raml");
    assumeTrue(apiFile.exists());

    final URI apiUri = URI.createURI(apiFile.toURI().toString());
    final URIConverter uriConverter = new RamlResourceSet().getURIConverter();
    final RAMLCustomLexer lexer = new RAMLCustomLexer(apiUri, uriConverter);
    final TokenStream tokenStream = new CommonTokenStream(lexer);

    final RAMLParser parser = new RAMLParser(tokenStream);
    final Resource resource = new RamlResourceSet().createResource(apiUri);

    final Scope scope = Scope.of(resource);
    final TypeDeclarationResolver resolver = new TypeDeclarationResolver();
    resolver.resolve(parser.api(), scope);

    assertThat(resource.getErrors()).isEmpty();
}
项目:urule    文件:CommonServletHandler.java   
public void scriptValidation(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String content=req.getParameter("content");
    if(StringUtils.isNotBlank(content)){
        ScriptType type=ScriptType.valueOf(req.getParameter("type"));
        ANTLRInputStream antlrInputStream=new ANTLRInputStream(content);
        RuleParserLexer lexer=new RuleParserLexer(antlrInputStream);
        CommonTokenStream steam=new CommonTokenStream(lexer);
        RuleParserParser parser=new RuleParserParser(steam);
        parser.removeErrorListeners();
        ScriptErrorListener errorListener=new ScriptErrorListener();
        parser.addErrorListener(errorListener);
        switch(type){
        case Script:
            parser.ruleSet();
            break;
        case DecisionNode:
            parser.condition();
            break;
        case ScriptNode:
            parser.actions();

        }
        List<ErrorInfo> infos=errorListener.getInfos();
        writeObjectToJson(resp, infos);
    }
}
项目:urule    文件:DSLRuleSetBuilder.java   
public RuleSet build(String script) throws IOException{
    ANTLRInputStream antlrInputStream=new ANTLRInputStream(script);
    RuleParserLexer lexer=new RuleParserLexer(antlrInputStream);
    CommonTokenStream tokenStream=new CommonTokenStream(lexer);
    RuleParserParser parser=new RuleParserParser(tokenStream);
    ScriptDecisionTableErrorListener errorListener=new ScriptDecisionTableErrorListener();
    parser.addErrorListener(errorListener);
    BuildRulesVisitor visitor=new BuildRulesVisitor(contextBuilders,tokenStream);
    RuleSet ruleSet=visitor.visitRuleSet(parser.ruleSet());
    rebuildRuleSet(ruleSet);
    String error=errorListener.getErrorMessage();
    if(error!=null){
        throw new RuleException("Script parse error:"+error);
    }
    return ruleSet;
}
项目:jobson    文件:TemplateStringEvaluator.java   
public static String evaluate(String templateString, Map<String, Object> environment) {
    final Scanner s = new Scanner(templateString);
    // TODO: This is a hack that fails if a string literal contains "}"
    // TODO: It's because i couldn't be bothered nesting grammars.
    s.useDelimiter("((?!\\\\)\\$\\{)|(})");

    StringBuilder ret = new StringBuilder();
    boolean isInsideExpr = templateString.startsWith("${");
    while(s.hasNext()) {
        final String str = s.next();

        if (isInsideExpr) {
            final JsLikeExpressionLexer lexer = new JsLikeExpressionLexer(CharStreams.fromString(str));
            final CommonTokenStream tokenStream = new CommonTokenStream(lexer);
            final JsLikeExpressionParser parser = new JsLikeExpressionParser(tokenStream);


            final TemplateStringEvaluatorVisitor visitor = new TemplateStringEvaluatorVisitor(environment);

            ret.append(parser.expression().accept(visitor).toString());
        } else {
            ret.append(str);
        }

        isInsideExpr = !isInsideExpr;
    }

    return ret.toString();
}
项目:pgcodekeeper    文件:AntlrParser.java   
private static <T extends Parser> T makeBasicParser(Class<T> parserClass,
        ANTLRInputStream stream, String parsedObjectName, List<AntlrError> errors) {
    Lexer lexer;
    Parser parser;
    if (parserClass.isAssignableFrom(SQLParser.class)) {
        lexer = new SQLLexer(stream);
        parser = new SQLParser(new CommonTokenStream(lexer));
    } else if (parserClass.isAssignableFrom(IgnoreListParser.class)) {
        lexer = new IgnoreListLexer(stream);
        parser = new IgnoreListParser(new CommonTokenStream(lexer));
    } else {
        throw new IllegalArgumentException("Unknown parser class: " + parserClass);
    }

    CustomAntlrErrorListener err = new CustomAntlrErrorListener(parsedObjectName, errors);
    lexer.removeErrorListeners();
    lexer.addErrorListener(err);
    parser.removeErrorListeners();
    parser.addErrorListener(err);

    return parserClass.cast(parser);
}
项目:Hydrograph    文件:LingualQueryCreatorTest.java   
@Test
public void itShouldGenerateLingualQuery() throws ParseException {
    String dateInString = "2012-01-10 00:00:00";
    remoteFilterJson.setCondition("(f1  not in ('1') and f2 = '"+dateInString+"') or (f3=23.23 and f4=1234.123)");
    String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
    formatter.setTimeZone(TimeZone.getDefault());
    Date date = formatter.parse(dateInString);
    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    dateInString = formatter.format(date);

    ANTLRInputStream stream = new ANTLRInputStream(remoteFilterJson.getCondition());
    QueryParserLexer lexer = new QueryParserLexer(stream);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    QueryParserParser parser = new QueryParserParser(tokenStream);
    parser.removeErrorListeners();
    LingualQueryCreator customVisitor = new LingualQueryCreator(remoteFilterJson.getSchema());
    String lingualExpression = customVisitor.visit(parser.eval());
    Assert.assertEquals(
            "((\"f1\" is not null and \"f1\" not in('1'))  and  \"f2\" = timestamp '"+dateInString+"')  or  (\"f3\" = cast(23.23 as float)  and  \"f4\" = cast(1234.123 as double))",
            lingualExpression);
}
项目:Hydrograph    文件:LingualQueryCreatorTest.java   
@Test
public void itShouldGenerateLingualQueryWithNotLikeClause() throws ParseException {
    String dateInString = "2012-01-10 00:00:00";
    remoteFilterJson.setCondition("(f1 not like 'condition' and f2 = '"+dateInString+"') or (f3=23.23 and f4=1234.123)");
    String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
    formatter.setTimeZone(TimeZone.getDefault());
    Date date = formatter.parse(dateInString);
    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    dateInString = formatter.format(date);

    ANTLRInputStream stream = new ANTLRInputStream(remoteFilterJson.getCondition());
    QueryParserLexer lexer = new QueryParserLexer(stream);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    QueryParserParser parser = new QueryParserParser(tokenStream);
    parser.removeErrorListeners();
    LingualQueryCreator customVisitor = new LingualQueryCreator(remoteFilterJson.getSchema());
    String lingualExpression = customVisitor.visit(parser.eval());

    Assert.assertEquals(
            "((\"f1\" is not null and \"f1\" not like 'condition')  and  \"f2\" = timestamp '"+dateInString+"')  or  (\"f3\" = cast(23.23 as float)  and  \"f4\" = cast(1234.123 as double))",
            lingualExpression);
}
项目:Hydrograph    文件:LingualQueryCreatorTest.java   
@Test
public void itShouldGenerateLingualQueryWithBetweenClause() {

    remoteFilterJson.setCondition("f3 between 10 and 20 and f3 BETWEEN 10 AND 20");

    ANTLRInputStream stream = new ANTLRInputStream(remoteFilterJson.getCondition());
    QueryParserLexer lexer = new QueryParserLexer(stream);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    QueryParserParser parser = new QueryParserParser(tokenStream);
    parser.removeErrorListeners();
    LingualQueryCreator customVisitor = new LingualQueryCreator(remoteFilterJson.getSchema());
    String lingualExpression = customVisitor.visit(parser.eval());
    Assert.assertEquals(
            "\"f3\" between  cast(10 as float)  and  cast(20 as float)  and  \"f3\" BETWEEN  cast(10 as float)  AND  cast(20 as float)",
            lingualExpression);
}
项目:Hydrograph    文件:LingualQueryCreatorTest.java   
@Test
public void itShouldGenerateLingualQueryWithMultipleCondition() {

    remoteFilterJson.setCondition("f1 = 'or maybe' AND f1 <> 'or mat' and f3 between 10 and 20");

    ANTLRInputStream stream = new ANTLRInputStream(remoteFilterJson.getCondition());
    QueryParserLexer lexer = new QueryParserLexer(stream);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    QueryParserParser parser = new QueryParserParser(tokenStream);
    parser.removeErrorListeners();
    LingualQueryCreator customVisitor = new LingualQueryCreator(remoteFilterJson.getSchema());
    String lingualExpression = customVisitor.visit(parser.eval());
    Assert.assertEquals(
            "\"f1\" = 'or maybe'  AND  \"f1\" <> 'or mat'  and  \"f3\" between  cast(10 as float)  and  cast(20 as float)",
            lingualExpression);
}
项目:Hydrograph    文件:LingualQueryCreatorTest.java   
@Test
public void itShouldGenerateLingualQueryForBetweenClauseAndTimestampDataType() throws ParseException {
    String dateInString1 = "1999-12-31 18:30:00";
    String dateInString2 = "2000-01-11 18:30:00";
    remoteFilterJson.setCondition("f2 between '"+dateInString1+"' AND '"+dateInString2+"'");
    String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
    formatter.setTimeZone(TimeZone.getDefault());
    Date date1 = formatter.parse(dateInString1);
    Date date2 = formatter.parse(dateInString2);
    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    dateInString1 = formatter.format(date1);
    dateInString2 = formatter.format(date2);

    ANTLRInputStream stream = new ANTLRInputStream(remoteFilterJson.getCondition());
    QueryParserLexer lexer = new QueryParserLexer(stream);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    QueryParserParser parser = new QueryParserParser(tokenStream);
    parser.removeErrorListeners();
    LingualQueryCreator customVisitor = new LingualQueryCreator(remoteFilterJson.getSchema());
    String lingualExpression = customVisitor.visit(parser.eval());
    Assert.assertEquals("\"f2\" between timestamp '"+dateInString1+"' AND  timestamp '"+dateInString2+"'", lingualExpression);
}
项目:boqa    文件:OboParser.java   
private void parseInputStream(CharStream inputStream, OboParseResultListener listener) {
  final OboLexer l = new OboLexer(inputStream);
  final Antlr4OboParser p = new Antlr4OboParser(new CommonTokenStream(l));

  p.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);
    }
  });

  if (debug) {
    p.addErrorListener(new DiagnosticErrorListener());
  }

  p.addParseListener(new OboParserListener(listener));

  p.oboFile();
}
项目:stvs    文件:IntervalParser.java   
/**
 * Parse an interval, for example <tt>[1,-]</tt> or <tt>-</tt> (a wildcard) or <tt>[1,4]</tt>.
 * Only fixed values are allowed, no variables.
 *
 * @param intervalAsString the string to be parsed.
 * @return a LowerBoundedInterval as the runtime representation of interval strings.
 * @throws ParseException in case the string doesn't fit the given fixed-interval grammar.
 */
public static LowerBoundedInterval parse(String intervalAsString) throws ParseException {
  CharStream charStream = new ANTLRInputStream(intervalAsString);
  CellExpressionLexer lexer = new CellExpressionLexer(charStream);
  TokenStream tokens = new CommonTokenStream(lexer);
  CellExpressionParser parser = new CellExpressionParser(tokens);
  parser.removeErrorListeners();
  parser.addErrorListener(new ThrowingErrorListener());
  try {
    CellExpressionParser.Fixed_intervalContext ctx = parser.fixed_interval();
    if (ctx == null) {
      throw new ParseException(0, 0, "Expected fixed interval");
    }
    return INSTANCE.visit(ctx);
  } catch (ParseRuntimeException runtimeException) {
    throw runtimeException.getParseException();
  }
}
项目:athena    文件:ParseTreeErrorListenerTest.java   
/**
 * Checks that no exception is generated for YANG file with valid syntax.
 */
@Test
public void checkValidYangFileForNoSyntaxError() throws IOException {

    ANTLRInputStream input = new ANTLRFileStream("src/test/resources/YangFileWithoutSyntaxError.yang");

    // Create a lexer that feeds off of input char stream.
    GeneratedYangLexer lexer = new GeneratedYangLexer(input);
    // Create a buffer of tokens pulled from the lexer.
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // Create a parser that feeds off the tokens buffer.
    GeneratedYangParser parser = new GeneratedYangParser(tokens);
    // Remove console error listener.
    parser.removeErrorListeners();
    // Create instance of customized error listener.
    ParseTreeErrorListener parseTreeErrorListener = new ParseTreeErrorListener();
    // Add customized error listener to catch errors during parsing.
    parser.addErrorListener(parseTreeErrorListener);
    // Begin parsing YANG file and generate parse tree.
    ParseTree tree = parser.yangfile();
}
项目:athena    文件:ParseTreeErrorListenerTest.java   
/**
 * Checks that exception is generated for YANG file with invalid syntax.
 */
@Test
public void checkInvalidYangFileForSyntaxError() throws IOException {

    // Get the exception occurred during parsing.
    thrown.expect(ParserException.class);
    thrown.expect(CustomExceptionMatcher.errorLocation(3, 0));
    thrown.expectMessage("no viable alternative at input 'yang-version 1\\nnamespace'");

    ANTLRInputStream input = new ANTLRFileStream("src/test/resources/YangFileWithSyntaxError.yang");

    // Create a lexer that feeds off of input char stream.
    GeneratedYangLexer lexer = new GeneratedYangLexer(input);
    // Create a buffer of tokens pulled from the lexer.
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // Create a parser that feeds off the tokens buffer.
    GeneratedYangParser parser = new GeneratedYangParser(tokens);
    // Remove console error listener.
    parser.removeErrorListeners();
    // Create instance of customized error listener.
    ParseTreeErrorListener parseTreeErrorListener = new ParseTreeErrorListener();
    // Add customized error listener to catch errors during parsing.
    parser.addErrorListener(parseTreeErrorListener);
    // Begin parsing YANG file and generate parse tree.
    ParseTree tree = parser.yangfile();
}
项目:antlr-examples    文件:ParserUtil.java   
public static <L extends Lexer, P extends Parser> P newParser(
    Function<CharStream, L> lexerFactory,
    Function<TokenStream, P> parserFactory,
    String input,
    boolean useBailErrorStrategy,
    boolean removeErrorListeners) {
  CharStream charStream = new ANTLRInputStream(input);
  L lexer = lexerFactory.apply(charStream);
  if (removeErrorListeners) {
    lexer.removeErrorListeners();
  }

  TokenStream tokenStream = new CommonTokenStream(lexer);
  P parser = parserFactory.apply(tokenStream);
  if (useBailErrorStrategy) {
    parser.setErrorHandler(new BailErrorStrategy());
  }
  if (removeErrorListeners) {
    parser.removeErrorListeners();
  }

  return parser;
}
项目:Suji    文件:SGFReader.java   
public SGFReader(String sgfString) {
    ANTLRInputStream inputStream = new ANTLRInputStream(sgfString);
    SGFLexer lexer = new SGFLexer(inputStream);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    parser = new SGFParser(tokens);

    visitor = new SGFVisitor();
}
项目:Suji    文件:SGFVisitorTest.java   
@Before
public void commonSetup() {
    ANTLRInputStream inputStream = new ANTLRInputStream("");
    sgfLexer = new SGFLexer(inputStream);
    CommonTokenStream tokenStream = new CommonTokenStream(sgfLexer);

    parser = new SGFParser(tokenStream);

    StringWriter errorWriter = new StringWriter();
    errorListener = new SGFErrorListener(errorWriter);
    parser.removeErrorListeners();
    parser.addErrorListener(errorListener);

    visitor = new SGFVisitor();
}
项目:Blindfold    文件:TreeViewGenerator.java   
public static Forest generate(String code) throws SyntaxError {
    Java8Lexer lexer = new Java8Lexer(CharStreams.fromString(code));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    Java8Parser parser = new Java8Parser(tokens);
    Java8Parser.CompilationUnitContext ast = parser.compilationUnit();
    if(parser.getNumberOfSyntaxErrors() > 0) {
        throw new SyntaxError();
    }
    Forest forest = new Forest();
    TreeViewListener treeViewListener = new TreeViewListener(forest);
    ParseTreeWalker.DEFAULT.walk(treeViewListener, ast);
    return forest;
}
项目:stvs    文件:ParsedCode.java   
/**
 * Parses a code. The handlers and listeners provided as parameters are called with the results
 * of the parsing; i.e. the parsedCodeListener is called with the resulting {@link ParsedCode},
 * the parsedTokenHandler is called with the list of parsed tokens etc.
 *
 * @param input the source code to parse
 * @param parsedTokenHandler a handler for lexed tokens
 * @param syntaxErrorsListener listener for a list of {@link SyntaxError}s
 * @param parsedCodeListener listener for parsed code.
 */
public static void parseCode(String input, ParsedTokenHandler parsedTokenHandler,
    ParsedSyntaxErrorHandler syntaxErrorsListener, ParsedCodeHandler parsedCodeListener) {
  SyntaxErrorListener syntaxErrorListener = new SyntaxErrorListener();

  IEC61131Lexer lexer = lex(input, parsedTokenHandler, syntaxErrorListener);

  TopLevelElements ast = parse(new CommonTokenStream(lexer), syntaxErrorListener);

  syntaxErrorsListener.accept(syntaxErrorListener.getSyntaxErrors());

  // Find types in parsed code
  TypeDeclarationVisitor typeVisitor = new TypeDeclarationVisitor();
  ast.accept(typeVisitor);
  Map<String, Type> definedTypesByName = new HashMap<>();
  typeVisitor.getDefinedTypes()
      .forEach(type -> definedTypesByName.put(type.getTypeName(), type));

  // Find IoVariables in parsed code
  VariableVisitor variableVisitor = new VariableVisitor();
  ast.accept(variableVisitor);

  // Find code blocks in parsed code
  BlockVisitor blockVisitor = new BlockVisitor();
  ast.accept(blockVisitor);
  List<FoldableCodeBlock> foldableCodeBlocks = blockVisitor.getFoldableCodeBlocks();

  parsedCodeListener.accept(new ParsedCode(foldableCodeBlocks,
      variableVisitor.getDefinedVariables(), typeVisitor.getDefinedTypes()));
}
项目:rapidminer    文件:AntlrParser.java   
/**
 * Parses the expression using antlr, aborts the parsing on the first error.
 *
 * @param expression
 *            an expression, not {@code null}
 * @return a {@link ParseTree} for further processing
 * @throws ExpressionException
 */
ParseTree parseExpression(String expression) throws ExpressionException {
    if (expression == null) {
        throw new IllegalArgumentException("expression must not be null");
    }
    ANTLRInputStream in = new ANTLRInputStream(expression);
    FunctionExpressionLexer lexer = new CapitulatingFunctionExpressionLexer(in);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    FunctionExpressionParser parser = new FunctionExpressionParser(tokens);

    parser.removeErrorListeners();
    lexer.removeErrorListeners();
    ExpressionErrorListener listener = new ExpressionErrorListener();
    parser.addErrorListener(listener);
    lexer.addErrorListener(listener);
    parser.setErrorHandler(new CapitulatingErrorStrategy());

    try {
        ParseTree tree = parser.operationExp();
        if (listener.containsError()) {
            throw new ExpressionException(listener.getErrorMessage(), listener.getErrorLine());
        } else {
            return tree;
        }
    } catch (CapitulatingRuntimeException e) {
        if (listener.containsError()) {
            throw new ExpressionException(listener.getErrorMessage(), listener.getErrorLine());
        } else {
            // cannot happen since the parser and lexer always register the error before trying
            // to recover
            throw new ExpressionException("Unknown error");
        }
    }

}
项目:BEAST    文件:BooleanExpANTLRHandler.java   
/**
 * Constructor
 * @param styledDocument the StyledDocument instance to analyse
 */
public BooleanExpANTLRHandler(StyledDocument styledDocument) {
    try {
        this.styledDocument = styledDocument;
        lexer = new FormalPropertyDescriptionLexer(new ANTLRInputStream(
                styledDocument.getText(0, styledDocument.getLength())));
        CommonTokenStream ts = new CommonTokenStream(lexer);
        parser = new FormalPropertyDescriptionParser(ts);
    } catch (BadLocationException ex) {
        Logger.getLogger(BooleanExpANTLRHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
}
项目:BEAST    文件:BooleanExpANTLRHandler.java   
/**
 * Method that parses the current input of a BooleanExpCodeArea and returns a
 * FormalPropertyDescriptionParser.BooleanExpListContext object which can then be used for building an AST
 * out of the input.
 * @return a BooleanExpListContext node from the ANTLR generated ParseTree.
 */
public FormalPropertyDescriptionParser.BooleanExpListContext getParseTree() {
    String text = null;
    try {
        text = styledDocument.getText(0, styledDocument.getLength());
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
    lexer.setInputStream(new ANTLRInputStream(text));
    CommonTokenStream ts = new CommonTokenStream(lexer);
    parser.setTokenStream(ts);
    return parser.booleanExpList();
}
项目:BEAST    文件:CAntlrHandler.java   
/**
 * constructor
 * @param pane the pane to hold the handler
*/
public CAntlrHandler(JTextPane pane) {
    this.pane = pane;
    lexer = new CLexer(new ANTLRInputStream(pane.getText()));
    CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
    cParser = new CParser(commonTokenStream);
}
项目:BEAST    文件:CAntlrHandler.java   
/**
 * gives the parse tree
 * @return the parse tree
*/
public ParseTree getCParseTree() {
    try {
        String code = pane.getStyledDocument().getText(0, pane.getStyledDocument().getLength());
        lexer.setInputStream(new ANTLRInputStream(code));
        CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
        cParser.setTokenStream(commonTokenStream);
        return cParser.compilationUnit();
    } catch (BadLocationException ex) {
        Logger.getLogger(CAntlrHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}
项目:BEAST    文件:CAntlrHandler.java   
/**
 * updates the parser
 */
public void updateParser() {
    try {
        String code = pane.getStyledDocument().getText(0, pane.getStyledDocument().getLength());
        lexer.setInputStream(new ANTLRInputStream(code));
        CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
        cParser.setTokenStream(commonTokenStream);
    } catch (BadLocationException ex) {
        Logger.getLogger(CAntlrHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
}
项目:BEAST    文件:FormalPropertySyntaxTreeToAstTranslaterTest.java   
@Test
public void testCreateAST() {
   String exp = "FOR_ALL_VOTERS(v) : EXISTS_ONE_CANDIDATE(c) : VOTES1(v) == c && VOTES1(v) == c;";
   FormalPropertyDescriptionLexer lexer = new FormalPropertyDescriptionLexer(new ANTLRInputStream(exp));
   CommonTokenStream tokenS = new CommonTokenStream(lexer);
   FormalPropertyDescriptionParser parser = new FormalPropertyDescriptionParser(tokenS);
   FormalPropertySyntaxTreeToAstTranslator translater = new FormalPropertySyntaxTreeToAstTranslator();
}
项目:BEAST    文件:ANTLRTest.java   
static void showGuiTreeView(final String code) {
    final org.antlr.v4.runtime.CharStream stream = new ANTLRInputStream(code);
    final FormalPropertyDescriptionLexer lexer = new FormalPropertyDescriptionLexer(stream);
    final CommonTokenStream tokens = new CommonTokenStream(lexer);
    final FormalPropertyDescriptionParser parser = new FormalPropertyDescriptionParser(tokens);
    final ParseTree tree = parser.booleanExpList();
    final List<String> ruleNames = Arrays.asList(FormalPropertyDescriptionParser.ruleNames);
    final TreeViewer view = new TreeViewer(ruleNames, tree);
    view.open();
}
项目:BEAST    文件:ANTLRTest.java   
static void showGuiTreeView(final String code) {
    final org.antlr.v4.runtime.CharStream stream = new ANTLRInputStream(code);
    final CLexer lexer = new CLexer(stream);
    final CommonTokenStream tokens = new CommonTokenStream(lexer);
    final CParser parser = new CParser(tokens);
    final ParseTree tree = parser.declaration();
    final List<String> ruleNames = Arrays.asList(CParser.ruleNames);
    final TreeViewer view = new TreeViewer(ruleNames, tree);
    view.open();
}
项目:travny    文件:Parser.java   
private static TravnyParser.SchemaFileContext parseContext(String dsl) {
    CharStream cs = new ANTLRInputStream(dsl);
    TravnyLexer lexer = new TravnyLexer(cs);
    lexer.removeErrorListeners();
    lexer.addErrorListener(new BreakingErrorListener());
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    TravnyParser parser = new TravnyParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(new BreakingErrorListener());

    TravnyParser.SchemaFileContext parsed = parser.schemaFile();


    return parsed;
}