java类org.antlr.v4.runtime.CharStreams的实例源码

PainterLogic.java 文件源码 项目:bitbox 阅读 20 收藏 0 点赞 0 评论 0
@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;
}
App.java 文件源码 项目:CTalk 阅读 36 收藏 0 点赞 0 评论 0
/**
 * @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));
}
SqlParser.java 文件源码 项目:coherence-sql 阅读 22 收藏 0 点赞 0 评论 0
/**
 * @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;
}
Linker.java 文件源码 项目:ETUmulator 阅读 23 收藏 0 点赞 0 评论 0
/**
 * 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);
}
TypeExpressionConstructor.java 文件源码 项目:rest-modeling-framework 阅读 17 收藏 0 点赞 0 评论 0
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;
}
TemplateStringEvaluator.java 文件源码 项目:jobson 阅读 15 收藏 0 点赞 0 评论 0
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();
}
UserAgentTreeFlattener.java 文件源码 项目:yauaa 阅读 24 收藏 0 点赞 0 评论 0
private UserAgentContext parseUserAgent(UserAgent userAgent) {
    String userAgentString = EvilManualUseragentStringHacks.fixIt(userAgent.getUserAgentString());

    CodePointCharStream input = CharStreams.fromString(userAgentString);
    UserAgentLexer lexer = new UserAgentLexer(input);

    CommonTokenStream tokens = new CommonTokenStream(lexer);

    UserAgentParser parser = new UserAgentParser(tokens);

    if (!verbose) {
        lexer.removeErrorListeners();
        parser.removeErrorListeners();
    }
    lexer.addErrorListener(userAgent);
    parser.addErrorListener(userAgent);

    return parser.userAgent();
}
PredicateCompiler.java 文件源码 项目:DataFrame 阅读 18 收藏 0 点赞 0 评论 0
public static FilterPredicate compile(String predicateString){
    predicateString = predicateString.trim();
    if(predicateString.isEmpty()){
        return FilterPredicate.empty();
    }
    PredicateCompileErrorListener errorListener = new PredicateCompileErrorListener(predicateString);

    CharStream stream = CharStreams.fromString(predicateString);
    PredicateLexer lexer = new PredicateLexer(stream);
    lexer.removeErrorListener(ConsoleErrorListener.INSTANCE);
    lexer.addErrorListener(errorListener);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    PredicateParser parser = new PredicateParser(tokens);
    parser.removeErrorListener(ConsoleErrorListener.INSTANCE);
    parser.addErrorListener(errorListener);
    FilterPredicateVisitor filterPredicateVisitor = new FilterPredicateVisitor();
    return  filterPredicateVisitor.visit(parser.compilationUnit());
}
ScoreTranslatorTest.java 文件源码 项目:fix-orchestra 阅读 16 收藏 0 点赞 0 评论 0
@Test
public void testExampleFieldCondition() throws Exception {
  ScoreLexer l = new ScoreLexer(CharStreams.fromString(expression));
  ScoreParser p = new ScoreParser(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(String.format(
          "Failed to parse at line %d position %d due to %s", line, charPositionInLine, msg), e);
    }
  });
  ScoreTranslator visitor = new ScoreTranslator();
  AnyExpressionContext ctx = p.anyExpression();
  String text = visitor.visitAnyExpression(ctx);
  System.out.println(text);
}
DslExpressionTest.java 文件源码 项目:fix-orchestra 阅读 17 收藏 0 点赞 0 评论 0
@Test
public void testExampleFieldCondition() throws Exception {
  ScoreLexer l = new ScoreLexer(CharStreams.fromString(fieldCondition));
  ScoreParser p = new ScoreParser(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(String.format(
          "Failed to parse at line %d position %d due to %s", line, charPositionInLine, msg), e);
    }
  });
  ScoreBaseVisitor<Object> visitor = new ScoreBaseVisitor<>();
  AnyExpressionContext ctx = p.anyExpression();
  Object expression = visitor.visitAnyExpression(ctx);
  //System.out.println(expression.getClass().getSimpleName());
}
Main.java 文件源码 项目:Alpha 阅读 20 收藏 0 点赞 0 评论 0
private static Program combineInput(boolean literate, String... fileNames) throws IOException {
    final Program result = new Program();

    for (String fileName : fileNames) {
        CharStream stream;
        if (!literate) {
            stream = CharStreams.fromFileName(fileName);
        } else {
            stream = CharStreams.fromChannel(
                streamToChannel(literate(lines(Paths.get(fileName)))),
                4096,
                CodingErrorAction.REPLACE,
                fileName
            );
        }
        result.accumulate(PARSER.parse(stream));
    }

    return result;
}
PluginHelper.java 文件源码 项目:sonar-tsql-plugin 阅读 22 收藏 0 点赞 0 评论 0
public static FillerRequest createRequest(final SourceLinesProvider linesProvider, final InputFile file, final Charset encoding)
        throws IOException, FileNotFoundException {
    final CharStream mainStream = CharStreams.fromPath(file.path(), encoding);
    final CharStream charStream = new CaseChangingCharStream(mainStream, true);
    final TSqlLexer lexer = new TSqlLexer(charStream);

    lexer.removeErrorListeners();

    final CommonTokenStream stream = new CommonTokenStream(lexer);

    stream.fill();
    final TSqlParser parser = new TSqlParser(stream);
    parser.removeErrorListeners();
    final ParseTree root = parser.tsql_file();
    final FillerRequest antrlFile = new FillerRequest(file, stream, root,
            linesProvider.getLines(new FileInputStream(file.file()), encoding));
    return antrlFile;
}
DefaultLinesProviderTest.java 文件源码 项目:sonar-tsql-plugin 阅读 16 收藏 0 点赞 0 评论 0
@Test
public void test() {

    final CharStream charStream = CharStreams.fromString("\r\nSELECT\r\n 1");

    final TSqlLexer lexer = new TSqlLexer(charStream);

    final CommonTokenStream stream = new CommonTokenStream(lexer);

    stream.fill();
    TSqlParser parser = new TSqlParser(stream);
    ParseTree child = parser.tsql_file().getChild(0);
    DefaultLinesProvider lines = new DefaultLinesProvider(stream);
    int line = lines.getLine(new ParsedNode(child));
    Assert.assertEquals(2, line);

}
SQLToKuduPredicatesTranslator.java 文件源码 项目:apex-malhar 阅读 15 收藏 0 点赞 0 评论 0
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 + " \"");
    }
  }
}
LocalFileReader.java 文件源码 项目:protostuff-compiler 阅读 16 收藏 0 点赞 0 评论 0
@Nullable
@Override
public CharStream read(String name) {
    for (Path prefix : pathList) {
        Path path = prefix.resolve(name);
        if (Files.isRegularFile(path)) {
            try {
                byte[] bytes = Files.readAllBytes(path);
                String result = new String(bytes, StandardCharsets.UTF_8);
                return CharStreams.fromString(result);
            } catch (IOException e) {
                LOGGER.trace("Could not read {}", path, e);
            }
        }
    }
    return null;
}
ServiceParseListenerTest.java 文件源码 项目:protostuff-compiler 阅读 38 收藏 0 点赞 0 评论 0
private Service parseService(String input) {
    CharStream stream = CharStreams.fromString(input);
    ProtoLexer lexer = new ProtoLexer(stream);
    lexer.removeErrorListeners();
    lexer.addErrorListener(TestUtils.ERROR_LISTENER);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    ProtoParser parser = new ProtoParser(tokenStream);
    parser.removeErrorListeners();
    parser.addErrorListener(TestUtils.ERROR_LISTENER);
    ProtoContext context = new ProtoContext("test.proto");
    ServiceParseListener serviceParseListener = new ServiceParseListener(tokenStream, context);
    OptionParseListener optionParseListener = new OptionParseListener(tokenStream, context);
    Proto proto = new Proto();
    context.push(proto);
    parser.addParseListener(serviceParseListener);
    parser.addParseListener(optionParseListener);
    parser.serviceBlock();
    return proto.getServices().get(0);
}
EnumParseListenerTest.java 文件源码 项目:protostuff-compiler 阅读 15 收藏 0 点赞 0 评论 0
private Enum parseEnumBlock(String input) {
    CharStream stream = CharStreams.fromString(input);
    ProtoLexer lexer = new ProtoLexer(stream);
    lexer.removeErrorListeners();
    lexer.addErrorListener(TestUtils.ERROR_LISTENER);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    ProtoParser parser = new ProtoParser(tokenStream);
    parser.setErrorHandler(new BailErrorStrategy());
    parser.removeErrorListeners();
    parser.addErrorListener(TestUtils.ERROR_LISTENER);
    ProtoContext context = new ProtoContext("test.proto");
    Proto proto = new Proto();
    context.push(proto);
    EnumParseListener enumParseListener = new EnumParseListener(tokenStream, context);
    OptionParseListener optionParseListener = new OptionParseListener(tokenStream, context);
    parser.addParseListener(enumParseListener);
    parser.addParseListener(optionParseListener);
    parser.enumBlock();
    return proto.getEnums().get(0);
}
MessageParseListenerTest.java 文件源码 项目:protostuff-compiler 阅读 16 收藏 0 点赞 0 评论 0
private Message parseMessage(String input) {
    CharStream stream = CharStreams.fromString(input);
    ProtoLexer lexer = new ProtoLexer(stream);
    lexer.removeErrorListeners();
    lexer.addErrorListener(TestUtils.ERROR_LISTENER);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    ProtoParser parser = new ProtoParser(tokenStream);
    parser.removeErrorListeners();
    parser.addErrorListener(TestUtils.ERROR_LISTENER);
    ProtoContext context = new ProtoContext("test.proto");
    MessageParseListener messageParseListener = new MessageParseListener(tokenStream, context);
    OptionParseListener optionParseListener = new OptionParseListener(tokenStream, context);
    Proto proto = new Proto();
    context.push(proto);
    parser.addParseListener(messageParseListener);
    parser.addParseListener(optionParseListener);
    parser.messageBlock();
    return proto.getMessages().get(0);
}
ProtoParseListenerTest.java 文件源码 项目:protostuff-compiler 阅读 14 收藏 0 点赞 0 评论 0
private Proto parseProto(String input) {
    CharStream stream = CharStreams.fromString(input);
    ProtoLexer lexer = new ProtoLexer(stream);
    lexer.removeErrorListeners();
    lexer.addErrorListener(TestUtils.ERROR_LISTENER);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    ProtoParser parser = new ProtoParser(tokenStream);
    parser.removeErrorListeners();
    parser.addErrorListener(TestUtils.ERROR_LISTENER);
    ProtoContext context = new ProtoContext("test.proto");
    ProtoParseListener protoParseListener = new ProtoParseListener(tokenStream, context);
    OptionParseListener optionParseListener = new OptionParseListener(tokenStream, context);
    parser.addParseListener(protoParseListener);
    parser.addParseListener(optionParseListener);
    parser.proto();
    return context.getProto();
}
AutomatonParserSpec.java 文件源码 项目:uima-tokens-regex 阅读 14 收藏 0 点赞 0 评论 0
private void initAutomataWithCustomHeader(String body, boolean allowMatchingEmptySequences, String header) {
    try {

        body = header + body;

        CharStream input = CharStreams.fromString(body);
        UimaTokenRegexParser parser = initListenerAndGetParser(input, new URL("file://dynamic.text"));
        parser.removeErrorListeners();
        parser.addErrorListener(new ThrowingErrorListener(new URL("file://dynamic.text")));

        listener.setAllowMatchingEmptySequences(allowMatchingEmptySequences);
        ParseTreeWalker.DEFAULT.walk(listener, parser.ruleList());
        this.rules = listener.getRules();
    } catch(MalformedURLException e) {
        throw new RuntimeException(e);
    }
}
SearchCondConverter.java 文件源码 项目:syncope 阅读 14 收藏 0 点赞 0 评论 0
public static SearchCond convert(final SearchCondVisitor visitor, final String filter) {
    SCIMFilterParser parser = new SCIMFilterParser(new CommonTokenStream(
            new SCIMFilterLexer(CharStreams.fromString(filter))));
    parser.setBuildParseTree(true);
    parser.setTrimParseTree(true);
    parser.setProfile(true);
    parser.removeErrorListeners();
    parser.setErrorHandler(new SCIMFilterErrorHandler());

    try {
        return visitor.visit(parser.scimFilter());
    } catch (Exception e) {
        LOG.error("Could not parse '{}'", filter, e);
        throw new BadRequestException(ErrorType.invalidFilter, "Could not parse '" + filter + "'");
    }
}
YangStatementStreamSource.java 文件源码 项目:yangtools 阅读 14 收藏 0 点赞 0 评论 0
private static StatementContext parseYangSource(final SourceIdentifier source, final InputStream stream)
        throws IOException, YangSyntaxErrorException {
    final YangStatementLexer lexer = new YangStatementLexer(CharStreams.fromStream(stream));
    final CommonTokenStream tokens = new CommonTokenStream(lexer);
    final YangStatementParser parser = new YangStatementParser(tokens);
    //disconnect from console error output
    parser.removeErrorListeners();

    final YangErrorListener errorListener = new YangErrorListener(source);
    parser.addErrorListener(errorListener);

    final StatementContext result = parser.statement();
    errorListener.validate();

    // Walk the resulting tree and replace each children with an immutable list, lowering memory requirements
    // and making sure the resulting tree will not get accidentally modified. An alternative would be to use
    // org.antlr.v4.runtime.Parser.TrimToSizeListener, but that does not make the tree immutable.
    ParseTreeWalker.DEFAULT.walk(MAKE_IMMUTABLE_LISTENER, result);

    return result;
}
RulesetGroupFactory.java 文件源码 项目:nominal 阅读 15 收藏 0 点赞 0 评论 0
private static RulesetGroup parseStream(RulesetGroup rulesetGroup, InputStream is) 
    throws IOException {
CharStream input = CharStreams.fromStream( is );
NominalLexer nominalLexer = new NominalLexer( input );
CommonTokenStream tokens = new CommonTokenStream( nominalLexer );
NominalParser nominalParser = new NominalParser( tokens );
ParseTree parseTree = nominalParser.file();

NominalVisitor nominalVisitor = 
    new NominalVisitorImplementation( rulesetGroup );
nominalVisitor.visit( parseTree );

addDummyRulesets( rulesetGroup );

return rulesetGroup;
   }
MemoryModelAST.java 文件源码 项目:org.pshdl 阅读 14 收藏 0 点赞 0 评论 0
public static Unit parseUnit(String string, Set<Problem> problems, int lineOffset) throws IOException {
    final CharStream input = CharStreams.fromString(string);
    final MemoryModelLexer lexer = new MemoryModelLexer(input);
    final CommonTokenStream tokens = new CommonTokenStream(lexer);
    final MemoryModelParser parser = new MemoryModelParser(tokens);
    final PSHDLParser.SyntaxErrorCollector listener = new PSHDLParser.SyntaxErrorCollector(tokens, problems, lineOffset);
    lexer.removeErrorListeners();
    lexer.addErrorListener(listener);
    parser.removeErrorListeners();
    parser.addErrorListener(listener);
    final UnitContext unit = parser.unit();
    if (problems.isEmpty()) {
        final MemoryModelAST modelAST = new MemoryModelAST();
        final ParseTreeWalker walker = new ParseTreeWalker();
        walker.walk(modelAST, unit);
        return modelAST.unit;
    }
    return null;
}
Main.java 文件源码 项目:ANTLR4 阅读 15 收藏 0 点赞 0 评论 0
public static int run(final String data) {
    // Create a CharStream that reads from standard input.
    final CharStream input = CharStreams.fromString(data);

    // Create a lexer that feeds off of input CharStream.
    final ExprSimpleLexer lexer = new ExprSimpleLexer(input);

    // Create a buffer of tokens pulled from the lexer.
    final CommonTokenStream tokens = new CommonTokenStream(lexer);

    // Create a parser that feeds off the tokens buffer.
    final ExprSimpleParser parser = new ExprSimpleParser(tokens);

    // Begin parsing at "prog" rule.
    final ParseTree tree = parser.prog();

    // Create a generic parse tree walker that can trigger callbacks.
    final ParseTreeWalker walker = new ParseTreeWalker();

    // Walk the tree created during the parse, trigger callbacks.
    final ExprListener listener = new ExprListener();
    walker.walk(listener, tree);
    return listener.getResult();
}
MainVisitor.java 文件源码 项目:ANTLR4 阅读 17 收藏 0 点赞 0 评论 0
public static int run(final String data) throws IOException {
    CharStream input;
    if (data == null) {
        // Create a CharStream that reads from standard input.
        input = CharStreams.fromStream(System.in);
    } else {
        input = CharStreams.fromString(data);
    }

    // Create a lexer that feeds off of input CharStream.
    final ExprAdvancedLexer lexer = new ExprAdvancedLexer(input);

    // Create a buffer of tokens pulled from the lexer.
    final CommonTokenStream tokens = new CommonTokenStream(lexer);

    // Create a parser that feeds off the tokens buffer.
    final ExprAdvancedParser parser = new ExprAdvancedParser(tokens);

    // Begin parsing at "prog" rule.
    final ParseTree tree = parser.prog();

    final ExprVisitor eval = new ExprVisitor();
    eval.visit(tree);
    return eval.getResult();
}
NameParserANTLR.java 文件源码 项目:name-parser 阅读 15 收藏 0 点赞 0 评论 0
/**
 * Fully parse the supplied name also trying to extract authorships, a conceptual sec reference, remarks or notes
 * on the nomenclatural status. In some cases the authorship parsing proves impossible and this nameparser will
 * return null.
 *
 * For strings which are no scientific names and scientific names that cannot be expressed by the ParsedName class
 * the parser will throw an UnparsableException with a given NameType and the original, unparsed name. This is the
 * case for all virus names and proper hybrid formulas, so make sure you catch and process this exception.
 *
 * @param scientificName the full scientific name to parse
 * @param rank the rank of the name if it is known externally. Helps identifying infrageneric names vs bracket authors
 *
 * @throws UnparsableNameException
 */
