@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; }
/** * @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)); }
/** * @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; }
/** * 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); }
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; }
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(); }
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(); }
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()); }
@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); }
@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()); }
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; }
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; }
@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); }
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 + " \""); } } }
@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; }
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); }
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); }
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); }
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(); }
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); } }
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 + "'"); } }
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; }
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; }
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; }
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(); }
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(); }
/** * 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()); }
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; }
@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(); }
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(); }
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; }
static long getResult(String equationString) throws LogicGrammarParsingException { CharStream is = CharStreams.fromString(equationString); LogicGrammarLexer lex = new LogicGrammarLexer(is); CommonTokenStream tokens = new CommonTokenStream(lex); LogicGrammarParser parser = new LogicGrammarParser(tokens); System.out.println("Parsing equation: " + equationString); long final_result; try { final_result = parser.root().result; } catch (RecognitionException | NumberFormatException nfe) { throw new LogicGrammarParsingException(); } return final_result; }
private void execute(String instruction) { ProcessorLexer lexer = new ProcessorLexer(CharStreams.fromString(instruction)); CommonTokenStream tokens = new CommonTokenStream(lexer); ProcessorParser parser = new ProcessorParser(tokens); ProcessorParser.ProgContext tree = parser.prog(); this.visit(tree); }
/** * Assembles the given code in to an {@link ExecutableCode}. * * @param code T code to be assembled. * * @return The executable code. * * @throws SyntaxError If the code contains syntax error(s). * @throws LabelError If an undefined label used or duplicate labels exist. * @see ExecutableCode * */ public ExecutableCode assemble(String code) throws SyntaxError, LabelError { AssemblerLexer lexer = new AssemblerLexer(CharStreams.fromString(code)); CommonTokenStream tokens = new CommonTokenStream(lexer); AssemblerParser parser = new AssemblerParser(tokens); parser.prog(); if(parser.getNumberOfSyntaxErrors() > 0) { throw new SyntaxError("You have error(s) in your code."); } ConstantValidator.validate(code); ExecutableCode executableCode = linker.link(code); loader.load(executableCode); return executableCode; }
public static void validate(String code) { ConstantValidator constantValidator = new ConstantValidator(); AssemblerLexer lexer = new AssemblerLexer(CharStreams.fromString(code)); CommonTokenStream tokens = new CommonTokenStream(lexer); AssemblerParser parser = new AssemblerParser(tokens); AssemblerParser.ProgContext program = parser.prog(); constantValidator.visit(program); }
private static void run(String expr) throws Exception { //构建流CodePointCharStream(4.7版本ANTLRInputStream标记为deprecated) CodePointCharStream in = CharStreams.fromString(expr); //词法分析 LionLexer lexer = new LionLexer(in); //token流 CommonTokenStream tokens = new CommonTokenStream(lexer); //语法分析器 LionParser parser = new LionParser(tokens); //验证 parser.prog(); }
/** * Build and return {@link Antlr4OboParser} for a given <code>text</code>. * * @param text String with the text to parse. * @param mode Name of the mode to use. * @return {@link Antlr4OboParser}, readily setup for parsing the OBO file. */ protected Antlr4OboParser buildParser(String text, String mode) { final CodePointCharStream inputStream = CharStreams.fromString(text); final OboLexer l = new OboLexer(inputStream); for (int i = 0; i < l.getModeNames().length; ++i) { if (mode.equals(l.getModeNames()[i])) { l.mode(i); } } 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); } }); p.addErrorListener(new DiagnosticErrorListener()); p.addParseListener(outerListener); return p; }
public static Matcher compile(String query) { try { CodePointCharStream input = CharStreams.fromReader(new StringReader(query)); ExprLexer lexer = new ExprLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); ExprParser parser = new ExprParser(tokens); ParseTree tree = parser.expr(); Visitor visitor = new Visitor(); visitor.visit(tree); return visitor.expression().normalize(); } catch (IOException e) { throw new RuntimeException(e.getLocalizedMessage(), e); } }
public static StringTemplate of(final String template) { final StringTemplateLexer lexer = new StringTemplateLexer(CharStreams.fromString(template)); final CommonTokenStream tokenStream = new CommonTokenStream(lexer); final StringTemplateParser.StringTemplateContext stringTemplateContext = new StringTemplateParser(tokenStream).stringTemplate(); final List<StringTemplate.Part> parts = new StringTemplateVisitor().visitStringTemplate(stringTemplateContext); return of(parts); }
public StructuredQuery parse(String query) throws ParseCancellationException { GraphflowLexer lexer = new GraphflowLexer(CharStreams.fromString(query)); lexer.removeErrorListeners(); // Remove default listeners first. lexer.addErrorListener(ErrorListener.INSTANCE); GraphflowParser parser = new GraphflowParser(new CommonTokenStream(lexer)); parser.removeErrorListeners(); // Remove default listeners first. parser.addErrorListener(ErrorListener.INSTANCE); try { ParseTree tree = parser.graphflow(); GraphflowVisitor visitor = new GraphflowVisitor(); return (StructuredQuery) visitor.visit(tree); } catch (Exception e) { throw new ParseCancellationException(e.getMessage()); } }