Permalink
Browse files

support multiple conditions in if, while, assert - for #170

  • Loading branch information...
1 parent 14e5f53 commit 86fbefdb8aa03a9cf1810251c574980344970d1d @gavinking gavinking committed Sep 8, 2012
View
@@ -599,8 +599,8 @@ assertion returns [Assertion assertion]
ASSERT
{ $assertion = new Assertion($ASSERT);
$assertion.setAnnotationList($annotations.annotationList); }
- condition
- { $assertion.setCondition($condition.condition); }
+ conditions
+ { $assertion.setConditionList($conditions.conditionList); }
{ expecting=SEMICOLON; }
SEMICOLON
{ $assertion.setEndToken($SEMICOLON);
@@ -795,7 +795,7 @@ parameters returns [ParameterList parameterList]
$parameterList.setEndToken(null); }
)
)*
- )?
+ )?
RPAREN
{ $parameterList.setEndToken($RPAREN); }
//-> ^(PARAMETER_LIST[$LPAREN] annotatedParameter*)
@@ -1659,8 +1659,8 @@ forComprehensionClause returns [ForComprehensionClause comprehensionClause]
ifComprehensionClause returns [IfComprehensionClause comprehensionClause]
: IF_CLAUSE
{ $comprehensionClause = new IfComprehensionClause($IF_CLAUSE); }
- condition
- { $comprehensionClause.setCondition($condition.condition); }
+ conditions
+ { $comprehensionClause.setConditionList($conditions.conditionList); }
comprehensionClause
{ $comprehensionClause.setComprehensionClause($comprehensionClause.comprehensionClause); }
;
@@ -2309,122 +2309,123 @@ compilerAnnotation returns [CompilerAnnotation annotation]
)?
;
+conditions returns [ConditionList conditionList]
+ : LPAREN
+ { $conditionList = new ConditionList($LPAREN); }
+ (
+ c1=condition
+ { $conditionList.addCondition($c1.condition); }
+ ( c=COMMA
+ { $conditionList.setEndToken($c); }
+ (
+ c2=condition
+ { $conditionList.addCondition($c2.condition);
+ $conditionList.setEndToken(null); }
+ | { displayRecognitionError(getTokenNames(),
+ new MismatchedTokenException(LIDENTIFIER, input)); } //TODO: sometimes it should be RPAREN!
+ )
+ )*
+ )?
+ RPAREN
+ { $conditionList.setEndToken($RPAREN); }
+ ;
+
condition returns [Condition condition]
- : (LPAREN EXISTS)=>existsCondition
+ : (EXISTS)=>existsCondition
{ $condition=$existsCondition.condition; }
- | (LPAREN NONEMPTY)=>nonemptyCondition
+ | (NONEMPTY)=>nonemptyCondition
{ $condition=$nonemptyCondition.condition; }
- | (LPAREN IS_OP)=>isCondition
+ | (IS_OP)=>isCondition
{ $condition=$isCondition.condition; }
- | (LPAREN SATISFIES)=>satisfiesCondition
+ | (SATISFIES)=>satisfiesCondition
{ $condition=$satisfiesCondition.condition; }
| booleanCondition
{ $condition=$booleanCondition.condition; }
;
booleanCondition returns [BooleanCondition condition]
- : LPAREN
- { $condition = new BooleanCondition($LPAREN); }
- (expression
- { $condition.setExpression($expression.expression); })?
- RPAREN
- { $condition.setEndToken($RPAREN); }
+ : { $condition = new BooleanCondition(null); }
+ expression
+ { $condition.setExpression($expression.expression); }
//-> ^(BOOLEAN_CONDITION expression)
;
existsCondition returns [Condition condition]
@init { ExistsCondition ec = null; }
- : (LPAREN EXISTS LIDENTIFIER RPAREN)
- => l1=LPAREN
- { ec = new ExistsCondition($l1);
+ : (EXISTS LIDENTIFIER (RPAREN|COMMA))
+ => e1=EXISTS
+ { ec = new ExistsCondition($e1);
$condition = ec; }
- e1=EXISTS impliedVariable
+ impliedVariable
{ ec.setVariable($impliedVariable.variable); }
- r1=RPAREN
- { ec.setEndToken($r1); }
//-> ^(EXISTS_CONDITION[$EXISTS] impliedVariable)
- | (LPAREN EXISTS compilerAnnotations (declarationStart|specificationStart))
- => l2=LPAREN
- { ec = new ExistsCondition($l2);
+ | (EXISTS compilerAnnotations (declarationStart|specificationStart))
+ => e2=EXISTS
+ { ec = new ExistsCondition($e2);
$condition = ec; }
- e2=EXISTS
specifiedVariable
{ ec.setVariable($specifiedVariable.variable); }
- r2=RPAREN
- { ec.setEndToken($r2); }
//-> ^(EXISTS_CONDITION[$EXISTS] specifiedVariable2)
- | bc=booleanCondition
- { $condition=$bc.condition; }
+ | booleanCondition
+ { $condition=$booleanCondition.condition; }
;
nonemptyCondition returns [Condition condition]
@init { NonemptyCondition nc = null; }
- : (LPAREN NONEMPTY LIDENTIFIER RPAREN)
- => l1=LPAREN
- { nc = new NonemptyCondition($l1);
+ : (NONEMPTY LIDENTIFIER (RPAREN|COMMA))
+ => n1=NONEMPTY
+ { nc = new NonemptyCondition($n1);
$condition = nc; }
- n1=NONEMPTY impliedVariable
+ impliedVariable
{ nc.setVariable($impliedVariable.variable); }
- r1=RPAREN
- { nc.setEndToken($r1); }
//-> ^(NONEMPTY_CONDITION[$NONEMPTY] impliedVariable)
- | (LPAREN NONEMPTY compilerAnnotations (declarationStart|specificationStart))
- => l2=LPAREN
- { nc = new NonemptyCondition($l2);
+ | (NONEMPTY compilerAnnotations (declarationStart|specificationStart))
+ => n2=NONEMPTY
+ { nc = new NonemptyCondition($n2);
$condition = nc; }
- n2=NONEMPTY
- (specifiedVariable
- { nc.setVariable($specifiedVariable.variable); })?
- r2=RPAREN
- { nc.setEndToken($r2); }
+ specifiedVariable
+ { nc.setVariable($specifiedVariable.variable); }
//-> ^(NONEMPTY_CONDITION[$NONEMPTY] specifiedVariable2)
- | bc=booleanCondition
- { $condition=$bc.condition; }
+ | booleanCondition
+ { $condition=$booleanCondition.condition; }
;
isCondition returns [Condition condition]
@init { IsCondition ic = null; }
- : (LPAREN IS_OP type LIDENTIFIER RPAREN)
- => l1=LPAREN
- { ic = new IsCondition($l1);
+ : (IS_OP type LIDENTIFIER (RPAREN|COMMA))
+ => i1=IS_OP
+ { ic = new IsCondition($i1);
$condition = ic; }
- i1=IS_OP t1=type impliedVariable
- { ic.setType($t1.type);
- ic.setVariable($impliedVariable.variable); }
- r1=RPAREN
- { ic.setEndToken($r1); }
+ t1=type
+ { ic.setType($t1.type); }
+ impliedVariable
+ { ic.setVariable($impliedVariable.variable); }
//-> ^(IS_CONDITION[$IS_OP] unionType ^(VARIABLE SYNTHETIC_VARIABLE memberName ^(SPECIFIER_EXPRESSION ^(EXPRESSION ^(BASE_MEMBER_EXPRESSION memberName)))))
- | (LPAREN IS_OP type LIDENTIFIER SPECIFY)
- => l2=LPAREN
- { ic = new IsCondition($l2);
+ | (IS_OP type LIDENTIFIER SPECIFY)
+ => i2=IS_OP
+ { ic = new IsCondition($i2);
$condition = ic; }
- i2=IS_OP
- ( t2=type
+ t2=type
{ ic.setType($t2.type);
Variable v = new Variable(null);
v.setType($t2.type);
ic.setVariable(v); }
memberName
{ ic.getVariable().setIdentifier($memberName.identifier); }
specifier
- { ic.getVariable().setSpecifierExpression($specifier.specifierExpression); })?
- r2=RPAREN
- { ic.setEndToken($r2); }
+ { ic.getVariable().setSpecifierExpression($specifier.specifierExpression); }
//-> ^(IS_CONDITION[$IS_OP] unionType ^(VARIABLE unionType memberName specifier))
- | bc=booleanCondition
- { $condition=$bc.condition; }
+ | booleanCondition
+ { $condition=$booleanCondition.condition; }
;
satisfiesCondition returns [SatisfiesCondition condition]
- : LPAREN
- { $condition = new SatisfiesCondition($LPAREN); }
- SATISFIES
+ : SATISFIES
+ { $condition = new SatisfiesCondition($SATISFIES); }
(t1=qualifiedType
{ $condition.setLeftType($t1.type); }
t2=qualifiedType
{ $condition.setRightType($t2.type); })?
- RPAREN
- { $condition.setEndToken($RPAREN); }
//-> ^(SATISFIES_CONDITION[$SATISFIES] type+)
;
@@ -2463,8 +2464,8 @@ ifElse returns [IfStatement statement]
ifBlock returns [IfClause clause]
: IF_CLAUSE
{ $clause = new IfClause($IF_CLAUSE); }
- condition
- { $clause.setCondition($condition.condition); }
+ conditions
+ { $clause.setConditionList($conditions.conditionList); }
controlBlock
{ $clause.setBlock($controlBlock.block); }
;
@@ -2524,8 +2525,10 @@ switchHeader returns [SwitchClause clause]
: SWITCH_CLAUSE
{ $clause = new SwitchClause($SWITCH_CLAUSE); }
LPAREN
+ (
expression
{ $clause.setExpression($expression.expression); }
+ )?
RPAREN
;
@@ -2546,8 +2549,10 @@ caseBlock returns [CaseClause clause]
: CASE_CLAUSE
{ $clause = new CaseClause($CASE_CLAUSE); }
LPAREN
- caseItem
+ (
+ caseItem
{ $clause.setCaseItem($caseItem.item); }
+ )?
RPAREN
block
{ $clause.setBlock($block.block); }
@@ -2669,8 +2674,8 @@ whileLoop returns [WhileStatement statement]
whileBlock returns [WhileClause clause]
: WHILE_CLAUSE
{ $clause = new WhileClause($WHILE_CLAUSE); }
- condition
- { $clause.setCondition($condition.condition); }
+ conditions
+ { $clause.setConditionList($conditions.conditionList); }
controlBlock
{ $clause.setBlock($controlBlock.block); }
;
@@ -2716,8 +2721,10 @@ catchBlock returns [CatchClause clause]
catchVariable returns [CatchVariable catchVariable]
: LPAREN
{ $catchVariable=new CatchVariable($LPAREN); }
- (variable
- { $catchVariable.setVariable($variable.variable); })?
+ (
+ variable
+ { $catchVariable.setVariable($variable.variable); }
+ )?
RPAREN
{ $catchVariable.setEndToken($RPAREN); }
;
@@ -2741,7 +2748,7 @@ resource returns [Resource resource]
| expression
{ $resource.setExpression($expression.expression); }
//-> ^(RESOURCE expression)
- )
+ )?
RPAREN
{ $resource.setEndToken($RPAREN); }
;
View
@@ -282,7 +282,7 @@
"An assertion."
^(ASSERTION:EXECUTABLE_STATEMENT
ANNOTATION_LIST?
- CONDITION)
+ CONDITION_LIST)
"A specification statement"
^(SPECIFIER_STATEMENT:EXECUTABLE_STATEMENT
@@ -302,7 +302,7 @@
IF_CLAUSE
ELSE_CLAUSE?)
^(IF_CLAUSE:CONTROL_CLAUSE
- CONDITION
+ CONDITION_LIST
BLOCK)
^(ELSE_CLAUSE:CONTROL_CLAUSE
BLOCK)
@@ -363,9 +363,12 @@
^(WHILE_STATEMENT:CONTROL_STATEMENT
WHILE_CLAUSE)
^(WHILE_CLAUSE:CONTROL_CLAUSE
- CONDITION
+ CONDITION_LIST
BLOCK)
+^(CONDITION_LIST
+ CONDITION*)
+
^(abstract CONDITION)
^(BOOLEAN_CONDITION:CONDITION
EXPRESSION)
@@ -673,5 +676,5 @@ argument list."
COMPREHENSION_CLAUSE)
^(IF_COMPREHENSION_CLAUSE:COMPREHENSION_CLAUSE
- CONDITION?
+ CONDITION_LIST
COMPREHENSION_CLAUSE)
@@ -230,6 +230,13 @@ private boolean isVisible(Declaration member, TypeDeclaration type) {
}
}
}
+
+ @Override public void visit(Tree.ConditionList that) {
+ if (that.getConditions().isEmpty()) {
+ that.addError("empty condition list");
+ }
+ super.visit(that);
+ }
private void initOriginalDeclaration(Tree.Variable that) {
if (that.getType() instanceof Tree.SyntheticVariable) {
@@ -623,8 +623,8 @@ public void visit(Tree.Continue that) {
public void visit(Tree.IfStatement that) {
if (that.getIfClause()!=null) {
- if (that.getIfClause().getCondition()!=null) {
- that.getIfClause().getCondition().visit(this);
+ if (that.getIfClause().getConditionList()!=null) {
+ that.getIfClause().getConditionList().visit(this);
}
}
@@ -745,8 +745,8 @@ public void visit(Tree.SwitchStatement that) {
@Override
public void visit(Tree.WhileStatement that) {
if (that.getWhileClause()!=null) {
- if (that.getWhileClause().getCondition()!=null) {
- that.getWhileClause().getCondition().visit(this);
+ if (that.getWhileClause().getConditionList()!=null) {
+ that.getWhileClause().getConditionList().visit(this);
}
}
@@ -6,4 +6,8 @@ void assertions() {
doc "name must be a string"
assert (is String n = name);
print(n.uppercased);
+ assert (is String name);
+ assert (exists arg = process.arguments[0],
+ arg=="gavin");
+ @error assert ();
}
@@ -258,4 +258,12 @@ class ControlStructures() {
@error switch (s)
case (is String) {}
+ if (exists arg=process.arguments[0],
+ arg=="verbose") {
+ print(arg);
+ }
+
+ @error if () {}
+ @error while () {}
+
}

0 comments on commit 86fbefd

Please sign in to comment.