[antlr-interest] how to check if a optional token exists or not ??

Jim Idle jimi at temporal-wave.com
Fri Nov 16 14:07:33 PST 2007


No - you should no start to build action the reference the internally 
generated variable names. If the format or names of these change, you 
will be in all sorts of trouble. That's one of the reasons for the 
dollar syntax.

The way to do this (in the absence of being able to use $meta) is to use 
a local Boolean intialized to false, and then set the boolean to true in 
an action after valueMetaType. Then test your Boolean.

Jim

-----Original Message-----
From: Austin Hastings [mailto:Austin_Hastings at Yahoo.com] 
Sent: Friday, November 16, 2007 12:44 AM
To: OJAY78 at gmx.de
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] how to check if a optional token exists or 
not ??

In java, ANTLR creates a named variable for each label you use. Check 
for meta == null, without using a '$' sign.

if (meta != null)

=Austin

OJAY78 at gmx.de wrote:
> Hi,
>
> I stuck on a small Problem. I have this rule with an optional token 
meta. In the Java Code I want to check if this token is null or not so 
that I can call a other method. But this check of the meta token will 
result in a nullpointer exception. Does anyone know how I can solve 
this? How can I check the meta token?
>
> Thanks
>
> valueType returns[StringObject value]
> 	:'value' ',' rv=function_parameter (',' meta=valueMetaType)?
> 	{
> 	
> 	String valName = $rv.value;
> 	FormValue fv = formInstance.getFormValue( valName 
> 		
> 		if( fv == null ) {
> 			log.debug("VALUE TYPE: Feld ist leer");
> 			$value = new StringObject("",null,null);
> 		}
> 		else {
> 			if ($meta.text == null){
> 			$value = new 
StringObject(fv.getFieldValue().toString(),fv,valName);
> 			}
> 			else{
> 			 $value=FELHelper.getFormValueMeta(fv,$meta.text);
> 			}
> 		}
>
> 	}
> 	;
>   





More information about the antlr-interest mailing list