verter_core 0.0.1-alpha.1

Vue 3 SFC compiler - transforms Vue Single File Components to render functions with TypeScript support
Documentation
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// This is meant to provide most of the information for vue, eg, new lines, etc.

use super::types::QuoteType;
use crate::{
    common::{ErrorCode, SourceLocation},
    cursor::cursor::{find_subslice, Cursor, CursorPosition},
    tokenizer::{
        helpers::{
            is_end_of_tag_section, is_tag_start_char, is_whitespace, DEFAULT_DELIMITER_CLOSE,
            DEFAULT_DELIMITER_OPEN,
        },
        types::{
            char_codes::{
                AMP, AT, COLON, DASH, DOT, EQ, EXCLAMATION_MARK, GT, LOWER_V, LT, NUMBER,
                QUESTION_MARK, SLASH,
            },
            EventSourceLocation,
        },
    },
};

/// Tokenize the input and call the callback for each event.
/// Events contain full position.
pub fn tokenize(input: &str, callback: impl FnMut(EventSourceLocation)) {
    tokenize_with_delimiters(
        input,
        callback,
        DEFAULT_DELIMITER_OPEN,
        DEFAULT_DELIMITER_CLOSE,
    )
}

/// Tokenize the input with custom delimiters and call the callback for each event.
/// Events contain full position.
pub fn tokenize_with_delimiters(
    input: &str,
    callback: impl FnMut(EventSourceLocation),
    delimiter_open: &[u8],
    delimiter_close: &[u8],
) {
    let mut tokenizer = Tokenizer::new(input, callback, delimiter_open, delimiter_close);
    tokenizer.run();
}

struct Tokenizer<'a, F: FnMut(EventSourceLocation)> {
    callback: F,
    delimiter_open: &'a [u8],
    delimiter_close: &'a [u8],

    delimiter_open_first_byte: u8,
    #[allow(dead_code)]
    delimiter_close_first_byte: u8,

    cursor: Cursor<'a>,
    section_start: Option<CursorPosition>,

    in_v_pre: bool,
    v_pre_depth: usize,
}

impl<'a, F: FnMut(EventSourceLocation)> Tokenizer<'a, F> {
    fn new(
        input: &'a str,
        callback: F,
        delimiter_open: &'a [u8],
        delimiter_close: &'a [u8],
    ) -> Self {
        Self {
            callback,

            delimiter_open,
            delimiter_open_first_byte: delimiter_open[0],
            delimiter_close,
            delimiter_close_first_byte: delimiter_close[0],

            cursor: Cursor::new(input),
            section_start: None,

            in_v_pre: false,
            v_pre_depth: 0,
        }
    }

    fn run(&mut self) {
        // Tokenization logic goes here
        while !self.cursor.ended() {
            self.handle_text();

            self.cursor.increment();
        }
    }

    fn emit(&mut self, event: EventSourceLocation) {
        (self.callback)(event);
    }

    fn handle_text(&mut self) {
        let next = if self.in_v_pre {
            self.cursor.search2(self.delimiter_open_first_byte, b'<')
        } else {
            self.cursor
                .search3(self.delimiter_open_first_byte, b'<', b'&')
        };

        match next {
            Some(idx) => {
                // Emit text up to idx
                self.cursor.advance(idx);
                let c = self.cursor.current_byte();
                if c == LT {
                    self.flush_text_section();

                    self.handle_tag_start()
                } else if c == AMP {
                    // TODO: Handle entity
                    self.cursor.increment();
                } else if c == self.delimiter_open_first_byte {
                    // self.cursor.advance(idx);
                    self.handle_interpolation_start()
                } else {
                    debug_assert!(false, "Unexpected byte encountered");
                    self.end();
                }
            }
            None => {
                // Emit remaining text
                self.end()
            }
        }
    }

