Repository navigation
Expand file tree
/
Copy pathMCStructuralTypes.mc4
More file actions
92 lines (83 loc) · 2.83 KB
/
Copy pathMCStructuralTypes.mc4
File metadata and controls
92 lines (83 loc) · 2.83 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
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.types;
/* This is a MontiCore stable grammar.
* Adaptations -- if any -- are conservative. */
import de.monticore.types.*;
/**
* This grammar introduces additional structural types
* as known partially from modern programming languages, like Kotlin.
*
* Union types: A|B
* Intersection types: A&B
* Tuple types: (A, B)
* Bracket types: (A) (i.e. actually a tuple with a single argument
* which is regarded to be isomporhic to its argument)
*
* These structural types simplify the definitipon of new types
* as results of functions, grouping values, providing alternatives
* results, etc.
*
* In a union type A|B value v is either of type A or B
* and only the operations apply, that are shared by both.
* In an intersection type A&B value v is necessarily member of both
* types, which is only possible if common subclasses exist, then
* all operations of A or B can be applied to v.
*
* The above types are actually type constructors with two (or several)
* types as arguments and special typing rules.
*
* This modularity of types and associated expressions greatly eases
* the reuse of type structures in languages.
*
* This grammar is part of a hierarchy of types, which can be found
* under
* https://github.com/MontiCore/monticore/blob/dev/monticore-grammar/
* src/main/grammars/de/monticore/types/Types.md
*
*/
component grammar MCStructuralTypes
extends MCBasicTypes {
/** ASTMCUnionType represents union types.
*
* Values of a union type are of (at least one of) the types in the union.
* Example:
* String|int var1 = "Person";
* String|int var2 = 3;
*/
MCUnionType implements MCType <140> =
left:MCType "|" right:MCType
;
/** ASTMCIntersectionType represents intersection types.
*
* Values of an intersection type are of all the types in the intersection.
* Example:
* (Assuming Car and Boat are not in a subtyping relationship, but share
* a common subclass AmphibianVehicle)
* AmphibianVehicle amphibianVehicle = ...;
* Car c = amphibianVehicle;
* Boat b = amphibianVehicle;
* Car&Boat a = amphibianVehicle;
*/
MCIntersectionType implements MCType <160> =
left:MCType "&" right:MCType
;
/** ASTMCTupleType represents tuple types.
* Tuple types have at least two elements.
*
* Values of a tuple type are a tuple of values
* Example:
* (float, int) vec2 = (1.1f, -2);
*/
MCTupleType implements MCType <30> =
"(" MCType "," (MCType || ",")+ ")"
;
/** ASTMCBracketType represents a type written in parentheses.
*
* The type is the type written inside the parentheses, e.g.,
* (A | B) is the same type as A | B
* (((A))) is the same type as A
*/
MCBracketType implements MCType <300> =
"(" MCType ")"
;
}