graphcal-compiler 0.0.1-alpha.14

Type-safe, unit-aware, Git-friendly reactive programming language for engineering calculations
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
use crate::syntax::comments::{BlankLine, Comment, CommentBody, CommentDelimiter, SourceMetadata};
use crate::syntax::span::{Span, Spanned};
use crate::syntax::token::{LexicalItem, LexicalToken, Token, TriviaToken};
use logos::Logos;
use peek_cache::{PeekCache, SourceItem};
use std::num::NonZeroUsize;

const LEXER_MAX_LOOKAHEAD: NonZeroUsize = NonZeroUsize::new(3).unwrap();

/// A peekable wrapper around `logos::Lexer` that yields `(Token, Span)` pairs.
///
/// # Internal design
///
/// The inner `logos::Lexer` is **only** advanced by `read_next_token()`, which
/// adapts the tokenizer into the generic private peek cache. `Lexer` cannot read
/// or take the cache slots directly; the cache fields are private to a nested
/// module, and its public methods fill the first slot before returning or
/// consuming it.
///
/// ```text
///   cache = []             (initial / just consumed)
//////       ▼  cache calls read_next_token()
///   cache = [Token(_), ...] (token ready)
///   cache.eof_seen = true   (EOF)
//////       ▼  cache.next() takes the token
///   cache = [...]           (remaining lookahead)
/// ```
///
/// When the underlying `logos::Lexer` encounters an unrecognized character, the
/// span of the *first* such character is recorded in `first_error_span` and the
/// character is skipped. The parser surfaces this as a `ParseError::UnknownToken`
/// when a top-level `parse_*` entry point finishes, regardless of whether the
/// downstream parse happened to succeed.
pub struct Lexer<'src> {
    inner: logos::Lexer<'src, LexicalToken>,
    peek_cache: PeekCache<(Token, Span)>,
    source: &'src str,
    source_metadata: SourceMetadata,
    /// Span of the first unrecognized character encountered during lexing, if any.
    first_error_span: Option<Span>,
}

impl<'src> Lexer<'src> {
    #[must_use]
    pub fn new(source: &'src str) -> Self {
        Self {
            inner: LexicalToken::lexer(source),
            peek_cache: PeekCache::new(LEXER_MAX_LOOKAHEAD),
            source,
            source_metadata: SourceMetadata::default(),
            first_error_span: None,
        }
    }

    /// Peek at the next token without consuming it.
    pub fn peek(&mut self) -> Option<&Token> {
        self.peek_with_span().map(|(tok, _)| tok)
    }

    /// Peek at the token after the next token without consuming either one.
    pub fn peek_second(&mut self) -> Option<&Token> {
        self.peek_token_at(1)
    }

    /// Peek at the third token from the current position without consuming any token.
    pub fn peek_third(&mut self) -> Option<&Token> {
        self.peek_token_at(2)
    }

    /// Peek at the next token and its span without consuming it.
    ///
    /// This delegates to the cache, which fills its first slot before returning
    /// a reference to it.
    pub fn peek_with_span(&mut self) -> Option<(&Token, Span)> {
        let inner = &mut self.inner;
        let source = self.source;
        let source_metadata = &mut self.source_metadata;
        let first_error_span = &mut self.first_error_span;
        self.peek_cache
            .peek(|| read_next_token(inner, source, source_metadata, first_error_span))
            .map(|(tok, span)| (tok, *span))
    }

    fn peek_token_at(&mut self, offset: usize) -> Option<&Token> {
        self.peek_with_span_at(offset).map(|(tok, _)| tok)
    }

    fn peek_with_span_at(&mut self, offset: usize) -> Option<(&Token, Span)> {
        let inner = &mut self.inner;
        let source = self.source;
        let source_metadata = &mut self.source_metadata;
        let first_error_span = &mut self.first_error_span;
        self.peek_cache
            .peek_at(offset, || {
                read_next_token(inner, source, source_metadata, first_error_span)
            })
            .map(|(tok, span)| (tok, *span))
    }

