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
use crate::types::{
BlockLocation, SfcCustomBlock, SfcDescriptor, SfcError, SfcParseOptions, SfcScriptBlock,
SfcStyleBlock, SfcTemplateBlock,
};
use memchr::{memchr, memchr_iter, memmem::Finder};
use std::borrow::Cow;
use super::block::{parse_block_fast, tag_name_eq};
// Tag name bytes for fast comparison
const TAG_TEMPLATE: &[u8] = b"template";
const TAG_SCRIPT: &[u8] = b"script";
const TAG_STYLE: &[u8] = b"style";
#[inline]
fn advance_line_column(bytes: &[u8], line: &mut usize, column: &mut usize) {
let mut last_newline = None;
for offset in memchr_iter(b'\n', bytes) {
*line += 1;
last_newline = Some(offset);
}
if let Some(offset) = last_newline {
*column = bytes.len() - offset;
} else {
*column += bytes.len();
}
}
/// Parse a Vue SFC into a descriptor with zero-copy strings
pub fn parse_sfc<'a>(
source: &'a str,
options: SfcParseOptions,
) -> Result<SfcDescriptor<'a>, SfcError> {
let mut descriptor = SfcDescriptor {
filename: Cow::Owned(options.filename.into()),
source: Cow::Borrowed(source),
..Default::default()
};
let bytes = source.as_bytes();
let len = bytes.len();
let mut pos = 0;
let mut line = 1;
let mut column = 1;
let comment_end_finder = Finder::new(b"-->");
while pos < len {
// Skip whitespace using byte comparison
while pos < len {
let c = bytes[pos];
if c == b' ' || c == b'\t' || c == b'\r' {
pos += 1;
column += 1;
} else if c == b'\n' {
pos += 1;
line += 1;
column = 1;
} else {
break;
}
}
if pos >= len {
break;
}
// Use memchr to find next '<' quickly
if bytes[pos] != b'<' {
if let Some(next_lt) = memchr(b'<', &bytes[pos..]) {
// Update line/column for skipped content
advance_line_column(&bytes[pos..pos + next_lt], &mut line, &mut column);
pos += next_lt;
} else {
break;
}
}
if pos >= len {
break;
}
// Skip HTML comments <!-- ... --> before block parsing.
if bytes[pos..].starts_with(b"<!--") {
let comment_body = &bytes[pos + 4..];
let end = comment_end_finder
.find(comment_body)
.map(|off| pos + 4 + off + 3) // position after '-->'
.unwrap_or(len); // unclosed comment: skip to EOF
// Update line/column for the skipped comment
advance_line_column(&bytes[pos..end], &mut line, &mut column);
pos = end;
continue;
}
// Parse block starting at '<'
match parse_block_fast(bytes, source, pos, line) {
Ok(Some(block_result)) => {
let (
tag_name,
attrs,
content,
content_start,
content_end,
end_pos,
end_line,
end_col,
) = block_result;
let loc = BlockLocation {
start: content_start,
end: content_end,
tag_start: pos,
tag_end: end_pos,
start_line: line,
start_column: column,
end_line,
end_column: end_col,
};
// Match tag name using byte comparison
if tag_name_eq(tag_name, TAG_TEMPLATE) {
if descriptor.template.is_some() {
return Err(SfcError {
message: "SFC can only contain one <template> block".into(),
code: Some("DUPLICATE_TEMPLATE".into()),
loc: Some(loc.clone()),
});
}
descriptor.template = Some(SfcTemplateBlock {
content,
loc,
lang: attrs.get("lang").cloned(),
src: attrs.get("src").cloned(),
attrs,
});
} else if tag_name_eq(tag_name, TAG_SCRIPT) {
let is_setup = attrs.contains_key("setup");
let script_block = SfcScriptBlock {
content,
loc,
lang: attrs.get("lang").cloned(),
src: attrs.get("src").cloned(),
setup: is_setup,
attrs,
bindings: None,
};
if is_setup {
if descriptor.script_setup.is_some() {
return Err(SfcError {
message: "SFC can only contain one <script setup> block".into(),
code: Some("DUPLICATE_SCRIPT_SETUP".into()),
loc: Some(script_block.loc),
});
}
descriptor.script_setup = Some(script_block);
} else {
if descriptor.script.is_some() {
return Err(SfcError {
message: "SFC can only contain one <script> block".into(),
code: Some("DUPLICATE_SCRIPT".into()),
loc: Some(script_block.loc),
});
}
descriptor.script = Some(script_block);
}
} else if tag_name_eq(tag_name, TAG_STYLE) {
let scoped = attrs.contains_key("scoped");
let module = if attrs.contains_key("module") {
Some(
attrs
.get("module")
.filter(|v| !v.is_empty())
.cloned()
.unwrap_or_else(|| Cow::Borrowed("$style")),
)
} else {
None
};
descriptor.styles.push(SfcStyleBlock {
content,
loc,
lang: attrs.get("lang").cloned(),
src: attrs.get("src").cloned(),
scoped,
module,
attrs,
});
} else {
// Custom block - use borrowed tag name
let tag_str = unsafe { std::str::from_utf8_unchecked(tag_name) };
descriptor.custom_blocks.push(SfcCustomBlock {
block_type: Cow::Borrowed(tag_str),
content,
loc,
attrs,
});
}
pos = end_pos;
line = end_line;
column = end_col;
}
Ok(None) => {
pos += 1;
column += 1;
}
Err((code, message)) => {
let mut end_line = line;
let mut end_column = column;
advance_line_column(&bytes[pos..len], &mut end_line, &mut end_column);
return Err(SfcError {
message,
code: Some(code.into()),
loc: Some(BlockLocation {
start: pos,
end: len,
tag_start: pos,
tag_end: len,
start_line: line,
start_column: column,
end_line,
end_column,
}),
});
}
}
}
Ok(descriptor)
}