options { STATIC = false; } PARSER_BEGIN(Rules) package dk.dtu.compute.se2.rules.parser; import java.util.*; import dk.dtu.compute.se2.rules.*; public class Rules { // The factory for creating objects of the UML model private static final RulesFactory RULESFACTORY = RulesFactory.eINSTANCE; public List parse() { try { return RULES(); } catch (TokenMgrError e) { return null; } catch (ParseException e) { return null; } } } PARSER_END(Rules) SKIP: { " " | "\t" | "\n" | "\r" | <"//" (~["\n","\r"])* ("\n"|"\r")> } TOKEN: { "||" | "&&" | ">=" | "==" | "(" | ")" | "." | ";" | ":" | "," | "when" | "if" | "then" } TOKEN: { | } // Token related rules (conversion to objects): String IDENTIFIER(): { Token identifierToken; }{ identifierToken = { return identifierToken.image; } } int NUMBER(): { Token numberToken; }{ numberToken = { return Integer.parseInt(numberToken.image); } } OP OP0(): { }{ ( "||" { return OP.OR; } ) } OP OP1(): { }{ ( "&&" { return OP.AND; } ) } OP OP2(): { }{ ( "==" { return OP.EQUAL; } ) | ( ">=" { return OP.GTEQUAL; } ) } // Actual grammar rules: List RULES(): { List rules = new ArrayList(); Rule rule; }{ ( rule = RULE() ";" { rules.add(rule); } )+ { return rules; } } Rule RULE(): { String identifier; Event event; Expression condition; List actions; }{ identifier = IDENTIFIER() ":" "when" event = EVENT() "if" condition = EXPRESSION() "then" actions = ACTIONS() { Rule rule = RULESFACTORY.createRule(); rule.setName(identifier); rule.setEvent(event); rule.setCondition(condition); rule.getActions().addAll(actions); return rule; } } Event EVENT(): { String identifier; }{ identifier = IDENTIFIER() { Event event = RULESFACTORY.createEvent(); event.setType(identifier); return event; } } Expression EXPRESSION(): { Expression expression; }{ expression = EXPRESSION0() { return expression; } } Expression EXPRESSION0(): { Expression expression1; Expression expression2; OP operator; }{ expression1 = EXPRESSION1() ( operator = OP0() expression2 = EXPRESSION1() { Operation operation = RULESFACTORY.createOperation(); operation.setOp(operator); operation.getOperands().add(expression1); operation.getOperands().add(expression2); expression1 = operation; } )* { return expression1; } } Expression EXPRESSION1(): { Expression expression1; Expression expression2; OP operator; }{ expression1 = EXPRESSION2() ( operator = OP1() expression2 = EXPRESSION2() { Operation operation = RULESFACTORY.createOperation(); operation.setOp(operator); operation.getOperands().add(expression1); operation.getOperands().add(expression2); expression1 = operation; } )* { return expression1; } } Expression EXPRESSION2(): { Expression expression1; Expression expression2; OP operator; }{ expression1 = ATOMIC() ( operator = OP2() expression2 = ATOMIC() { Operation operation = RULESFACTORY.createOperation(); operation.setOp(operator); operation.getOperands().add(expression1); operation.getOperands().add(expression2); expression1 = operation; } )* { return expression1; } } Expression ATOMIC(): { Expression expression; }{ ( "(" expression = EXPRESSION() ")" { return expression; } ) | ( expression = ATTRIBUTE() {return expression; } ) | ( expression = CONSTANT() {return expression; } ) } Attribute ATTRIBUTE(): { String object; String slot; }{ object = IDENTIFIER() "." slot = IDENTIFIER() { Attribute attribute = RULESFACTORY.createAttribute(); attribute.setObject(object); attribute.setSlot(slot); return attribute; } } Constant CONSTANT(): { int constantValue; }{ constantValue = NUMBER() { Constant constant = RULESFACTORY.createConstant(); constant.setValue(constantValue); return constant; } } List ACTIONS(): { List actions = new ArrayList(); Action action; }{ ( action = ACTION() { actions.add(action); } ) ( "," action = ACTION() { actions.add(action); } )* { return actions; } } Action ACTION(): { String actorName; String actionName; List parameters; }{ actorName = IDENTIFIER() "." actionName = IDENTIFIER() parameters = PARAMETERS() { ActorAction action = RULESFACTORY.createActorAction(); action.setName(actionName); action.setActor(actorName); action.getParameters().addAll(parameters); return action; } } List PARAMETERS(): { List parameters = new ArrayList(); Expression parameter; }{ "(" [ parameter = EXPRESSION() { parameters.add(parameter); } ( "," parameter = EXPRESSION() { parameters.add(parameter); } )* ] ")" { return parameters; } }