    /// Return the span of the first unrecognized character encountered during lexing.
    ///
    /// Returns `None` if the lexer has not yet seen any invalid input. The value
    /// is set lazily as tokens are consumed; callers that need an up-to-date
    /// answer at a specific point should ensure lexing has progressed past the
    /// region of interest (e.g., by draining the remaining tokens).
    #[must_use]
    pub const fn first_error_span(&self) -> Option<Span> {
        self.first_error_span
    }

    /// Consume and return the next token and its span.
    ///
    /// The cache fills its first slot before taking from it, so this method
    /// cannot accidentally consume an uninitialized cache slot.
    pub fn next_token(&mut self) -> Option<(Token, Span)> {
        let inner = &mut self.inner;
        let source = self.source;
        let source_metadata = &mut self.source_metadata;
        let first_error_span = &mut self.first_error_span;
        self.peek_cache
            .next(|| read_next_token(inner, source, source_metadata, first_error_span))
    }

    /// Get the source text corresponding to a span.
    #[must_use]
    pub fn slice_at(&self, span: Span) -> &'src str {
        &self.source[span.offset()..span.offset() + span.len()]
    }

    /// Return the total length (in bytes) of the source string.
    #[must_use]
    pub const fn source_len(&self) -> usize {
        self.source.len()
    }

    #[must_use]
    pub fn into_source_metadata(self) -> SourceMetadata {
        self.source_metadata
    }
}

fn read_next_token(
    inner: &mut logos::Lexer<'_, LexicalToken>,
    source: &str,
    source_metadata: &mut SourceMetadata,
    first_error_span: &mut Option<Span>,
) -> SourceItem<(Token, Span)> {
    loop {
        let Some(result) = inner.next() else {
            break SourceItem::Eof;
        };
        let slice_span = inner.span();
        let span = Span::new(slice_span.start, slice_span.end - slice_span.start);
        match result.map(LexicalToken::classify) {
            Ok(LexicalItem::Trivia(TriviaToken::Whitespace)) => {
                record_blank_lines(source, span, source_metadata);
            }
            Ok(LexicalItem::Trivia(TriviaToken::Comment)) => {
                record_comment(source, span, source_metadata);
            }
            Ok(LexicalItem::Syntax(token)) => break SourceItem::Item((token, span)),
            Err(()) => {
                if first_error_span.is_none() {
                    *first_error_span = Some(span);
                }
            }
        }
    }
}

fn record_comment(source: &str, span: Span, source_metadata: &mut SourceMetadata) {
    let lexeme = &source[span.offset()..span.offset() + span.len()];
    let delimiter = comment_delimiter(lexeme);
    let body = CommentBody::new(&lexeme[delimiter.len()..]);
    source_metadata.push_comment(Spanned::new(Comment::new(delimiter, body), span));
}

fn comment_delimiter(lexeme: &str) -> CommentDelimiter {
    let bytes = lexeme.as_bytes();
    match (bytes.get(2).copied(), bytes.get(3).copied()) {
        (Some(b'/'), Some(b'/')) => CommentDelimiter::Line,
        (Some(b'/'), _) => CommentDelimiter::Doc,
        _ => CommentDelimiter::Line,
    }
}

fn record_blank_lines(source: &str, span: Span, source_metadata: &mut SourceMetadata) {
    let whitespace = &source[span.offset()..span.offset() + span.len()];
    let mut previous_line_ending_start: Option<usize> = None;
    let mut pos = 0;
    while pos < whitespace.len() {
        match line_ending_at(whitespace.as_bytes(), pos) {
            Some(line_ending) => {
                if let Some(start) = previous_line_ending_start {
                    let end = pos + line_ending.len();
                    source_metadata.push_blank_line(BlankLine::new(Span::new(
                        span.offset() + start,
                        end - start,
                    )));
                }
                previous_line_ending_start = Some(pos);
                pos += line_ending.len();
            }
            None => pos += 1,
        }
    }
}