    // aka state_before_tag_name
    fn handle_tag_start(&mut self) {
        // Placeholder for tag handling logic
        // self.cursor.increment(); // Move past '<'
        self.cursor.increment();

        let c = self.cursor.current_byte();
        if c == EXCLAMATION_MARK {
            self.handle_before_declaration();
        } else if c == QUESTION_MARK {
            self.handle_processing_instruction();
        } else if c == SLASH {
            self.handle_closing_tag();
        } else if is_tag_start_char(c) {
            self.handle_tag_name()
        }
    }

    fn handle_interpolation_start(&mut self) {
        // Placeholder for interpolation handling logic
        if self.cursor.next_bytes_equal(self.delimiter_open) {
            self.flush_text_section();

            let start = self.cursor.position;
            self.cursor.advance(self.delimiter_open.len());

            match find_subslice(self.delimiter_close, self.cursor.remaining()) {
                Some(idx) => {
                    self.cursor.advance(idx + self.delimiter_close.len());

                    self.emit(EventSourceLocation::Interpolation(
                        SourceLocation::from_source(
                            self.cursor.input,
                            start.to_position(),
                            self.cursor.position.to_position(),
                        ),
                    ));

                    self.section_start = Some(self.cursor.position);
                    // Note we could call self.handle_text() here directly to continue processing
                    // that would lead to deep recursion on pathological inputs
                    // so we just return to the main loop to prevent deep recursion.
                }
                None => {
                    // TODO error
                    self.end();
                }
            }
        }
        /*else if self.in_rcdata {
           // TODO
        } */
        else {
            self.cursor.increment();
        }
    }

    fn handle_before_declaration(&mut self) {
        // Placeholder for declaration handling logic
        self.cursor.increment(); // Move past '!'
    }

    fn handle_processing_instruction(&mut self) {
        // Placeholder for processing instruction handling logic
        self.cursor.increment(); // Move past '?'
        self.section_start = Some(self.cursor.position);
    }

    fn handle_closing_tag(&mut self) {
        // Placeholder for closing tag handling logic
        // self.cursor.increment(); // Move past '/'
        // self.section_start = Some(self.cursor.position);

        // let start = self.cursor.position;
    }

    fn handle_tag_name(&mut self) {
        // Placeholder for tag name handling logic
        // self.cursor.increment(); // Move past tag name start
        // self.section_start = Some(self.cursor.position);

        let start = self.cursor.position;

        while !self.cursor.ended() && !is_end_of_tag_section(self.cursor.current_byte()) {
            self.cursor.increment();
        }

        if self.cursor.ended() {
            // TODO error
            self.end();
            return;
        }

        let end = self.cursor.position;

        // TODO add check_and_setup_rcdata
        self.emit(EventSourceLocation::OpenTagName(
            SourceLocation::from_source(self.cursor.input, start.to_position(), end.to_position()),
        ));

        let c = self.cursor.current_byte();
        if c == GT {
            self.emit(EventSourceLocation::OpenTagEnd(end.to_position()));
            // TODO if rcdata handle rcdata
            self.cursor.increment();
            self.section_start = Some(self.cursor.position);
        } else if c == SLASH {
            self.cursor.increment();
            if self.cursor.current_byte() == GT {
                self.emit(EventSourceLocation::SelfClosingTag(
                    self.cursor.position.to_position(),
                ));
                self.cursor.increment();
                // TODO reset rcdata
                // self.in_rcdata = false;
                self.section_start = Some(self.cursor.position);
            } else if self.cursor.ended() {
                // TODO error
                self.end();
            } else {
                // TODO error
                self.handle_before_attribute_name();
            }
        } else {
            self.cursor.increment();
        }
    }

