From 76a8649b4119b1d52cd5cc0d3eea5ce46b6d6e04 Mon Sep 17 00:00:00 2001 From: Dominik Nakamura Date: Fri, 29 Dec 2023 18:37:19 +0900 Subject: [PATCH] feat: allow to omit IDs and derive them instead Similar to Rust basic enums, the identifier for each variant doesn't have to be specified explicitly. Instead it can be derived by continuing to count upwards from the last explicit value. Therefore, now the field IDs as well as enum variant IDs can be omitted and are derived by the compiler. They can be mixed as well to create gaps in the ID range. --- book/highlight/src/languages/stef.ts | 20 +- .../compiler__compile@optional_ids.stef.snap | 373 ++++++++++++ crates/stef-compiler/src/lib.rs | 44 ++ crates/stef-compiler/src/simplify.rs | 19 +- crates/stef-compiler/src/validate/ids.rs | 38 +- .../render__render@optional_ids.stef.snap | 492 ++++++++++++++++ .../render__render@optional_ids.stef.snap | 537 ++++++++++++++++++ .../stef-lsp/src/handlers/semantic_tokens.rs | 37 +- crates/stef-parser/src/lib.rs | 26 +- crates/stef-parser/src/parser/enums.rs | 2 +- crates/stef-parser/src/parser/fields.rs | 4 +- .../tests/inputs/optional_ids.stef | 17 + .../parser__parse@enum_basic.stef.snap | 56 +- .../parser__parse@enum_generics.stef.snap | 56 +- .../parser__parse@enum_many_ws.stef.snap | 56 +- .../parser__parse@enum_min_ws.stef.snap | 72 ++- .../snapshots/parser__parse@mixed.stef.snap | 264 +++++---- .../parser__parse@module_basic.stef.snap | 24 +- .../parser__parse@optional_ids.stef.snap | 238 ++++++++ .../parser__parse@schema_basic.stef.snap | 72 ++- .../parser__parse@struct_basic.stef.snap | 16 +- .../parser__parse@struct_generics.stef.snap | 16 +- .../parser__parse@struct_many_ws.stef.snap | 24 +- .../parser__parse@struct_min_ws.stef.snap | 24 +- .../parser__parse@struct_tuple.stef.snap | 16 +- .../parser__parse@types_basic.stef.snap | 168 ++++-- .../parser__parse@types_generic.stef.snap | 80 ++- .../parser__parse@types_nested.stef.snap | 8 +- .../parser__parse@types_non_zero.stef.snap | 120 ++-- .../parser__parse@types_ref.stef.snap | 40 +- .../parser__print@optional_ids.stef.snap | 24 + vscode-extension/package.json | 8 + 32 files changed, 2535 insertions(+), 456 deletions(-) create mode 100644 crates/stef-build/tests/snapshots/compiler__compile@optional_ids.stef.snap create mode 100644 crates/stef-doc/tests/snapshots/render__render@optional_ids.stef.snap create mode 100644 crates/stef-go/tests/snapshots/render__render@optional_ids.stef.snap create mode 100644 crates/stef-parser/tests/inputs/optional_ids.stef create mode 100644 crates/stef-parser/tests/snapshots/parser__parse@optional_ids.stef.snap create mode 100644 crates/stef-parser/tests/snapshots/parser__print@optional_ids.stef.snap diff --git a/book/highlight/src/languages/stef.ts b/book/highlight/src/languages/stef.ts index 9c08141..b9fad70 100644 --- a/book/highlight/src/languages/stef.ts +++ b/book/highlight/src/languages/stef.ts @@ -98,7 +98,17 @@ export function stef(hljs: HLJSApi): Language { 4: "symbol", 5: "punctuation", }, - relevance: 2, + relevance: 4, + }, + { + match: [/[a-z0-9_]+/, ":", /[a-zA-Z0-9_<>:,& ]+?/, /,?/], + scope: { + 1: "attr", + 2: "punctuation", + 3: "type", + 4: "punctuation", + }, + relevance: 3, }, { match: [/[a-zA-Z0-9_<>,& ]+/, /@\d+/, /,?/], @@ -107,6 +117,14 @@ export function stef(hljs: HLJSApi): Language { 2: "symbol", 3: "punctuation", }, + relevance: 2, + }, + { + match: [/[a-zA-Z0-9_<>,& ]+?/, /,?/], + scope: { + 1: "type", + 2: "punctuation", + }, relevance: 1, }, { diff --git a/crates/stef-build/tests/snapshots/compiler__compile@optional_ids.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@optional_ids.stef.snap new file mode 100644 index 0000000..ff81c2b --- /dev/null +++ b/crates/stef-build/tests/snapshots/compiler__compile@optional_ids.stef.snap @@ -0,0 +1,373 @@ +--- +source: crates/stef-build/tests/compiler.rs +description: "struct SampleNamed {\n field1: u32,\n field2: u32 @100,\n field3: u32,\n}\n\nstruct SampleUnnamed(u32, u32 @100, u32)\n\nenum SampleEnum {\n Named {\n field1: u32,\n field2: u32 @100,\n field3: u32,\n },\n Unit @50,\n Unnamed(u32, u32 @100, u32),\n}" +input_file: crates/stef-parser/tests/inputs/optional_ids.stef +--- +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode, Size}; +#[derive(Clone, Debug, PartialEq)] +#[allow(clippy::module_name_repetitions, clippy::option_option)] +pub struct SampleNamed { + pub field1: u32, + pub field2: u32, + pub field3: u32, +} +#[automatically_derived] +impl ::stef::Encode for SampleNamed { + #[allow( + clippy::borrow_deref_ref, + clippy::deref_addrof, + clippy::explicit_auto_deref, + clippy::needless_borrow, + clippy::too_many_lines, + )] + fn encode(&self, w: &mut impl ::stef::BufMut) { + let Self { field1, field2, field3 } = self; + ::stef::buf::encode_field( + w, + ::stef::FieldId::new(1, ::stef::FieldEncoding::Varint), + |w| { + ::stef::buf::encode_u32(w, *field1); + }, + ); + ::stef::buf::encode_field( + w, + ::stef::FieldId::new(100, ::stef::FieldEncoding::Varint), + |w| { + ::stef::buf::encode_u32(w, *field2); + }, + ); + ::stef::buf::encode_field( + w, + ::stef::FieldId::new(101, ::stef::FieldEncoding::Varint), + |w| { + ::stef::buf::encode_u32(w, *field3); + }, + ); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); + } +} +#[automatically_derived] +impl ::stef::Decode for SampleNamed { + #[allow(clippy::type_complexity, clippy::too_many_lines)] + fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { + let mut field1: Option = None; + let mut field2: Option = None; + let mut field3: Option = None; + loop { + let id = ::stef::buf::decode_id(r)?; + match id.value { + ::stef::buf::END_MARKER => break, + 1 => field1 = Some(::stef::buf::decode_u32(r)?), + 100 => field2 = Some(::stef::buf::decode_u32(r)?), + 101 => field3 = Some(::stef::buf::decode_u32(r)?), + _ => ::stef::buf::decode_skip(r, id.encoding)?, + } + } + Ok(Self { + field1: field1 + .ok_or(::stef::buf::Error::MissingField { + id: 1, + name: Some("field1"), + })?, + field2: field2 + .ok_or(::stef::buf::Error::MissingField { + id: 100, + name: Some("field2"), + })?, + field3: field3 + .ok_or(::stef::buf::Error::MissingField { + id: 101, + name: Some("field3"), + })?, + }) + } +} +#[automatically_derived] +impl ::stef::buf::Size for SampleNamed { + #[allow( + clippy::borrow_deref_ref, + clippy::deref_addrof, + clippy::explicit_auto_deref, + clippy::needless_borrow, + clippy::too_many_lines, + )] + fn size(&self) -> usize { + let Self { field1, field2, field3 } = self; + ::stef::buf::size_field(1, || { ::stef::buf::size_u32(*field1) }) + + ::stef::buf::size_field(100, || { ::stef::buf::size_u32(*field2) }) + + ::stef::buf::size_field(101, || { ::stef::buf::size_u32(*field3) }) + + ::stef::buf::size_u32(::stef::buf::END_MARKER) + } +} +#[derive(Clone, Debug, PartialEq)] +#[allow(clippy::module_name_repetitions, clippy::option_option)] +pub struct SampleUnnamed(pub u32, pub u32, pub u32); +#[automatically_derived] +impl ::stef::Encode for SampleUnnamed { + #[allow( + clippy::borrow_deref_ref, + clippy::deref_addrof, + clippy::explicit_auto_deref, + clippy::needless_borrow, + clippy::too_many_lines, + )] + fn encode(&self, w: &mut impl ::stef::BufMut) { + let Self(n0, n1, n2) = self; + ::stef::buf::encode_field( + w, + ::stef::FieldId::new(1, ::stef::FieldEncoding::Varint), + |w| { + ::stef::buf::encode_u32(w, *n0); + }, + ); + ::stef::buf::encode_field( + w, + ::stef::FieldId::new(100, ::stef::FieldEncoding::Varint), + |w| { + ::stef::buf::encode_u32(w, *n1); + }, + ); + ::stef::buf::encode_field( + w, + ::stef::FieldId::new(101, ::stef::FieldEncoding::Varint), + |w| { + ::stef::buf::encode_u32(w, *n2); + }, + ); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); + } +} +#[automatically_derived] +impl ::stef::Decode for SampleUnnamed { + #[allow(clippy::type_complexity, clippy::too_many_lines)] + fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { + let mut n0: Option = None; + let mut n1: Option = None; + let mut n2: Option = None; + loop { + let id = ::stef::buf::decode_id(r)?; + match id.value { + ::stef::buf::END_MARKER => break, + 1 => n0 = Some(::stef::buf::decode_u32(r)?), + 100 => n1 = Some(::stef::buf::decode_u32(r)?), + 101 => n2 = Some(::stef::buf::decode_u32(r)?), + _ => ::stef::buf::decode_skip(r, id.encoding)?, + } + } + Ok( + Self( + n0 + .ok_or(::stef::buf::Error::MissingField { + id: 1, + name: None, + })?, + n1 + .ok_or(::stef::buf::Error::MissingField { + id: 100, + name: None, + })?, + n2 + .ok_or(::stef::buf::Error::MissingField { + id: 101, + name: None, + })?, + ), + ) + } +} +#[automatically_derived] +impl ::stef::buf::Size for SampleUnnamed { + #[allow( + clippy::borrow_deref_ref, + clippy::deref_addrof, + clippy::explicit_auto_deref, + clippy::needless_borrow, + clippy::too_many_lines, + )] + fn size(&self) -> usize { + let Self(n0, n1, n2) = self; + ::stef::buf::size_field(1, || { ::stef::buf::size_u32(*n0) }) + + ::stef::buf::size_field(100, || { ::stef::buf::size_u32(*n1) }) + + ::stef::buf::size_field(101, || { ::stef::buf::size_u32(*n2) }) + + ::stef::buf::size_u32(::stef::buf::END_MARKER) + } +} +#[derive(Clone, Debug, PartialEq)] +#[allow(clippy::module_name_repetitions, clippy::option_option)] +pub enum SampleEnum { + Named { field1: u32, field2: u32, field3: u32 }, + Unit, + Unnamed(u32, u32, u32), +} +#[automatically_derived] +impl ::stef::Encode for SampleEnum { + #[allow( + clippy::borrow_deref_ref, + clippy::deref_addrof, + clippy::semicolon_if_nothing_returned, + clippy::too_many_lines, + )] + fn encode(&self, w: &mut impl ::stef::BufMut) { + match self { + Self::Named { field1, field2, field3 } => { + ::stef::buf::encode_variant_id(w, ::stef::VariantId::new(1)); + ::stef::buf::encode_field( + w, + ::stef::FieldId::new(1, ::stef::FieldEncoding::Varint), + |w| { + ::stef::buf::encode_u32(w, *field1); + }, + ); + ::stef::buf::encode_field( + w, + ::stef::FieldId::new(100, ::stef::FieldEncoding::Varint), + |w| { + ::stef::buf::encode_u32(w, *field2); + }, + ); + ::stef::buf::encode_field( + w, + ::stef::FieldId::new(101, ::stef::FieldEncoding::Varint), + |w| { + ::stef::buf::encode_u32(w, *field3); + }, + ); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); + } + Self::Unit => { + ::stef::buf::encode_variant_id(w, ::stef::VariantId::new(50)); + } + Self::Unnamed(n0, n1, n2) => { + ::stef::buf::encode_variant_id(w, ::stef::VariantId::new(51)); + ::stef::buf::encode_field( + w, + ::stef::FieldId::new(1, ::stef::FieldEncoding::Varint), + |w| { + ::stef::buf::encode_u32(w, *n0); + }, + ); + ::stef::buf::encode_field( + w, + ::stef::FieldId::new(100, ::stef::FieldEncoding::Varint), + |w| { + ::stef::buf::encode_u32(w, *n1); + }, + ); + ::stef::buf::encode_field( + w, + ::stef::FieldId::new(101, ::stef::FieldEncoding::Varint), + |w| { + ::stef::buf::encode_u32(w, *n2); + }, + ); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); + } + } + } +} +#[automatically_derived] +impl ::stef::Decode for SampleEnum { + #[allow(clippy::too_many_lines)] + fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { + match ::stef::buf::decode_variant_id(r)?.value { + 1 => { + let mut field1: Option = None; + let mut field2: Option = None; + let mut field3: Option = None; + loop { + let id = ::stef::buf::decode_id(r)?; + match id.value { + ::stef::buf::END_MARKER => break, + 1 => field1 = Some(::stef::buf::decode_u32(r)?), + 100 => field2 = Some(::stef::buf::decode_u32(r)?), + 101 => field3 = Some(::stef::buf::decode_u32(r)?), + _ => ::stef::buf::decode_skip(r, id.encoding)?, + } + } + Ok(Self::Named { + field1: field1 + .ok_or(::stef::buf::Error::MissingField { + id: 1, + name: Some("field1"), + })?, + field2: field2 + .ok_or(::stef::buf::Error::MissingField { + id: 100, + name: Some("field2"), + })?, + field3: field3 + .ok_or(::stef::buf::Error::MissingField { + id: 101, + name: Some("field3"), + })?, + }) + } + 50 => Ok(Self::Unit), + 51 => { + let mut n0: Option = None; + let mut n1: Option = None; + let mut n2: Option = None; + loop { + let id = ::stef::buf::decode_id(r)?; + match id.value { + ::stef::buf::END_MARKER => break, + 1 => n0 = Some(::stef::buf::decode_u32(r)?), + 100 => n1 = Some(::stef::buf::decode_u32(r)?), + 101 => n2 = Some(::stef::buf::decode_u32(r)?), + _ => ::stef::buf::decode_skip(r, id.encoding)?, + } + } + Ok( + Self::Unnamed( + n0 + .ok_or(::stef::buf::Error::MissingField { + id: 1, + name: None, + })?, + n1 + .ok_or(::stef::buf::Error::MissingField { + id: 100, + name: None, + })?, + n2 + .ok_or(::stef::buf::Error::MissingField { + id: 101, + name: None, + })?, + ), + ) + } + id => Err(::stef::buf::Error::UnknownVariant(id)), + } + } +} +#[automatically_derived] +impl ::stef::buf::Size for SampleEnum { + #[allow( + clippy::borrow_deref_ref, + clippy::deref_addrof, + clippy::semicolon_if_nothing_returned, + clippy::too_many_lines, + )] + fn size(&self) -> usize { + match self { + Self::Named { field1, field2, field3 } => { + ::stef::buf::size_id(1) + + ::stef::buf::size_field(1, || { ::stef::buf::size_u32(*field1) }) + + ::stef::buf::size_field(100, || { ::stef::buf::size_u32(*field2) }) + + ::stef::buf::size_field(101, || { ::stef::buf::size_u32(*field3) }) + + ::stef::buf::size_u32(::stef::buf::END_MARKER) + } + Self::Unit => ::stef::buf::size_id(50), + Self::Unnamed(n0, n1, n2) => { + ::stef::buf::size_id(51) + + ::stef::buf::size_field(1, || { ::stef::buf::size_u32(*n0) }) + + ::stef::buf::size_field(100, || { ::stef::buf::size_u32(*n1) }) + + ::stef::buf::size_field(101, || { ::stef::buf::size_u32(*n2) }) + + ::stef::buf::size_u32(::stef::buf::END_MARKER) + } + } + } +} + diff --git a/crates/stef-compiler/src/lib.rs b/crates/stef-compiler/src/lib.rs index 74257c5..cade6b7 100644 --- a/crates/stef-compiler/src/lib.rs +++ b/crates/stef-compiler/src/lib.rs @@ -22,9 +22,53 @@ pub use resolve::schemas as resolve_schemas; pub use simplify::schema as simplify_schema; +use stef_parser::Spanned; pub use validate::schema as validate_schema; mod highlight; pub mod resolve; pub mod simplify; pub mod validate; + +/// Generator that is responsible for deriving omitted field and enum identifiers. +struct IdGenerator { + /// The next available ID. + next_id: u32, +} + +impl IdGenerator { + /// Create a new instance of the ID generator. + fn new() -> Self { + Self { next_id: 1 } + } + + /// Get the next ID, which is either already explicitly defined by the given parameter, or + /// derived otherwise. + /// + /// Identifiers can have gaps, and must be consistently increasing (currently). In the future, + /// the ID counter might jump forth and back, as long as each ID is guaranteed to be unique. + fn next(&mut self, id: Option<&stef_parser::Id>) -> u32 { + let id = id.map_or(self.next_id, stef_parser::Id::get); + self.next_id = id + 1; + id + } + + /// Get the next ID, but additionally combine it with a span to construct a new + /// [`stef_parser::Id`] instance. + /// + /// If an ID wasn't explicitly defined, the `fallback` closure is used to retrieve an + /// alternative span. For example, this could be the span of a full struct field. + fn next_with_span( + &mut self, + id: Option<&stef_parser::Id>, + fallback: impl FnOnce() -> stef_parser::Span, + ) -> stef_parser::Id { + let new_id = self.next(id); + let span = match id { + Some(id) => id.span(), + None => fallback(), + }; + + (new_id, span.into()).into() + } +} diff --git a/crates/stef-compiler/src/simplify.rs b/crates/stef-compiler/src/simplify.rs index d1766a8..886f10b 100644 --- a/crates/stef-compiler/src/simplify.rs +++ b/crates/stef-compiler/src/simplify.rs @@ -4,6 +4,8 @@ use std::borrow::Cow; +use crate::IdGenerator; + /// Uppermost element, describing a single schema file. #[cfg_attr(feature = "json", derive(schemars::JsonSchema, serde::Serialize))] pub struct Schema<'a> { @@ -349,6 +351,8 @@ fn simplify_struct<'a>(item: &'a stef_parser::Struct<'_>) -> Struct<'a> { } fn simplify_enum<'a>(item: &'a stef_parser::Enum<'_>) -> Enum<'a> { + let mut id_gen = IdGenerator::new(); + Enum { source: item, comment: comment(&item.comment), @@ -357,22 +361,27 @@ fn simplify_enum<'a>(item: &'a stef_parser::Enum<'_>) -> Enum<'a> { variants: item .variants .iter() - .map(|variant| simplify_variant(variant)) + .map(|variant| simplify_variant(variant, &mut id_gen)) .collect(), } } -fn simplify_variant<'a>(item: &'a stef_parser::Variant<'_>) -> Variant<'a> { +fn simplify_variant<'a>( + item: &'a stef_parser::Variant<'_>, + id_gen: &mut IdGenerator, +) -> Variant<'a> { Variant { source: item, comment: comment(&item.comment), name: item.name.get(), fields: simplify_fields(&item.fields), - id: item.id.get(), + id: id_gen.next(item.id.as_ref()), } } fn simplify_fields<'a>(item: &'a stef_parser::Fields<'_>) -> Fields<'a> { + let mut id_gen = IdGenerator::new(); + match item { stef_parser::Fields::Named(named) => Fields { source: item, @@ -383,7 +392,7 @@ fn simplify_fields<'a>(item: &'a stef_parser::Fields<'_>) -> Fields<'a> { comment: comment(&field.comment), name: field.name.get().into(), ty: simplify_type(&field.ty), - id: field.id.get(), + id: id_gen.next(field.id.as_ref()), }) .collect(), kind: FieldKind::Named, @@ -398,7 +407,7 @@ fn simplify_fields<'a>(item: &'a stef_parser::Fields<'_>) -> Fields<'a> { comment: Box::default(), name: format!("n{i}").into(), ty: simplify_type(&field.ty), - id: field.id.get(), + id: id_gen.next(field.id.as_ref()), }) .collect(), kind: FieldKind::Unnamed, diff --git a/crates/stef-compiler/src/validate/ids.rs b/crates/stef-compiler/src/validate/ids.rs index 9bb6ca8..03b9941 100644 --- a/crates/stef-compiler/src/validate/ids.rs +++ b/crates/stef-compiler/src/validate/ids.rs @@ -4,6 +4,8 @@ use miette::Diagnostic; use stef_parser::{Enum, Fields, Id, Spanned, Struct}; use thiserror::Error; +use crate::IdGenerator; + /// Duplicate ID was encountered for two elements in the same scope. #[derive(Debug, Diagnostic, Error)] pub enum DuplicateId { @@ -96,19 +98,23 @@ pub(crate) fn validate_struct_ids(value: &Struct<'_>) -> Result<(), DuplicateFie /// potential fields in a variant are unique (within that variant). pub(crate) fn validate_enum_ids(value: &Enum<'_>) -> Result<(), DuplicateId> { let mut visited = HashMap::with_capacity(value.variants.len()); + let mut id_gen = IdGenerator::new(); + value .variants .iter() .find_map(|variant| { + let id = id_gen.next_with_span(variant.id.as_ref(), || variant.span()); + visited - .insert(variant.id.get(), (variant.name.get(), variant.id.span())) + .insert(id.get(), (variant.name.get(), id.span())) .map(|(other_name, other_span)| { DuplicateVariantId { - id: variant.id.clone(), name: variant.name.get().to_owned(), other_name: other_name.to_owned(), first: other_span.into(), - second: variant.id.span().into(), + second: id.span().into(), + id, } .into() }) @@ -126,34 +132,42 @@ fn validate_field_ids(value: &Fields<'_>) -> Result<(), DuplicateFieldId> { match value { Fields::Named(named) => { let mut visited = HashMap::with_capacity(named.len()); + let mut id_gen = IdGenerator::new(); + named .iter() .find_map(|field| { - visited - .insert(field.id.get(), (field.name.get(), field.id.span())) - .map(|(other_field, other_span)| DuplicateNamedFieldId { - id: field.id.clone(), + let id = id_gen.next_with_span(field.id.as_ref(), || field.span()); + + visited.insert(id.get(), (field.name.get(), id.span())).map( + |(other_field, other_span)| DuplicateNamedFieldId { name: field.name.get().to_owned(), other_name: other_field.to_owned(), first: other_span.into(), - second: field.id.span().into(), - }) + second: id.span().into(), + id, + }, + ) }) .map_or(Ok(()), Err)?; } Fields::Unnamed(unnamed) => { let mut visited = HashMap::with_capacity(unnamed.len()); + let mut id_gen = IdGenerator::new(); + unnamed .iter() .enumerate() .find_map(|(pos, field)| { - visited.insert(field.id.get(), (pos, field.id.span())).map( + let id = id_gen.next_with_span(field.id.as_ref(), || field.span()); + + visited.insert(id.get(), (pos, id.span())).map( |(other_position, other_span)| DuplicateUnnamedFieldId { - id: field.id.clone(), position: pos + 1, other_position: other_position + 1, first: other_span.into(), - second: field.id.span().into(), + second: id.span().into(), + id, }, ) }) diff --git a/crates/stef-doc/tests/snapshots/render__render@optional_ids.stef.snap b/crates/stef-doc/tests/snapshots/render__render@optional_ids.stef.snap new file mode 100644 index 0000000..2a32c24 --- /dev/null +++ b/crates/stef-doc/tests/snapshots/render__render@optional_ids.stef.snap @@ -0,0 +1,492 @@ +--- +source: crates/stef-doc/tests/render.rs +description: "struct SampleNamed {\n field1: u32,\n field2: u32 @100,\n field3: u32,\n}\n\nstruct SampleUnnamed(u32, u32 @100, u32)\n\nenum SampleEnum {\n Named {\n field1: u32,\n field2: u32 @100,\n field3: u32,\n },\n Unit @50,\n Unnamed(u32, u32 @100, u32),\n}" +input_file: crates/stef-parser/tests/inputs/optional_ids.stef +--- +--- optional_ids/index.html + + + + + + + + + + + optional_ids - Stef + + + +
+
+

