[antlr-interest] specific number of repetitions

Rob Hill rob.hill at blueyonder.co.uk
Sun Mar 11 13:20:36 PDT 2007


I haven't seen any way to specify a rep count, but you could count the
matches on a closure and stop/change when you've got enough.

rule 
{
	Int count = 0;
}
: (DIGIT { count++; 
 		if (count > n) {
	       Console.Writeline("Got " + n + " digits");
		 //code to exit rule?/ throw exception.
	      }
      }
   )*

I don't know how to make the rule terminate when you've found enough though.
I've something similar to this, but for me, it's an error to have more than
n chars, so i just throw an exception and that exits the rule.

Remember that you can insert arbitrary code right inside almost any part of
the rule, handy for situations like this.


Cheers,
/2ob



> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Brian Lewis
> Sent: 11 March 2007 06:47
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] specific number of repetitions
> 
> I want to parse something that looks like
> 4:abcd5:12345
> 
> so I want to code something like
> field:
>   n=integer ':' ('a'..'z' | '0'..'9'){n}
> 
> to read exactly n characters after the :. I don't know how to do it,
> though. Is there a way?




More information about the antlr-interest mailing list