nativeview-cli 0.1.0-rc.1

Developer CLI (`nv`) for Native View Engine: dev hot-reload, builds, pairing, SDK management, app scaffolding, and docs.
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
//! Zero-dependency Rust tokenizer producing compact struct-of-arrays tokens (13 B/token: a kind
//! byte plus `u32` start/end offsets and matching-delimiter index), with nested block comments,
//! raw/escaped strings, lifetime vs. char disambiguation and delimiter balance verification.

/// Token class; the token text is `src[start..end]`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub(crate) enum Tok {
    Ident,
    Punct,
    /// `"..."` string literal.
    Str,
    /// `r"..."` / `r#"..."#` raw string literal.
    RawStr,
    /// Integer or float literal (any radix, `_` separators, suffix).
    Number,
    /// Char, byte, byte-string and C-string literals, lifetimes.
    OtherLit,
}

/// Owned token table of one source file (no borrow of the source, so it can be cached).
#[derive(Debug, Clone, Default)]
pub struct Tokens {
    kinds: Vec<Tok>,
    starts: Vec<u32>,
    ends: Vec<u32>,
    /// Matching closing-delimiter index for `{`, `(`, `[` (`0` for every other token).
    close: Vec<u32>,
}

impl Tokens {
    fn with_capacity(cap: usize) -> Self {
        Self {
            kinds: Vec::with_capacity(cap),
            starts: Vec::with_capacity(cap),
            ends: Vec::with_capacity(cap),
            close: Vec::with_capacity(cap),
        }
    }

    #[inline]
    fn push(&mut self, kind: Tok, start: usize, end: usize) {
        self.kinds.push(kind);
        self.starts.push(start as u32);
        self.ends.push(end as u32);
        self.close.push(0);
    }
}

/// A token table paired with the source it was lexed from.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Lex<'a> {
    pub(crate) src: &'a str,
    tokens: &'a Tokens,
}

impl<'a> Lex<'a> {
    pub(crate) fn new(src: &'a str, tokens: &'a Tokens) -> Self {
        Self { src, tokens }
    }

    #[inline]
    pub(crate) fn len(self) -> usize {
        self.tokens.kinds.len()
    }

    #[inline]
    pub(crate) fn kind(self, i: usize) -> Tok {
        self.tokens.kinds[i]
    }

    #[inline]
    pub(crate) fn start(self, i: usize) -> usize {
        self.tokens.starts[i] as usize
    }

    #[inline]
    pub(crate) fn text(self, i: usize) -> &'a str {
        let (s, e) = (self.tokens.starts[i] as usize, self.tokens.ends[i] as usize);
        &self.src[s..e]
    }

    /// Index of the closing delimiter matching the opener at `i`.
    #[inline]
    pub(crate) fn close(self, i: usize) -> usize {
        self.tokens.close[i] as usize
    }

    /// Whether token `i` exists and is the ASCII punctuation `c`.
    #[inline]
    pub(crate) fn is_punct(self, i: usize, c: u8) -> bool {
        i < self.len() && self.kind(i) == Tok::Punct && self.src.as_bytes()[self.start(i)] == c
    }

    #[inline]
    pub(crate) fn ident(self, i: usize) -> Option<&'a str> {
        (i < self.len() && self.kind(i) == Tok::Ident).then(|| self.text(i))
    }

    /// Token `i` of `self` equals token `j` of `other` (kind and text).
    #[inline]
    pub(crate) fn same(self, i: usize, other: Lex<'_>, j: usize) -> bool {
        self.kind(i) == other.kind(j) && self.text(i) == other.text(j)
    }

    /// `self[i..i_end]` equals `other[j..j + (i_end - i)]` (false when `other` is too short).
    pub(crate) fn same_range(self, i: usize, i_end: usize, other: Lex<'_>, j: usize) -> bool {
        let n = i_end - i;
        j + n <= other.len()
            && self.tokens.kinds[i..i_end] == other.tokens.kinds[j..j + n]
            && (0..n).all(|k| self.text(i + k) == other.text(j + k))
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum LexError {
    UnterminatedString,
    UnterminatedBlockComment,
    UnterminatedChar,
    UnbalancedDelimiter,
    InvalidEscape,
    SourceTooLarge,
}

impl LexError {
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            Self::UnterminatedString => "unterminated string literal",
            Self::UnterminatedBlockComment => "unterminated block comment",
            Self::UnterminatedChar => "unterminated char literal",
            Self::UnbalancedDelimiter => "unbalanced delimiter",
            Self::InvalidEscape => "invalid string escape",
            Self::SourceTooLarge => "source file exceeds 4 GiB",
        }
    }
}

/// ASCII identifier start, or any non-ASCII lead byte (refined per char by [`ident_char`]).
fn is_ident_start(b: u8) -> bool {
    b.is_ascii_alphabetic() || b == b'_' || !b.is_ascii()
}