Schema optional_ids

+
+ +
+
+
+

Modules

+ +
+
+
+

Structs

+ + + + + + + + + +
+ SampleNamed + + +
+ SampleUnnamed + + +
+
+
+

Enums

+ + + + + +
+ SampleEnum + + +
+
+
+

Aliases

+ +
+
+
+

Constants

+ +
+
+
+ + + +--- optional_ids/struct.SampleNamed.html + + + + + + + + + + + SampleNamed - Stef + + + +
+
+

+ Struct + optional_ids::SampleNamed +

+
struct SampleNamed {
+    field1: u32 @1,
+    field2: u32 @100,
+    field3: u32 @101,
+}
+
+ +
+
+
+

Metadata

+
+

The next free ID is 102

+
+
+
+

Fields

+
    +
  • + + field1: + u32 + @1 + +
    + +
    +
    +

    Metadata

    +
    +

    The size range is:

    +
    +

    u32 1..5

    +
    +
    +
    +
  • +
  • + + field2: + u32 + @100 + +
    + +
    +
    +

    Metadata

    +
    +

    The size range is:

    +
    +

    u32 1..5

    +
    +
    +
    +
  • +
  • + + field3: + u32 + @101 + +
    + +
    +
    +

    Metadata

    +
    +

    The size range is:

    +
    +

    u32 1..5

    +
    +
    +
    +
  • +