    fn handle_before_attribute_name(&mut self) {
        while !self.cursor.ended() {
            let c = self.cursor.current_byte();
            if c == GT {
                self.emit(EventSourceLocation::OpenTagEnd(
                    self.cursor.position.to_position(),
                ));
                self.cursor.increment();
                // TODO set rcdata
                // if self.in_rcdata {
                //     self.state = State::InRCDATA;
                //     self.section_start = self.index;
                //     self.state_in_rcdata();
                // }
                self.section_start = Some(self.cursor.position);

                return;
            } else if c == SLASH {
                // self.state = State::InSelfClosingTag;
                // self.index += 1;
                // if self.index < self.input.len() && self.input[self.index] == GT {
                //     self.emit(Event::SelfClosingTag {
                //         end: self.index as u32,
                //     });
                //     self.index += 1;
                //     self.in_rcdata = false;
                //     self.state = State::Text;
                //     self.section_start = self.index;
                //     return;
                // } else {
                //     self.emit(Event::Error {
                //         code: ErrorCode::UNEXPECTED_SOLIDUS_IN_TAG,
                //         index: self.index as u32,
                //     });
                //     return;
                // }

                self.cursor.increment();
                if !self.cursor.ended() && self.cursor.current_byte() == GT {
                    self.emit(EventSourceLocation::SelfClosingTag(
                        self.cursor.position.to_position(),
                    ));
                    self.cursor.increment();
                    // TODO reset rcdata
                    // self.in_rcdata = false;
                    self.section_start = Some(self.cursor.position);
                    return;
                } else {
                    // TODO error
                    self.emit(EventSourceLocation::Error {
                        code: ErrorCode::UNEXPECTED_SOLIDUS_IN_TAG,
                        position: self.cursor.position.to_position(),
                    });
                    return;
                }
            } else if c == LT && self.cursor.next_byte() == SLASH {
                self.emit(EventSourceLocation::OpenTagEnd(
                    self.cursor.position.to_position(),
                ));
                self.section_start = Some(self.cursor.position);
                self.handle_tag_start();
                return;
            } else if !is_whitespace(c) {
                if c == EQ {
                    self.emit(EventSourceLocation::Error {
                        code: ErrorCode::UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME,
                        position: self.cursor.position.to_position(),
                    });
                }

                if c == LOWER_V && self.cursor.next_byte() == DASH {
                    self.section_start = Some(self.cursor.position);
                    self.handle_dir_name();
                    return;
                } else if c == DOT || c == COLON || c == AT || c == NUMBER {
                    let start = self.cursor.position;
                    self.cursor.increment();

                    self.emit(EventSourceLocation::DirName(SourceLocation::from_source(
                        self.cursor.input,
                        start.to_position(),
                        self.cursor.position.to_position(),
                    )));
                    self.section_start = Some(self.cursor.position);
                    self.handle_dir_arg();
                    return;
                } else {
                    self.section_start = Some(self.cursor.position);
                    self.handle_attr_name();
                    return;
                }
            } else {
                self.cursor.increment();
            }
        }
    }