fn is_ident_continue(b: u8) -> bool {
    b.is_ascii_alphanumeric() || b == b'_'
}

/// Width of the identifier char at `pos` (`XID`-approximated for non-ASCII), or `None`.
fn ident_char(src: &str, pos: usize, first: bool) -> Option<usize> {
    let b = *src.as_bytes().get(pos)?;
    if b.is_ascii() {
        return (if first {
            is_ident_start(b)
        } else {
            is_ident_continue(b)
        })
        .then_some(1);
    }
    let c = src[pos..].chars().next()?;
    (c.is_alphanumeric() && !(first && c.is_numeric())).then(|| c.len_utf8())
}

/// Tokenizes `src`. Initial capacity `len / 4` covers dense Rust (one token per ~4-5 bytes) so
/// the four columns rarely reallocate.
pub(crate) fn tokenize(src: &str) -> Result<Tokens, LexError> {
    let bytes = src.as_bytes();
    let len = bytes.len();
    if len > u32::MAX as usize {
        return Err(LexError::SourceTooLarge);
    }
    let mut pos = 0usize;
    let mut out = Tokens::with_capacity((len / 4).max(16));
    let mut delim_stack: Vec<(u8, u32)> = Vec::with_capacity(64);

    while pos < len {
        let b = bytes[pos];
        let start = pos;

        // Whitespace (ASCII and Unicode).
        if b.is_ascii_whitespace() {
            pos += 1;
            continue;
        }
        if !b.is_ascii() {
            let Some(ch) = src[pos..].chars().next() else {
                break;
            };
            if ch.is_whitespace() {
                pos += ch.len_utf8();
                continue;
            }
        }

        // Line comment.
        if b == b'/' && bytes.get(pos + 1) == Some(&b'/') {
            while pos < len && bytes[pos] != b'\n' {
                pos += 1;
            }
            continue;
        }

        // Nested block comment.
        if b == b'/' && bytes.get(pos + 1) == Some(&b'*') {
            pos += 2;
            let mut depth = 1usize;
            while pos < len && depth > 0 {
                match (bytes[pos], bytes.get(pos + 1)) {
                    (b'/', Some(b'*')) => (depth, pos) = (depth + 1, pos + 2),
                    (b'*', Some(b'/')) => (depth, pos) = (depth - 1, pos + 2),
                    _ => pos += 1,
                }
            }
            if depth != 0 {
                return Err(LexError::UnterminatedBlockComment);
            }
            continue;
        }

        if b == b'"' {
            pos = scan_quoted_string(bytes, pos)?;
            out.push(Tok::Str, start, pos);
            continue;
        }

        // Raw string (`r"..."`, `r#"..."#`) or raw identifier (`r#ident`).
        if b == b'r' {
            if let Some(end) = try_scan_raw_string(bytes, pos)? {
                out.push(Tok::RawStr, start, end);
                pos = end;
                continue;
            }
            if bytes.get(pos + 1) == Some(&b'#')
                && bytes.get(pos + 2).is_some_and(|&c| is_ident_start(c))
            {
                pos += 2;
                while pos < len && is_ident_continue(bytes[pos]) {
                    pos += 1;
                }
                out.push(Tok::Ident, start, pos);
                continue;
            }
        }

        // Byte / C strings and byte chars (`b"..."`, `br#"..."#`, `c"..."`, `b'a'`).
        if b == b'b' || b == b'c' {
            let end = if bytes.get(pos + 1) == Some(&b'"') {
                Some(scan_quoted_string(bytes, pos + 1)?)
            } else if bytes.get(pos + 1) == Some(&b'r') {
                try_scan_raw_string(bytes, pos + 1)?
            } else if b == b'b' && bytes.get(pos + 1) == Some(&b'\'') {
                Some(try_scan_char_literal(src, pos + 1)?.ok_or(LexError::UnterminatedChar)?)
            } else {
                None
            };
            if let Some(end) = end {
                out.push(Tok::OtherLit, start, end);
                pos = end;
                continue;
            }
        }

        // Char literal or lifetime (`'a'`, `'\n'`, `'static`).
        if b == b'\'' {
            if let Some(end) = try_scan_char_literal(src, pos)? {
                out.push(Tok::OtherLit, start, end);
                pos = end;
                continue;
            }
            if bytes.get(pos + 1).is_some_and(|&c| is_ident_start(c)) {
                pos += 2;
                while pos < len && is_ident_continue(bytes[pos]) {
                    pos += 1;
                }
                out.push(Tok::OtherLit, start, pos);
                continue;
            }
            out.push(Tok::Punct, start, pos + 1);
            pos += 1;
            continue;
        }

        if b.is_ascii_digit() {
            pos = scan_number(bytes, pos);
            out.push(Tok::Number, start, pos);
            continue;
        }

        if let Some(width) = ident_char(src, pos, true) {
            pos += width;
            while let Some(width) = ident_char(src, pos, false) {
                pos += width;
            }
            out.push(Tok::Ident, start, pos);
            continue;
        }

        // Punctuation + delimiter balancing.
        let width = src[pos..].chars().next().map_or(1, char::len_utf8);
        let idx = out.kinds.len() as u32;
        match b {
            b'{' | b'(' | b'[' => delim_stack.push((b, idx)),
            b'}' | b')' | b']' => {
                let expected = match b {
                    b'}' => b'{',
                    b')' => b'(',
                    _ => b'[',
                };
                match delim_stack.pop() {
                    Some((open, open_idx)) if open == expected => {
                        out.close[open_idx as usize] = idx;
                    }
                    _ => return Err(LexError::UnbalancedDelimiter),
                }
            }
            _ => {}
        }
        out.push(Tok::Punct, start, pos + width);
        pos += width;
    }

    if !delim_stack.is_empty() {
        return Err(LexError::UnbalancedDelimiter);
    }
    Ok(out)
}

