css_lexer/
cursor.rs

1use crate::{AssociatedWhitespaceRules, CommentStyle, Kind, KindSet, QuoteStyle, SourceOffset, Span, ToSpan, Token};
2
3/// Wraps [Token] with a [SourceOffset], allows it to reason about the character data of the source text.
4#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
5pub struct Cursor(SourceOffset, Token);
6
7impl Cursor {
8	pub const DUMMY_SITE_NUMBER_ZERO: Self = Self(SourceOffset::DUMMY, Token::NUMBER_ZERO);
9	pub const EMPTY: Self = Self(SourceOffset::ZERO, Token::EMPTY);
10
11	#[inline(always)]
12	pub const fn new(offset: SourceOffset, token: Token) -> Self {
13		Self(offset, token)
14	}
15
16	#[inline(always)]
17	pub const fn dummy(token: Token) -> Self {
18		Self(SourceOffset::DUMMY, token)
19	}
20
21	#[inline(always)]
22	pub const fn token(&self) -> Token {
23		self.1
24	}
25
26	#[inline(always)]
27	pub const fn offset(&self) -> SourceOffset {
28		self.0
29	}
30
31	#[inline(always)]
32	pub fn end_offset(&self) -> SourceOffset {
33		if self.offset() == SourceOffset::DUMMY {
34			return self.offset();
35		}
36		SourceOffset(self.offset().0 + self.len())
37	}
38
39	#[inline(always)]
40	pub const fn is_empty(&self) -> bool {
41		self.token().is_empty()
42	}
43
44	#[inline(always)]
45	pub const fn len(&self) -> u32 {
46		self.token().len()
47	}
48
49	#[inline(always)]
50	pub fn span(&self) -> Span {
51		Span::new(self.offset(), self.end_offset())
52	}
53
54	#[inline(always)]
55	pub fn str_slice<'a>(&self, str: &'a str) -> &'a str {
56		debug_assert!(
57			str.len() >= (self.end_offset().0 as usize),
58			"attempted to index out of bounds ({} < {})",
59			str.len(),
60			self.end_offset().0
61		);
62		&str[(self.offset().0 as usize)..(self.end_offset().0 as usize)]
63	}
64
65	pub fn with_quotes(&self, quote_style: QuoteStyle) -> Self {
66		if *self == quote_style || *self != Kind::String {
67			return *self;
68		}
69		Self::new(self.offset(), self.token().with_quotes(quote_style))
70	}
71
72	pub fn with_associated_whitespace(&self, rules: AssociatedWhitespaceRules) -> Self {
73		debug_assert!(self.1 == KindSet::DELIM_LIKE);
74		if self.1.associated_whitespace().to_bits() == rules.to_bits() {
75			return *self;
76		}
77		Self::new(self.offset(), self.token().with_associated_whitespace(rules))
78	}
79
80	#[inline]
81	pub fn atom_bits(&self) -> u32 {
82		self.1.atom_bits()
83	}
84}
85
86impl From<Cursor> for Token {
87	fn from(cursor: Cursor) -> Self {
88		cursor.token()
89	}
90}
91
92impl PartialEq<Token> for Cursor {
93	fn eq(&self, other: &Token) -> bool {
94		self.1 == *other
95	}
96}
97
98impl ToSpan for Cursor {
99	fn to_span(&self) -> Span {
100		self.span()
101	}
102}
103
104impl From<Cursor> for Span {
105	fn from(cursor: Cursor) -> Self {
106		cursor.span()
107	}
108}
109
110impl PartialEq<Span> for Cursor {
111	fn eq(&self, other: &Span) -> bool {
112		self.span() == *other
113	}
114}
115
116impl From<Cursor> for Kind {
117	fn from(cursor: Cursor) -> Self {
118		cursor.token().kind()
119	}
120}
121
122impl PartialEq<Kind> for Cursor {
123	fn eq(&self, other: &Kind) -> bool {
124		self.1 == *other
125	}
126}
127
128impl PartialEq<CommentStyle> for Cursor {
129	fn eq(&self, other: &CommentStyle) -> bool {
130		self.1 == *other
131	}
132}
133
134impl From<Cursor> for KindSet {
135	fn from(cursor: Cursor) -> Self {
136		cursor.token().into()
137	}
138}
139
140impl PartialEq<KindSet> for Cursor {
141	fn eq(&self, other: &KindSet) -> bool {
142		self.1 == *other
143	}
144}
145
146impl From<Cursor> for QuoteStyle {
147	fn from(cursor: Cursor) -> Self {
148		cursor.token().into()
149	}
150}
151
152impl PartialEq<QuoteStyle> for Cursor {
153	fn eq(&self, other: &QuoteStyle) -> bool {
154		self.1 == *other
155	}
156}
157
158impl PartialEq<AssociatedWhitespaceRules> for Cursor {
159	fn eq(&self, other: &AssociatedWhitespaceRules) -> bool {
160		self.1 == *other
161	}
162}
163
164impl PartialEq<char> for Cursor {
165	fn eq(&self, other: &char) -> bool {
166		self.1 == *other
167	}
168}
169
170impl PartialEq<CommentStyle> for &Cursor {
171	fn eq(&self, other: &CommentStyle) -> bool {
172		self.1 == *other
173	}
174}
175
176impl PartialEq<Kind> for &Cursor {
177	fn eq(&self, other: &Kind) -> bool {
178		self.1 == *other
179	}
180}
181
182impl PartialEq<KindSet> for &Cursor {
183	fn eq(&self, other: &KindSet) -> bool {
184		self.1 == *other
185	}
186}
187
188impl PartialEq<QuoteStyle> for &Cursor {
189	fn eq(&self, other: &QuoteStyle) -> bool {
190		self.1 == *other
191	}
192}
193
194impl PartialEq<char> for &Cursor {
195	fn eq(&self, other: &char) -> bool {
196		self.1 == *other
197	}
198}
199
200#[cfg(feature = "miette")]
201impl From<Cursor> for miette::SourceSpan {
202	fn from(val: Cursor) -> Self {
203		let span = val.span();
204		span.into()
205	}
206}
207
208#[cfg(feature = "serde")]
209impl serde::ser::Serialize for Cursor {
210	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
211	where
212		S: serde::ser::Serializer,
213	{
214		use serde::ser::SerializeStruct;
215		if self.token() == Token::EMPTY {
216			return serializer.serialize_none();
217		}
218		let mut state = serializer.serialize_struct("Cursor", 3)?;
219		state.serialize_field("kind", self.token().kind().as_str())?;
220		state.serialize_field("offset", &self.offset())?;
221		state.serialize_field("len", &self.token().len())?;
222		state.end()
223	}
224}
225
226#[test]
227fn size_test() {
228	assert_eq!(::std::mem::size_of::<Cursor>(), 12);
229}