nsi-parse 0.2.0

Fast parser for Nodal Scene Interface – ɴsɪ – streams and Lua scenes.
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
542
543
544
545
546
547
548
//! Parameter values, and the scratch they are parsed into.
//!
//! A statement's parameters are read in two passes: everything lands in
//! per-type buffers first, then the [`nsi_ffi_wrap::Arg`]s are built
//! over disjoint ranges of those buffers. One pass would not borrow-check
//! -- an `Arg` holds a slice while the next parameter still wants to
//! push -- and it is also what keeps the steady state allocation-free,
//! since the buffers are cleared rather than freed.

use crate::{
    Error,
    lex::{Lexer, Token},
};
use alloc_free::{SmallArgs, SmallStrs};
use core::num::NonZeroUsize;
use nsi_ffi_wrap::{
    Arg, ArgData, StringSlice,
    argument::{
        Color3F32Slice, IntegerI32Slice, IntegerI64Slice, Matrix4F32Slice,
        Matrix4F64Slice, Normal3F32Slice, Point3F32Slice, Point4F32Slice,
        RealF32Slice, RealF64Slice, Vector3F32Slice,
    },
};
use nsi_trait::Action;
use std::borrow::Cow;

/// Which buffer a parameter's values live in, and how to rebuild it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Base {
    F32,
    F64,
    I32,
    I64,
    String,
    Color,
    Point,
    Vector,
    Normal,
    MatrixF32,
    MatrixF64,
    /// A homogeneous point, four floats: 3Delight 2.9.210's `hpoint`.
    HPoint,
}

impl Base {
    /// The ɴsɪ stream spelling, without flags or array suffix.
    fn parse(name: &str) -> Option<Self> {
        Some(match name {
            "float" => Self::F32,
            "double" => Self::F64,
            "int" => Self::I32,
            "int64" => Self::I64,
            "string" => Self::String,
            "color" => Self::Color,
            "point" => Self::Point,
            "vector" => Self::Vector,
            "normal" => Self::Normal,
            "matrix" => Self::MatrixF32,
            "doublematrix" => Self::MatrixF64,
            "hpoint" => Self::HPoint,
            _ => return None,
        })
    }

    /// Whether values go in the float, double, integer or string buffer.
    const fn storage(self) -> Storage {
        match self {
            Self::F32
            | Self::Color
            | Self::Point
            | Self::Vector
            | Self::Normal
            | Self::MatrixF32
            | Self::HPoint => Storage::F32,
            Self::F64 | Self::MatrixF64 => Storage::F64,
            Self::I32 => Storage::I32,
            Self::I64 => Storage::I64,
            Self::String => Storage::String,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Storage {
    F32,
    F64,
    I32,
    I64,
    String,
}

/// One parsed parameter, as a range into the scratch buffers.
struct Descriptor<'a> {
    name: Cow<'a, str>,
    base: Base,
    /// ɴsɪ's `array_len`, when the type carried a `[n]` suffix.
    array_length: Option<NonZeroUsize>,
    /// `v`, `f` and `l` prefixes.
    per_vertex: bool,
    per_face: bool,
    linear: bool,
    start: usize,
    end: usize,
}

/// Reused buffers for one statement's parameters.
#[derive(Default)]
pub(crate) struct Scratch<'a> {
    descriptors: Vec<Descriptor<'a>>,
    f32s: Vec<f32>,
    f64s: Vec<f64>,
    i32s: Vec<i32>,
    i64s: Vec<i64>,
    /// Borrowed from the input unless the literal carried escapes.
    ///
    /// Bytes: an ɴsɪ string value is whatever the C API was handed, and
    /// a file name is not required to be UTF-8.
    strings: Vec<Cow<'a, [u8]>>,
    /// Colour, point, vector and normal, which the argument types take
    /// as triples rather than as a flat slice.
    triples: Vec<[f32; 3]>,
    /// Homogeneous points, likewise taken as quadruples.
    quads: Vec<[f32; 4]>,
    matrices_f32: Vec<[f32; 16]>,
    matrices_f64: Vec<[f64; 16]>,
}

impl<'a> Scratch<'a> {
    /// Drop the previous statement's parameters, keeping the capacity.
    pub(crate) fn clear(&mut self) {
        self.descriptors.clear();
        self.f32s.clear();
        self.f64s.clear();
        self.i32s.clear();
        self.i64s.clear();
        self.strings.clear();
        self.triples.clear();
        self.quads.clear();
        self.matrices_f32.clear();
        self.matrices_f64.clear();
    }