/// Scans a number literal from its first digit. `.` and signed exponents belong only to decimal
/// literals (`0xfe-1` is `0xfe`, `-`, `1`); a suffix (`u8`, `f32`) ends both.
fn scan_number(bytes: &[u8], mut pos: usize) -> usize {
    let len = bytes.len();
    let radix_prefixed =
        bytes[pos] == b'0' && matches!(bytes.get(pos + 1), Some(b'x' | b'o' | b'b'));
    pos += if radix_prefixed { 2 } else { 1 };
    let decimal = !radix_prefixed;
    let (mut seen_dot, mut seen_exp, mut in_suffix) = (false, false, false);
    while pos < len {
        let c = bytes[pos];
        let next = bytes.get(pos + 1).copied();
        if c.is_ascii_digit() || c == b'_' {
            pos += 1;
        } else if c.is_ascii_alphabetic() {
            let exponent = decimal
                && !in_suffix
                && !seen_exp
                && (c == b'e' || c == b'E')
                && next.is_some_and(|n| n.is_ascii_digit() || n == b'_' || n == b'+' || n == b'-');
            if exponent {
                seen_exp = true;
            } else {
                in_suffix = true;
            }
            pos += 1;
        } else if (c == b'+' || c == b'-')
            && decimal
            && seen_exp
            && !in_suffix
            && matches!(bytes[pos - 1], b'e' | b'E')
            && next.is_some_and(|n| n.is_ascii_digit())
        {
            pos += 1;
        } else if c == b'.'
            && decimal
            && !seen_dot
            && !seen_exp
            && !in_suffix
            && next.is_none_or(|n| n != b'.' && !is_ident_start(n))
        {
            seen_dot = true;
            pos += 1;
        } else {
            break;
        }
    }
    pos
}

fn scan_quoted_string(bytes: &[u8], open_quote: usize) -> Result<usize, LexError> {
    let mut pos = open_quote + 1;
    while pos < bytes.len() {
        match bytes[pos] {
            b'\\' => pos += 2,
            b'"' => return Ok(pos + 1),
            _ => pos += 1,
        }
    }
    Err(LexError::UnterminatedString)
}

/// Scans `r"..."` or `r#"..."#` starting at `r_pos`. Returns `Ok(None)` if not a raw string.
fn try_scan_raw_string(bytes: &[u8], r_pos: usize) -> Result<Option<usize>, LexError> {
    let len = bytes.len();
    let mut pos = r_pos + 1;
    let mut hashes = 0usize;
    while pos < len && bytes[pos] == b'#' {
        hashes += 1;
        pos += 1;
    }
    if pos >= len || bytes[pos] != b'"' {
        return Ok(None);
    }
    pos += 1;
    while pos < len {
        if bytes[pos] == b'"'
            && bytes
                .get(pos + 1..pos + 1 + hashes)
                .is_some_and(|h| h.iter().all(|&c| c == b'#'))
        {
            return Ok(Some(pos + 1 + hashes));
        }
        pos += 1;
    }
    Err(LexError::UnterminatedString)
}

fn try_scan_char_literal(src: &str, quote_pos: usize) -> Result<Option<usize>, LexError> {
    let bytes = src.as_bytes();
    let len = bytes.len();
    let mut pos = quote_pos + 1;
    if pos >= len {
        return Err(LexError::UnterminatedChar);
    }
    if bytes[pos] == b'\\' {
        // Escaped char literal (`'\n'`, `'\''`, `'\x7f'`, `'\u{1F600}'`).
        pos += 2;
        while pos < len && bytes[pos] != b'\'' && bytes[pos] != b'\n' {
            pos += 1;
        }
        return if pos < len && bytes[pos] == b'\'' {
            Ok(Some(pos + 1))
        } else {
            Err(LexError::UnterminatedChar)
        };
    }
    let Some(ch) = src[pos..].chars().next() else {
        return Err(LexError::UnterminatedChar);
    };
    let after = pos + ch.len_utf8();
    Ok((bytes.get(after) == Some(&b'\'')).then_some(after + 1))
}