Search code examples
javajavaccparseexception

How to make custom JavaCC Exception messages?


I want to just start this of by saying I know that a similar question exists here: How to create specific error messages based on parse exceptions in JavaCC

But I don't have the required 50 rep to comment on it to ask my question.

I'm trying to essentially make it so that if the token is not uppercase it will say "Input must be uppercase.". There's a bit more to my code, but that's essentially what I want to do.

I tried to do as the answer suggests and have an or at the end that just contain the code to throw the custom message exception, but it doesn't seem to every reach it, and prints out the normal Exception message.

<UPPER: (["A"-"Z"])+> is my token.

void start(): {} { <UPPER> | {throw new ParseException("custom message");} }

I feel it may be to do with the fact that my token is any number of uppercase characters, but I'm not sure.

Thanks in advance for any help.


Solution

  • I've since looked at my answer, after a few days, and realised the problem was some mis-matches brackets which let to the final | not being contained in the correct place.

    Need to make sure in future that all brackets match to the right place...