How should I add arbitrary type names to this grammar?
I have the problem that adding it the way I'd expect to do it breaks something in the grammar and I'm not sure what breaks.
What I currently have is the rule:
TYPE : 'Int' | 'String' | 'Bool' | 'Object';
and when I change this to
TYPE: 'Int' | 'String' | 'Bool' | 'Object' | ID;
which is followed by:
ID
: LetterOrDigit
;
fragment
LetterOrDigit
: [a-zA-Z]+ [0-9$_]* // these are the "java letters or digits" below 0x7F
| // covers all characters above 0x7F which are not a surrogate
~[\u0000-\u007F\uD800-\uDBFF]
| // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
[\uD800-\uDBFF] [\uDC00-\uDFFF]
;
Then the parser seems to stop recognizing normal IDs and will not reach the TYPE rule either.
Before the TYPE-rule I merely have the Cool language grammar, like:
grammar CoolGrammar;
s : program;
program : (classdef';')+ EOF;
classdef : 'class ' TYPE ( ' inherits ' TYPE )? '{' ( feature ';' )* '}';
feature : ID '(' (formal (',' formal)* ) ')' ':' TYPE '{' expr '}' | ID ':' TYPE ('<-' expr)?;
expr : ID '<-' expr
| expr ('@' TYPE)? '.' ID '(' (expr (',' expr)*)? ')'
| ID '(' expr (',' expr*)? ')'
| 'if ' expr ' then ' expr ' else ' expr ' fi'
| 'while ' expr ' loop ' expr ' pool'
| '{' (expr ';')+ '}'
| 'let ' formal ('<-' expr)? (',' formal ('<-' expr)? )* ' in ' expr
| 'case ' expr ' of ' (formal ' => ' expr ';')+ ' esac'
| 'new ' TYPE
| 'isvoid ' expr
| expr '+' expr
| expr '-' expr
| expr '*' expr
| expr '/' expr
| '~' expr
| expr '<' expr
| expr '<=' expr
| expr '=' expr
| 'not ' expr
| '(' expr ')'
| ID
| Integer
| String
| 'true'
| 'false';
formal : ID ':' TYPE;
/*
These are special strings that must be matched before
the more general ones below.
*/
TRUETK: 'true';
FALSETK: 'false';
TYPE : ...
Any idea what's causing the problem?
Using the following example program:
class Int {
c ( d : Int) : String { not 2 };
};
the error is:
line 2:4 mismatched input 'c' expecting {<INVALID>, '}'}
line 3:0 extraneous input '}' expecting {<EOF>, 'class '}