aboutsummaryrefslogtreecommitdiff
path: root/src/trace.rs
blob: 620d4f31538e8e8b835e584c097243149a90dfbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//! Provides the [`Trace`] type (byte offsets and syntax information about tokens).

use std::{
    num::NonZeroUsize,
    ops::{Index, Range},
};

use crate::let_else::assume;
use crate::token::AttributeTraceIdx;

/// Provides byte offsets and syntax information about a [`Token`].
///
/// [`Token`]: crate::token::Token
#[allow(missing_docs)]
#[derive(Eq, PartialEq, Debug)]
pub enum Trace {
    Char,
    StartTag(StartTagTrace),
    EndTag(EndTagTrace),
    Comment(CommentTrace),
    Doctype(DoctypeTrace),
    EndOfFile(usize),
}

/// Provides byte offsets and syntax information for a [`StartTag`] token.
///
/// [`StartTag`]: crate::token::StartTag
#[derive(Eq, PartialEq, Debug)]
pub struct StartTagTrace {
    /// The span of the tag.
    pub span: Range<usize>,

    /// The span of the tag name.
    pub name_span: Range<usize>,

    /// List of [`AttributeTrace`]s for the attributes that were present in the source.
    pub attribute_traces: AttributeTraceList,
}

/// Provides byte offsets for an [`EndTag`] token.
///
/// [`EndTag`]: crate::token::EndTag
#[derive(Eq, PartialEq, Debug)]
pub struct EndTagTrace {
    /// The span of the tag.
    pub span: Range<usize>,

    /// The span of the tag name.
    pub name_span: Range<usize>,
}

/// Provides byte offsets for a [`Token::Comment`].
///
/// [`Token::Comment`]: crate::token::Token::Comment
#[derive(Eq, PartialEq, Debug)]
pub struct CommentTrace {
    /// The offset of the comment data.
    pub data_span: Range<usize>,
}

/// Provides byte offsets for a [`Doctype`] token.
///
/// [`Doctype`]: crate::token::Doctype
#[derive(Eq, PartialEq, Debug)]
pub struct DoctypeTrace {
    pub(crate) span: Range<usize>,
    // Using NonZeroUsize to optimize the size of the struct.
    name_span: Option<Range<std::num::NonZeroUsize>>,
    public_id_span: Option<Range<std::num::NonZeroUsize>>,
    system_id_span: Option<Range<std::num::NonZeroUsize>>,
}

impl DoctypeTrace {
    /// Returns the span of the DOCTYPE.
    pub fn span(&self) -> Range<usize> {
        self.span.clone()
    }

    /// Returns the span of the name.
    pub fn name_span(&self) -> Option<Range<usize>> {
        self.name_span
            .as_ref()
            .map(|range| range.start.get()..range.end.get())
    }

    /// Returns the span of the public identifier.
    pub fn public_id_span(&self) -> Option<Range<usize>> {
        self.public_id_span
            .as_ref()
            .map(|range| range.start.get()..range.end.get())
    }

    /// Returns the span of the system identifier.
    pub fn system_id_span(&self) -> Option<Range<usize>> {
        self.system_id_span
            .as_ref()
            .map(|range| range.start.get()..range.end.get())
    }
}

/// Internal [`DoctypeTrace`] methods.
///
/// Note that even though it stands to reason that the offsets provided to the `set_`
/// methods can never be zero, we intentionally don't use `new_unchecked` since
/// actually verifying that the offsets provided to the respective Emitter methods can
/// never be zero would non-trivial (since the tokenizer state machine has 80 states).
impl DoctypeTrace {
    #[inline]
    pub(crate) fn new(span_start: usize) -> Self {
        Self {
            span: span_start..0,
            name_span: None,
            public_id_span: None,
            system_id_span: None,
        }
    }

    #[inline]
    pub(crate) fn set_name_start(&mut self, start: usize) {
        let start = NonZeroUsize::new(start).expect("expected offset to be non-zero");
        self.name_span = Some(start..start);
    }