    // state_in_dir_name
    fn handle_dir_name(&mut self) {
        // Placeholder for directive name handling logic
        // self.cursor.increment(); // Move past directive name start
        // self.section_start = Some(self.cursor.position);

        while !self.cursor.ended() && is_end_of_tag_section(self.cursor.current_byte()) {
            self.cursor.increment();
        }
        if self.cursor.ended() {
            // TODO error
            self.end();
            return;
        }
        let end = self.cursor.position;
        if let Some(start) = self.section_start {
            if self.cursor.position.byte_index - start.byte_index == 5
                && (self.cursor.bytes[start.byte_index] | 0x20) == b'v'
                && self.cursor.bytes[start.byte_index + 1] == b'-'
                && (self.cursor.bytes[start.byte_index + 2] | 0x20) == b'p'
                && (self.cursor.bytes[start.byte_index + 3] | 0x20) == b'r'
                && (self.cursor.bytes[start.byte_index + 4] | 0x20) == b'e'
            {
                self.in_v_pre = true;
                self.v_pre_depth = 0; // Will be incremented to 1 when OpenTagEnd is emitted
                self.emit(EventSourceLocation::DirVPre(SourceLocation::from_source(
                    self.cursor.input,
                    start.to_position(),
                    end.to_position(),
                )));
            }

            self.emit(EventSourceLocation::DirName(SourceLocation::from_source(
                self.cursor.input,
                start.to_position(),
                end.to_position(),
            )));
        }

        let c = self.cursor.current_byte();
        if c == COLON {
            self.handle_dir_arg();
        } else if c == DOT {
            self.handle_dir_modifier();
        } else if c == GT {
            self.emit(EventSourceLocation::OpenTagEnd(
                self.cursor.position.to_position(),
            ));
            self.cursor.increment();
            self.section_start = Some(self.cursor.position);
        } else if c == SLASH {
            self.emit(EventSourceLocation::OpenTagEnd(
                self.cursor.position.to_position(),
            ));
            self.emit(EventSourceLocation::AttribEnd {
                quote: QuoteType::NoValue,
                position: self.cursor.position.to_position(),
            });
            self.cursor.increment();
            if !self.cursor.ended() && self.cursor.current_byte() == GT {
                self.emit(EventSourceLocation::SelfClosingTag(
                    self.cursor.position.to_position(),
                ));
                self.cursor.increment();
                // TODO reset rcdata
                // self.in_rcdata = false;
                self.section_start = Some(self.cursor.position);
            } else if self.cursor.ended() {
                // TODO error
                self.end();
            } else {
                // TODO error
                self.handle_before_attribute_name();
            }
        } else if c == EQ {
            self.emit(EventSourceLocation::AttribNameEnd(
                self.cursor.position.to_position(),
            ));
            self.handle_before_attribute_value()
        } else {
            self.handle_before_attribute_name();
        }
    }

    // state_in_dir_arg
    fn handle_dir_arg(&mut self) {
        // skip past ':'
        self.cursor.increment();
    }

    // state_in_dir_modifier
    fn handle_dir_modifier(&mut self) {
        // skip past '.'
        self.cursor.increment();
    }

    // state_in_attr_name
    fn handle_attr_name(&mut self) {
        // TODO
        self.cursor.increment();
    }
    // state_in_before_attr_name
    fn handle_before_attribute_value(&mut self) {
        // skip past '='
        self.cursor.increment();
    }

    fn end(&mut self) {
        if !self.cursor.ended() {
            let start = self.cursor.position;
            let end = self.cursor.to_end();
            self.emit(EventSourceLocation::Text(SourceLocation::from_source(
                self.cursor.input,
                start.to_position(),
                end.to_position(),
            )));
        }

        self.emit(EventSourceLocation::End);
    }

    fn flush_text_section(&mut self) {
        if let Some(start) = self.section_start {
            if self.cursor.position.byte_index > start.byte_index {
                let end = self.cursor.position;
                self.emit(EventSourceLocation::Text(SourceLocation::from_source(
                    self.cursor.input,
                    start.to_position(),
                    end.to_position(),
                )));
            }
            self.section_start = None;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tokenizer::string::tokenize;

    #[test]
    fn test_simple_text() {
        let mut events = Vec::new();
        tokenize("hello world", |e: EventSourceLocation| {
            events.push(format!("{:?}", e));
        });

        assert!(!events.is_empty());
        println!("Simple text events: {:?}", events);
    }

    #[test]
    fn test_simple_html_tag() {
        let mut events = Vec::new();
        tokenize("<div>content</div>", |e: EventSourceLocation| {
            events.push(format!("{:?}", e));
        });

        assert!(!events.is_empty());
        println!("HTML tag events: {:#?}", events);
    }

    #[test]
    fn test_vue_interpolation() {
        let mut events = Vec::new();
        tokenize("{{ message }}", |e: EventSourceLocation| {
            events.push(format!("{:?}", e));
        });

        assert!(!events.is_empty());
        println!("Vue interpolation events: {:#?}", events);
    }

    #[test]
    fn test_unicode() {
        let mut events = Vec::new();
        tokenize("<div>张张张张张张</div>", |e: EventSourceLocation| {
            events.push(format!("{:?}", e));
        });

        assert!(!events.is_empty());
        println!("Vue directive events: {:#?}", events);
    }
}