    /// ɴsɪ's `RenderControl` action, if the parameters name one.
    pub(crate) fn action(&self) -> Option<Action> {
        let descriptor = self.descriptors.iter().find(|d| {
            d.name == "action"
                && d.base == Base::String
                // A zero-count `action` would otherwise read the *next*
                // parameter's first string as the action.
                && d.end - d.start == 1
        })?;
        // Byte literals: string values are bytes, and an action that
        // is not one of these six is `None` either way.
        match self.strings.get(descriptor.start)?.as_ref() {
            b"start" => Some(Action::Start),
            b"stop" => Some(Action::Stop),
            b"suspend" => Some(Action::Suspend),
            b"resume" => Some(Action::Resume),
            b"wait" => Some(Action::Wait),
            b"synchronize" => Some(Action::Synchronize),
            _ => None,
        }
    }

    /// Build the arguments and hand them to `apply`.
    ///
    /// They borrow the scratch, so they cannot outlive the call -- which
    /// is exactly the lifetime `Nsi`'s methods want.
    pub(crate) fn with_args<T, E>(
        &self,
        apply: impl FnOnce(&[Arg<'_, 'static>]) -> Result<T, E>,
    ) -> Result<T, E> {
        self.with_args_except(None, apply)
    }

    /// The same, without the parameter of the given name.
    ///
    /// `RenderControl` needs it: the action is passed as an argument to
    /// `Nsi::render_control`, which appends it again.
    pub(crate) fn with_args_except<T, E>(
        &self,
        skip: Option<&str>,
        apply: impl FnOnce(&[Arg<'_, 'static>]) -> Result<T, E>,
    ) -> Result<T, E> {
        // `StringSlice` takes anything `Into<Vec<u8>>`, so byte slices
        // pass straight through. The view is on the stack:
        // heap-allocating it would put an allocation back on every
        // statement that carries a string.
        let borrowed: SmallStrs<'_> =
            self.strings.iter().map(Cow::as_ref).collect();

        let args: SmallArgs<'_> = self
            .descriptors
            .iter()
            .filter(|d| Some(d.name.as_ref()) != skip)
            .map(|d| {
                let data = match d.base {
                    Base::F32 => ArgData::from(RealF32Slice::new(
                        &self.f32s[d.start..d.end],
                    )),
                    Base::F64 => ArgData::from(RealF64Slice::new(
                        &self.f64s[d.start..d.end],
                    )),
                    Base::I32 => ArgData::from(IntegerI32Slice::new(
                        &self.i32s[d.start..d.end],
                    )),
                    Base::I64 => ArgData::from(IntegerI64Slice::new(
                        &self.i64s[d.start..d.end],
                    )),
                    Base::String => ArgData::from(StringSlice::new(
                        &borrowed[d.start..d.end],
                    )),
                    Base::Color => ArgData::from(Color3F32Slice::new(
                        &self.triples[d.start..d.end],
                    )),
                    Base::Point => ArgData::from(Point3F32Slice::new(
                        &self.triples[d.start..d.end],
                    )),
                    Base::Vector => ArgData::from(Vector3F32Slice::new(
                        &self.triples[d.start..d.end],
                    )),
                    Base::Normal => ArgData::from(Normal3F32Slice::new(
                        &self.triples[d.start..d.end],
                    )),
                    Base::MatrixF32 => ArgData::from(Matrix4F32Slice::new(
                        &self.matrices_f32[d.start..d.end],
                    )),
                    Base::MatrixF64 => ArgData::from(Matrix4F64Slice::new(
                        &self.matrices_f64[d.start..d.end],
                    )),
                    Base::HPoint => ArgData::from(Point4F32Slice::new(
                        &self.quads[d.start..d.end],
                    )),
                };

                let mut arg = Arg::new(&d.name, data);
                if let Some(length) = d.array_length {
                    arg = arg.array_len(length);
                }
                if d.per_vertex {
                    arg = arg.per_vertex();
                }
                if d.per_face {
                    arg = arg.per_face();
                }
                if d.linear {
                    arg = arg.linear_interpolation();
                }
                arg
            })
            .collect();

        apply(&args)
    }
}

/// Read one parameter -- `"name" "type" count value...` -- into scratch.
pub(crate) fn read<'a, E>(
    name: Cow<'a, str>,
    lexer: &mut Lexer<'a>,
    scratch: &mut Scratch<'a>,
) -> Result<(), Error<E>> {
    let Some(Token::Quoted(spelling)) = lexer.next_token()? else {
        return Err(Error::Syntax {
            offset: lexer.offset(),
            expected: "a parameter type",
        });
    };
    let type_offset = lexer.offset();
    // A type spelling names something, so it must be text.
    let spelling = spelling.into_ident(type_offset)?;

    let (base, array_length, per_vertex, per_face, linear) =
        parse_type(spelling.as_str()).ok_or(Error::Syntax {
            offset: type_offset,
            expected: "a known ɴsɪ type",
        })?;

    // The count is authoritative, not decorative: given
    // `"P" "point" 1 [ 0 0 0 1 2 3 ]` the renderer warns and keeps one
    // point. Ignoring it here would silently yield two.
    let count: usize = match lexer.next_token()? {
        Some(Token::Word(text)) => text.parse().map_err(|_| Error::Syntax {
            offset: lexer.offset(),
            expected: "an element count",
        })?,
        _ => {
            return Err(Error::Syntax {
                offset: lexer.offset(),
                expected: "an element count",
            });
        }
    };
    let count_offset = lexer.offset();

    // Where this parameter's flat values begin. Folding must move only
    // its own run: an earlier `float` parameter in the same statement
    // still owns the values before it.
    let flat = (scratch.f32s.len(), scratch.f64s.len());

    let start = start_of(scratch, base);
    read_values(lexer, scratch, base)?;
    fold(scratch, base, flat).map_err(|()| Error::Syntax {
        offset: type_offset,
        expected: "a whole number of elements for the type",
    })?;
    let end = start_of(scratch, base);

    // Elements, times the array length the type spelling carried.
    let declared = count
        .checked_mul(array_length.map_or(1, NonZeroUsize::get))
        .ok_or(Error::Syntax {
            offset: count_offset,
            expected: "an element count that does not overflow",
        })?;
    if end - start != declared {
        return Err(Error::Syntax {
            offset: count_offset,
            expected: "as many values as the count declares",
        });
    }

    scratch.descriptors.push(Descriptor {
        name,
        base,
        array_length,
        per_vertex,
        per_face,
        linear,
        start,
        end,
    });

    Ok(())
}