+
+ +
+ + + +--- optional_ids/struct.SampleUnnamed.html + + + + + + + + + + + SampleUnnamed - Stef + + + +
+
+

+ Struct + optional_ids::SampleUnnamed +

+
struct SampleUnnamed(u32 @1, u32 @100, u32 @101)
+
+ +
+
+
+

Metadata

+
+

The next free ID is 102

+
+
+
+

Fields

+
    +
  • + + n0: + u32 + @1 + +
    + +
    +
    +

    Metadata

    +
    +

    The size range is:

    +
    +

    u32 1..5

    +
    +
    +
    +
  • +
  • + + n1: + u32 + @100 + +
    + +
    +
    +

    Metadata

    +
    +

    The size range is:

    +
    +

    u32 1..5

    +
    +
    +
    +
  • +
  • + + n2: + u32 + @101 + +
    + +
    +
    +

    Metadata

    +
    +

    The size range is:

    +
    +

    u32 1..5

    +
    +
    +
    +
  • +
+
+ +
+ + + +--- optional_ids/enum.SampleEnum.html + + + + + + + + + + + SampleEnum - Stef + + + +
+
+

+ Enum + optional_ids::SampleEnum +

+
enum SampleEnum {
+    Named {
+        field1: u32 @1,
+        field2: u32 @100,
+        field3: u32 @101,
+    } @1,
+    Unit @50,
+    Unnamed(u32 @1, u32 @100, u32 @101) @51,
+}
+
+ +
+
+
+

Variants

+
    +
  • + + Named + @1 + +
    + +
    +
    +

    Fields

    +
      +
    • + + field1: + u32 + @1 + +
      + +
      +
      +

      Metadata

      +
      +

      The size range is:

      +
      +

      u32 1..5

      +
      +
      +
      +
    • +
    • + + field2: + u32 + @100 + +
      + +
      +
      +

      Metadata

      +
      +

      The size range is:

      +
      +

      u32 1..5

      +
      +
      +
      +
    • +
    • + + field3: + u32 + @101 + +
      + +
      +
      +

      Metadata

      +
      +

      The size range is:

      +
      +

      u32 1..5

      +
      +
      +
      +
    • +
    +
    +
  • +
  • + + Unit + @50 + +
    + +
    +
  • +
  • + + Unnamed + @51 + +
    + +
    +
    +

    Fields

    +
      +
    • + + n0: + u32 + @1 + +
      + +
      +
      +

      Metadata

      +
      +

      The size range is:

      +
      +

      u32 1..5

      +
      +
      +
      +
    • +
    • + + n1: + u32 + @100 + +
      + +
      +
      +

      Metadata

      +
      +

      The size range is:

      +
      +

      u32 1..5

      +
      +
      +
      +
    • +
    • + + n2: + u32 + @101 + +
      + +
      +
      +

      Metadata

      +
      +

      The size range is:

      +
      +

      u32 1..5

      +
      +
      +
      +
    • +
    +
    +
  • +
