/** Creates a properly-configured parser object for the E4X grammar. */ public static RegexSimpleParser parserOn(Reader in) throws IOException { ANTLRReaderStream cs = new ANTLRReaderStream(in); RegexSimpleLexer lexer = new RegexSimpleLexer(cs); LinkedListTokenSource source = new LinkedListTokenSource(lexer); LinkedListTokenStream stream = new LinkedListTokenStream(source); RegexSimpleParser parser = new RegexSimpleParser(stream); parser.setTreeAdaptor(TREE_ADAPTOR); parser.setInput(lexer, cs); return parser; }
private static RegexSimpleParser regexpParserOn(Reader in, LinkedListTokenStream stream) throws IOException { ANTLRReaderStream cs = new ANTLRReaderStream(in); RegexSimpleLexer lexer = new RegexSimpleLexer(cs); LinkedListTokenSource source = (LinkedListTokenSource) stream.getTokenSource(); source.setDelegate(lexer); // The AS3 grammar will see the initial '/' as an DIV (divide) // token, and lookahead in the AS3Parser will have already // grabbed references to that token in order to make it the // startToken for various AST subtrees, so we can't just delete // it. We therefore find the DIC token and change its type to // match the regexp vocabulary, and then rewind the token stream // so that this will be the first token that the // SinpleRegexpParser will see. LinkedListToken startMarker = (LinkedListToken) stream.LT(-1); startMarker.setType(RegexSimpleParser.REGEXP_DELIMITER); stream.seek(stream.index() - 1); RegexSimpleParser parser = new RegexSimpleParser(stream); parser.setTreeAdaptor(new LinkedListTreeAdaptor()); parser.setInput(lexer, cs); return parser; }