    #[inline]
    pub(crate) fn set_public_id_start(&mut self, start: usize) {
        let start = NonZeroUsize::new(start).expect("expected offset to be non-zero");
        self.public_id_span = Some(start..start);
    }

    #[inline]
    pub(crate) fn set_system_id_start(&mut self, start: usize) {
        let start = NonZeroUsize::new(start).expect("expected offset to be non-zero");
        self.system_id_span = Some(start..start);
    }

    #[inline]
    pub(crate) fn set_name_end(&mut self, end: usize) {
        assume!(Some(span), &mut self.name_span);
        span.end = NonZeroUsize::new(end).expect("expected offset to be non-zero");
    }

    #[inline]
    pub(crate) fn set_public_id_end(&mut self, end: usize) {
        assume!(Some(span), &mut self.public_id_span);
        span.end = NonZeroUsize::new(end).expect("expected offset to be non-zero");
    }

    #[inline]
    pub(crate) fn set_system_id_end(&mut self, end: usize) {
        assume!(Some(span), &mut self.system_id_span);
        span.end = NonZeroUsize::new(end).expect("expected offset to be non-zero");
    }
}

/// The syntax of the attribute value.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum AttrValueSyntax {
    /// An unquoted attribute value, e.g. `id=foo`.
    Unquoted,
    /// A single-quoted attribute value, e.g. `id='foo'`.
    SingleQuoted,
    /// A double-quoted attribute value, e.g. `id="foo"`.
    DoubleQuoted,
}

/// Provides byte offsets and the [`AttrValueSyntax`] for an attribute that was present in the source.
#[derive(Eq, PartialEq, Debug)]
pub struct AttributeTrace {
    pub(crate) value_syntax: Option<AttrValueSyntax>,
    pub(crate) name_span: Range<usize>,
    /// We intentionally don't use `Option<Range<O>>` here to spare us a byte (and padding) per attribute.
    /// For the empty attribute syntax this is just `O::default()..O::default()`.
    pub(crate) value_span: Range<usize>,
}

impl AttributeTrace {
    /// [`AttributeTrace`] intentionally doesn't implement Default
    /// (since it's part of the public API and it wouldn't make sense semantically).
    pub(crate) fn new() -> Self {
        Self {
            value_syntax: None,
            name_span: Default::default(),
            value_span: Default::default(),
        }
    }

    /// Returns the span of the attribute name.
    pub fn name_span(&self) -> Range<usize> {
        self.name_span.clone()
    }

    /// For explicitly defined values returns the span of the attribute value.
    ///
    /// Returns `None` for attributes using the empty attribute syntax (e.g. `disabled` in `<input disabled>`).
    pub fn value_span(&self) -> Option<Range<usize>> {
        if self.value_syntax.is_none() {
            return None;
        }
        Some(self.value_span.clone())
    }

    /// Returns the attribute value syntax in case the value is explicitly defined.
    ///
    /// Returns `None` for attributes using the empty attribute syntax (e.g. `disabled` in `<input disabled>`).
    pub fn value_syntax(&self) -> Option<AttrValueSyntax> {
        self.value_syntax
    }
}

/// List of [`AttributeTrace`]s for the attributes that were present in the source.
#[derive(Eq, PartialEq, Debug)]
pub struct AttributeTraceList {
    /// We don't use `HashMap<String, AttributeTrace>` since this would require
    /// the attribute names to be cloned (which would be less efficient).
    traces: Vec<AttributeTrace>,
}

impl Index<AttributeTraceIdx> for AttributeTraceList {
    type Output = AttributeTrace;

    fn index(&self, index: AttributeTraceIdx) -> &Self::Output {
        &self.traces[index.0.get() - 1]
    }
}

impl AttributeTraceList {
    pub(crate) fn new() -> Self {
        Self {
            traces: Default::default(),
        }
    }

    pub(crate) fn insert(&mut self, trace: AttributeTrace) -> AttributeTraceIdx {
        self.traces.push(trace);
        let len = self.traces.len();
        AttributeTraceIdx(
            // SAFETY: len cannot be zero because we push before calling Vec::len.
            unsafe { std::num::NonZeroUsize::new_unchecked(len) },
        )
    }
}