[antlr-interest] Re: Taking control of the input stream

weitzman_d weitzman_d at yahoo.com
Wed Sep 18 13:51:32 PDT 2002


I've found a solution.  Just thought I'd share.  It's a token 
definition.  This is what it looks like:

LITERAL	:	"{" num:NUMBER "}" CRLF
	{
	  int length = Integer.parseInt(num.getText());
	  InputBuffer buf = getInputBuffer();
	  buf.mark();
	  buf.fill(length);
	  for (int i = 0; i < length; i++) {
	    buf.consume();
	  }
	  String value = buf.getLAChars().substring(0, length);
	  buf.commit();
	  System.out.println("Matched literal [" + value + "]");
	  setText(value);
	}
	;

David Weitzman

--- In antlr-interest at y..., "weitzman_d" <weitzman_d at y...> wrote:
> Howdy.  I just started using ANTLR and it looks like a great tool.  
> I'm trying to write a grammer that parses IMAP commands (the spec 
> doesn't seem consistant enough to allow generic parsing).  Here's 
the 
> tricky part:
> 
> IMAP allows strings to be expressed as something it calls 
a 'literal' 
> (not to be confused with what everyone else calls a literal).  
> Servers must accept quoted strings and 
> atoms (like the quoted-string message content here)
> 
> C: A003 APPEND saved-messages (\Seen) "Date: Mon, ..."
> 
> But servers must also be able to accept in the 'literal' form like 
> this:
> 
> C: A003 APPEND saved-messages (\Seen) {310}
> S: + continue
> C: Date: Mon, 7 Feb 1994 21:52:25 -0800 (PST)
> C: ...
> C: (310 bytes later...)
> S: A003 OK APPEND completed
> 
> I want my ANTLR grammer to see the {NUM} and eat up the next NUM 
> bytes into a single token.  A general outline of the rule would 
look 
> something like this:
> 
> literal	:	"{" len:NUMBER "}" CRLF
> 	{
> 	int length = Integer.parseInt(len.getText());
>         out.println("+ continue");
> 	String value = [read the next 'length' bytes]
> 	}
> 	;
> 
> How can I do this (or is it not possible)?
> 
> Thanks,
> 
> David Weitzman


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list