Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fb3b66511cbb170cca268df2cd6b3f1ba68c2226 (plain) (blame)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*******************************************************************************
 * Copyright (c) 2010 E.D.Willink and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     E.D.Willink - initial API and implementation
 *******************************************************************************/
grammar org.eclipse.ocl.xtext.essentialocl.EssentialOCL with org.eclipse.ocl.xtext.base.Base
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
import "platform:/resource/org.eclipse.ocl.pivot/model/Pivot.ecore" as pivot
import "platform:/resource/org.eclipse.ocl.xtext.base/model/BaseCS.ecore" as base
import "platform:/resource/org.eclipse.ocl.xtext.essentialocl/model/EssentialOCLCS.ecore"
//generate essentialOCLCST "http://www.eclipse.org/ocl/3.0.0/EssentialOCLCST"
Model returns ContextCS:
	ownedExpression=ExpCS;

/** <<<This is a join point for derived grammars - replace with a more disciplined grammar extensibility>>> */
EssentialOCLReservedKeyword:
	'and'
	| 'else'
	| 'endif'
	| 'if'
	| 'implies'
	| 'in'
	| 'let'
	| 'not'
	| 'or'
	| 'then'
	| 'xor';

/** <<<This is a join point for derived grammars - replace with a more disciplined grammar extensibility>>> */
EssentialOCLUnaryOperatorName:
	'-' | 'not';

/** <<<This is a join point for derived grammars - replace with a more disciplined grammar extensibility>>> */
EssentialOCLInfixOperatorName:
	'*' | '/' | '+' | '-' | '>' | '<' | '>=' | '<=' | '=' | '<>' | 'and' | 'or' | 'xor' | 'implies';

/** <<<This is a join point for derived grammars - replace with a more disciplined grammar extensibility>>> */
EssentialOCLNavigationOperatorName:
	'.' | '->' | '?.' | '?->';

BinaryOperatorName:
	InfixOperatorName | NavigationOperatorName;

InfixOperatorName: 			// Intended to be overrideable
	EssentialOCLInfixOperatorName;

NavigationOperatorName: 	// Intended to be overrideable
	EssentialOCLNavigationOperatorName;

UnaryOperatorName: 			// Intended to be overrideable
	EssentialOCLUnaryOperatorName;

//---------------------------------------------------------------------
//  Names
//---------------------------------------------------------------------
/** <<<This is a join point for derived grammars - replace with a more disciplined grammar extensibility>>> */
EssentialOCLUnrestrictedName returns ecore::EString:
	Identifier;

UnrestrictedName returns ecore::EString: // Intended to be overridden
	EssentialOCLUnrestrictedName;

/** <<<This is a join point for derived grammars - replace with a more disciplined grammar extensibility>>> */
EssentialOCLUnreservedName returns ecore::EString:
	UnrestrictedName
|	CollectionTypeIdentifier
|	PrimitiveTypeIdentifier
|	'Map'
|	'Tuple'
;

UnreservedName returns ecore::EString: // Intended to be overridden
	EssentialOCLUnreservedName;

URIPathNameCS returns base::PathNameCS:
	ownedPathElements+=URIFirstPathElementCS ('::' ownedPathElements+=NextPathElementCS)*;

URIFirstPathElementCS returns base::PathElementCS:
	referredElement=[pivot::NamedElement|UnrestrictedName] | {base::PathElementWithURICS} referredElement=[pivot::Namespace|URI];

//---------------------------------------------------------------------
//  Types
//---------------------------------------------------------------------
PrimitiveTypeIdentifier:
	'Boolean'
	| 'Integer'
	| 'Real'
	| 'String'
	| 'UnlimitedNatural'
	| 'OclAny'
	| 'OclInvalid'
	| 'OclVoid';

PrimitiveTypeCS returns base::PrimitiveTypeRefCS:
	name=PrimitiveTypeIdentifier;

CollectionTypeIdentifier returns ecore::EString:
	'Set'
	| 'Bag'
	| 'Sequence'
	| 'Collection'
	| 'OrderedSet';

CollectionTypeCS returns CollectionTypeCS:
	name=CollectionTypeIdentifier ('(' ownedType=TypeExpCS ')')?;

MapTypeCS returns MapTypeCS:
	name='Map' ('(' ownedKeyType=TypeExpCS ',' ownedValueType=TypeExpCS ')')?;

TupleTypeCS returns base::TupleTypeCS:
	name='Tuple' ('(' (ownedParts+=TuplePartCS (',' ownedParts+=TuplePartCS)*)? ')')?;

TuplePartCS returns base::TuplePartCS:
	name=UnrestrictedName ':' ownedType=TypeExpCS;

//---------------------------------------------------------------------
//  Literals
//---------------------------------------------------------------------
CollectionLiteralExpCS returns CollectionLiteralExpCS:
	ownedType=CollectionTypeCS
	'{' (ownedParts+=CollectionLiteralPartCS
	(',' ownedParts+=CollectionLiteralPartCS)*)?
	'}';

