[antlr-interest] TokenStreamSelector/includeFile example for 3.0?

Peter White pwhite at peter-white.info
Thu Feb 8 17:10:31 PST 2007


Being somewhat of an ANTLR noob, I didn't realize my problem could be
solved with only a lexer and no parser.  The following include method
did the trick:

@lexer::members {
public void include(String filename) {
		if (filename.endsWith("top.jspf\"")) return;
		indent();
		// Might need to escape filename...
		String relativePath = filename.substring(1, filename.length()-1);
		String absolutePath = BASE_DIR + "/" + relativePath;
		System.out.println("<!-- Including: " + absolutePath + " -->");
		
		try {
			UITagLexer lex = new UITagLexer(new ANTLRFileStream(absolutePath));
			while (lex.nextToken().getType() != EOF) {
	            		lex.nextToken();
	        	}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

On Mon, 2007-02-05 at 16:38 -0800, Peter White wrote:

> Hello,
> 
> I've written a simple filtering grammar to extract instances of our
> custom JSP tags from our JSP pages and I've been unable to parse JSP
> includes "in-line" successfully. Initially, I tried to include files
> by using the following "include" method in my lexer class but that
> only resulted in the exception following my code snippet.
> 
> @lexer::members {
> public void include(String filename) {
> if (filename.endsWith("top.jspf\"")) return;
> indent();
> // Might need to escape filename...
> String relativePath = filename.substring(1, filename.length()-1);
> String absolutePath = BASE_DIR + "/" + relativePath;
> System.out.println("<!-- Including: " + absolutePath + " -->");
> 
> 
> try {
> UITagLexer lex = new UITagLexer(new ANTLRFileStream(absolutePath));
> CommonTokenStream tokens = new CommonTokenStream(lex);
> 
> UITagParser g = new UITagParser(tokens);
> g.document();
> } catch (IOException e) {
> e.printStackTrace();
> } catch (RecognitionException e) {
> e.printStackTrace();
> }
> }
> }
> 
> org.antlr.runtime.debug.DebugParser.reportError(DebugParser.java:87):
> java.net.BindException: Address already in use: JVM_Bind
> 
> After searching the mail archives, I realized the above approach
> wouldn't work and I came across several references to the includeFile
> example so I downloaded the source to 2.7.7 to examine it. I've
> noticed that TokenStreamSelector.java isn't included in the 3.0 beta
> source distribution and was wondering if there's some other mechanism
> for achieving the same results in 3.0. My apologies if this is
> currently documented somewhere - I searched the ANTLR website and mail
> archives and didn't see anything.
> 
> Any pointers in the right direction would be greatly appreciated!
> 
> Thanks,
> Peter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070208/1c8498e1/attachment-0001.html 


More information about the antlr-interest mailing list