diff --git a/crates/moxy-ast/src/use_tree/mod.rs b/crates/moxy-ast/src/use_tree/mod.rs index 6281fb26..6a131a68 100644 --- a/crates/moxy-ast/src/use_tree/mod.rs +++ b/crates/moxy-ast/src/use_tree/mod.rs @@ -3,12 +3,14 @@ mod use_group; mod use_name; mod use_path; mod use_rename; +mod use_segment; pub use use_glob::*; pub use use_group::*; pub use use_name::*; pub use use_path::*; pub use use_rename::*; +pub use use_segment::*; use moxy_token::{Span, Spanner, ToTokens, TokenStream}; diff --git a/crates/moxy-ast/src/use_tree/use_name.rs b/crates/moxy-ast/src/use_tree/use_name.rs index 470788a7..05d7bed2 100644 --- a/crates/moxy-ast/src/use_tree/use_name.rs +++ b/crates/moxy-ast/src/use_tree/use_name.rs @@ -7,7 +7,7 @@ use crate::*; #[cfg_attr(feature = "derives", derive(Debug, PartialEq, Eq))] #[cfg_attr(feature = "serde", derive(serde::Serialize))] pub struct UseName { - pub ident: Ident, + pub segment: UseSegment, } impl Parse for UseName { @@ -17,7 +17,7 @@ impl Parse for UseName { fn parse(parser: &Parser) -> Result { Ok(Self { - ident: <_ as Parse>::parse(parser)?, + segment: <_ as Parse>::parse(parser)?, }) } @@ -28,12 +28,12 @@ impl Parse for UseName { impl Spanner for UseName { fn span(&self) -> Span { - self.ident.span() + self.segment.span() } } impl ToTokens for UseName { fn to_tokens(&self, t: &mut TokenStream) { - self.ident.to_tokens(t); + self.segment.to_tokens(t); } } diff --git a/crates/moxy-ast/src/use_tree/use_path.rs b/crates/moxy-ast/src/use_tree/use_path.rs index 3d3bc3e6..640d0d30 100644 --- a/crates/moxy-ast/src/use_tree/use_path.rs +++ b/crates/moxy-ast/src/use_tree/use_path.rs @@ -1,6 +1,5 @@ -use moxy_token::{Ident, Span, Spanner, ToTokens, TokenStream}; +use moxy_token::{Span, Spanner, ToTokens, TokenStream}; -use super::UseTree; use crate::*; /// A use path segment (`foo::`). @@ -9,7 +8,7 @@ use crate::*; #[cfg_attr(feature = "serde", derive(serde::Serialize))] pub struct UsePath { pub prefix: Option, - pub ident: Ident, + pub segment: UseSegment, pub path_sep: Token![::], pub tree: Box, } @@ -17,13 +16,13 @@ pub struct UsePath { impl Parse for UsePath { fn peek(cursor: Cursor<'_>) -> bool { let cursor = Option::::skip(cursor).unwrap_or(cursor); - Ident::peek(cursor) && ::peek(cursor.offset(1)) + UseSegment::peek(cursor) && ::peek(cursor.offset(1)) } fn parse(parser: &Parser) -> Result { Ok(Self { prefix: <_ as Parse>::parse(parser)?, - ident: <_ as Parse>::parse(parser)?, + segment: <_ as Parse>::parse(parser)?, path_sep: <_ as Parse>::parse(parser)?, tree: Box::new(<_ as Parse>::parse(parser)?), }) @@ -31,7 +30,7 @@ impl Parse for UsePath { fn skip(mut cursor: Cursor<'_>) -> Option> { cursor = Option::::skip(cursor)?; - cursor = Ident::skip(cursor)?; + cursor = UseSegment::skip(cursor)?; cursor = ::skip(cursor)?; UseTree::skip(cursor) } @@ -39,14 +38,14 @@ impl Parse for UsePath { impl Spanner for UsePath { fn span(&self) -> Span { - self.ident.span().join(self.tree.span()) + self.segment.span().join(self.tree.span()) } } impl ToTokens for UsePath { fn to_tokens(&self, t: &mut TokenStream) { self.prefix.to_tokens(t); - self.ident.to_tokens(t); + self.segment.to_tokens(t); self.path_sep.to_tokens(t); self.tree.to_tokens(t); } diff --git a/crates/moxy-ast/src/use_tree/use_rename.rs b/crates/moxy-ast/src/use_tree/use_rename.rs index c6426cce..bca28908 100644 --- a/crates/moxy-ast/src/use_tree/use_rename.rs +++ b/crates/moxy-ast/src/use_tree/use_rename.rs @@ -7,38 +7,38 @@ use crate::*; #[cfg_attr(feature = "derives", derive(Debug, PartialEq, Eq))] #[cfg_attr(feature = "serde", derive(serde::Serialize))] pub struct UseRename { - pub ident: Ident, + pub segment: UseSegment, pub as_keyword: Token![as], pub rename: Ident, } impl Parse for UseRename { fn peek(cursor: Cursor<'_>) -> bool { - Ident::peek(cursor) && ::peek(cursor.offset(1)) && Ident::peek(cursor.offset(2)) + UseSegment::peek(cursor) && ::peek(cursor.offset(1)) && Ident::peek(cursor.offset(2)) } fn parse(parser: &Parser) -> Result { Ok(Self { - ident: <_ as Parse>::parse(parser)?, + segment: <_ as Parse>::parse(parser)?, as_keyword: <_ as Parse>::parse(parser)?, rename: <_ as Parse>::parse(parser)?, }) } fn skip(cursor: Cursor<'_>) -> Option> { - Ident::skip(::skip(Ident::skip(cursor)?)?) + Ident::skip(::skip(UseSegment::skip(cursor)?)?) } } impl Spanner for UseRename { fn span(&self) -> Span { - self.ident.span().join(self.rename.span()) + self.segment.span().join(self.rename.span()) } } impl ToTokens for UseRename { fn to_tokens(&self, t: &mut TokenStream) { - self.ident.to_tokens(t); + self.segment.to_tokens(t); self.as_keyword.to_tokens(t); self.rename.to_tokens(t); } diff --git a/crates/moxy-ast/src/use_tree/use_segment.rs b/crates/moxy-ast/src/use_tree/use_segment.rs new file mode 100644 index 00000000..28781089 --- /dev/null +++ b/crates/moxy-ast/src/use_tree/use_segment.rs @@ -0,0 +1,97 @@ +use moxy_token::{Crate, Ident, SelfValue, Span, Spanner, Super, ToTokens, TokenStream}; + +use crate::{Cursor, Parse, ParseError, Parser}; + +/// A segment of an `use` path. +#[derive(Clone)] +#[cfg_attr(feature = "derives", derive(Debug, PartialEq, Eq))] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] +pub enum UseSegment { + Ident(Ident), + Super(Super), + Crate(Crate), + SelfValue(SelfValue), +} + +impl UseSegment { + pub fn is_ident(&self) -> bool { + matches!(self, Self::Ident(_)) + } + + pub fn is_super(&self) -> bool { + matches!(self, Self::Super(_)) + } + + pub fn is_crate(&self) -> bool { + matches!(self, Self::Crate(_)) + } + + pub fn is_self_value(&self) -> bool { + matches!(self, Self::SelfValue(_)) + } + + pub fn as_ident(&self) -> Option<&Ident> { + if let Self::Ident(v) = self { Some(v) } else { None } + } + + pub fn as_super(&self) -> Option<&Super> { + if let Self::Super(v) = self { Some(v) } else { None } + } + + pub fn as_crate(&self) -> Option<&Crate> { + if let Self::Crate(v) = self { Some(v) } else { None } + } + + pub fn as_self_value(&self) -> Option<&SelfValue> { + if let Self::SelfValue(v) = self { Some(v) } else { None } + } +} + +impl Spanner for UseSegment { + fn span(&self) -> Span { + match self { + Self::Ident(v) => v.span(), + Self::Super(v) => v.span(), + Self::Crate(v) => v.span(), + Self::SelfValue(v) => v.span(), + } + } +} + +impl Parse for UseSegment { + fn peek(cursor: Cursor<'_>) -> bool { + Ident::peek(cursor) || Super::peek(cursor) || Crate::peek(cursor) || SelfValue::peek(cursor) + } + + fn parse(parser: &Parser) -> Result { + if Ident::peek(parser.cursor()) { + Ok(Self::Ident(<_ as Parse>::parse(parser)?)) + } else if Super::peek(parser.cursor()) { + Ok(Self::Super(<_ as Parse>::parse(parser)?)) + } else if Crate::peek(parser.cursor()) { + Ok(Self::Crate(<_ as Parse>::parse(parser)?)) + } else if SelfValue::peek(parser.cursor()) { + Ok(Self::SelfValue(<_ as Parse>::parse(parser)?)) + } else { + Err(parser.error("expected identifier, 'super', 'crate' or 'self'")) + } + } + + fn skip(cursor: Cursor<'_>) -> Option> { + Ident::skip(cursor) + .or_else(|| Super::skip(cursor)) + .or_else(|| SelfValue::skip(cursor)) + .or_else(|| Crate::skip(cursor)) + } +} + +impl ToTokens for UseSegment { + fn to_tokens(&self, t: &mut TokenStream) { + match self { + Self::Ident(v) => v.to_tokens(t), + Self::Super(v) => v.to_tokens(t), + Self::SelfValue(v) => v.to_tokens(t), + Self::Crate(v) => v.to_tokens(t), + } + } +} diff --git a/crates/moxy-fmt/src/ast/item.rs b/crates/moxy-fmt/src/ast/item.rs index 1757a1c0..e6e99f01 100644 --- a/crates/moxy-fmt/src/ast/item.rs +++ b/crates/moxy-fmt/src/ast/item.rs @@ -205,21 +205,32 @@ impl Format for UsePath { f.text("::")?; } - self.ident.format(f)?; + self.segment.format(f)?; f.text("::")?; self.tree.format(f) } } +impl Format for UseSegment { + fn format(&self, f: &mut Formatter) -> Result<(), FmtError> { + match self { + UseSegment::Ident(v) => v.format(f), + UseSegment::Super(_) => f.text("super"), + UseSegment::SelfValue(_) => f.text("self"), + UseSegment::Crate(_) => f.text("crate"), + } + } +} + impl Format for UseName { fn format(&self, f: &mut Formatter) -> Result<(), FmtError> { - self.ident.format(f) + self.segment.format(f) } } impl Format for UseRename { fn format(&self, f: &mut Formatter) -> Result<(), FmtError> { - self.ident.format(f)?; + self.segment.format(f)?; f.text(" as ")?; self.rename.format(f) } diff --git a/fixtures/grammar/matrix.json b/fixtures/grammar/matrix.json index 5ab1a623..17669e95 100644 --- a/fixtures/grammar/matrix.json +++ b/fixtures/grammar/matrix.json @@ -674,6 +674,27 @@ "support_expectation": "moxy_accepts", "fixture_directory": "fixtures/grammar/paths/qualified-path" }, + { + "id": "paths.segments", + "reference": { + "production": "Paths > Simple Path Segment", + "anchor": "https://doc.rust-lang.org/reference/grammar.html" + }, + "baseline": { + "toolchain": "stable", + "edition": "2024" + }, + "upstream": { + "repository": "rust-lang/rust", + "commit": "2350b22a89d30fe814a299a73c9ab83c72cafaf3", + "paths": [ + "tests/ui/parser/" + ] + }, + "parse_root": "crate", + "support_expectation": "moxy_accepts", + "fixture_directory": "fixtures/grammar/paths/segments" + }, { "id": "paths.turbofish", "reference": { diff --git a/fixtures/grammar/paths/segments/fail.rs b/fixtures/grammar/paths/segments/fail.rs new file mode 100644 index 00000000..9d12c5aa --- /dev/null +++ b/fixtures/grammar/paths/segments/fail.rs @@ -0,0 +1 @@ +use crate super self; diff --git a/fixtures/grammar/paths/segments/pass.rs b/fixtures/grammar/paths/segments/pass.rs new file mode 100644 index 00000000..62443326 --- /dev/null +++ b/fixtures/grammar/paths/segments/pass.rs @@ -0,0 +1,4 @@ +use some_ident; +use crate; +use super; +use crate::super::self; diff --git a/tests/use_items.rs b/tests/use_items.rs index 2ab65f5c..0d69accc 100644 --- a/tests/use_items.rs +++ b/tests/use_items.rs @@ -14,3 +14,17 @@ fn leading_paths_and_single_names_render_without_extra_spaces() { let item: Item = moxy::parse!("use ::core::fmt;").unwrap(); assert_eq!(moxy::fmt!(&item).unwrap(), "use ::core::fmt;"); } + +#[test] +fn use_path_can_contain_special_segments() { + for case in [ + "use crate as my_library;", + "use super::*;", + "use crate::super::self;", + "use crate::super::self::{crate::a, super::b, self::c};", + ] { + eprintln!("{case}"); + let item: Item = moxy::parse!(case).unwrap(); + assert_eq!(moxy::fmt!(&item).unwrap(), case); + } +}