CollectionLiteralPartCS returns CollectionLiteralPartCS:
	(ownedExpression=ExpCS ('..' ownedLastExpression=ExpCS)?) | ownedExpression=PatternExpCS;

CollectionPatternCS returns CollectionPatternCS:
	ownedType=CollectionTypeCS
	'{' (ownedParts+=PatternExpCS
	(',' ownedParts+=PatternExpCS)*
	('++' restVariableName=Identifier))?
	'}';

ShadowPartCS returns ShadowPartCS:		// PatternPartCS
	referredProperty=[pivot::Property|UnrestrictedName] '='ownedInitExpression=(ExpCS|PatternExpCS);

PatternExpCS:
	patternVariableName=UnrestrictedName? ':' ownedPatternType=TypeExpCS;

LambdaLiteralExpCS returns LambdaLiteralExpCS:
	'Lambda' '{' ownedExpressionCS=ExpCS '}';

MapLiteralExpCS returns MapLiteralExpCS:
	ownedType=MapTypeCS '{' (ownedParts+=MapLiteralPartCS (',' ownedParts+=MapLiteralPartCS)*)? '}';

MapLiteralPartCS returns MapLiteralPartCS:
	ownedKey=ExpCS '<-' ownedValue=ExpCS;

PrimitiveLiteralExpCS returns PrimitiveLiteralExpCS:
	NumberLiteralExpCS
	| StringLiteralExpCS
	| BooleanLiteralExpCS
	| UnlimitedNaturalLiteralExpCS
	| InvalidLiteralExpCS
	| NullLiteralExpCS;

TupleLiteralExpCS returns TupleLiteralExpCS:
	'Tuple' '{' ownedParts+=TupleLiteralPartCS (',' ownedParts+=TupleLiteralPartCS)* '}';

TupleLiteralPartCS returns TupleLiteralPartCS:
	name=UnrestrictedName (':' ownedType=TypeExpCS)? '=' ownedInitExpression=ExpCS;

NumberLiteralExpCS returns NumberLiteralExpCS:
	symbol=NUMBER_LITERAL;

StringLiteralExpCS returns StringLiteralExpCS:
	segments+=StringLiteral+;

BooleanLiteralExpCS returns BooleanLiteralExpCS:
	symbol='true'
	| symbol='false';

UnlimitedNaturalLiteralExpCS returns UnlimitedNaturalLiteralExpCS:
	{UnlimitedNaturalLiteralExpCS} '*';

InvalidLiteralExpCS returns InvalidLiteralExpCS:
	{InvalidLiteralExpCS} 'invalid';

NullLiteralExpCS returns NullLiteralExpCS:
	{NullLiteralExpCS} 'null';

TypeLiteralCS returns base::TypedRefCS:
	PrimitiveTypeCS
	| CollectionTypeCS
	| MapTypeCS
	| TupleTypeCS;

TypeLiteralWithMultiplicityCS returns base::TypedRefCS:
	TypeLiteralCS ownedMultiplicity=MultiplicityCS?;

TypeLiteralExpCS returns TypeLiteralExpCS:
	ownedType=TypeLiteralWithMultiplicityCS;

TypeNameExpCS returns TypeNameExpCS:
	ownedPathName=PathNameCS (ownedCurlyBracketedClause=CurlyBracketedClauseCS ('{' ownedPatternGuard=ExpCS '}')?)?;

TypeExpCS returns base::TypedRefCS:
	(TypeNameExpCS | TypeLiteralCS | CollectionPatternCS) ownedMultiplicity=MultiplicityCS?;

//---------------------------------------------------------------------
//  Expressions
//---------------------------------------------------------------------
// An ExpCS permits a LetExpCS only in the final term to ensure
//  that let is right associative, whereas infix operators are left associative.
//   a = 64 / 16 / let b : Integer in 8 / let c : Integer in 4 
// is
//   a = (64 / 16) / (let b : Integer in 8 / (let c : Integer in 4 ))
/* An expression elaborates a prefixed expression with zero or more binary operator and expression suffixes.
 * An optionally prefixed let expression is permitted except when suffixed with further expressions.*/
ExpCS returns ExpCS:
//	({InfixExpCS} ownedSource=PrefixedExpCS name=BinaryOperatorName ownedArgument=ExpCS)
//| 	PrefixedExpCS
// the above takes exponential or worse time for backtracking, below is fast
	(PrefixedPrimaryExpCS ({InfixExpCS.ownedLeft=current} name=BinaryOperatorName ownedRight=ExpCS)?)
| 	PrefixedLetExpCS;

/* A prefixed let expression elaborates a let expression with zero or more unary prefix operators. */
PrefixedLetExpCS returns ExpCS:
	({PrefixExpCS} name=UnaryOperatorName ownedRight=PrefixedLetExpCS)
| 	LetExpCS;

/* A prefixed primary expression elaborates a primary expression with zero or more unary prefix operators. */
PrefixedPrimaryExpCS returns ExpCS:
	({PrefixExpCS} name=UnaryOperatorName ownedRight=PrefixedPrimaryExpCS)
| 	PrimaryExpCS;

/* A primary expression identifies the basic expressions from which more complex expressions may be constructed. */
PrimaryExpCS returns ExpCS:
	NestedExpCS
