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 | def parse_type(
self,
*,
allow_template_vars: bool = True,
allow_union: bool = False,
type_context: TypeUseContext = TypeUseContext.GENERAL,
) -> astx.DataType:
"""
title: Parse a type annotation.
parameters:
allow_template_vars:
type: bool
allow_union:
type: bool
type_context:
type: TypeUseContext
returns:
type: astx.DataType
"""
if self.tokens.cur_tok.kind == TokenKind.none_literal:
self.tokens.get_next_token() # eat none
type_: astx.DataType = astx.NoneType()
else:
if self.tokens.cur_tok.kind != TokenKind.identifier:
raise ParserException("Parser: Expected a type name")
type_name = cast(str, self.tokens.cur_tok.value)
template_bound = None
if allow_template_vars:
template_bound = self._lookup_template_bound(type_name)
if type_name == "list":
self.tokens.get_next_token() # eat list
self._consume_operator("[")
elem_type = self.parse_type(
allow_template_vars=allow_template_vars,
allow_union=allow_union,
type_context=TypeUseContext.NESTED,
)
if self._is_operator(","):
raise ParserException(
"List types accept exactly one element type."
)
self._consume_operator("]")
type_ = astx.ListType([cast(astx.ExprType, elem_type)])
elif type_name == "tensor":
self.tokens.get_next_token() # eat tensor
self._consume_operator("[")
elem_type = self.parse_type(
allow_template_vars=allow_template_vars,
allow_union=allow_union,
type_context=TypeUseContext.NESTED,
)
shape: list[int] = []
runtime_shape = False
if self._is_operator(","):
self._consume_operator(",")
if self._is_operator("."):
self._consume_runtime_shape_marker()
runtime_shape = True
else:
while True:
dimension_token = self.tokens.cur_tok
if dimension_token.kind != TokenKind.int_literal:
raise ParserException(
"Tensor dimensions must be integer "
"literals."
)
shape.append(cast(int, dimension_token.value))
self.tokens.get_next_token()
if not self._is_operator(","):
break
self._consume_operator(",")
if self._is_operator("."):
self._consume_runtime_shape_marker()
raise ParserException(
"Runtime-shaped tensor ellipsis cannot "
"be combined with static dimensions."
)
if runtime_shape and self._is_operator(","):
raise ParserException(
"Runtime-shaped tensor ellipsis cannot be combined "
"with static dimensions."
)
self._consume_operator("]")
if runtime_shape:
self._ensure_runtime_layout_allowed(
"tensor",
type_context,
)
try:
type_ = runtime_tensor_type(elem_type)
except ValueError as err:
raise ParserException(str(err)) from err
else:
if not shape:
raise ParserException(
"Tensor types require at least one static shape "
"dimension, for example tensor[i32, 4]. Use "
"tensor[i32, ...] for runtime-shaped tensor "
"parameters."
)
try:
type_ = tensor_type(elem_type, tuple(shape))
except ValueError as err:
raise ParserException(str(err)) from err
else:
type_map: dict[str, astx.DataType] = {
"i8": astx.Int8(),
"i16": astx.Int16(),
"i32": astx.Int32(),
"i64": astx.Int64(),
"int8": astx.Int8(),
"int16": astx.Int16(),
"int32": astx.Int32(),
"int64": astx.Int64(),
"f16": astx.Float16(),
"f32": astx.Float32(),
"f64": astx.Float64(),
"float16": astx.Float16(),
"float32": astx.Float32(),
"float64": astx.Float64(),
"bool": astx.Boolean(),
"boolean": astx.Boolean(),
"none": astx.NoneType(),
"str": astx.String(),
"string": astx.String(),
"char": astx.Int8(),
"datetime": astx.DateTime(),
"timestamp": astx.Timestamp(),
"date": astx.Date(),
"time": astx.Time(),
}
self.tokens.get_next_token() # eat type identifier
if type_name in type_map:
type_ = type_map[type_name]
elif template_bound is not None:
type_ = astx.TemplateTypeVar(
type_name,
bound=template_bound,
)
elif type_name in self.known_class_names:
type_ = astx.ClassType(type_name)
else:
raise ParserException(
f"Parser: Unknown type '{type_name}'."
)
if not allow_union or not self._is_operator("|"):
return type_
members = [type_]
while self._is_operator("|"):
self._consume_operator("|")
members.append(
self.parse_type(
allow_template_vars=allow_template_vars,
allow_union=False,
type_context=type_context,
)
)
return astx.UnionType(members)
|