static Tree parseTree(CharSource input) throws IOException { try (Reader reader = input.openStream()) { ThriftLexer lexer = new ThriftLexer(new ANTLRReaderStream(reader)); ThriftParser parser = new ThriftParser(new CommonTokenStream(lexer)); try { Tree tree = (Tree) parser.document().getTree(); if (parser.getNumberOfSyntaxErrors() > 0) { throw new IllegalArgumentException("syntax error"); } return tree; } catch (RecognitionException e) { throw new IllegalArgumentException(e); } } }
private static CommonTree getKoopaAST(String filename) throws FileNotFoundException, IOException, RecognitionException { System.out.println("Reading " + filename); Reader reader = new FileReader(filename); KGLexer lexer = new KGLexer(new ANTLRReaderStream(reader)); CommonTokenStream tokens = new CommonTokenStream(lexer); KGParser parser = new KGParser(tokens); KGParser.koopa_return koopa = parser.koopa(); CommonTree ast = (CommonTree) koopa.getTree(); if (SHOW_AST) { new ASTFrame("KG", ast).setVisible(true); } return ast; }
@Override public IParseResult doParse(Reader reader) { try { return parse(getDefaultRuleName(), new ANTLRReaderStream(reader)); } catch (IOException e) { throw new WrappedException(e); } }
@Override public IParseResult parse(ParserRule rule, Reader reader) { try { IParseResult parseResult = parse(rule.getName(), new ANTLRReaderStream(reader)); return parseResult; } catch (IOException e) { throw new WrappedException(e); } }
@Override public IParseResult parse(RuleCall ruleCall, Reader reader, int initialLookAhead) { try { NodeModelBuilder builder = nodeModelBuilder.get(); builder.setForcedFirstGrammarElement(ruleCall); IParseResult parseResult = doParse(ruleCall.getRule().getName(), new ANTLRReaderStream(reader), builder, initialLookAhead); return parseResult; } catch (IOException e) { throw new WrappedException(e); } }
protected CharStream getCharStream(final Reader reader) { try { return new ANTLRReaderStream(reader); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
/** * The only method that should be called to initiate the process * @param is The input stream from where to get the data * @param os The output stream to write the processed fragment/document to * @param htmlFilter An interface called during the processing of the document. Can be used to modify elements * @param convertIntoValidXML Converts the output into valid XML for XSL processing for example */ public static void process( Reader reader, Writer writer, IHTMLFilter htmlFilter, boolean convertIntoValidXML ) throws HandlingException { try { // Open a char stream input for the document ANTLRStringStream input = new ANTLRReaderStream( reader ); // Start lexing the input htmlLexerLexer lex = new htmlLexerLexer(input); // Tokenstream for the parser. CommonTokenStream tokens = new CommonTokenStream(lex); htmlParserParser parser = new htmlParserParser(tokens); htmlParserParser.document_return root = parser.document(); // Set up the tree parser CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree)root.getTree()); htmlTreeParser walker = new htmlTreeParser(nodes); // Initialize data structures topNode = new ThreadLocal(); currentNode = new ThreadLocal(); attrNode = new ThreadLocal(); // Walk in the entire document using the tree parser. walker.document(); // Get the top node TagNode top = (TagNode)topNode.get(); // Write the clean document out. top.writeAll( writer, htmlFilter, convertIntoValidXML, false ); } catch ( IOException ioe ) { throw new HandlingException( "Could not parse document" ); } catch ( RecognitionException re ) { throw new HandlingException( "Could not parse document" ); } }
private static ANTLRReaderStream getAntlrStream(String code) { try { return new ANTLRReaderStream(new StringReader(code)); } catch (IOException e) { // Will never happen, since it's a StringReader... return null; } }
public static CSSInputStream stringStream(String source) throws IOException { CSSInputStream stream = new CSSInputStream(); stream.rawData = source; stream.encoding = Charset.defaultCharset().name(); BufferedReader br = new BufferedReader( new InputStreamReader(new ByteArrayInputStream(source.getBytes()), stream.encoding)); stream.input = new ANTLRReaderStream(br); return stream; }
private List<Integer> tokenStartPositions() throws IOException { List<Integer> res = new ArrayList<Integer>(); ANTLRReaderStream input = new ANTLRInputStream(new FileInputStream(masterFile)); v4Lexer lexer = new v4Lexer(input); Token t = lexer.nextToken(); while (t.getType() != Token.EOF) { if (t.getChannel() != Token.HIDDEN_CHANNEL) { res.add(lexer.getCharIndex()); } t = lexer.nextToken(); } return res; }
public static ObjCppParser newParser(String s) throws IOException { Result result = new Result(new JNAeratorConfig(), null, null); ObjCppParser parser = new ObjCppParser(new CommonTokenStream(new ObjCppLexer( new ANTLRReaderStream(new StringReader(s)))) // , new DummyDebugEventListener() ); parser.objCParserHelper = result.typeConverter; return parser; }
static ObjCDemanglingParser newObjCDemangler(String s, final boolean verbose) throws IOException { return new ObjCDemanglingParser( new CommonTokenStream( new ObjCDemanglingLexer( new ANTLRReaderStream(new StringReader(s)))) // , new DummyDebugEventListener() ) { @Override public void reportError(RecognitionException arg0) { if (verbose) { // System.err.println(arg0 + " (next = '" + next() + "')"); super.reportError(arg0); } } }; }
protected ObjCppParser newObjCppParser(TypeConversion typeConverter, String s, final boolean verbose, final PrintStream errorOut) throws IOException { ObjCppParser parser = new ObjCppParser( new CommonTokenStream( new ObjCppLexer( new ANTLRReaderStream(new StringReader(s)))) // , new DummyDebugEventListener() ) { @Override public void emitErrorMessage(String msg) { if (errorOut == null) { // if (verbose) // super.emitErrorMessage(msg); } else { errorOut.println(msg); } } @Override public void reportError(RecognitionException arg0) { if (verbose) { super.reportError(arg0); } } }; parser.setupScopes(); parser.objCParserHelper = typeConverter; return parser; }
private List<String> extractTagsFromString(String in) throws IOException { try (Reader stringToInputStream = new StringReader(in)) { TagLexer tagsLexer = new TagLexer(new ANTLRReaderStream(stringToInputStream)); CommonTokenStream tokens = new CommonTokenStream(tagsLexer); TagParser pa = new TagParser(tokens); return pa.getParsedTags(); } }
public static CommonTokenStream getTokenStream(java.io.Reader reader) throws java.io.IOException { SExpressionLexer lexer = new SExpressionLexer(new ANTLRReaderStream(reader)); return new CommonTokenStream(lexer); }
/** * Loads the proto from an {@link ANTLRReaderStream}. */ public static void loadFrom(ANTLRReaderStream input, Proto target) throws Exception { // Create an ExprLexer that feeds from that stream ProtoLexer lexer = new ProtoLexer(input); // Create a stream of tokens fed by the lexer CommonTokenStream tokens = new CommonTokenStream(lexer); // Create a parser that feeds off the token stream ProtoParser parser = new ProtoParser(tokens); // Begin parsing at rule parse parser.parse(target); }
@Override public CLObject parse(Reader in) throws CLScriptException { try { return invokeParser(new ANTLRReaderStream(in)); } catch (IOException ex) { throw new CLScriptException(ex); } }
@Override public CLCommand parse(Reader in) throws CLScriptException { try { return invokeParser(new ANTLRReaderStream(in)); } catch (IOException ex) { throw new CLScriptException(ex); } }
@Override public CLParmAbstractComponent parse(Reader in) throws CLScriptException { try { return invokeParser(new ANTLRReaderStream(in)); } catch (IOException ex) { throw new CLScriptException(ex); } }
@Override public QExpression parse(Reader in) throws CLScriptException { try { return invokeParser(new ANTLRReaderStream(in)); } catch (IOException ex) { throw new CLScriptException(ex); } }
static MatlabLexer getLexer(String filename) throws IOException { BufferedReader in = new BufferedReader(new FileReader(filename)); MatlabLexer lexer = new MatlabLexer(new ANTLRReaderStream(in)); return lexer; }
public TCCTokenizer(Reader reader) throws IOException { initialize(new ANTLRReaderStream(reader)); }
/** * Loads the proto from a {@link Reader}. */ public static void loadFrom(Reader reader, Proto target) throws Exception { loadFrom(new ANTLRReaderStream(reader), target); }
private void colorize(StyledDocument document, String pathToGrammarResource) { try { final InputStream resourceStream = GrammarView.class .getResourceAsStream(pathToGrammarResource); final InputStreamReader reader = new InputStreamReader( resourceStream); KGLexer lexer = new KGLexer(new ANTLRReaderStream(reader)); boolean justSawDef = false; while (true) { CommonToken token = (CommonToken) lexer.nextToken(); if (token.getType() == Token.EOF) break; Style style = null; if (token.getType() == KGLexer.COMMENT) { style = getCommentStyle(document); justSawDef = false; } else if ("def".equals(token.getText())) { style = getKeywordStyle(document); justSawDef = true; } else if ("def".equals(token.getText())) { style = getKeywordStyle(document); justSawDef = false; } else if (token.getType() == KGLexer.IDENTIFIER) { if (!token.getText().toUpperCase().equals(token.getText())) style = getIdentifierStyle(document); if (justSawDef) ruleNameOffsets.put(token.getText(), token.getStartIndex()); justSawDef = false; } else if (token.getType() == KGLexer.NATIVE_CODE) { style = getNativeStyle(document); justSawDef = false; } else if (token.getType() == KGLexer.WHITESPACE) { // Nop. } else { justSawDef = false; } if (style != null) { final int start = token.getStartIndex(); final int stop = token.getStopIndex(); final int len = stop - start + 1; document.setCharacterAttributes(start, len, style, false); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public static String generate(String name, String pack, String path) throws IOException, RecognitionException { if (!path.endsWith("/")) path += "/"; Reader reader = new FileReader(path + name + ".kg"); KGLexer lexer = new KGLexer(new ANTLRReaderStream(reader)); CommonTokenStream tokens = new CommonTokenStream(lexer); KGParser parser = new KGParser(tokens); KGParser.koopa_return koopa = parser.koopa(); CommonTree ast = (CommonTree) koopa.getTree(); if (SHOW_AST) { new ASTFrame("KG", ast).setVisible(true); } // The following is for testing purposes. // CommonTreeNodeStream nodes = new CommonTreeNodeStream(ast); // KGTreeParser treeParser = new KGTreeParser(nodes); // treeParser.koopa(); Reader templatesIn = new InputStreamReader( KGG.class .getResourceAsStream("/koopa/grammars/generator/koopa.stg")); StringTemplateGroup templates = new StringTemplateGroup(templatesIn, DefaultTemplateLexer.class); // templates.emitDebugStartStopStrings(true); CommonTreeNodeStream nodes = new CommonTreeNodeStream(ast); KGGenerator walker = new KGGenerator(nodes); walker.setTemplateLib(templates); String formatted = walker.koopa(name, pack, contents(path + name + ".kg-imports"), contents(path + name + ".kg-usercode")).toString(); // System.out.println(formatted); return formatted; }