/// The current length of the buffer `base` is *indexed by*.
///
/// For a tuple type that is the folded buffer, not the flat one the
/// values are read into.
fn start_of(scratch: &Scratch<'_>, base: Base) -> usize {
    match base {
        Base::Color | Base::Point | Base::Vector | Base::Normal => {
            scratch.triples.len()
        }
        Base::HPoint => scratch.quads.len(),
        Base::MatrixF32 => scratch.matrices_f32.len(),
        Base::MatrixF64 => scratch.matrices_f64.len(),
        Base::F32 => scratch.f32s.len(),
        Base::F64 => scratch.f64s.len(),
        Base::I32 => scratch.i32s.len(),
        Base::I64 => scratch.i64s.len(),
        Base::String => scratch.strings.len(),
    }
}

/// Move a tuple type's flat values into its typed buffer.
///
/// The argument types take `&[[f32; 3]]` and `&[[f32; 16]]`, so the
/// flat run a stream writes has to be grouped. A run that is not a whole
/// number of elements is malformed input, not something to truncate.
fn fold(
    scratch: &mut Scratch<'_>,
    base: Base,
    (flat_f32, flat_f64): (usize, usize),
) -> Result<(), ()> {
    match base {
        Base::Color | Base::Point | Base::Vector | Base::Normal => {
            let run = &scratch.f32s[flat_f32..];
            if !run.len().is_multiple_of(3) {
                return Err(());
            }
            for chunk in run.as_chunks::<3>().0 {
                scratch.triples.push([chunk[0], chunk[1], chunk[2]]);
            }
            scratch.f32s.truncate(flat_f32);
        }
        Base::HPoint => {
            let run = &scratch.f32s[flat_f32..];
            if !run.len().is_multiple_of(4) {
                return Err(());
            }
            for chunk in run.as_chunks::<4>().0 {
                scratch.quads.push(*chunk);
            }
            scratch.f32s.truncate(flat_f32);
        }
        Base::MatrixF32 => {
            let run = &scratch.f32s[flat_f32..];
            if !run.len().is_multiple_of(16) {
                return Err(());
            }
            for chunk in run.as_chunks::<16>().0 {
                scratch.matrices_f32.push(*chunk);
            }
            scratch.f32s.truncate(flat_f32);
        }
        Base::MatrixF64 => {
            let run = &scratch.f64s[flat_f64..];
            if !run.len().is_multiple_of(16) {
                return Err(());
            }
            for chunk in run.as_chunks::<16>().0 {
                scratch.matrices_f64.push(*chunk);
            }
            scratch.f64s.truncate(flat_f64);
        }
        _ => {}
    }
    Ok(())
}

