-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSetExpressions.mc4
More file actions
173 lines (147 loc) · 5.42 KB
/
Copy pathSetExpressions.mc4
File metadata and controls
173 lines (147 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// (c) https://github.com/MontiCore/monticore
package de.monticore.ocl;
/* This is a MontiCore stable grammar.
* Adaptations -- if any -- are conservative. */
import de.monticore.expressions.*;
import de.monticore.types.*;
import de.monticore.symbols.*;
/**
* This grammar defines set expressions, such as
* union, intersect, setand, setor and set comprehension.
*
* Set expressions can savely (i.e. as conservative extension)
* be composed if with other forms of expressions
* given in the MontiCore core project.
* Especially common expressions should be added.
*
* This grammar is part of a hierarchy of expressions, which can be found
* under
* https://github.com/MontiCore/monticore/blob/dev/monticore-grammar/
* src/main/grammars/de/monticore/expressions/Expressions.md
*
*/
component grammar SetExpressions
extends ExpressionsBasis,
MCBasicTypes,
BasicSymbols
{
/*=================================================================*/
SetInExpression implements Expression <150> =
elem:Expression
operator:"isin"
set:Expression;
SetNotInExpression implements Expression <150> =
elem:Expression
operator:"notin"
set:Expression;
/*=================================================================*/
UnionExpression implements Expression <180> =
left:Expression
operator:"union"
right:Expression;
IntersectionExpression implements Expression <180> =
left:Expression
operator:"intersect"
right:Expression;
SetMinusExpression implements Expression <180> =
left:Expression
operator:"\\"
right:Expression;
/*=================================================================*/
// sets of sets united (i.e. flattened) and intersected
SetUnionExpression implements Expression <170> =
"union" set:Expression;
SetIntersectionExpression implements Expression <170> =
"intersect" set:Expression;
/*=================================================================*/
// Logical expressions extended to sets of arguments
SetAndExpression implements Expression <130> =
"setand" set:Expression;
SetOrExpression implements Expression <130> =
"setor" set:Expression;
/*=================================================================*/
SetVariableDeclaration implements Variable =
MCType? Name (dim:"[" "]")* ("=" Expression)?;
/*=================================================================*/
/**
* ASTSetComprehension defines a comprehension with given
* characteristic.
* @attribute set
* Optional type Set of comprehension
* @attribute left
* A comprehension-item (e.g. "x*x" or "x in Y") that describes
* or introduces the elements stored in the set.
* @attribute setComprehensionItems
* Characterization of comprehension as a list of
* comprehension-items. This can be generators, vardefinitions
* or filters.
* Example:
* {x * x | x in y, x < 10}
* Note that we assume at least one generator (e.g. x in Y) in this AST.
*/
scope (non_exporting) SetComprehension implements Expression <40> =
( set:key("Set")?
openingBracket:"{" left:SetComprehensionItem "|"
(SetComprehensionItem || ",")+ "}" ) |
( openingBracket:"[" left:SetComprehensionItem "|"
(SetComprehensionItem || ",")+ "]" )
;
/**
* ASTSetComprehensionItem defines the items that can occur
* on the right hand side of a comprehension.
* This can be
* Boolean expressions that act as filter, e.g. x < 6
* introductions on new local variables that act as
* intermediate result, e.g. int y = 2*x
* and generators that introduce a new variable and let them
* range over a set of values, e.g. x in S,
* y in {3..10}, z in Set{3,5,10..20}
*/
SetComprehensionItem =
Expression |
SetVariableDeclaration |
GeneratorDeclaration
;
/**
* ASTGeneratorDeclaration defines a generator that introduces a new
* variable and lets it range over a set
* @attribute MCType
* Optional type of variable
* @attribute Name
* Name of the variable
* @attribute Expression
* Expression that describes or references a set
*/
GeneratorDeclaration implements Variable =
MCType? Name "in" Expression;
/**
* ASTSetEnumeration is used for an enumeration of
* comprehension elements. Note that collection items are optional.
* @attribute set
* Optional type Set of comprehension
* @attribute setCollectionItems
* Enumerated elements as a list separated by , (comma).
* (e.g.: "1..3, x, y..z")
* Examples:
* {1 .. 3, x+1 .. 10, 2*y, 21} defines a set
* [1..3], [a..b] defines a list
*/
SetEnumeration implements Expression <40> =
( set:key("Set")?
openingBracket:"{" (SetCollectionItem || ",")* "}" ) |
( openingBracket:"[" (SetCollectionItem || ",")* "]" )
;
/**
* SetCollectionItem is used to enumerate Lists and Sets.
* It can be a value or a range
*/
interface SetCollectionItem;
// individual value
SetValueItem implements SetCollectionItem =
Expression
;
// range of values
SetValueRange implements SetCollectionItem =
lowerBound:Expression ".." upperBound:Expression
;
}