| 1 | use crate::component::*; |
| 2 | use crate::core; |
| 3 | use crate::kw; |
| 4 | use crate::parser::Lookahead1; |
| 5 | use crate::parser::Peek; |
| 6 | use crate::parser::{Parse, Parser, Result}; |
| 7 | use crate::token::Index; |
| 8 | use crate::token::LParen; |
| 9 | use crate::token::{Id, NameAnnotation, Span}; |
| 10 | |
| 11 | /// A core type declaration. |
| 12 | #[derive (Debug)] |
| 13 | pub struct CoreType<'a> { |
| 14 | /// Where this type was defined. |
| 15 | pub span: Span, |
| 16 | /// An optional identifier to refer to this `core type` by as part of name |
| 17 | /// resolution. |
| 18 | pub id: Option<Id<'a>>, |
| 19 | /// An optional name for this type stored in the custom `name` section. |
| 20 | pub name: Option<NameAnnotation<'a>>, |
| 21 | /// The core type's definition. |
| 22 | pub def: CoreTypeDef<'a>, |
| 23 | } |
| 24 | |
| 25 | impl<'a> Parse<'a> for CoreType<'a> { |
| 26 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 27 | let span: Span = parser.parse::<kw::core>()?.0; |
| 28 | parser.parse::<kw::r#type>()?; |
| 29 | let id: Option> = parser.parse()?; |
| 30 | let name: Option> = parser.parse()?; |
| 31 | let def: CoreTypeDef<'a> = parser.parens(|p: Parser<'a>| p.parse())?; |
| 32 | |
| 33 | Ok(Self { |
| 34 | span, |
| 35 | id, |
| 36 | name, |
| 37 | def, |
| 38 | }) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /// Represents a core type definition. |
| 43 | /// |
| 44 | /// In the future this may be removed when module types are a part of |
| 45 | /// a core module. |
| 46 | #[derive (Debug)] |
| 47 | pub enum CoreTypeDef<'a> { |
| 48 | /// The type definition is one of the core types. |
| 49 | Def(core::TypeDef<'a>), |
| 50 | /// The type definition is a module type. |
| 51 | Module(ModuleType<'a>), |
| 52 | } |
| 53 | |
| 54 | impl<'a> Parse<'a> for CoreTypeDef<'a> { |
| 55 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 56 | if parser.peek::<kw::module>()? { |
| 57 | parser.parse::<kw::module>()?; |
| 58 | Ok(Self::Module(parser.parse()?)) |
| 59 | } else { |
| 60 | Ok(Self::Def(parser.parse()?)) |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /// A type definition for a core module. |
| 66 | #[derive (Debug)] |
| 67 | pub struct ModuleType<'a> { |
| 68 | /// The declarations of the module type. |
| 69 | pub decls: Vec<ModuleTypeDecl<'a>>, |
| 70 | } |
| 71 | |
| 72 | impl<'a> Parse<'a> for ModuleType<'a> { |
| 73 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 74 | parser.depth_check()?; |
| 75 | Ok(Self { |
| 76 | decls: parser.parse()?, |
| 77 | }) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// The declarations of a [`ModuleType`]. |
| 82 | #[derive (Debug)] |
| 83 | pub enum ModuleTypeDecl<'a> { |
| 84 | /// A core type. |
| 85 | Type(core::Type<'a>), |
| 86 | /// A core recursion group. |
| 87 | Rec(core::Rec<'a>), |
| 88 | /// An alias local to the component type. |
| 89 | Alias(Alias<'a>), |
| 90 | /// An import. |
| 91 | Import(core::Import<'a>), |
| 92 | /// An export. |
| 93 | Export(&'a str, core::ItemSig<'a>), |
| 94 | } |
| 95 | |
| 96 | impl<'a> Parse<'a> for ModuleTypeDecl<'a> { |
| 97 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 98 | let mut l: Lookahead1<'_> = parser.lookahead1(); |
| 99 | if l.peek::<kw::r#type>()? { |
| 100 | Ok(Self::Type(parser.parse()?)) |
| 101 | } else if l.peek::<kw::rec>()? { |
| 102 | Ok(Self::Rec(parser.parse()?)) |
| 103 | } else if l.peek::<kw::alias>()? { |
| 104 | Ok(Self::Alias(Alias::parse_outer_core_type_alias(parser)?)) |
| 105 | } else if l.peek::<kw::import>()? { |
| 106 | Ok(Self::Import(parser.parse()?)) |
| 107 | } else if l.peek::<kw::export>()? { |
| 108 | parser.parse::<kw::export>()?; |
| 109 | let name: &str = parser.parse()?; |
| 110 | let et: ItemSig<'_> = parser.parens(|parser: Parser<'a>| parser.parse())?; |
| 111 | Ok(Self::Export(name, et)) |
| 112 | } else { |
| 113 | Err(l.error()) |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | impl<'a> Parse<'a> for Vec<ModuleTypeDecl<'a>> { |
| 119 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 120 | let mut decls: Vec> = Vec::new(); |
| 121 | while !parser.is_empty() { |
| 122 | decls.push(parser.parens(|parser: Parser<'a>| parser.parse())?); |
| 123 | } |
| 124 | Ok(decls) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /// A type declaration in a component. |
| 129 | #[derive (Debug)] |
| 130 | pub struct Type<'a> { |
| 131 | /// Where this type was defined. |
| 132 | pub span: Span, |
| 133 | /// An optional identifier to refer to this `type` by as part of name |
| 134 | /// resolution. |
| 135 | pub id: Option<Id<'a>>, |
| 136 | /// An optional name for this type stored in the custom `name` section. |
| 137 | pub name: Option<NameAnnotation<'a>>, |
| 138 | /// If present, inline export annotations which indicate names this |
| 139 | /// definition should be exported under. |
| 140 | pub exports: InlineExport<'a>, |
| 141 | /// The type definition. |
| 142 | pub def: TypeDef<'a>, |
| 143 | } |
| 144 | |
| 145 | impl<'a> Type<'a> { |
| 146 | /// Parses a `Type` while allowing inline `(export "...")` names to be |
| 147 | /// defined. |
| 148 | pub fn parse_maybe_with_inline_exports(parser: Parser<'a>) -> Result<Self> { |
| 149 | Type::parse(parser, true) |
| 150 | } |
| 151 | |
| 152 | fn parse_no_inline_exports(parser: Parser<'a>) -> Result<Self> { |
| 153 | Type::parse(parser, false) |
| 154 | } |
| 155 | |
| 156 | fn parse(parser: Parser<'a>, allow_inline_exports: bool) -> Result<Self> { |
| 157 | let span = parser.parse::<kw::r#type>()?.0; |
| 158 | let id = parser.parse()?; |
| 159 | let name = parser.parse()?; |
| 160 | let exports = if allow_inline_exports { |
| 161 | parser.parse()? |
| 162 | } else { |
| 163 | Default::default() |
| 164 | }; |
| 165 | let def = parser.parse()?; |
| 166 | |
| 167 | Ok(Self { |
| 168 | span, |
| 169 | id, |
| 170 | name, |
| 171 | exports, |
| 172 | def, |
| 173 | }) |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /// A definition of a component type. |
| 178 | #[derive (Debug)] |
| 179 | pub enum TypeDef<'a> { |
| 180 | /// A defined value type. |
| 181 | Defined(ComponentDefinedType<'a>), |
| 182 | /// A component function type. |
| 183 | Func(ComponentFunctionType<'a>), |
| 184 | /// A component type. |
| 185 | Component(ComponentType<'a>), |
| 186 | /// An instance type. |
| 187 | Instance(InstanceType<'a>), |
| 188 | /// A resource type. |
| 189 | Resource(ResourceType<'a>), |
| 190 | } |
| 191 | |
| 192 | impl<'a> Parse<'a> for TypeDef<'a> { |
| 193 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 194 | if parser.peek::<LParen>()? { |
| 195 | parser.parens(|parser| { |
| 196 | let mut l = parser.lookahead1(); |
| 197 | if l.peek::<kw::func>()? { |
| 198 | parser.parse::<kw::func>()?; |
| 199 | Ok(Self::Func(parser.parse()?)) |
| 200 | } else if l.peek::<kw::component>()? { |
| 201 | parser.parse::<kw::component>()?; |
| 202 | Ok(Self::Component(parser.parse()?)) |
| 203 | } else if l.peek::<kw::instance>()? { |
| 204 | parser.parse::<kw::instance>()?; |
| 205 | Ok(Self::Instance(parser.parse()?)) |
| 206 | } else if l.peek::<kw::resource>()? { |
| 207 | parser.parse::<kw::resource>()?; |
| 208 | Ok(Self::Resource(parser.parse()?)) |
| 209 | } else { |
| 210 | Ok(Self::Defined(ComponentDefinedType::parse_non_primitive( |
| 211 | parser, l, |
| 212 | )?)) |
| 213 | } |
| 214 | }) |
| 215 | } else { |
| 216 | // Only primitive types have no parens |
| 217 | Ok(Self::Defined(ComponentDefinedType::Primitive( |
| 218 | parser.parse()?, |
| 219 | ))) |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /// A primitive value type. |
| 225 | #[allow (missing_docs)] |
| 226 | #[derive (Debug, Clone, Copy, PartialEq, Eq)] |
| 227 | pub enum PrimitiveValType { |
| 228 | Bool, |
| 229 | S8, |
| 230 | U8, |
| 231 | S16, |
| 232 | U16, |
| 233 | S32, |
| 234 | U32, |
| 235 | S64, |
| 236 | U64, |
| 237 | F32, |
| 238 | F64, |
| 239 | Char, |
| 240 | String, |
| 241 | } |
| 242 | |
| 243 | impl<'a> Parse<'a> for PrimitiveValType { |
| 244 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 245 | let mut l = parser.lookahead1(); |
| 246 | if l.peek::<kw::bool_>()? { |
| 247 | parser.parse::<kw::bool_>()?; |
| 248 | Ok(Self::Bool) |
| 249 | } else if l.peek::<kw::s8>()? { |
| 250 | parser.parse::<kw::s8>()?; |
| 251 | Ok(Self::S8) |
| 252 | } else if l.peek::<kw::u8>()? { |
| 253 | parser.parse::<kw::u8>()?; |
| 254 | Ok(Self::U8) |
| 255 | } else if l.peek::<kw::s16>()? { |
| 256 | parser.parse::<kw::s16>()?; |
| 257 | Ok(Self::S16) |
| 258 | } else if l.peek::<kw::u16>()? { |
| 259 | parser.parse::<kw::u16>()?; |
| 260 | Ok(Self::U16) |
| 261 | } else if l.peek::<kw::s32>()? { |
| 262 | parser.parse::<kw::s32>()?; |
| 263 | Ok(Self::S32) |
| 264 | } else if l.peek::<kw::u32>()? { |
| 265 | parser.parse::<kw::u32>()?; |
| 266 | Ok(Self::U32) |
| 267 | } else if l.peek::<kw::s64>()? { |
| 268 | parser.parse::<kw::s64>()?; |
| 269 | Ok(Self::S64) |
| 270 | } else if l.peek::<kw::u64>()? { |
| 271 | parser.parse::<kw::u64>()?; |
| 272 | Ok(Self::U64) |
| 273 | } else if l.peek::<kw::f32>()? { |
| 274 | parser.parse::<kw::f32>()?; |
| 275 | Ok(Self::F32) |
| 276 | } else if l.peek::<kw::f64>()? { |
| 277 | parser.parse::<kw::f64>()?; |
| 278 | Ok(Self::F64) |
| 279 | } else if l.peek::<kw::float32>()? { |
| 280 | parser.parse::<kw::float32>()?; |
| 281 | Ok(Self::F32) |
| 282 | } else if l.peek::<kw::float64>()? { |
| 283 | parser.parse::<kw::float64>()?; |
| 284 | Ok(Self::F64) |
| 285 | } else if l.peek::<kw::char>()? { |
| 286 | parser.parse::<kw::char>()?; |
| 287 | Ok(Self::Char) |
| 288 | } else if l.peek::<kw::string>()? { |
| 289 | parser.parse::<kw::string>()?; |
| 290 | Ok(Self::String) |
| 291 | } else { |
| 292 | Err(l.error()) |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | impl Peek for PrimitiveValType { |
| 298 | fn peek(cursor: crate::parser::Cursor<'_>) -> Result<bool> { |
| 299 | Ok(matches!( |
| 300 | cursor.keyword()?, |
| 301 | Some(("bool" , _)) |
| 302 | | Some(("s8" , _)) |
| 303 | | Some(("u8" , _)) |
| 304 | | Some(("s16" , _)) |
| 305 | | Some(("u16" , _)) |
| 306 | | Some(("s32" , _)) |
| 307 | | Some(("u32" , _)) |
| 308 | | Some(("s64" , _)) |
| 309 | | Some(("u64" , _)) |
| 310 | | Some(("f32" , _)) |
| 311 | | Some(("f64" , _)) |
| 312 | | Some(("float32" , _)) |
| 313 | | Some(("float64" , _)) |
| 314 | | Some(("char" , _)) |
| 315 | | Some(("string" , _)) |
| 316 | )) |
| 317 | } |
| 318 | |
| 319 | fn display() -> &'static str { |
| 320 | "primitive value type" |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /// A component value type. |
| 325 | #[allow (missing_docs)] |
| 326 | #[derive (Debug)] |
| 327 | pub enum ComponentValType<'a> { |
| 328 | /// The value type is an inline defined type. |
| 329 | Inline(ComponentDefinedType<'a>), |
| 330 | /// The value type is an index reference to a defined type. |
| 331 | Ref(Index<'a>), |
| 332 | } |
| 333 | |
| 334 | impl<'a> Parse<'a> for ComponentValType<'a> { |
| 335 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 336 | if parser.peek::<Index<'_>>()? { |
| 337 | Ok(Self::Ref(parser.parse()?)) |
| 338 | } else { |
| 339 | Ok(Self::Inline(InlineComponentValType::parse(parser)?.0)) |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | impl Peek for ComponentValType<'_> { |
| 345 | fn peek(cursor: crate::parser::Cursor<'_>) -> Result<bool> { |
| 346 | Ok(Index::peek(cursor)? || ComponentDefinedType::peek(cursor)?) |
| 347 | } |
| 348 | |
| 349 | fn display() -> &'static str { |
| 350 | "component value type" |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /// An inline-only component value type. |
| 355 | /// |
| 356 | /// This variation does not parse type indexes. |
| 357 | #[allow (missing_docs)] |
| 358 | #[derive (Debug)] |
| 359 | pub struct InlineComponentValType<'a>(ComponentDefinedType<'a>); |
| 360 | |
| 361 | impl<'a> Parse<'a> for InlineComponentValType<'a> { |
| 362 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 363 | if parser.peek::<LParen>()? { |
| 364 | parser.parens(|parser: Parser<'a>| { |
| 365 | Ok(Self(ComponentDefinedType::parse_non_primitive( |
| 366 | parser, |
| 367 | l:parser.lookahead1(), |
| 368 | )?)) |
| 369 | }) |
| 370 | } else { |
| 371 | Ok(Self(ComponentDefinedType::Primitive(parser.parse()?))) |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | // A component defined type. |
| 377 | #[allow (missing_docs)] |
| 378 | #[derive (Debug)] |
| 379 | pub enum ComponentDefinedType<'a> { |
| 380 | Primitive(PrimitiveValType), |
| 381 | Record(Record<'a>), |
| 382 | Variant(Variant<'a>), |
| 383 | List(List<'a>), |
| 384 | Tuple(Tuple<'a>), |
| 385 | Flags(Flags<'a>), |
| 386 | Enum(Enum<'a>), |
| 387 | Option(OptionType<'a>), |
| 388 | Result(ResultType<'a>), |
| 389 | Own(Index<'a>), |
| 390 | Borrow(Index<'a>), |
| 391 | Stream(Stream<'a>), |
| 392 | Future(Future<'a>), |
| 393 | } |
| 394 | |
| 395 | impl<'a> ComponentDefinedType<'a> { |
| 396 | fn parse_non_primitive(parser: Parser<'a>, mut l: Lookahead1<'a>) -> Result<Self> { |
| 397 | parser.depth_check()?; |
| 398 | if l.peek::<kw::record>()? { |
| 399 | Ok(Self::Record(parser.parse()?)) |
| 400 | } else if l.peek::<kw::variant>()? { |
| 401 | Ok(Self::Variant(parser.parse()?)) |
| 402 | } else if l.peek::<kw::list>()? { |
| 403 | Ok(Self::List(parser.parse()?)) |
| 404 | } else if l.peek::<kw::tuple>()? { |
| 405 | Ok(Self::Tuple(parser.parse()?)) |
| 406 | } else if l.peek::<kw::flags>()? { |
| 407 | Ok(Self::Flags(parser.parse()?)) |
| 408 | } else if l.peek::<kw::enum_>()? { |
| 409 | Ok(Self::Enum(parser.parse()?)) |
| 410 | } else if l.peek::<kw::option>()? { |
| 411 | Ok(Self::Option(parser.parse()?)) |
| 412 | } else if l.peek::<kw::result>()? { |
| 413 | Ok(Self::Result(parser.parse()?)) |
| 414 | } else if l.peek::<kw::own>()? { |
| 415 | parser.parse::<kw::own>()?; |
| 416 | Ok(Self::Own(parser.parse()?)) |
| 417 | } else if l.peek::<kw::borrow>()? { |
| 418 | parser.parse::<kw::borrow>()?; |
| 419 | Ok(Self::Borrow(parser.parse()?)) |
| 420 | } else if l.peek::<kw::stream>()? { |
| 421 | Ok(Self::Stream(parser.parse()?)) |
| 422 | } else if l.peek::<kw::future>()? { |
| 423 | Ok(Self::Future(parser.parse()?)) |
| 424 | } else { |
| 425 | Err(l.error()) |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | impl Default for ComponentDefinedType<'_> { |
| 431 | fn default() -> Self { |
| 432 | Self::Primitive(PrimitiveValType::Bool) |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | impl Peek for ComponentDefinedType<'_> { |
| 437 | fn peek(cursor: crate::parser::Cursor<'_>) -> Result<bool> { |
| 438 | if PrimitiveValType::peek(cursor)? { |
| 439 | return Ok(true); |
| 440 | } |
| 441 | |
| 442 | Ok(match cursor.lparen()? { |
| 443 | Some(cursor) => matches!( |
| 444 | cursor.keyword()?, |
| 445 | Some(("record" , _)) |
| 446 | | Some(("variant" , _)) |
| 447 | | Some(("list" , _)) |
| 448 | | Some(("tuple" , _)) |
| 449 | | Some(("flags" , _)) |
| 450 | | Some(("enum" , _)) |
| 451 | | Some(("option" , _)) |
| 452 | | Some(("result" , _)) |
| 453 | | Some(("own" , _)) |
| 454 | | Some(("borrow" , _)) |
| 455 | ), |
| 456 | None => false, |
| 457 | }) |
| 458 | } |
| 459 | |
| 460 | fn display() -> &'static str { |
| 461 | "component defined type" |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | /// A record defined type. |
| 466 | #[derive (Debug)] |
| 467 | pub struct Record<'a> { |
| 468 | /// The fields of the record. |
| 469 | pub fields: Vec<RecordField<'a>>, |
| 470 | } |
| 471 | |
| 472 | impl<'a> Parse<'a> for Record<'a> { |
| 473 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 474 | parser.parse::<kw::record>()?; |
| 475 | let mut fields: Vec> = Vec::new(); |
| 476 | while !parser.is_empty() { |
| 477 | fields.push(parser.parens(|p: Parser<'a>| p.parse())?); |
| 478 | } |
| 479 | Ok(Self { fields }) |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /// A record type field. |
| 484 | #[derive (Debug)] |
| 485 | pub struct RecordField<'a> { |
| 486 | /// The name of the field. |
| 487 | pub name: &'a str, |
| 488 | /// The type of the field. |
| 489 | pub ty: ComponentValType<'a>, |
| 490 | } |
| 491 | |
| 492 | impl<'a> Parse<'a> for RecordField<'a> { |
| 493 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 494 | parser.parse::<kw::field>()?; |
| 495 | Ok(Self { |
| 496 | name: parser.parse()?, |
| 497 | ty: parser.parse()?, |
| 498 | }) |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | /// A variant defined type. |
| 503 | #[derive (Debug)] |
| 504 | pub struct Variant<'a> { |
| 505 | /// The cases of the variant type. |
| 506 | pub cases: Vec<VariantCase<'a>>, |
| 507 | } |
| 508 | |
| 509 | impl<'a> Parse<'a> for Variant<'a> { |
| 510 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 511 | parser.parse::<kw::variant>()?; |
| 512 | let mut cases: Vec> = Vec::new(); |
| 513 | while !parser.is_empty() { |
| 514 | cases.push(parser.parens(|p: Parser<'a>| p.parse())?); |
| 515 | } |
| 516 | Ok(Self { cases }) |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | /// A case of a variant type. |
| 521 | #[derive (Debug)] |
| 522 | pub struct VariantCase<'a> { |
| 523 | /// Where this `case` was defined |
| 524 | pub span: Span, |
| 525 | /// An optional identifier to refer to this case by as part of name |
| 526 | /// resolution. |
| 527 | pub id: Option<Id<'a>>, |
| 528 | /// The name of the case. |
| 529 | pub name: &'a str, |
| 530 | /// The optional type of the case. |
| 531 | pub ty: Option<ComponentValType<'a>>, |
| 532 | /// The optional refinement. |
| 533 | pub refines: Option<Refinement<'a>>, |
| 534 | } |
| 535 | |
| 536 | impl<'a> Parse<'a> for VariantCase<'a> { |
| 537 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 538 | let span: Span = parser.parse::<kw::case>()?.0; |
| 539 | let id: Option> = parser.parse()?; |
| 540 | let name: &'a str = parser.parse()?; |
| 541 | let ty: Option> = parser.parse()?; |
| 542 | let refines: Option> = if !parser.is_empty() { |
| 543 | Some(parser.parse()?) |
| 544 | } else { |
| 545 | None |
| 546 | }; |
| 547 | Ok(Self { |
| 548 | span, |
| 549 | id, |
| 550 | name, |
| 551 | ty, |
| 552 | refines, |
| 553 | }) |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | /// A refinement for a variant case. |
| 558 | #[derive (Debug)] |
| 559 | pub enum Refinement<'a> { |
| 560 | /// The refinement is referenced by index. |
| 561 | Index(Span, Index<'a>), |
| 562 | /// The refinement has been resolved to an index into |
| 563 | /// the cases of the variant. |
| 564 | Resolved(u32), |
| 565 | } |
| 566 | |
| 567 | impl<'a> Parse<'a> for Refinement<'a> { |
| 568 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 569 | parser.parens(|parser: Parser<'a>| { |
| 570 | let span: Span = parser.parse::<kw::refines>()?.0; |
| 571 | let id: Index<'_> = parser.parse()?; |
| 572 | Ok(Self::Index(span, id)) |
| 573 | }) |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | /// A list type. |
| 578 | #[derive (Debug)] |
| 579 | pub struct List<'a> { |
| 580 | /// The element type of the array. |
| 581 | pub element: Box<ComponentValType<'a>>, |
| 582 | } |
| 583 | |
| 584 | impl<'a> Parse<'a> for List<'a> { |
| 585 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 586 | parser.parse::<kw::list>()?; |
| 587 | Ok(Self { |
| 588 | element: Box::new(parser.parse()?), |
| 589 | }) |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | /// A tuple type. |
| 594 | #[derive (Debug)] |
| 595 | pub struct Tuple<'a> { |
| 596 | /// The types of the fields of the tuple. |
| 597 | pub fields: Vec<ComponentValType<'a>>, |
| 598 | } |
| 599 | |
| 600 | impl<'a> Parse<'a> for Tuple<'a> { |
| 601 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 602 | parser.parse::<kw::tuple>()?; |
| 603 | let mut fields: Vec> = Vec::new(); |
| 604 | while !parser.is_empty() { |
| 605 | fields.push(parser.parse()?); |
| 606 | } |
| 607 | Ok(Self { fields }) |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | /// A flags type. |
| 612 | #[derive (Debug)] |
| 613 | pub struct Flags<'a> { |
| 614 | /// The names of the individual flags. |
| 615 | pub names: Vec<&'a str>, |
| 616 | } |
| 617 | |
| 618 | impl<'a> Parse<'a> for Flags<'a> { |
| 619 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 620 | parser.parse::<kw::flags>()?; |
| 621 | let mut names: Vec<&'a str> = Vec::new(); |
| 622 | while !parser.is_empty() { |
| 623 | names.push(parser.parse()?); |
| 624 | } |
| 625 | Ok(Self { names }) |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | /// An enum type. |
| 630 | #[derive (Debug)] |
| 631 | pub struct Enum<'a> { |
| 632 | /// The tag names of the enum. |
| 633 | pub names: Vec<&'a str>, |
| 634 | } |
| 635 | |
| 636 | impl<'a> Parse<'a> for Enum<'a> { |
| 637 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 638 | parser.parse::<kw::enum_>()?; |
| 639 | let mut names: Vec<&'a str> = Vec::new(); |
| 640 | while !parser.is_empty() { |
| 641 | names.push(parser.parse()?); |
| 642 | } |
| 643 | Ok(Self { names }) |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | /// An optional type. |
| 648 | #[derive (Debug)] |
| 649 | pub struct OptionType<'a> { |
| 650 | /// The type of the value, when a value is present. |
| 651 | pub element: Box<ComponentValType<'a>>, |
| 652 | } |
| 653 | |
| 654 | impl<'a> Parse<'a> for OptionType<'a> { |
| 655 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 656 | parser.parse::<kw::option>()?; |
| 657 | Ok(Self { |
| 658 | element: Box::new(parser.parse()?), |
| 659 | }) |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | /// A result type. |
| 664 | #[derive (Debug)] |
| 665 | pub struct ResultType<'a> { |
| 666 | /// The type on success. |
| 667 | pub ok: Option<Box<ComponentValType<'a>>>, |
| 668 | /// The type on failure. |
| 669 | pub err: Option<Box<ComponentValType<'a>>>, |
| 670 | } |
| 671 | |
| 672 | impl<'a> Parse<'a> for ResultType<'a> { |
| 673 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 674 | parser.parse::<kw::result>()?; |
| 675 | |
| 676 | let ok: Option<ComponentValType> = parser.parse()?; |
| 677 | let err: Option<ComponentValType> = if parser.peek::<LParen>()? { |
| 678 | Some(parser.parens(|parser: Parser<'a>| { |
| 679 | parser.parse::<kw::error>()?; |
| 680 | parser.parse() |
| 681 | })?) |
| 682 | } else { |
| 683 | None |
| 684 | }; |
| 685 | |
| 686 | Ok(Self { |
| 687 | ok: ok.map(Box::new), |
| 688 | err: err.map(Box::new), |
| 689 | }) |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | /// A stream type. |
| 694 | #[derive (Debug)] |
| 695 | pub struct Stream<'a> { |
| 696 | /// The element type of the stream. |
| 697 | pub element: Box<ComponentValType<'a>>, |
| 698 | } |
| 699 | |
| 700 | impl<'a> Parse<'a> for Stream<'a> { |
| 701 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 702 | parser.parse::<kw::stream>()?; |
| 703 | Ok(Self { |
| 704 | element: Box::new(parser.parse()?), |
| 705 | }) |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | /// A future type. |
| 710 | #[derive (Debug)] |
| 711 | pub struct Future<'a> { |
| 712 | /// The element type of the future, if any. |
| 713 | pub element: Option<Box<ComponentValType<'a>>>, |
| 714 | } |
| 715 | |
| 716 | impl<'a> Parse<'a> for Future<'a> { |
| 717 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 718 | parser.parse::<kw::future>()?; |
| 719 | Ok(Self { |
| 720 | element: parser.parse::<Option<ComponentValType>>()?.map(Box::new), |
| 721 | }) |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | /// A component function type with parameters and result. |
| 726 | #[derive (Debug)] |
| 727 | pub struct ComponentFunctionType<'a> { |
| 728 | /// The parameters of a function, optionally each having an identifier for |
| 729 | /// name resolution and a name for the custom `name` section. |
| 730 | pub params: Box<[ComponentFunctionParam<'a>]>, |
| 731 | /// The result of a function, optionally each having an identifier for |
| 732 | /// name resolution and a name for the custom `name` section. |
| 733 | pub results: Box<[ComponentFunctionResult<'a>]>, |
| 734 | } |
| 735 | |
| 736 | impl<'a> Parse<'a> for ComponentFunctionType<'a> { |
| 737 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 738 | let mut params: Vec<ComponentFunctionParam> = Vec::new(); |
| 739 | while parser.peek2::<kw::param>()? { |
| 740 | params.push(parser.parens(|p: Parser<'a>| p.parse())?); |
| 741 | } |
| 742 | |
| 743 | let mut results: Vec<ComponentFunctionResult> = Vec::new(); |
| 744 | while parser.peek2::<kw::result>()? { |
| 745 | results.push(parser.parens(|p: Parser<'a>| p.parse())?); |
| 746 | } |
| 747 | |
| 748 | Ok(Self { |
| 749 | params: params.into(), |
| 750 | results: results.into(), |
| 751 | }) |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | /// A parameter of a [`ComponentFunctionType`]. |
| 756 | #[derive (Debug)] |
| 757 | pub struct ComponentFunctionParam<'a> { |
| 758 | /// The name of the parameter |
| 759 | pub name: &'a str, |
| 760 | /// The type of the parameter. |
| 761 | pub ty: ComponentValType<'a>, |
| 762 | } |
| 763 | |
| 764 | impl<'a> Parse<'a> for ComponentFunctionParam<'a> { |
| 765 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 766 | parser.parse::<kw::param>()?; |
| 767 | Ok(Self { |
| 768 | name: parser.parse()?, |
| 769 | ty: parser.parse()?, |
| 770 | }) |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | /// A result of a [`ComponentFunctionType`]. |
| 775 | #[derive (Debug)] |
| 776 | pub struct ComponentFunctionResult<'a> { |
| 777 | /// An optionally-specified name of this result |
| 778 | pub name: Option<&'a str>, |
| 779 | /// The type of the result. |
| 780 | pub ty: ComponentValType<'a>, |
| 781 | } |
| 782 | |
| 783 | impl<'a> Parse<'a> for ComponentFunctionResult<'a> { |
| 784 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 785 | parser.parse::<kw::result>()?; |
| 786 | Ok(Self { |
| 787 | name: parser.parse()?, |
| 788 | ty: parser.parse()?, |
| 789 | }) |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | /// The type of an exported item from an component or instance type. |
| 794 | #[derive (Debug)] |
| 795 | pub struct ComponentExportType<'a> { |
| 796 | /// Where this export was defined. |
| 797 | pub span: Span, |
| 798 | /// The name of this export. |
| 799 | pub name: ComponentExternName<'a>, |
| 800 | /// The signature of the item. |
| 801 | pub item: ItemSig<'a>, |
| 802 | } |
| 803 | |
| 804 | impl<'a> Parse<'a> for ComponentExportType<'a> { |
| 805 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 806 | let span: Span = parser.parse::<kw::export>()?.0; |
| 807 | let id: Option> = parser.parse()?; |
| 808 | let debug_name: Option> = parser.parse()?; |
| 809 | let name: ComponentExternName<'a> = parser.parse()?; |
| 810 | let item: ItemSig<'_> = parser.parens(|p: Parser<'a>| { |
| 811 | let mut item: ItemSig<'_> = p.parse::<ItemSigNoName<'_>>()?.0; |
| 812 | item.id = id; |
| 813 | item.name = debug_name; |
| 814 | Ok(item) |
| 815 | })?; |
| 816 | Ok(Self { span, name, item }) |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | /// A type definition for a component type. |
| 821 | #[derive (Debug, Default)] |
| 822 | pub struct ComponentType<'a> { |
| 823 | /// The declarations of the component type. |
| 824 | pub decls: Vec<ComponentTypeDecl<'a>>, |
| 825 | } |
| 826 | |
| 827 | impl<'a> Parse<'a> for ComponentType<'a> { |
| 828 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 829 | parser.depth_check()?; |
| 830 | Ok(Self { |
| 831 | decls: parser.parse()?, |
| 832 | }) |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | /// A declaration of a component type. |
| 837 | #[derive (Debug)] |
| 838 | pub enum ComponentTypeDecl<'a> { |
| 839 | /// A core type definition local to the component type. |
| 840 | CoreType(CoreType<'a>), |
| 841 | /// A type definition local to the component type. |
| 842 | Type(Type<'a>), |
| 843 | /// An alias local to the component type. |
| 844 | Alias(Alias<'a>), |
| 845 | /// An import of the component type. |
| 846 | Import(ComponentImport<'a>), |
| 847 | /// An export of the component type. |
| 848 | Export(ComponentExportType<'a>), |
| 849 | } |
| 850 | |
| 851 | impl<'a> Parse<'a> for ComponentTypeDecl<'a> { |
| 852 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 853 | let mut l: Lookahead1<'_> = parser.lookahead1(); |
| 854 | if l.peek::<kw::core>()? { |
| 855 | Ok(Self::CoreType(parser.parse()?)) |
| 856 | } else if l.peek::<kw::r#type>()? { |
| 857 | Ok(Self::Type(Type::parse_no_inline_exports(parser)?)) |
| 858 | } else if l.peek::<kw::alias>()? { |
| 859 | Ok(Self::Alias(parser.parse()?)) |
| 860 | } else if l.peek::<kw::import>()? { |
| 861 | Ok(Self::Import(parser.parse()?)) |
| 862 | } else if l.peek::<kw::export>()? { |
| 863 | Ok(Self::Export(parser.parse()?)) |
| 864 | } else { |
| 865 | Err(l.error()) |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | impl<'a> Parse<'a> for Vec<ComponentTypeDecl<'a>> { |
| 871 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 872 | let mut decls: Vec> = Vec::new(); |
| 873 | while !parser.is_empty() { |
| 874 | decls.push(parser.parens(|parser: Parser<'a>| parser.parse())?); |
| 875 | } |
| 876 | Ok(decls) |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | /// A type definition for an instance type. |
| 881 | #[derive (Debug)] |
| 882 | pub struct InstanceType<'a> { |
| 883 | /// The declarations of the instance type. |
| 884 | pub decls: Vec<InstanceTypeDecl<'a>>, |
| 885 | } |
| 886 | |
| 887 | impl<'a> Parse<'a> for InstanceType<'a> { |
| 888 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 889 | parser.depth_check()?; |
| 890 | Ok(Self { |
| 891 | decls: parser.parse()?, |
| 892 | }) |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | /// A declaration of an instance type. |
| 897 | #[derive (Debug)] |
| 898 | pub enum InstanceTypeDecl<'a> { |
| 899 | /// A core type definition local to the component type. |
| 900 | CoreType(CoreType<'a>), |
| 901 | /// A type definition local to the instance type. |
| 902 | Type(Type<'a>), |
| 903 | /// An alias local to the instance type. |
| 904 | Alias(Alias<'a>), |
| 905 | /// An export of the instance type. |
| 906 | Export(ComponentExportType<'a>), |
| 907 | } |
| 908 | |
| 909 | impl<'a> Parse<'a> for InstanceTypeDecl<'a> { |
| 910 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 911 | let mut l: Lookahead1<'_> = parser.lookahead1(); |
| 912 | if l.peek::<kw::core>()? { |
| 913 | Ok(Self::CoreType(parser.parse()?)) |
| 914 | } else if l.peek::<kw::r#type>()? { |
| 915 | Ok(Self::Type(Type::parse_no_inline_exports(parser)?)) |
| 916 | } else if l.peek::<kw::alias>()? { |
| 917 | Ok(Self::Alias(parser.parse()?)) |
| 918 | } else if l.peek::<kw::export>()? { |
| 919 | Ok(Self::Export(parser.parse()?)) |
| 920 | } else { |
| 921 | Err(l.error()) |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | impl<'a> Parse<'a> for Vec<InstanceTypeDecl<'a>> { |
| 927 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 928 | let mut decls: Vec> = Vec::new(); |
| 929 | while !parser.is_empty() { |
| 930 | decls.push(parser.parens(|parser: Parser<'a>| parser.parse())?); |
| 931 | } |
| 932 | Ok(decls) |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | /// A type definition for an instance type. |
| 937 | #[derive (Debug)] |
| 938 | pub struct ResourceType<'a> { |
| 939 | /// Representation, in core WebAssembly, of this resource. |
| 940 | pub rep: core::ValType<'a>, |
| 941 | /// The declarations of the instance type. |
| 942 | pub dtor: Option<CoreItemRef<'a, kw::func>>, |
| 943 | } |
| 944 | |
| 945 | impl<'a> Parse<'a> for ResourceType<'a> { |
| 946 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 947 | let rep: ValType<'a> = parser.parens(|p: Parser<'a>| { |
| 948 | p.parse::<kw::rep>()?; |
| 949 | p.parse() |
| 950 | })?; |
| 951 | let dtor: Option> = if parser.is_empty() { |
| 952 | None |
| 953 | } else { |
| 954 | Some(parser.parens(|p: Parser<'a>| { |
| 955 | p.parse::<kw::dtor>()?; |
| 956 | p.parens(|p: Parser<'a>| p.parse()) |
| 957 | })?) |
| 958 | }; |
| 959 | Ok(Self { rep, dtor }) |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | /// A value type declaration used for values in import signatures. |
| 964 | #[derive (Debug)] |
| 965 | pub struct ComponentValTypeUse<'a>(pub ComponentValType<'a>); |
| 966 | |
| 967 | impl<'a> Parse<'a> for ComponentValTypeUse<'a> { |
| 968 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 969 | match ComponentTypeUse::<'a, InlineComponentValType<'a>>::parse(parser)? { |
| 970 | ComponentTypeUse::Ref(i: ItemRef<'_, r#type>) => Ok(Self(ComponentValType::Ref(i.idx))), |
| 971 | ComponentTypeUse::Inline(t: InlineComponentValType<'a>) => Ok(Self(ComponentValType::Inline(t.0))), |
| 972 | } |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | /// A reference to a core type defined in this component. |
| 977 | /// |
| 978 | /// This is the same as `TypeUse`, but accepts `$T` as shorthand for |
| 979 | /// `(type $T)`. |
| 980 | #[derive (Debug, Clone)] |
| 981 | pub enum CoreTypeUse<'a, T> { |
| 982 | /// The type that we're referencing. |
| 983 | Ref(CoreItemRef<'a, kw::r#type>), |
| 984 | /// The inline type. |
| 985 | Inline(T), |
| 986 | } |
| 987 | |
| 988 | impl<'a, T: Parse<'a>> Parse<'a> for CoreTypeUse<'a, T> { |
| 989 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 990 | // Here the core context is assumed, so no core prefix is expected |
| 991 | if parser.peek::<LParen>()? && parser.peek2::<CoreItemRef<'a, kw::r#type>>()? { |
| 992 | Ok(Self::Ref(parser.parens(|parser: Parser<'a>| parser.parse())?)) |
| 993 | } else { |
| 994 | Ok(Self::Inline(parser.parse()?)) |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | impl<T> Default for CoreTypeUse<'_, T> { |
| 1000 | fn default() -> Self { |
| 1001 | let span: Span = Span::from_offset(0); |
| 1002 | Self::Ref(CoreItemRef { |
| 1003 | idx: Index::Num(0, span), |
| 1004 | kind: kw::r#type(span), |
| 1005 | export_name: None, |
| 1006 | }) |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | /// A reference to a type defined in this component. |
| 1011 | /// |
| 1012 | /// This is the same as `TypeUse`, but accepts `$T` as shorthand for |
| 1013 | /// `(type $T)`. |
| 1014 | #[derive (Debug, Clone)] |
| 1015 | pub enum ComponentTypeUse<'a, T> { |
| 1016 | /// The type that we're referencing. |
| 1017 | Ref(ItemRef<'a, kw::r#type>), |
| 1018 | /// The inline type. |
| 1019 | Inline(T), |
| 1020 | } |
| 1021 | |
| 1022 | impl<'a, T: Parse<'a>> Parse<'a> for ComponentTypeUse<'a, T> { |
| 1023 | fn parse(parser: Parser<'a>) -> Result<Self> { |
| 1024 | if parser.peek::<LParen>()? && parser.peek2::<ItemRef<'a, kw::r#type>>()? { |
| 1025 | Ok(Self::Ref(parser.parens(|parser: Parser<'a>| parser.parse())?)) |
| 1026 | } else { |
| 1027 | Ok(Self::Inline(parser.parse()?)) |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | impl<T> Default for ComponentTypeUse<'_, T> { |
| 1033 | fn default() -> Self { |
| 1034 | let span: Span = Span::from_offset(0); |
| 1035 | Self::Ref(ItemRef { |
| 1036 | idx: Index::Num(0, span), |
| 1037 | kind: kw::r#type(span), |
| 1038 | export_names: Vec::new(), |
| 1039 | }) |
| 1040 | } |
| 1041 | } |
| 1042 | |