fn line_ending_at(bytes: &[u8], pos: usize) -> Option<LineEnding> {
    match bytes.get(pos).copied() {
        Some(b'\n') => Some(LineEnding::Lf),
        Some(b'\r') if bytes.get(pos + 1) == Some(&b'\n') => Some(LineEnding::CrLf),
        Some(b'\r') => Some(LineEnding::Cr),
        _ => None,
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum LineEnding {
    Lf,
    CrLf,
    Cr,
}

impl LineEnding {
    const fn len(self) -> usize {
        match self {
            Self::Lf | Self::Cr => 1,
            Self::CrLf => 2,
        }
    }
}

mod peek_cache {
    use std::collections::VecDeque;
    use std::num::NonZeroUsize;

    pub(super) struct PeekCache<T> {
        items: VecDeque<T>,
        eof_seen: bool,
        max_lookahead: NonZeroUsize,
    }

    impl<T> PeekCache<T> {
        pub(super) fn new(max_lookahead: NonZeroUsize) -> Self {
            Self {
                items: VecDeque::with_capacity(max_lookahead.get()),
                eof_seen: false,
                max_lookahead,
            }
        }

        pub(super) fn peek<F>(&mut self, load_next: F) -> Option<&T>
        where
            F: FnMut() -> SourceItem<T>,
        {
            self.peek_at(0, load_next)
        }

        pub(super) fn peek_at<F>(&mut self, offset: usize, load_next: F) -> Option<&T>
        where
            F: FnMut() -> SourceItem<T>,
        {
            debug_assert!(offset < self.max_lookahead.get());
            if offset >= self.max_lookahead.get() {
                return None;
            }

            self.fill_until(offset, load_next);
            self.items.get(offset)
        }

        pub(super) fn next<F>(&mut self, load_next: F) -> Option<T>
        where
            F: FnMut() -> SourceItem<T>,
        {
            self.fill_until(0, load_next);
            self.items.pop_front()
        }

        fn fill_until<F>(&mut self, offset: usize, mut load_next: F)
        where
            F: FnMut() -> SourceItem<T>,
        {
            // Private callers preserve this invariant: `peek_at` validates
            // arbitrary offsets, and `next` only asks for slot 0, which is
            // guaranteed by the nonzero lookahead bound.
            debug_assert!(offset < self.max_lookahead.get());
            while self.items.len() <= offset && !self.eof_seen {
                match load_next() {
                    SourceItem::Item(item) => self.items.push_back(item),
                    SourceItem::Eof => self.eof_seen = true,
                }
            }
        }
    }

    pub(super) enum SourceItem<T> {
        Item(T),
        Eof,
    }
}

#[cfg(test)]
mod tests {
    use super::peek_cache::SourceItem;
    use super::*;

    #[test]
    fn lexer_yields_tokens_with_spans() {
        let input = "param x = 1.0;";
        let mut lexer = Lexer::new(input);

        let (tok, span) = lexer.next_token().unwrap();
        assert_eq!(tok, Token::Param);
        assert_eq!(lexer.slice_at(span), "param");

        let (tok, span) = lexer.next_token().unwrap();
        assert_eq!(tok, Token::Ident);
        assert_eq!(lexer.slice_at(span), "x");

        let (tok, span) = lexer.next_token().unwrap();
        assert_eq!(tok, Token::Eq);
        assert_eq!(lexer.slice_at(span), "=");

        let (tok, span) = lexer.next_token().unwrap();
        assert_eq!(tok, Token::Number);
        assert_eq!(lexer.slice_at(span), "1.0");

        let (tok, _) = lexer.next_token().unwrap();
        assert_eq!(tok, Token::Semicolon);

        assert!(lexer.next_token().is_none());
    }

    #[test]
    fn peek_does_not_consume() {
        let input = "param x";
        let mut lexer = Lexer::new(input);

        assert_eq!(lexer.peek(), Some(&Token::Param));
        assert_eq!(lexer.peek(), Some(&Token::Param));

        lexer.next_token();
        assert_eq!(lexer.peek(), Some(&Token::Ident));
    }

    #[test]
    fn peek_with_span_returns_correct_span() {
        let input = "const node g0 = 9.80665;";
        let mut lexer = Lexer::new(input);

        let (tok, span) = lexer.peek_with_span().unwrap();
        assert_eq!(*tok, Token::Const);
        assert_eq!(lexer.slice_at(span), "const");
    }

    #[test]
    fn exhaust_lexer() {
        let input = "42";
        let mut lexer = Lexer::new(input);
        let (tok, _) = lexer.next_token().unwrap();
        assert_eq!(tok, Token::Number);
        assert!(lexer.next_token().is_none());
        assert!(lexer.peek().is_none());
    }

    #[test]
    fn next_token_without_prior_peek() {
        // next_token() internally peeks first, so calling it directly
        // (without an explicit peek()) must work identically.
        let input = "param x";
        let mut lexer = Lexer::new(input);

        let (tok, span) = lexer.next_token().unwrap();
        assert_eq!(tok, Token::Param);
        assert_eq!(lexer.slice_at(span), "param");

        let (tok, span) = lexer.next_token().unwrap();
        assert_eq!(tok, Token::Ident);
        assert_eq!(lexer.slice_at(span), "x");

        assert!(lexer.next_token().is_none());
    }

    #[test]
    fn lookahead_does_not_consume() {
        let input = "param x = 1.0;";
        let mut lexer = Lexer::new(input);

        assert_eq!(lexer.peek(), Some(&Token::Param));
        assert_eq!(lexer.peek_second(), Some(&Token::Ident));
        assert_eq!(lexer.peek_third(), Some(&Token::Eq));

        let (token, _) = lexer.next_token().unwrap();
        assert_eq!(token, Token::Param);
        let (token, _) = lexer.next_token().unwrap();
        assert_eq!(token, Token::Ident);
    }

    #[test]
    fn unknown_character_is_recorded() {
        // `§` is not part of the grammar. The lexer should skip it while
        // recording the span for the parser to surface as `UnknownToken`.
        let input = "param §x = 1.0;";
        let mut lexer = Lexer::new(input);
        // Drain all tokens so the lexer encounters the stray character.
        while lexer.next_token().is_some() {}
        let err_span = lexer.first_error_span().expect("expected an error span");
        assert_eq!(
            &input[err_span.offset()..err_span.offset() + err_span.len()],
            "§"
        );
    }

    #[test]
    fn only_first_unknown_character_is_recorded() {
        // Multiple stray characters: the lexer reports only the first so that
        // the diagnostic points at the original culprit.
        let input = "§§";
        let mut lexer = Lexer::new(input);
        assert!(lexer.next_token().is_none());
        let err_span = lexer.first_error_span().expect("expected an error span");
        assert_eq!(err_span.offset(), 0);
    }

    #[test]
    fn spans_are_byte_accurate() {
        //          0123456789...
        let input = "node delta_v = @v_exhaust * ln(@mass_ratio);";
        let mut lexer = Lexer::new(input);

        let (_, span) = lexer.next_token().unwrap(); // node
        assert_eq!(span.offset(), 0);
        assert_eq!(span.len(), 4);

        let (_, span) = lexer.next_token().unwrap(); // delta_v
        assert_eq!(span.offset(), 5);
        assert_eq!(span.len(), 7);
    }

    #[test]
    fn peek_cache_uses_supplied_items_without_lexer_knowledge() {
        let mut cache = PeekCache::<u8>::new(NonZeroUsize::new(2).unwrap());
        let mut next = 0;

        assert_eq!(
            cache.peek(|| {
                next += 1;
                SourceItem::Item(next)
            }),
            Some(&1)
        );
        assert_eq!(
            cache.peek(|| {
                next += 1;
                SourceItem::Item(next)
            }),
            Some(&1)
        );
        assert_eq!(
            cache.next(|| {
                next += 1;
                SourceItem::Item(next)
            }),
            Some(1)
        );
        assert_eq!(
            cache.next(|| {
                next += 1;
                SourceItem::Item(next)
            }),
            Some(2)
        );
        assert_eq!(next, 2);
    }

    #[test]
    fn peek_cache_respects_caller_supplied_lookahead() {
        let mut cache = PeekCache::<u8>::new(NonZeroUsize::new(4).unwrap());
        let mut next = 0;

        assert_eq!(
            cache.peek_at(3, || {
                next += 1;
                SourceItem::Item(next)
            }),
            Some(&4)
        );
        assert_eq!(next, 4);
    }
}