@mirascript/typed 0.1.64 → 0.1.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/type.peggy CHANGED
@@ -37,6 +37,11 @@ function union(types) {
37
37
  return { kind: "union", types };
38
38
  }
39
39
 
40
+ function intersection(types) {
41
+ if (types.length === 1) return types[0];
42
+ return { kind: "intersection", types };
43
+ }
44
+
40
45
  function record(fields) {
41
46
  for(let i = 0; i < fields.length; i++) {
42
47
  fields[i].name ??= String(i);
@@ -55,6 +60,10 @@ function string(parts) {
55
60
  }
56
61
  return { kind: 'template', parts };
57
62
  }
63
+
64
+ function tupleElement(type, spread) {
65
+ return { type, spread: spread ?? false };
66
+ }
58
67
  }
59
68
 
60
69
  Start
@@ -62,17 +71,22 @@ Start
62
71
  / _ @Type _
63
72
 
64
73
  ///////////////////////////////////////////////////////////////////////////////
65
- // Type (union)
74
+ // Type (union / intersection)
66
75
  ///////////////////////////////////////////////////////////////////////////////
67
76
 
68
77
  Type
69
78
  = UnionType
70
79
 
71
80
  UnionType
72
- = _ ( "|" _ )? u:PostfixType|1.., _ "|" _ | {
81
+ = _ ( "|" _ )? u:IntersectionType|1.., _ "|" _ | {
73
82
  return union(u);
74
83
  }
75
84
 
85
+ IntersectionType
86
+ = _ ( "&" _ )? i:PostfixType|1.., _ "&" _ | {
87
+ return intersection(i);
88
+ }
89
+
76
90
  ///////////////////////////////////////////////////////////////////////////////
77
91
  // Array
78
92
  ///////////////////////////////////////////////////////////////////////////////
@@ -91,6 +105,8 @@ PostfixType
91
105
 
92
106
  PrimaryType
93
107
  = FunctionType
108
+ / ReflectionType
109
+ / TupleType
94
110
  / name:Identifier generic:(_ "<" _ @TypeArgList _ ">")? {
95
111
  if (generic) {
96
112
  if (name === 'array') {
@@ -110,6 +126,10 @@ PrimaryType
110
126
  if (BUILT_IN_TYPES.has(name)) {
111
127
  return name;
112
128
  }
129
+ // `type` is a non-reserved keyword — allow it as a plain named type
130
+ if (name === 'type') {
131
+ return name;
132
+ }
113
133
  return named(name);
114
134
  }
115
135
  / lit:String {
@@ -120,6 +140,33 @@ PrimaryType
120
140
  / RecordType
121
141
 
122
142
 
143
+ ///////////////////////////////////////////////////////////////////////////////
144
+ // Reflection
145
+ ///////////////////////////////////////////////////////////////////////////////
146
+
147
+ ReflectionType
148
+ = "type" _ "(" _ name:Identifier _ ")" {
149
+ return { kind: "reflection", name };
150
+ }
151
+
152
+ ///////////////////////////////////////////////////////////////////////////////
153
+ // Tuple
154
+ ///////////////////////////////////////////////////////////////////////////////
155
+
156
+ TupleType
157
+ = "[" _ elements:TupleElementList? _ "]" {
158
+ return { kind: "tuple", elements: elements ?? [] };
159
+ }
160
+
161
+ TupleElementList
162
+ = @TupleElement|1.., _ "," _ | ( _ "," _ )?
163
+
164
+ TupleElement
165
+ = spread:".."? _ type:Type {
166
+ return tupleElement(type, spread != null);
167
+ }
168
+
169
+
123
170
  ///////////////////////////////////////////////////////////////////////////////
124
171
  // Generics
125
172
  ///////////////////////////////////////////////////////////////////////////////