[antlr-interest] recover: which token set if any?

Brannon King brannonking at yahoo.com
Wed Jan 4 09:18:12 PST 2006


I'm having a hard time understanding the right way to do this recovery.
I had my project working well with all the error handling turned off: I
would just capture the exception on the top level. However, that would only
return for me the first error. So I took my error handling code and put it
into the reportError function. I added a constructor for the parser as well
so that I could pass in the necessary params. The issue, then, is that I
don't know how to handle this situation properly: 

cellType :
	LP! CELLTYPE^ i:IDENTIFIER
		 { -1 != Arrays.binarySearch(cellTypes,
			i.getText().toUpperCase()) }?
	RP!;
	exception catch[SemanticException ex]{
		reportError(new RecognitionException(
			"expecting one of [" +
			arrayToString(cellTypes,", ") + 
			"], found '" + i.getText() + "'",
			i.getFilename(), i.getLine(), i.getColumn());
		recover(ex, BLANK);
	}

Can someone fill in the BLANK? I just want it to consume the RP and pick up
where it left off. When I just rethrow a RecognitionException instead of
calling reportError directly, which is what I tried first, it seems to
consume all the RPs that exist in a row. Is that possible? I say this
because it says that the next LP should be an RP. 

Errors like the above also need different location information than my other
errors. The following works for most errors:

MarkerUtilities.setCharStart(attributes, 
	offset + ex.getColumn()); 
MarkerUtilities.setCharEnd(attributes, 
	offset + ex.getColumn() + parser.LT(1).getText().length());

But I need to use LT(0) for errors like the semantic one above. 

It would be nice if I could just tie an array of strings to i and have it
automatically check to make sure that i was in the list. Again, thanks for
any help.



More information about the antlr-interest mailing list