/// Read a bare scalar or a bracketed list into the right buffer.
fn read_values<'a, E>(
    lexer: &mut Lexer<'a>,
    scratch: &mut Scratch<'a>,
    base: Base,
) -> Result<(), Error<E>> {
    match lexer.next_token()? {
        Some(Token::Open) => loop {
            match lexer.next_token()? {
                Some(Token::Close) => return Ok(()),
                Some(token) => {
                    let offset = lexer.offset();
                    push(token, scratch, base, offset)?;
                }
                None => {
                    return Err(Error::Syntax {
                        offset: lexer.offset(),
                        expected: "a closing bracket",
                    });
                }
            }
        },
        Some(token) => {
            let offset = lexer.offset();
            push(token, scratch, base, offset)
        }
        None => Err(Error::Syntax {
            offset: lexer.offset(),
            expected: "a parameter value",
        }),
    }
}

/// Push one value, parsed as the parameter's storage type.
fn push<'a, E>(
    token: Token<'a>,
    scratch: &mut Scratch<'a>,
    base: Base,
    offset: usize,
) -> Result<(), Error<E>> {
    let bad = Error::Syntax {
        offset,
        expected: "a value of the parameter's type",
    };

    match (base.storage(), token) {
        (Storage::String, Token::Quoted(text)) => {
            // ɴsɪ strings become C strings, so an interior NUL would
            // panic in `CString::new`. 3Delight truncates there; this
            // refuses, because silently dropping half a string is the
            // kind of quiet wrong answer the crate exists to avoid.
            let text = text.into_cow();
            if text.contains(&0) {
                return Err(Error::Syntax {
                    offset,
                    expected: "a string without an interior NUL",
                });
            }
            scratch.strings.push(text);
            Ok(())
        }
        (Storage::F32, Token::Word(text)) => {
            scratch.f32s.push(text.parse().map_err(|_| bad)?);
            Ok(())
        }
        (Storage::F64, Token::Word(text)) => {
            scratch.f64s.push(text.parse().map_err(|_| bad)?);
            Ok(())
        }
        (Storage::I32, Token::Word(text)) => {
            scratch.i32s.push(text.parse().map_err(|_| bad)?);
            Ok(())
        }
        (Storage::I64, Token::Word(text)) => {
            scratch.i64s.push(text.parse().map_err(|_| bad)?);
            Ok(())
        }
        _ => Err(bad),
    }
}

/// Split a stream type spelling into its parts.
///
/// `"v point"` is a per-vertex point; `"int[2]"` is a two-element array
/// of integers; `"fl normal"` carries two flags. The flag letters come
/// first, separated by a space, and the array length rides inside the
/// type name -- both as `specs/003` recorded them from the renderer.
fn parse_type(
    spelling: &str,
) -> Option<(Base, Option<NonZeroUsize>, bool, bool, bool)> {
    let (flags, rest) = match spelling.split_once(' ') {
        Some((flags, rest)) => (flags, rest),
        None => ("", spelling),
    };

    let mut per_vertex = false;
    let mut per_face = false;
    let mut linear = false;
    for letter in flags.chars() {
        match letter {
            'v' => per_vertex = true,
            'f' => per_face = true,
            'l' => linear = true,
            _ => return None,
        }
    }

    let (name, array_length) = match rest.split_once('[') {
        Some((name, length)) => {
            let length = length.strip_suffix(']')?.parse().ok()?;
            (name, Some(NonZeroUsize::new(length)?))
        }
        None => (rest, None),
    };

    Some((
        Base::parse(name)?,
        array_length,
        per_vertex,
        per_face,
        linear,
    ))
}

/// A stack-backed argument list.
///
/// A statement's arguments cannot live in [`Scratch`] -- they borrow it
/// -- so without this every statement would allocate one vector. Eight
/// covers the parameter counts a renderer writes; more spills to the
/// heap rather than failing.
mod alloc_free {
    use nsi_ffi_wrap::Arg;

    pub(super) type SmallArgs<'a> = smallvec::SmallVec<[Arg<'a, 'static>; 8]>;
    pub(super) type SmallStrs<'a> = smallvec::SmallVec<[&'a [u8]; 8]>;
}