On Jan 31, 2010, at 2:32 PM, Kay Röpke wrote:
> i always try to push as much code into separate classes as possible, if only 
> for the reason that having it in actions makes it really hard to use an IDE.
> plus it makes the grammar itself much more readable, especially if the helper 
> method names are appropriately named.
> 
>> Other option is to put error conditions in grammar with predicates and 
>> repeat syntactic tree pattern.
> 
> "repeat" somehow raises a red flag on this side of the pond ;)

Yeah, decided it's a good idea to separate.  I have

rule:   ^( RULE r=ID .*) {BasicSemanticChecks.checkInvalidRuleDef(gtype, 
fileName, $r.token);}
    ;

in BasicSemanticTriggers.g and then matching class:

public class BasicSemanticChecks {
    protected static void checkInvalidRuleDef(int gtype, String fileName, Token 
ruleID) {
        if ( gtype== ANTLRParser.LEXER_GRAMMAR && 
Character.isLowerCase(ruleID.getText().charAt(0)) ) {
            ErrorManager.grammarError(ErrorType.PARSER_RULES_NOT_ALLOWED,
                                      fileName, ruleID, ruleID.getText());
        }
        if ( 
(gtype==ANTLRParser.PARSER_GRAMMAR||gtype==ANTLRParser.TREE_GRAMMAR) &&
             Character.isUpperCase(ruleID.getText().charAt(0)) )
        {
            ErrorManager.grammarError(ErrorType.LEXER_RULES_NOT_ALLOWED,
                                      fileName, ruleID, ruleID.getText());
        }
    }

    // todo: get filename from stream via token?
    protected static void checkInvalidRuleRef(int gtype, String fileName, Token 
ruleID) {}
}

who knows?  ANTLR v4 might even be readable!!! nah...why should i change my 
ways after 20 years? ;)

Ter

_______________________________________________
antlr-dev mailing list
[email protected]
http://www.antlr.org/mailman/listinfo/antlr-dev

Reply via email to