public ParsedName parse(final String scientificName, Rank rank) throws UnparsableNameException {
  if (Strings.isNullOrEmpty(scientificName)) {
    unparsable(NameType.NO_NAME, null);
  }

  SciNameLexer lexer = new SciNameLexer(CharStreams.fromString(scientificName));

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

  SciNameParser parser = new SciNameParser(tokens);

  // use visitor to transform to ParsedName
  ParsedNameVisitor visitor = new ParsedNameVisitor();
  return visitor.visit(parser.scientificName());

}
WorkspacePresentation.java 文件源码 项目:Ultrastructure 阅读 15 收藏 0 点赞 0 评论 0
public static WorkspaceContext getWorkspaceContext(InputStream source) throws IOException {
    WorkspaceLexer l = new WorkspaceLexer(CharStreams.fromStream(source));
    WorkspaceParser p = new WorkspaceParser(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);
        }
    });
    WorkspaceContext ctx = p.workspace();
    return ctx;
}
TestParse.java 文件源码 项目:Ultrastructure 阅读 31 收藏 0 点赞 0 评论 0
@Test
public void testParse() throws Exception {
    WorkspaceLexer l = new WorkspaceLexer(CharStreams.fromStream(getClass().getResourceAsStream("/thing.wsp")));
    WorkspaceParser p = new WorkspaceParser(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);
        }
    });
    p.workspace();
}
Spa.java 文件源码 项目:Ultrastructure 阅读 15 收藏 0 点赞 0 评论 0
public static Spa manifest(String resource) throws IOException {
    SpaLexer l = new SpaLexer(CharStreams.fromStream(Utils.resolveResource(Spa.class,
                                                                           resource)));
    SpaParser p = new SpaParser(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);
        }
    });
    SpaContext spa = p.spa();
    SpaImporter importer = new SpaImporter();
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(importer, spa);
    return importer.getSpa();
}


问题


面经


文章

微信
公众号

扫码关注公众号