+
+ +
+ + diff --git a/crates/stef-go/tests/snapshots/render__render@optional_ids.stef.snap b/crates/stef-go/tests/snapshots/render__render@optional_ids.stef.snap new file mode 100644 index 0000000..aa969d1 --- /dev/null +++ b/crates/stef-go/tests/snapshots/render__render@optional_ids.stef.snap @@ -0,0 +1,537 @@ +--- +source: crates/stef-go/tests/render.rs +description: "struct SampleNamed {\n field1: u32,\n field2: u32 @100,\n field3: u32,\n}\n\nstruct SampleUnnamed(u32, u32 @100, u32)\n\nenum SampleEnum {\n Named {\n field1: u32,\n field2: u32 @100,\n field3: u32,\n },\n Unit @50,\n Unnamed(u32, u32 @100, u32),\n}" +input_file: crates/stef-parser/tests/inputs/optional_ids.stef +--- +--- sample.go + +// Code generated by stef-go (v0.1.0). DO NOT EDIT. + +import ( + "github.com/dnaka91/stef-go" + "github.com/dnaka91/stef-go/buf" +) + +package sample + +type SampleNamed struct { + Field1 uint32 + Field2 uint32 + Field3 uint32 +} + +func NewSampleNamed( + field1 uint32, + field2 uint32, + field3 uint32, +) SampleNamed { + return SampleNamed{ + Field1: field1, + Field2: field2, + Field3: field3, + } +} + +var _ buf.Encode = (*SampleNamed)(nil) + +func (v *SampleNamed) Encode(w []byte) []byte { + w = buf.EncodeField(w, 1, func (w []byte) []byte { + return buf.EncodeU32(w, v.Field1) + }) + w = buf.EncodeField(w, 100, func (w []byte) []byte { + return buf.EncodeU32(w, v.Field2) + }) + w = buf.EncodeField(w, 101, func (w []byte) []byte { + return buf.EncodeU32(w, v.Field3) + }) + w = buf.EncodeU32(w, buf.EndMarker) + return w +} + +var _ buf.Decode = (*SampleNamed)(nil) + +func (v *SampleNamed) Decode(r []byte) ([]byte, error) { + foundField1 := false + foundField2 := false + foundField3 := false + + for len(r) > 0 { + r2, id, err := buf.DecodeID(r) + if err != nil { + return nil, err + } + r = r2 + + switch id { + case 1: + r2, value, err := buf.DecodeU32(r) + if err != nil { + return nil, err + } + r = r2 + v.Field1 = value + foundField1 = true + case 100: + r2, value, err := buf.DecodeU32(r) + if err != nil { + return nil, err + } + r = r2 + v.Field2 = value + foundField2 = true + case 101: + r2, value, err := buf.DecodeU32(r) + if err != nil { + return nil, err + } + r = r2 + v.Field3 = value + foundField3 = true + case buf.EndMarker: + break + } + } + + if !foundField1 { + return nil, buf.MissingFieldError{ + ID: 1 + Field: "field1" + } + } + if !foundField2 { + return nil, buf.MissingFieldError{ + ID: 100 + Field: "field2" + } + } + if !foundField3 { + return nil, buf.MissingFieldError{ + ID: 101 + Field: "field3" + } + } + + return r, nil +} + +var _ buf.Size = (*SampleNamed)(nil) + +func (v *SampleNamed) Size() int { + size := 0 + size += buf.SizeField(1, func() int { + return buf.SizeU32(v.Field1) + }) + size += buf.SizeField(100, func() int { + return buf.SizeU32(v.Field2) + }) + size += buf.SizeField(101, func() int { + return buf.SizeU32(v.Field3) + }) + size += buf.SizeU32(buf.EndMarker) + return size +} + +type SampleUnnamed struct { + N0 uint32 + N1 uint32 + N2 uint32 +} + +func NewSampleUnnamed( + n0 uint32, + n1 uint32, + n2 uint32, +) SampleUnnamed { + return SampleUnnamed{ + N0: n0, + N1: n1, + N2: n2, + } +} + +var _ buf.Encode = (*SampleUnnamed)(nil) + +func (v *SampleUnnamed) Encode(w []byte) []byte { + w = buf.EncodeField(w, 1, func (w []byte) []byte { + return buf.EncodeU32(w, v.N0) + }) + w = buf.EncodeField(w, 100, func (w []byte) []byte { + return buf.EncodeU32(w, v.N1) + }) + w = buf.EncodeField(w, 101, func (w []byte) []byte { + return buf.EncodeU32(w, v.N2) + }) + w = buf.EncodeU32(w, buf.EndMarker) + return w +} + +var _ buf.Decode = (*SampleUnnamed)(nil) + +func (v *SampleUnnamed) Decode(r []byte) ([]byte, error) { + foundN0 := false + foundN1 := false + foundN2 := false + + for len(r) > 0 { + r2, id, err := buf.DecodeID(r) + if err != nil { + return nil, err + } + r = r2 + + switch id { + case 1: + r2, value, err := buf.DecodeU32(r) + if err != nil { + return nil, err + } + r = r2 + v.N0 = value + foundN0 = true + case 100: + r2, value, err := buf.DecodeU32(r) + if err != nil { + return nil, err + } + r = r2 + v.N1 = value + foundN1 = true + case 101: + r2, value, err := buf.DecodeU32(r) + if err != nil { + return nil, err + } + r = r2 + v.N2 = value + foundN2 = true + case buf.EndMarker: + break + } + } + + if !foundN0 { + return nil, buf.MissingFieldError{ + ID: 1 + Field: "" + } + } + if !foundN1 { + return nil, buf.MissingFieldError{ + ID: 100 + Field: "" + } + } + if !foundN2 { + return nil, buf.MissingFieldError{ + ID: 101 + Field: "" + } + } + + return r, nil +} + +var _ buf.Size = (*SampleUnnamed)(nil) + +func (v *SampleUnnamed) Size() int { + size := 0 + size += buf.SizeField(1, func() int { + return buf.SizeU32(v.N0) + }) + size += buf.SizeField(100, func() int { + return buf.SizeU32(v.N1) + }) + size += buf.SizeField(101, func() int { + return buf.SizeU32(v.N2) + }) + size += buf.SizeU32(buf.EndMarker) + return size +} + +type SampleEnumVariant interface { + sealed() +} + +type SampleEnum SampleEnumVariant + +type SampleEnum_Named struct { + Field1 uint32 + Field2 uint32 + Field3 uint32 +} + +func (v SampleEnum_Named) sealed() {} + +func NewSampleEnum_Named( + field1 uint32, + field2 uint32, + field3 uint32, +) SampleEnum_Named { + return SampleEnum_Named{ + Field1: field1, + Field2: field2, + Field3: field3, + } +} + +var _ buf.Encode = (*SampleEnum_Named)(nil) + +func (v *SampleEnum_Named) Encode(w []byte) []byte { + w = buf.EncodeField(w, 1, func (w []byte) []byte { + return buf.EncodeU32(w, v.Field1) + }) + w = buf.EncodeField(w, 100, func (w []byte) []byte { + return buf.EncodeU32(w, v.Field2) + }) + w = buf.EncodeField(w, 101, func (w []byte) []byte { + return buf.EncodeU32(w, v.Field3) + }) + w = buf.EncodeU32(w, buf.EndMarker) + return nil +} + +var _ buf.Decode = (*SampleEnum_Named)(nil) + +func (v *SampleEnum_Named) Decode(r []byte) ([]byte, error) { + foundField1 := false + foundField2 := false + foundField3 := false + + for len(r) > 0 { + r2, id, err := buf.DecodeID(r) + if err != nil { + return nil, err + } + r = r2 + + switch id { + case 1: + r2, value, err := buf.DecodeU32(r) + if err != nil { + return nil, err + } + r = r2 + v.Field1 = value + foundField1 = true + case 100: + r2, value, err := buf.DecodeU32(r) + if err != nil { + return nil, err + } + r = r2 + v.Field2 = value + foundField2 = true + case 101: + r2, value, err := buf.DecodeU32(r) + if err != nil { + return nil, err + } + r = r2 + v.Field3 = value + foundField3 = true + case buf.EndMarker: + break + } + } + + if !foundField1 { + return nil, buf.MissingFieldError{ + ID: 1 + Field: "field1" + } + } + if !foundField2 { + return nil, buf.MissingFieldError{ + ID: 100 + Field: "field2" + } + } + if !foundField3 { + return nil, buf.MissingFieldError{ + ID: 101 + Field: "field3" + } + } + + return r, nil +} + +var _ buf.Size = (*SampleEnum_Named)(nil) + +func (v *SampleEnum_Named) Size() int { + size := 0 + size += buf.SizeField(1, func() int { + return buf.SizeU32(v.Field1) + }) + size += buf.SizeField(100, func() int { + return buf.SizeU32(v.Field2) + }) + size += buf.SizeField(101, func() int { + return buf.SizeU32(v.Field3) + }) + size += buf.SizeU32(buf.EndMarker) + return size +} + +type SampleEnum_Unit struct{} + +func (v SampleEnum_Unit) sealed() {} + +func NewSampleEnum_Unit() SampleEnum_Unit { + return SampleEnum_Unit{} +} + +var _ buf.Encode = (*SampleEnum_Unit)(nil) + +func (v *SampleEnum_Unit) Encode(w []byte) []byte { + return nil +} + +var _ buf.Decode = (*SampleEnum_Unit)(nil) + +func (v *SampleEnum_Unit) Decode(r []byte) ([]byte, error) { + + for len(r) > 0 { + r2, id, err := buf.DecodeID(r) + if err != nil { + return nil, err + } + r = r2 + + switch id { + case buf.EndMarker: + break + } + } + + + return r, nil +} + +var _ buf.Size = (*SampleEnum_Unit)(nil) + +func (v *SampleEnum_Unit) Size() int { + size := 0 + return size +} + +type SampleEnum_Unnamed struct { + N0 uint32 + N1 uint32 + N2 uint32 +} + +func (v SampleEnum_Unnamed) sealed() {} + +func NewSampleEnum_Unnamed( + n0 uint32, + n1 uint32, + n2 uint32, +) SampleEnum_Unnamed { + return SampleEnum_Unnamed{ + N0: n0, + N1: n1, + N2: n2, + } +} + +var _ buf.Encode = (*SampleEnum_Unnamed)(nil) + +func (v *SampleEnum_Unnamed) Encode(w []byte) []byte { + w = buf.EncodeField(w, 1, func (w []byte) []byte { + return buf.EncodeU32(w, v.N0) + }) + w = buf.EncodeField(w, 100, func (w []byte) []byte { + return buf.EncodeU32(w, v.N1) + }) + w = buf.EncodeField(w, 101, func (w []byte) []byte { + return buf.EncodeU32(w, v.N2) + }) + w = buf.EncodeU32(w, buf.EndMarker) + return nil +} + +var _ buf.Decode = (*SampleEnum_Unnamed)(nil) + +func (v *SampleEnum_Unnamed) Decode(r []byte) ([]byte, error) { + foundN0 := false + foundN1 := false + foundN2 := false + + for len(r) > 0 { + r2, id, err := buf.DecodeID(r) + if err != nil { + return nil, err + } + r = r2 + + switch id { + case 1: + r2, value, err := buf.DecodeU32(r) + if err != nil { + return nil, err + } + r = r2 + v.N0 = value + foundN0 = true + case 100: + r2, value, err := buf.DecodeU32(r) + if err != nil { + return nil, err + } + r = r2 + v.N1 = value + foundN1 = true + case 101: + r2, value, err := buf.DecodeU32(r) + if err != nil { + return nil, err + } + r = r2 + v.N2 = value + foundN2 = true + case buf.EndMarker: + break + } + } + + if !foundN0 { + return nil, buf.MissingFieldError{ + ID: 1 + Field: "" + } + } + if !foundN1 { + return nil, buf.MissingFieldError{ + ID: 100 + Field: "" + } + } + if !foundN2 { + return nil, buf.MissingFieldError{ + ID: 101 + Field: "" + } + } + + return r, nil +} + +var _ buf.Size = (*SampleEnum_Unnamed)(nil) + +func (v *SampleEnum_Unnamed) Size() int { + size := 0 + size += buf.SizeField(1, func() int { + return buf.SizeU32(v.N0) + }) + size += buf.SizeField(100, func() int { + return buf.SizeU32(v.N1) + }) + size += buf.SizeField(101, func() int { + return buf.SizeU32(v.N2) + }) + size += buf.SizeU32(buf.EndMarker) + return size +} + + diff --git a/crates/stef-lsp/src/handlers/semantic_tokens.rs b/crates/stef-lsp/src/handlers/semantic_tokens.rs index eaa30ef..10b7d22 100644 --- a/crates/stef-lsp/src/handlers/semantic_tokens.rs +++ b/crates/stef-lsp/src/handlers/semantic_tokens.rs @@ -4,8 +4,8 @@ use anyhow::{ensure, Context, Result}; use line_index::{LineIndex, TextSize, WideLineCol}; use lsp_types::{SemanticToken, SemanticTokenModifier, SemanticTokenType}; use stef_parser::{ - Comment, Const, DataType, Definition, Enum, Fields, Generics, Literal, LiteralValue, Module, - NamedField, Schema, Span, Spanned, Struct, Type, TypeAlias, UnnamedField, Variant, + Comment, Const, DataType, Definition, Enum, Fields, Generics, Id, Literal, LiteralValue, + Module, NamedField, Schema, Span, Spanned, Struct, Type, TypeAlias, UnnamedField, Variant, }; pub(crate) use self::{modifiers::TOKEN_MODIFIERS, types::TOKEN_TYPES}; @@ -55,6 +55,7 @@ define_semantic_token_types! { custom { (BOOLEAN, "boolean"), (BUILTIN_TYPE, "builtinType"), + (IDENTIFIER, "identifier"), (TYPE_ALIAS, "typeAlias"), } } @@ -116,7 +117,7 @@ fn token_modifier_bitset(modifiers: &[SemanticTokenModifier]) -> u32 { pub struct Visitor<'a> { index: &'a LineIndex, tokens: Vec, - delta: (u32, u32), + delta: WideLineCol, } impl<'a> Visitor<'a> { @@ -124,7 +125,7 @@ impl<'a> Visitor<'a> { Self { index, tokens: Vec::new(), - delta: (0, 0), + delta: WideLineCol { line: 0, col: 0 }, } } @@ -150,10 +151,10 @@ impl<'a> Visitor<'a> { fn lsl(&self, start: WideLineCol, end: WideLineCol) -> (u32, u32, u32) { ( - start.line - self.delta.0, + start.line - self.delta.line, start.col - - if self.delta.0 == start.line { - self.delta.1 + - if self.delta.line == start.line { + self.delta.col } else { 0 }, @@ -177,7 +178,10 @@ impl<'a> Visitor<'a> { token_type: token_type_pos(token_type), token_modifiers_bitset: token_modifier_bitset(token_modifiers), }); - self.delta = (start.line, start.col); + self.delta = WideLineCol { + line: start.line, + col: start.col, + }; Ok(()) } @@ -242,7 +246,8 @@ impl<'a> Visitor<'a> { fn visit_variant(&mut self, item: &Variant<'_>) -> Result<()> { self.visit_comment(&item.comment)?; self.add_span(&item.name, &types::ENUM_MEMBER, &[modifiers::DECLARATION])?; - self.visit_fields(&item.fields) + self.visit_fields(&item.fields)?; + self.visit_id(&item.id) } fn visit_fields(&mut self, item: &Fields<'_>) -> Result<()> { @@ -266,11 +271,21 @@ impl<'a> Visitor<'a> { fn visit_named_field(&mut self, item: &NamedField<'_>) -> Result<()> { self.visit_comment(&item.comment)?; self.add_span(&item.name, &types::PROPERTY, &[modifiers::DECLARATION])?; - self.visit_type(&item.ty) + self.visit_type(&item.ty)?; + self.visit_id(&item.id) } fn visit_unnamed_field(&mut self, item: &UnnamedField<'_>) -> Result<()> { - self.visit_type(&item.ty) + self.visit_type(&item.ty)?; + self.visit_id(&item.id) + } + + fn visit_id(&mut self, item: &Option) -> Result<()> { + if let Some(id) = item { + self.add_span(id, &types::IDENTIFIER, &[])?; + } + + Ok(()) } fn visit_alias(&mut self, item: &TypeAlias<'_>) -> Result<()> { diff --git a/crates/stef-parser/src/lib.rs b/crates/stef-parser/src/lib.rs index 0e00c37..e53ab6c 100644 --- a/crates/stef-parser/src/lib.rs +++ b/crates/stef-parser/src/lib.rs @@ -394,7 +394,7 @@ pub struct Variant<'a> { /// Fields of this variant, if any. pub fields: Fields<'a>, /// Identifier for this variant, that must be unique within the current enum. - pub id: Id, + pub id: Option, /// Source code location. span: Span, } @@ -413,9 +413,12 @@ impl Print for Variant<'_> { Self::indent(f, level)?; f.write_str(name.get())?; - fields.print(f, level)?; - write!(f, " {id},") + if let Some(id) = id { + write!(f, " {id},") + } else { + write!(f, ",") + } } } @@ -545,7 +548,7 @@ pub struct NamedField<'a> { /// Data type that defines the shape of the contained data. pub ty: Type<'a>, /// Identifier for this field, that must be unique within the current element. - pub id: Id, + pub id: Option, /// Source code location. span: Span, } @@ -563,7 +566,12 @@ impl Print for NamedField<'_> { comment.print(f, level)?; Self::indent(f, level)?; - write!(f, "{name}: {ty} {id}") + + if let Some(id) = id { + write!(f, "{name}: {ty} {id}") + } else { + write!(f, "{name}: {ty}") + } } } @@ -592,7 +600,7 @@ pub struct UnnamedField<'a> { /// Data type that defines the shape of the contained data. pub ty: Type<'a>, /// Identifier for this field, that must be unique within the current element. - pub id: Id, + pub id: Option, /// Source code location. span: Span, } @@ -606,7 +614,11 @@ impl Spanned for UnnamedField<'_> { impl Display for UnnamedField<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let Self { ty, id, span: _ } = self; - write!(f, "{ty} {id}") + if let Some(id) = id { + write!(f, "{ty} {id}") + } else { + write!(f, "{ty}") + } } } diff --git a/crates/stef-parser/src/parser/enums.rs b/crates/stef-parser/src/parser/enums.rs index 3993f43..3b86619 100644 --- a/crates/stef-parser/src/parser/enums.rs +++ b/crates/stef-parser/src/parser/enums.rs @@ -139,7 +139,7 @@ fn parse_variant<'i>(input: &mut Input<'i>) -> Result, Cause> { ( preceded(space0, parse_variant_name.with_span()), preceded(space0, fields::parse.map_err(Cause::from)), - preceded(space0, ids::parse.map_err(Cause::from)), + opt(preceded(space0, ids::parse.map_err(Cause::from))), ) .with_span(), ) diff --git a/crates/stef-parser/src/parser/fields.rs b/crates/stef-parser/src/parser/fields.rs index 39599f3..7b16936 100644 --- a/crates/stef-parser/src/parser/fields.rs +++ b/crates/stef-parser/src/parser/fields.rs @@ -115,7 +115,7 @@ fn parse_unit(input: &mut Input<'_>) -> Result<(), Cause> { fn parse_unnamed_field<'i>(input: &mut Input<'i>) -> Result, Cause> { ( ws(types::parse.map_err(Cause::from)), - preceded(space0, cut_err(ids::parse.map_err(Cause::from))), + opt(preceded(space0, ids::parse.map_err(Cause::from))), ) .with_span() .parse_next(input) @@ -132,7 +132,7 @@ fn parse_named_field<'i>(input: &mut Input<'i>) -> Result, Cause> ( delimited(space0, parse_field_name, ':'), preceded(space0, types::parse.map_err(Cause::from)), - preceded(space0, ids::parse.map_err(Cause::from)), + opt(preceded(space0, ids::parse.map_err(Cause::from))), ) .with_span(), ) diff --git a/crates/stef-parser/tests/inputs/optional_ids.stef b/crates/stef-parser/tests/inputs/optional_ids.stef new file mode 100644 index 0000000..e21ff50 --- /dev/null +++ b/crates/stef-parser/tests/inputs/optional_ids.stef @@ -0,0 +1,17 @@ +struct SampleNamed { + field1: u32, + field2: u32 @100, + field3: u32, +} + +struct SampleUnnamed(u32, u32 @100, u32) + +enum SampleEnum { + Named { + field1: u32, + field2: u32 @100, + field3: u32, + }, + Unit @50, + Unnamed(u32, u32 @100, u32), +} diff --git a/crates/stef-parser/tests/snapshots/parser__parse@enum_basic.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@enum_basic.stef.snap index f80cef5..c9dfe1b 100644 --- a/crates/stef-parser/tests/snapshots/parser__parse@enum_basic.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__parse@enum_basic.stef.snap @@ -39,9 +39,11 @@ Schema { value: "One", }, fields: Unit, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, Variant { comment: Comment( @@ -60,23 +62,29 @@ Schema { ty: Type { value: U32, }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, UnnamedField { ty: Type { value: U64, }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, ], ), - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, Variant { comment: Comment( @@ -97,9 +105,11 @@ Schema { ty: Type { value: U32, }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, NamedField { comment: Comment( @@ -115,15 +125,19 @@ Schema { ty: Type { value: Bool, }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, ], ), - id: Id { - value: 3, - }, + id: Some( + Id { + value: 3, + }, + ), }, ], }, diff --git a/crates/stef-parser/tests/snapshots/parser__parse@enum_generics.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@enum_generics.stef.snap index ddba1d1..f65f90e 100644 --- a/crates/stef-parser/tests/snapshots/parser__parse@enum_generics.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__parse@enum_generics.stef.snap @@ -52,9 +52,11 @@ Schema { value: "One", }, fields: Unit, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, Variant { comment: Comment( @@ -77,9 +79,11 @@ Schema { }, ), }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, UnnamedField { ty: Type { @@ -93,15 +97,19 @@ Schema { }, ), }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, ], ), - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, Variant { comment: Comment( @@ -130,9 +138,11 @@ Schema { }, ), }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, NamedField { comment: Comment( @@ -152,15 +162,19 @@ Schema { }, ), }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, ], ), - id: Id { - value: 3, - }, + id: Some( + Id { + value: 3, + }, + ), }, ], }, diff --git a/crates/stef-parser/tests/snapshots/parser__parse@enum_many_ws.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@enum_many_ws.stef.snap index 0efb6bc..562cf46 100644 --- a/crates/stef-parser/tests/snapshots/parser__parse@enum_many_ws.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__parse@enum_many_ws.stef.snap @@ -39,9 +39,11 @@ Schema { value: "One", }, fields: Unit, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, Variant { comment: Comment( @@ -56,23 +58,29 @@ Schema { ty: Type { value: U32, }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, UnnamedField { ty: Type { value: U64, }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, ], ), - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, Variant { comment: Comment( @@ -93,9 +101,11 @@ Schema { ty: Type { value: U32, }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, NamedField { comment: Comment( @@ -107,15 +117,19 @@ Schema { ty: Type { value: Bool, }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, ], ), - id: Id { - value: 3, - }, + id: Some( + Id { + value: 3, + }, + ), }, ], }, diff --git a/crates/stef-parser/tests/snapshots/parser__parse@enum_min_ws.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@enum_min_ws.stef.snap index 359cb37..d6271c5 100644 --- a/crates/stef-parser/tests/snapshots/parser__parse@enum_min_ws.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__parse@enum_min_ws.stef.snap @@ -39,9 +39,11 @@ Schema { value: "One", }, fields: Unit, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, Variant { comment: Comment( @@ -56,17 +58,21 @@ Schema { ty: Type { value: U32, }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, UnnamedField { ty: Type { value: U64, }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, UnnamedField { ty: Type { @@ -80,15 +86,19 @@ Schema { }, ), }, - id: Id { - value: 3, - }, + id: Some( + Id { + value: 3, + }, + ), }, ], ), - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, Variant { comment: Comment( @@ -109,9 +119,11 @@ Schema { ty: Type { value: U32, }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, NamedField { comment: Comment( @@ -123,9 +135,11 @@ Schema { ty: Type { value: Bool, }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, NamedField { comment: Comment( @@ -145,15 +159,19 @@ Schema { }, ), }, - id: Id { - value: 3, - }, + id: Some( + Id { + value: 3, + }, + ), }, ], ), - id: Id { - value: 3, - }, + id: Some( + Id { + value: 3, + }, + ), }, ], }, diff --git a/crates/stef-parser/tests/snapshots/parser__parse@mixed.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@mixed.stef.snap index 6f7fe20..67be1d2 100644 --- a/crates/stef-parser/tests/snapshots/parser__parse@mixed.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__parse@mixed.stef.snap @@ -63,9 +63,11 @@ Schema { }, ), }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, NamedField { comment: Comment( @@ -93,9 +95,11 @@ Schema { }, ), }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, NamedField { comment: Comment( @@ -107,9 +111,11 @@ Schema { ty: Type { value: U8, }, - id: Id { - value: 3, - }, + id: Some( + Id { + value: 3, + }, + ), }, NamedField { comment: Comment( @@ -133,9 +139,11 @@ Schema { }, ), }, - id: Id { - value: 4, - }, + id: Some( + Id { + value: 4, + }, + ), }, ], ), @@ -171,9 +179,11 @@ Schema { ty: Type { value: String, }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, NamedField { comment: Comment( @@ -189,9 +199,11 @@ Schema { }, ), }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, NamedField { comment: Comment( @@ -203,9 +215,11 @@ Schema { ty: Type { value: String, }, - id: Id { - value: 3, - }, + id: Some( + Id { + value: 3, + }, + ), }, ], ), @@ -278,9 +292,11 @@ Schema { ty: Type { value: String, }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, NamedField { comment: Comment( @@ -304,9 +320,11 @@ Schema { }, ), }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, NamedField { comment: Comment( @@ -318,9 +336,11 @@ Schema { ty: Type { value: String, }, - id: Id { - value: 3, - }, + id: Some( + Id { + value: 3, + }, + ), }, ], ), @@ -368,15 +388,19 @@ Schema { ty: Type { value: U16, }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, ], ), - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, Variant { comment: Comment( @@ -395,15 +419,19 @@ Schema { ty: Type { value: String, }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, ], ), - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, ], }, @@ -481,9 +509,11 @@ Schema { ty: Type { value: U16, }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, NamedField { comment: Comment( @@ -503,9 +533,11 @@ Schema { }, ), }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, NamedField { comment: Comment( @@ -517,15 +549,19 @@ Schema { ty: Type { value: U8, }, - id: Id { - value: 3, - }, + id: Some( + Id { + value: 3, + }, + ), }, ], ), - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, Variant { comment: Comment( @@ -561,15 +597,19 @@ Schema { }, ), }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, ], ), - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, Variant { comment: Comment( @@ -583,9 +623,11 @@ Schema { value: "Unknown", }, fields: Unit, - id: Id { - value: 3, - }, + id: Some( + Id { + value: 3, + }, + ), }, ], }, @@ -664,9 +706,11 @@ Schema { value: "January", }, fields: Unit, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, Variant { comment: Comment( @@ -676,9 +720,11 @@ Schema { value: "February", }, fields: Unit, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, Variant { comment: Comment( @@ -688,9 +734,11 @@ Schema { value: "March", }, fields: Unit, - id: Id { - value: 3, - }, + id: Some( + Id { + value: 3, + }, + ), }, Variant { comment: Comment( @@ -700,9 +748,11 @@ Schema { value: "April", }, fields: Unit, - id: Id { - value: 4, - }, + id: Some( + Id { + value: 4, + }, + ), }, Variant { comment: Comment( @@ -712,9 +762,11 @@ Schema { value: "May", }, fields: Unit, - id: Id { - value: 5, - }, + id: Some( + Id { + value: 5, + }, + ), }, Variant { comment: Comment( @@ -724,9 +776,11 @@ Schema { value: "June", }, fields: Unit, - id: Id { - value: 6, - }, + id: Some( + Id { + value: 6, + }, + ), }, Variant { comment: Comment( @@ -736,9 +790,11 @@ Schema { value: "July", }, fields: Unit, - id: Id { - value: 7, - }, + id: Some( + Id { + value: 7, + }, + ), }, Variant { comment: Comment( @@ -748,9 +804,11 @@ Schema { value: "August", }, fields: Unit, - id: Id { - value: 8, - }, + id: Some( + Id { + value: 8, + }, + ), }, Variant { comment: Comment( @@ -760,9 +818,11 @@ Schema { value: "September", }, fields: Unit, - id: Id { - value: 9, - }, + id: Some( + Id { + value: 9, + }, + ), }, Variant { comment: Comment( @@ -772,9 +832,11 @@ Schema { value: "October", }, fields: Unit, - id: Id { - value: 10, - }, + id: Some( + Id { + value: 10, + }, + ), }, Variant { comment: Comment( @@ -784,9 +846,11 @@ Schema { value: "November", }, fields: Unit, - id: Id { - value: 11, - }, + id: Some( + Id { + value: 11, + }, + ), }, Variant { comment: Comment( @@ -796,9 +860,11 @@ Schema { value: "December", }, fields: Unit, - id: Id { - value: 12, - }, + id: Some( + Id { + value: 12, + }, + ), }, ], }, diff --git a/crates/stef-parser/tests/snapshots/parser__parse@module_basic.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@module_basic.stef.snap index a512a9c..6adf826 100644 --- a/crates/stef-parser/tests/snapshots/parser__parse@module_basic.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__parse@module_basic.stef.snap @@ -57,9 +57,11 @@ Schema { value: "One", }, fields: Unit, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, ], }, @@ -93,9 +95,11 @@ Schema { ty: Type { value: U32, }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, NamedField { comment: Comment( @@ -119,9 +123,11 @@ Schema { }, ), }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, ], ), diff --git a/crates/stef-parser/tests/snapshots/parser__parse@optional_ids.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@optional_ids.stef.snap new file mode 100644 index 0000000..486c56d --- /dev/null +++ b/crates/stef-parser/tests/snapshots/parser__parse@optional_ids.stef.snap @@ -0,0 +1,238 @@ +--- +source: crates/stef-parser/tests/parser.rs +description: "struct SampleNamed {\n field1: u32,\n field2: u32 @100,\n field3: u32,\n}\n\nstruct SampleUnnamed(u32, u32 @100, u32)\n\nenum SampleEnum {\n Named {\n field1: u32,\n field2: u32 @100,\n field3: u32,\n },\n Unit @50,\n Unnamed(u32, u32 @100, u32),\n}" +input_file: crates/stef-parser/tests/inputs/optional_ids.stef +--- +Schema { + path: Some( + "optional_ids.stef", + ), + source: "struct SampleNamed {\n field1: u32,\n field2: u32 @100,\n field3: u32,\n}\n\nstruct SampleUnnamed(u32, u32 @100, u32)\n\nenum SampleEnum {\n Named {\n field1: u32,\n field2: u32 @100,\n field3: u32,\n },\n Unit @50,\n Unnamed(u32, u32 @100, u32),\n}\n", + comment: Comment( + [], + ), + definitions: [ + Struct( + Struct { + comment: Comment( + [], + ), + attributes: Attributes( + [], + ), + name: Name { + value: "SampleNamed", + }, + generics: Generics( + [], + ), + fields: Named( + [ + NamedField { + comment: Comment( + [], + ), + name: Name { + value: "field1", + }, + ty: Type { + value: U32, + }, + id: None, + }, + NamedField { + comment: Comment( + [], + ), + name: Name { + value: "field2", + }, + ty: Type { + value: U32, + }, + id: Some( + Id { + value: 100, + }, + ), + }, + NamedField { + comment: Comment( + [], + ), + name: Name { + value: "field3", + }, + ty: Type { + value: U32, + }, + id: None, + }, + ], + ), + }, + ), + Struct( + Struct { + comment: Comment( + [], + ), + attributes: Attributes( + [], + ), + name: Name { + value: "SampleUnnamed", + }, + generics: Generics( + [], + ), + fields: Unnamed( + [ + UnnamedField { + ty: Type { + value: U32, + }, + id: None, + }, + UnnamedField { + ty: Type { + value: U32, + }, + id: Some( + Id { + value: 100, + }, + ), + }, + UnnamedField { + ty: Type { + value: U32, + }, + id: None, + }, + ], + ), + }, + ), + Enum( + Enum { + comment: Comment( + [], + ), + attributes: Attributes( + [], + ), + name: Name { + value: "SampleEnum", + }, + generics: Generics( + [], + ), + variants: [ + Variant { + comment: Comment( + [], + ), + name: Name { + value: "Named", + }, + fields: Named( + [ + NamedField { + comment: Comment( + [], + ), + name: Name { + value: "field1", + }, + ty: Type { + value: U32, + }, + id: None, + }, + NamedField { + comment: Comment( + [], + ), + name: Name { + value: "field2", + }, + ty: Type { + value: U32, + }, + id: Some( + Id { + value: 100, + }, + ), + }, + NamedField { + comment: Comment( + [], + ), + name: Name { + value: "field3", + }, + ty: Type { + value: U32, + }, + id: None, + }, + ], + ), + id: None, + }, + Variant { + comment: Comment( + [], + ), + name: Name { + value: "Unit", + }, + fields: Unit, + id: Some( + Id { + value: 50, + }, + ), + }, + Variant { + comment: Comment( + [], + ), + name: Name { + value: "Unnamed", + }, + fields: Unnamed( + [ + UnnamedField { + ty: Type { + value: U32, + }, + id: None, + }, + UnnamedField { + ty: Type { + value: U32, + }, + id: Some( + Id { + value: 100, + }, + ), + }, + UnnamedField { + ty: Type { + value: U32, + }, + id: None, + }, + ], + ), + id: None, + }, + ], + }, + ), + ], +} diff --git a/crates/stef-parser/tests/snapshots/parser__parse@schema_basic.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@schema_basic.stef.snap index 91d7de4..f0c43f5 100644 --- a/crates/stef-parser/tests/snapshots/parser__parse@schema_basic.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__parse@schema_basic.stef.snap @@ -42,9 +42,11 @@ Schema { ty: Type { value: U32, }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, NamedField { comment: Comment( @@ -56,9 +58,11 @@ Schema { ty: Type { value: Bool, }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, ], ), @@ -91,9 +95,11 @@ Schema { value: "One", }, fields: Unit, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, Variant { comment: Comment( @@ -108,23 +114,29 @@ Schema { ty: Type { value: U32, }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, UnnamedField { ty: Type { value: U64, }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, ], ), - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, Variant { comment: Comment( @@ -145,9 +157,11 @@ Schema { ty: Type { value: U32, }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, NamedField { comment: Comment( @@ -159,15 +173,19 @@ Schema { ty: Type { value: Bool, }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, ], ), - id: Id { - value: 3, - }, + id: Some( + Id { + value: 3, + }, + ), }, ], }, diff --git a/crates/stef-parser/tests/snapshots/parser__parse@struct_basic.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@struct_basic.stef.snap index c52c264..b58a92d 100644 --- a/crates/stef-parser/tests/snapshots/parser__parse@struct_basic.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__parse@struct_basic.stef.snap @@ -42,9 +42,11 @@ Schema { ty: Type { value: U32, }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, NamedField { comment: Comment( @@ -60,9 +62,11 @@ Schema { ty: Type { value: Bool, }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, ], ), diff --git a/crates/stef-parser/tests/snapshots/parser__parse@struct_generics.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@struct_generics.stef.snap index c6cd888..0e176ab 100644 --- a/crates/stef-parser/tests/snapshots/parser__parse@struct_generics.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__parse@struct_generics.stef.snap @@ -57,9 +57,11 @@ Schema { }, ), }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, NamedField { comment: Comment( @@ -79,9 +81,11 @@ Schema { }, ), }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, ], ), diff --git a/crates/stef-parser/tests/snapshots/parser__parse@struct_many_ws.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@struct_many_ws.stef.snap index ae16364..47aed91 100644 --- a/crates/stef-parser/tests/snapshots/parser__parse@struct_many_ws.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__parse@struct_many_ws.stef.snap @@ -46,9 +46,11 @@ Schema { ty: Type { value: U32, }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, NamedField { comment: Comment( @@ -60,9 +62,11 @@ Schema { ty: Type { value: Bool, }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, NamedField { comment: Comment( @@ -82,9 +86,11 @@ Schema { }, ), }, - id: Id { - value: 3, - }, + id: Some( + Id { + value: 3, + }, + ), }, ], ), diff --git a/crates/stef-parser/tests/snapshots/parser__parse@struct_min_ws.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@struct_min_ws.stef.snap index 35b9396..47a8394 100644 --- a/crates/stef-parser/tests/snapshots/parser__parse@struct_min_ws.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__parse@struct_min_ws.stef.snap @@ -42,9 +42,11 @@ Schema { ty: Type { value: U32, }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, NamedField { comment: Comment( @@ -56,9 +58,11 @@ Schema { ty: Type { value: Bool, }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, NamedField { comment: Comment( @@ -78,9 +82,11 @@ Schema { }, ), }, - id: Id { - value: 3, - }, + id: Some( + Id { + value: 3, + }, + ), }, ], ), diff --git a/crates/stef-parser/tests/snapshots/parser__parse@struct_tuple.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@struct_tuple.stef.snap index c0ff80f..17535fb 100644 --- a/crates/stef-parser/tests/snapshots/parser__parse@struct_tuple.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__parse@struct_tuple.stef.snap @@ -36,17 +36,21 @@ Schema { ty: Type { value: U32, }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, UnnamedField { ty: Type { value: Bool, }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, ], ), diff --git a/crates/stef-parser/tests/snapshots/parser__parse@types_basic.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@types_basic.stef.snap index 340b6e7..adc64dd 100644 --- a/crates/stef-parser/tests/snapshots/parser__parse@types_basic.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__parse@types_basic.stef.snap @@ -38,9 +38,11 @@ Schema { ty: Type { value: Bool, }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, NamedField { comment: Comment( @@ -52,9 +54,11 @@ Schema { ty: Type { value: U8, }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, NamedField { comment: Comment( @@ -66,9 +70,11 @@ Schema { ty: Type { value: U16, }, - id: Id { - value: 3, - }, + id: Some( + Id { + value: 3, + }, + ), }, NamedField { comment: Comment( @@ -80,9 +86,11 @@ Schema { ty: Type { value: U32, }, - id: Id { - value: 4, - }, + id: Some( + Id { + value: 4, + }, + ), }, NamedField { comment: Comment( @@ -94,9 +102,11 @@ Schema { ty: Type { value: U64, }, - id: Id { - value: 5, - }, + id: Some( + Id { + value: 5, + }, + ), }, NamedField { comment: Comment( @@ -108,9 +118,11 @@ Schema { ty: Type { value: U128, }, - id: Id { - value: 6, - }, + id: Some( + Id { + value: 6, + }, + ), }, NamedField { comment: Comment( @@ -122,9 +134,11 @@ Schema { ty: Type { value: I8, }, - id: Id { - value: 7, - }, + id: Some( + Id { + value: 7, + }, + ), }, NamedField { comment: Comment( @@ -136,9 +150,11 @@ Schema { ty: Type { value: I16, }, - id: Id { - value: 8, - }, + id: Some( + Id { + value: 8, + }, + ), }, NamedField { comment: Comment( @@ -150,9 +166,11 @@ Schema { ty: Type { value: I32, }, - id: Id { - value: 9, - }, + id: Some( + Id { + value: 9, + }, + ), }, NamedField { comment: Comment( @@ -164,9 +182,11 @@ Schema { ty: Type { value: I64, }, - id: Id { - value: 10, - }, + id: Some( + Id { + value: 10, + }, + ), }, NamedField { comment: Comment( @@ -178,9 +198,11 @@ Schema { ty: Type { value: I128, }, - id: Id { - value: 11, - }, + id: Some( + Id { + value: 11, + }, + ), }, NamedField { comment: Comment( @@ -192,9 +214,11 @@ Schema { ty: Type { value: F32, }, - id: Id { - value: 12, - }, + id: Some( + Id { + value: 12, + }, + ), }, NamedField { comment: Comment( @@ -206,9 +230,11 @@ Schema { ty: Type { value: F64, }, - id: Id { - value: 13, - }, + id: Some( + Id { + value: 13, + }, + ), }, NamedField { comment: Comment( @@ -220,9 +246,11 @@ Schema { ty: Type { value: String, }, - id: Id { - value: 14, - }, + id: Some( + Id { + value: 14, + }, + ), }, NamedField { comment: Comment( @@ -234,9 +262,11 @@ Schema { ty: Type { value: StringRef, }, - id: Id { - value: 15, - }, + id: Some( + Id { + value: 15, + }, + ), }, NamedField { comment: Comment( @@ -248,9 +278,11 @@ Schema { ty: Type { value: Bytes, }, - id: Id { - value: 16, - }, + id: Some( + Id { + value: 16, + }, + ), }, NamedField { comment: Comment( @@ -262,9 +294,11 @@ Schema { ty: Type { value: BytesRef, }, - id: Id { - value: 17, - }, + id: Some( + Id { + value: 17, + }, + ), }, NamedField { comment: Comment( @@ -276,9 +310,11 @@ Schema { ty: Type { value: BoxString, }, - id: Id { - value: 18, - }, + id: Some( + Id { + value: 18, + }, + ), }, NamedField { comment: Comment( @@ -290,9 +326,11 @@ Schema { ty: Type { value: BoxBytes, }, - id: Id { - value: 19, - }, + id: Some( + Id { + value: 19, + }, + ), }, NamedField { comment: Comment( @@ -316,9 +354,11 @@ Schema { ], ), }, - id: Id { - value: 20, - }, + id: Some( + Id { + value: 20, + }, + ), }, NamedField { comment: Comment( @@ -335,9 +375,11 @@ Schema { 12, ), }, - id: Id { - value: 21, - }, + id: Some( + Id { + value: 21, + }, + ), }, ], ), diff --git a/crates/stef-parser/tests/snapshots/parser__parse@types_generic.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@types_generic.stef.snap index 9ff87e4..39064db 100644 --- a/crates/stef-parser/tests/snapshots/parser__parse@types_generic.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__parse@types_generic.stef.snap @@ -42,9 +42,11 @@ Schema { }, ), }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, NamedField { comment: Comment( @@ -65,9 +67,11 @@ Schema { ), ), }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, NamedField { comment: Comment( @@ -83,9 +87,11 @@ Schema { }, ), }, - id: Id { - value: 3, - }, + id: Some( + Id { + value: 3, + }, + ), }, NamedField { comment: Comment( @@ -101,9 +107,11 @@ Schema { }, ), }, - id: Id { - value: 4, - }, + id: Some( + Id { + value: 4, + }, + ), }, NamedField { comment: Comment( @@ -119,9 +127,11 @@ Schema { }, ), }, - id: Id { - value: 5, - }, + id: Some( + Id { + value: 5, + }, + ), }, ], ), @@ -151,9 +161,11 @@ Schema { }, ), }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, UnnamedField { ty: Type { @@ -168,9 +180,11 @@ Schema { ), ), }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, UnnamedField { ty: Type { @@ -180,9 +194,11 @@ Schema { }, ), }, - id: Id { - value: 3, - }, + id: Some( + Id { + value: 3, + }, + ), }, UnnamedField { ty: Type { @@ -192,9 +208,11 @@ Schema { }, ), }, - id: Id { - value: 4, - }, + id: Some( + Id { + value: 4, + }, + ), }, UnnamedField { ty: Type { @@ -204,9 +222,11 @@ Schema { }, ), }, - id: Id { - value: 5, - }, + id: Some( + Id { + value: 5, + }, + ), }, ], ), diff --git a/crates/stef-parser/tests/snapshots/parser__parse@types_nested.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@types_nested.stef.snap index d8d8741..0020717 100644 --- a/crates/stef-parser/tests/snapshots/parser__parse@types_nested.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__parse@types_nested.stef.snap @@ -59,9 +59,11 @@ Schema { }, ), }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, ], ), diff --git a/crates/stef-parser/tests/snapshots/parser__parse@types_non_zero.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@types_non_zero.stef.snap index b4778e0..6b1dee7 100644 --- a/crates/stef-parser/tests/snapshots/parser__parse@types_non_zero.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__parse@types_non_zero.stef.snap @@ -42,9 +42,11 @@ Schema { }, ), }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, NamedField { comment: Comment( @@ -60,9 +62,11 @@ Schema { }, ), }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, NamedField { comment: Comment( @@ -78,9 +82,11 @@ Schema { }, ), }, - id: Id { - value: 3, - }, + id: Some( + Id { + value: 3, + }, + ), }, NamedField { comment: Comment( @@ -96,9 +102,11 @@ Schema { }, ), }, - id: Id { - value: 4, - }, + id: Some( + Id { + value: 4, + }, + ), }, NamedField { comment: Comment( @@ -114,9 +122,11 @@ Schema { }, ), }, - id: Id { - value: 5, - }, + id: Some( + Id { + value: 5, + }, + ), }, NamedField { comment: Comment( @@ -132,9 +142,11 @@ Schema { }, ), }, - id: Id { - value: 6, - }, + id: Some( + Id { + value: 6, + }, + ), }, NamedField { comment: Comment( @@ -150,9 +162,11 @@ Schema { }, ), }, - id: Id { - value: 7, - }, + id: Some( + Id { + value: 7, + }, + ), }, NamedField { comment: Comment( @@ -168,9 +182,11 @@ Schema { }, ), }, - id: Id { - value: 8, - }, + id: Some( + Id { + value: 8, + }, + ), }, NamedField { comment: Comment( @@ -186,9 +202,11 @@ Schema { }, ), }, - id: Id { - value: 9, - }, + id: Some( + Id { + value: 9, + }, + ), }, NamedField { comment: Comment( @@ -204,9 +222,11 @@ Schema { }, ), }, - id: Id { - value: 10, - }, + id: Some( + Id { + value: 10, + }, + ), }, NamedField { comment: Comment( @@ -222,9 +242,11 @@ Schema { }, ), }, - id: Id { - value: 11, - }, + id: Some( + Id { + value: 11, + }, + ), }, NamedField { comment: Comment( @@ -240,9 +262,11 @@ Schema { }, ), }, - id: Id { - value: 12, - }, + id: Some( + Id { + value: 12, + }, + ), }, NamedField { comment: Comment( @@ -262,9 +286,11 @@ Schema { }, ), }, - id: Id { - value: 13, - }, + id: Some( + Id { + value: 13, + }, + ), }, NamedField { comment: Comment( @@ -289,9 +315,11 @@ Schema { }, ), }, - id: Id { - value: 14, - }, + id: Some( + Id { + value: 14, + }, + ), }, NamedField { comment: Comment( @@ -311,9 +339,11 @@ Schema { }, ), }, - id: Id { - value: 15, - }, + id: Some( + Id { + value: 15, + }, + ), }, ], ), diff --git a/crates/stef-parser/tests/snapshots/parser__parse@types_ref.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@types_ref.stef.snap index b92f04b..034b4a4 100644 --- a/crates/stef-parser/tests/snapshots/parser__parse@types_ref.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__parse@types_ref.stef.snap @@ -46,9 +46,11 @@ Schema { }, ), }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, NamedField { comment: Comment( @@ -75,9 +77,11 @@ Schema { }, ), }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, ], ), @@ -106,9 +110,11 @@ Schema { value: "Value", }, fields: Unit, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, ], }, @@ -154,9 +160,11 @@ Schema { }, ), }, - id: Id { - value: 1, - }, + id: Some( + Id { + value: 1, + }, + ), }, NamedField { comment: Comment( @@ -176,9 +184,11 @@ Schema { }, ), }, - id: Id { - value: 2, - }, + id: Some( + Id { + value: 2, + }, + ), }, ], ), diff --git a/crates/stef-parser/tests/snapshots/parser__print@optional_ids.stef.snap b/crates/stef-parser/tests/snapshots/parser__print@optional_ids.stef.snap new file mode 100644 index 0000000..eb98e36 --- /dev/null +++ b/crates/stef-parser/tests/snapshots/parser__print@optional_ids.stef.snap @@ -0,0 +1,24 @@ +--- +source: crates/stef-parser/tests/parser.rs +description: "struct SampleNamed {\n field1: u32,\n field2: u32 @100,\n field3: u32,\n}\n\nstruct SampleUnnamed(u32, u32 @100, u32)\n\nenum SampleEnum {\n Named {\n field1: u32,\n field2: u32 @100,\n field3: u32,\n },\n Unit @50,\n Unnamed(u32, u32 @100, u32),\n}" +input_file: crates/stef-parser/tests/inputs/optional_ids.stef +--- +struct SampleNamed { + field1: u32, + field2: u32 @100, + field3: u32, +} + +struct SampleUnnamed(u32, u32 @100, u32) + +enum SampleEnum { + Named { + field1: u32, + field2: u32 @100, + field3: u32, + }, + Unit @50, + Unnamed(u32, u32 @100, u32), +} + + diff --git a/vscode-extension/package.json b/vscode-extension/package.json index 97d3a72..c61029a 100644 --- a/vscode-extension/package.json +++ b/vscode-extension/package.json @@ -65,6 +65,11 @@ "description": "Style for builtin types", "superType": "type" }, + { + "id": "identifier", + "description": "Style for struct/enum identifiers", + "superType": "number" + }, { "id": "typeAlias", "description": "Style for type aliases", @@ -87,6 +92,9 @@ "builtinType": [ "support.type.primitive.stef" ], + "identifier": [ + "constant.numeric.stef" + ], "typeAlias": [ "entity.name.type.declaration.stef" ],