|	IfExpCS
| 	SelfExpCS
| 	PrimitiveLiteralExpCS
| 	TupleLiteralExpCS
| 	MapLiteralExpCS
| 	CollectionLiteralExpCS
| 	LambdaLiteralExpCS
| 	TypeLiteralExpCS
| 	NameExpCS;

/* A name expression is a generalised rule for expressions that start with a name and which may be followed by square, round or
 * curly bracket clauses and optionally an @pre as well.*/
NameExpCS returns NameExpCS:
	ownedPathName=PathNameCS ownedSquareBracketedClauses+=SquareBracketedClauseCS*
	ownedRoundBracketedClause=RoundBracketedClauseCS? ownedCurlyBracketedClause=CurlyBracketedClauseCS? (isPre?='@' 'pre')?;

/* A curly bracket clause is a generalized rule for the literal arguments of collections, maps, tuples and shadows.*/
CurlyBracketedClauseCS:
	{CurlyBracketedClauseCS} '{'
		( ((ownedParts+=ShadowPartCS (',' ownedParts+=ShadowPartCS)*))?
		| (value=StringLiteral)
		) '}'
	;
	
/* A curly bracket clause is a generalized rule for template specialisations and operations arguments.*/
RoundBracketedClauseCS:
	{RoundBracketedClauseCS} '(' (
		ownedArguments+=NavigatingArgCS (ownedArguments+=NavigatingCommaArgCS)*
		(ownedArguments+=NavigatingSemiArgCS (ownedArguments+=NavigatingCommaArgCS)*)?
		(ownedArguments+=NavigatingBarArgCS (ownedArguments+=NavigatingCommaArgCS)*)*
		)? ')'
	;

/* A square bracket clause is a generalized rule for association class qualifiers and roles.*/
SquareBracketedClauseCS:
	'[' ownedTerms+=ExpCS (',' ownedTerms+=ExpCS)* ']';

/* A navigating argument is a generalized rule for the first argument in a round bracket clause. This is typically the first operation
 * parameter or an iterator. */
NavigatingArgCS returns NavigatingArgCS:
	(ownedNameExpression=NavigatingArgExpCS ((':' ownedType=TypeExpCS ('=' ownedInitExpression=ExpCS)?) | ('in' ownedInitExpression=ExpCS))?)
	| (':' ownedType=TypeExpCS);	// Type-less init is an illegal infix expression

/* A navigating bar argument is a generalized rule for a bar-prefixed argument in a round bracket clause. This is typically the body of an iteration. */
NavigatingBarArgCS returns NavigatingArgCS:
	prefix='|' ownedNameExpression=NavigatingArgExpCS (':' ownedType=TypeExpCS ('=' ownedInitExpression=ExpCS)?)?;	// Type-less init is an illegal infix expression

/* A navigating comma argument is a generalized rule for non-first argument in a round bracket clause. These are typically non-first operation
 * parameters or a second iterator. */
NavigatingCommaArgCS returns NavigatingArgCS:
	prefix=',' ownedNameExpression=NavigatingArgExpCS ((':' ownedType=TypeExpCS ('=' ownedInitExpression=ExpCS)?) | ('in' ownedInitExpression=ExpCS))?;	// Type-less init is an illegal infix expression

/* A navigating semi argument is a generalized rule for a semicolon prefixed argument in a round bracket clause. This is typically an iterate accumulator. */
NavigatingSemiArgCS returns NavigatingArgCS:
	prefix=';' ownedNameExpression=NavigatingArgExpCS (':' ownedType=TypeExpCS ('=' ownedInitExpression=ExpCS)?)?;	// Type-less init is an illegal infix expression

NavigatingArgExpCS returns ExpCS: // Intended to be overridden
	ExpCS
	//	'?'	-- defined by Complete OCL
;

IfExpCS returns IfExpCS:
	'if' ownedCondition=(ExpCS|PatternExpCS)
	'then' ownedThenExpression=ExpCS
//	ifThenExpressions+=IfThenExpCS
	(ownedIfThenExpressions+=ElseIfThenExpCS)*
	'else' ownedElseExpression=ExpCS
	'endif';
//IfThenExpCS returns IfThenExpCS:
//	'if' condition=ExpCS
//	'then' thenExpression=ExpCS
//;
ElseIfThenExpCS returns IfThenExpCS:
	'elseif' ownedCondition=ExpCS
	'then' ownedThenExpression=ExpCS
;

LetExpCS returns LetExpCS:
	'let' ownedVariables+=LetVariableCS (',' ownedVariables+=LetVariableCS)*
	'in' ownedInExpression=ExpCS;

LetVariableCS returns LetVariableCS:
	name=UnrestrictedName ownedRoundBracketedClause=RoundBracketedClauseCS? (':' ownedType=TypeExpCS)? '=' ownedInitExpression=ExpCS;

NestedExpCS returns NestedExpCS:
	'(' ownedExpression=ExpCS ')';

SelfExpCS returns SelfExpCS:
	{SelfExpCS} 'self';

Back to the top