kamo 0.8.0

A library to assist in the creation of an interpreter or compiler and its associated runtime.
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
//! S-expression parser.
//!
//! The syntax the parser processes is as defined by the Scheme standard R7RS
//! for the `read` procedure. The syntactic definition is the `<datum>` in
//! section [`7.1.2 External representations`](https://standards.scheme.org/official/r7rs.pdf)
//! of the standard.
//!  
//! The syntax deviations from the standard are:
//!  
//! - The extactness (`#e` and `#i`) of numbers is not supported. Floating-point
//!   numbers are always inexact and integers are always exact.
//!  
//! - Numbers may only be signed 64-bit integers or IEEE 754 double precision
//!   floating-point numbers. The standard allows for arbitrary precision
//!   integers, rationals and complex numbers.
//!  
//! - Labels are not supported.
//!  
//! - The `#;` comment syntax is only supported in the methods which parse
//!   multiple s-expressions. The `#;` comment syntax may not be nested.
//!  
//! - Character literals defined by a hex escape sequence may have 1 to 6
//!   digits. The standard excepts 1 or more digits. The code must be a valid
//!   Unicode code point.
//!
//! The parser is implemented with the combination parser library
//! [`kamo::parser`](crate::parser). The parser is implemented by the struct
//! [`Sexpr`].

use std::{cell::RefCell, fs::read_to_string, path::Path};

use crate::{
    mem::MutatorRef,
    parser::{Code, Input, ParseError, ParseResult, Parser},
    value::{PositionMap, Value},
    Position,
};

/// A parser for S-expressions.
///
/// The parser is implemented with the combination parser library
/// [`kamo::parser`](crate::parser). It is the runtime equivalent of the
/// proc-macro implemented by the
/// [kamo-macros](https://crates.io/crates/kamo-macros) crate.
///
/// The parser implements the trait [`Parser`] for the type [`Value`]. It parses
/// a single datum optionally delimited by intertoken whitespace and followed by
/// an end of file.
///
/// The parser requires a mutatator to allocate new values. All parse-methods
/// which return a parser-closure can potentially allocate new values. The
/// static methods only return intermediate values.
///
/// The generic parameter `const ECO: Code` is the error code offset for the
/// parser. The error codes are defined in the module
/// [`sexpr::error::code`](crate::form::sexpr::error::code). The `ECO` parameter
/// is added to the error codes to create a unique error code domain for the
/// parser when used in combination with other parsers. Error codes of this
/// parser all start with [`ERR_CUSTOM`](crate::parser::code::ERR_CUSTOM). The
/// last error code not included in the domain is
/// [`ERR_SEXPR_END`](crate::form::sexpr::error::code::ERR_SEXPR_END).
///
/// Optionally there can be a [`PositionMap`] chained to the parser. The
/// positions are used to annotate the values of list nodes with their source
/// code positions. For every pair the position of the car-value is added to the
/// position map. It is possible to chain a pre-existing position map to the
/// parser or to create a new position map. The position map can be taken from
/// the parser after parsing. The position map can be used by stages of the
/// compiler to annotate errors with source code positions.
///
/// # Example
///
/// ```rust
/// use kamo::form::sexpr::Sexpr;
/// use kamo::mem::Mutator;
/// use kamo::parser::Parser;
/// use kamo::value::Value;
/// use kamo_macros::sexpr;
///
/// let mut m = Mutator::new_ref();
/// let mut p = Sexpr::<0>::new(m.to_owned());
/// let result = p.parse("(+ 1 2)".into());
///
/// assert!(result.is_ok());
///
/// let (value, _) = result.unwrap();
/// assert_eq!(value, sexpr!(m, "(+ 1 2)"));
/// ```
///
/// # Grammar
///
/// The parser implements the following grammar for S-expressions:
///
/// ```text
/// datum                  = boolean | character | number | string | bytevec
///                        | vector | symbol | list | quote | quasiquote
///                        | unquote | unquote_splicing
/// boolean                = "#t" | "#f" | "#true" | "#false"
/// character              = "#\\" <any character>
///                        | "#\\" character_name
///                        | "#\\x" character_code
/// character_name         = "alarm" | "backspace" | "delete | "escape"
///                        | "newline" | "null" | "return" | "space"
///                        | "tab"
/// character_code         = [0-9A-Fa-f]{1, 6}
/// number                 = number_binary | number_octal | number_hexdec
///                        | number_decimal | decimal | infnan
/// number_binary          = "#b" ((sign [0-1]+) | infnan)
/// number_octal           = "#o" ((sign [0-7]+) | infnan)
/// number_hexdec          = "#x" ((sign [0-9a-fA-F]+) | infnan)
/// number_decimal         = "#d" (decimal | infnan)
/// decimal                = sign (([0-9]+ (decimal_fraction | decimal_suffix)?)
///                                | decimal_fraction)
/// decimal_fraction       = "." [0-9]+ decimal_suffix?
/// decimal_suffix         = [eE] sign [0-9]+
/// sign                   = ("+" | "-")?
/// infnan                 = "+inf.0" | "-inf.0" | "+nan.0" | "-nan.0"
/// string                 = "\"" string-text "\""
/// string-text            = (string-escape | string-char)*
/// string-escape          = "\\" ( "a" | "b" | "t" | "n" | "r" | "\\" | "\""
///                               | "x" hex-code ";" | ilws )
/// string-char            = <any> - ["\\" | "\""]
/// symbol                 = (symbol_initial symbol_subsequent*)
///                        | ([+\-]
///                              ( (symbol_sign_subsequent symbol_subsequent*)
///                              | ([.] symbol_dot_subsequent symbol_subsequent*))
///                        | ([.] symbol_dot_subsequent symbol_subsequent*))
///                        | ("|" symbol_text "|")
/// symbol_text            = (symbol_escape | symbol_char)*
/// symbol_escape          = "\" ( "a" | "b" | "t" | "n" | "r" | "\\" | "|"
///                              | "x" hex-code ";")
/// symbol_char            = <any> - ["\\" | "|"]
/// symbol_initial         = [a-zA-Z] | [!$%&*/:<=>?^_~]
/// symbol_subsequent      = symbol_initial | [0-9] | [+\-.@]
/// symbol_sign_subsequent = symbol_initial | [+\-@]
/// symbol_dot_subsequent  = symbol_sign_subsequent | "."
/// list                   = "(" intertoken
///                              ((datum intertoken)+ ("." intertoken datum)?)?
///                          intertoken ")"
/// hex-code               = [0-9a-fA-F]{1,6}
/// ilws                   = [ \t]* ("\n" | "\r\n") [ \t]*
/// bytevec                = "#u8(" intertoken (byte intertoken)* ")"
/// byte                   = <any extact number between 0 and 255>
/// vector                 = "#(" intertoken (datum intertoken)* ")"
/// quote                  = "'" intertoken datum
/// quasiquote             = "`" intertoken datum
/// unquote                = "," intertoken datum
/// unquote_splicing       = ",@" intertoken datum
/// intertoken             = (whitespace | comment)*
/// whitespace             = <Unicode whitespace>+
/// comment                = comment-line | comment-nested
/// comment-line           = ";" <any>* ("\n" | eof)
/// comment-nested         = "#|" comment-text comment-cont* "|#"
/// comment-text           = <any except "#|" or "|#">*
/// comment-cont           = comment-nested comment-text
/// ```
#[derive(Clone, Debug)]
pub struct Sexpr<'a, const ECO: Code> {
    m: MutatorRef<'a>,
    pmap: Option<RefCell<PositionMap>>,
}

impl<'a, const ECO: Code> Sexpr<'a, ECO> {
    /// Creates a new parser for S-expressions.
    ///
    /// Optionally, a position map can be chained to the parser. The positions
    /// are used to annotate the values of list nodes with their source code
    /// positions. For every pair the position of the car-value is added to the
    /// position map.
    #[must_use]
    pub const fn new(m: MutatorRef<'a>) -> Self {
        Self { m, pmap: None }
    }

    /// Chains an existing [`PositionMap`] to the parser. The parser takes the
    /// ownership of the position map.
    #[must_use]
    pub fn with_pmap(mut self, pos: PositionMap) -> Self {
        self.pmap = Some(RefCell::new(pos));
        self
    }

    /// Chains an empty [`PositionMap`] to the parser.
    #[must_use]
    pub fn tracked(mut self) -> Self {
        self.pmap = Some(RefCell::new(PositionMap::new()));
        self
    }

    /// Takes the [`PositionMap`] if it is present.
    pub fn take_positions(&mut self) -> Option<PositionMap> {
        self.pmap.take().map(RefCell::into_inner)
    }

    /// Parses zero or more expressions delimited by intertoken whitespace and
    /// followed by an end of file. An expression may be shadowed.
    ///
    /// # Errors
    ///
    /// If the input is not a valid script or if the script is not terminated
    /// with an end of file, an error is returned.
    ///
    /// # Grammar
    ///
    /// ```text
    /// script = intertoken ((datum | shadowed) intertoken)* eof
    /// ```
    pub fn parse_script<'b>(&mut self, input: Input<'b>) -> ParseResult<'b, Box<[Value<'a>]>> {
        use crate::parser::prelude::*;

        let (script, cursor) = delimited(
            Self::intertoken,
            fold_many0(
                terminated(self.datum_or_shadowed(), Self::intertoken),
                Vec::new,
                |mut acc, _, value| {
                    if let Datum::Exposed(value) = value {
                        acc.push(value);
                    }
                    acc
                },
            ),
            eof,
        )(input)?;

        Ok((script.into_boxed_slice(), cursor))
    }

    /// Reads the content of a file and parses it as a script by calling
    /// [`parse_script()`](Self::parse_script).
    ///
    /// # Errors
    ///
    /// If the file cannot be read, the input is not a valid script or the
    /// script is not terminated with an end of file, an error is returned.
    pub fn parse_file<P: AsRef<Path>>(&mut self, path: P) -> ParseResult<Box<[Value<'a>]>> {
        use error::code::ERR_FILE;

        let input = &read_to_string(path.as_ref()).map_err(|err| {
            ParseError::new(
                Position::new(0, 1, 1),
                ERR_FILE + ECO,
                SexprError::FileError(path.as_ref()),
            )
            .and_source(err)
        })?;
        let (exprs, cursor) = self.parse_script(input.into())?;

        Ok((exprs, Input::eof_from(cursor)))
    }
}

impl<'a, 'b, const ECO: Code> Parser<'b, 'a, Value<'a>> for Sexpr<'a, ECO> {
    /// Parses a single datum as defined in [`datum()`](Self::datum) delimited
    /// by [`intertoken()`](Self::intertoken) and followed by an end of file.
    ///
    /// # Grammar
    ///
    /// ```text
    /// sexpr = intertoken datum intertoken eof
    /// ```
    fn parse(&mut self, input: Input<'b>) -> ParseResult<'b, Value<'a>> {
        use crate::parser::prelude::*;

        terminated(
            delimited(Self::intertoken, self.datum(), Self::intertoken),
            eof,
        )(input)
    }
}

mod abbreviation;
mod boolean;
mod bytevec;
mod character;
mod datum;
mod error;
mod list;
mod number;
mod string;
mod symbol;
mod vector;
mod whitespace;

pub use datum::Datum;
#[allow(clippy::module_name_repetitions)]
pub use error::{code, SexprError};

#[cfg(test)]
mod tests {
    use kamo_macros::{sexpr, sexpr_file};

    use crate::{mem::Mutator, Position};

    use super::*;

    #[test]
    fn parse_success() {
        let m = Mutator::new_ref();
        let mut p = Sexpr::<0>::new(m.clone());

        let result = p.parse("; Adding two numbers\n(+ 1 2) ".into());
        assert_eq!(result, Ok((sexpr!(m, "(+ 1 2)"), Input::new(""))));
    }

    #[test]
    fn parse_files_success() {
        let m = Mutator::new_ref();
        let mut p = Sexpr::<0>::new(m.clone());

        parse_file_chapter_01(m.clone(), &mut p);
        parse_file_chapter_02(m.clone(), &mut p);
        parse_file_chapter_03(m.clone(), &mut p);
        parse_file_chapter_04(m.clone(), &mut p);
        parse_file_chapter_05(m.clone(), &mut p);
        parse_file_chapter_06(m.clone(), &mut p);
        parse_file_chapter_07(m.clone(), &mut p);
        parse_file_chapter_08(m.clone(), &mut p);
        parse_file_chapter_09(m.clone(), &mut p);
        parse_file_chapter_10(m.clone(), &mut p);
        parse_file_the_little_schemer(&mut p);
        // dbg!(m.borrow().stats());
    }

    #[allow(clippy::needless_pass_by_value)]
    fn parse_file_chapter_01(m: MutatorRef<'_>, p: &mut Sexpr<0>) {
        let expected = &sexpr_file!(m, "tests/sexpr/chapter-01.scm");
        let result = p.parse_file("tests/sexpr/chapter-01.scm");
        assert!(result.is_ok());
        let (exprs, rest) = result.unwrap();
        assert_eq!(rest.position(), Position::new(4864, 184, 66));
        assert_eq!(exprs.len(), expected.len());
        for (i, (actual, expected)) in exprs.iter().zip(expected.iter()).enumerate() {
            assert_eq!(actual, expected, "Expression {} does not match", i + 1);
        }
    }

    #[allow(clippy::needless_pass_by_value)]
    fn parse_file_chapter_02(m: MutatorRef<'_>, p: &mut Sexpr<0>) {
        let expected = &sexpr_file!(m, "tests/sexpr/chapter-02.scm");
        let result = p.parse_file("tests/sexpr/chapter-02.scm");
        assert!(result.is_ok());
        let (exprs, rest) = result.unwrap();
        assert_eq!(rest.position(), Position::new(1373, 48, 49));
        assert_eq!(exprs.len(), expected.len());
        for (i, (actual, expected)) in exprs.iter().zip(expected.iter()).enumerate() {
            assert_eq!(actual, expected, "Expression {} does not match", i + 1);
        }
    }

    #[allow(clippy::needless_pass_by_value)]
    fn parse_file_chapter_03(m: MutatorRef<'_>, p: &mut Sexpr<0>) {
        let expected = &sexpr_file!(m, "tests/sexpr/chapter-03.scm");
        let result = p.parse_file("tests/sexpr/chapter-03.scm");
        assert!(result.is_ok());
        let (exprs, rest) = result.unwrap();
        assert_eq!(rest.position(), Position::new(3638, 102, 56));
        assert_eq!(exprs.len(), expected.len());
        for (i, (actual, expected)) in exprs.iter().zip(expected.iter()).enumerate() {
            assert_eq!(actual, expected, "Expression {} does not match", i + 1);
        }
    }

    #[allow(clippy::needless_pass_by_value)]
    fn parse_file_chapter_04(m: MutatorRef<'_>, p: &mut Sexpr<0>) {
        let expected = &sexpr_file!(m, "tests/sexpr/chapter-04.scm");
        let result = p.parse_file("tests/sexpr/chapter-04.scm");
        assert!(result.is_ok());
        let (exprs, rest) = result.unwrap();
        assert_eq!(rest.position(), Position::new(3861, 161, 23));
        assert_eq!(exprs.len(), expected.len());
        for (i, (actual, expected)) in exprs.iter().zip(expected.iter()).enumerate() {
            assert_eq!(actual, expected, "Expression {} does not match", i + 1);
        }
    }

    #[allow(clippy::needless_pass_by_value)]
    fn parse_file_chapter_05(m: MutatorRef<'_>, p: &mut Sexpr<0>) {
        let expected = &sexpr_file!(m, "tests/sexpr/chapter-05.scm");
        let result = p.parse_file("tests/sexpr/chapter-05.scm");
        assert!(result.is_ok());
        let (exprs, rest) = result.unwrap();
        assert_eq!(rest.position(), Position::new(5786, 152, 61));
        assert_eq!(exprs.len(), expected.len());
        for (i, (actual, expected)) in exprs.iter().zip(expected.iter()).enumerate() {
            assert_eq!(actual, expected, "Expression {} does not match", i + 1);
        }
    }

    #[allow(clippy::needless_pass_by_value)]
    fn parse_file_chapter_06(m: MutatorRef<'_>, p: &mut Sexpr<0>) {
        let expected = &sexpr_file!(m, "tests/sexpr/chapter-06.scm");
        let result = p.parse_file("tests/sexpr/chapter-06.scm");
        assert!(result.is_ok());
        let (exprs, rest) = result.unwrap();
        assert_eq!(rest.position(), Position::new(1640, 65, 92));
        assert_eq!(exprs.len(), expected.len());
        for (i, (actual, expected)) in exprs.iter().zip(expected.iter()).enumerate() {
            assert_eq!(actual, expected, "Expression {} does not match", i + 1);
        }
    }

    #[allow(clippy::needless_pass_by_value)]
    fn parse_file_chapter_07(m: MutatorRef<'_>, p: &mut Sexpr<0>) {
        let expected = &sexpr_file!(m, "tests/sexpr/chapter-07.scm");
        let result = p.parse_file("tests/sexpr/chapter-07.scm");
        assert!(result.is_ok());
        let (exprs, rest) = result.unwrap();
        assert_eq!(rest.position(), Position::new(2589, 86, 63));
        assert_eq!(exprs.len(), expected.len());
        for (i, (actual, expected)) in exprs.iter().zip(expected.iter()).enumerate() {
            assert_eq!(actual, expected, "Expression {} does not match", i + 1);
        }
    }

    #[allow(clippy::needless_pass_by_value)]
    fn parse_file_chapter_08(m: MutatorRef<'_>, p: &mut Sexpr<0>) {
        let expected = &sexpr_file!(m, "tests/sexpr/chapter-08.scm");
        let result = p.parse_file("tests/sexpr/chapter-08.scm");
        assert!(result.is_ok());
        let (exprs, rest) = result.unwrap();
        assert_eq!(rest.position(), Position::new(2342, 76, 44));
        assert_eq!(exprs.len(), expected.len());
        for (i, (actual, expected)) in exprs.iter().zip(expected.iter()).enumerate() {
            assert_eq!(actual, expected, "Expression {} does not match", i + 1);
        }
    }

    #[allow(clippy::needless_pass_by_value)]
    fn parse_file_chapter_09(m: MutatorRef<'_>, p: &mut Sexpr<0>) {
        let expected = &sexpr_file!(m, "tests/sexpr/chapter-09.scm");
        let result = p.parse_file("tests/sexpr/chapter-09.scm");
        assert!(result.is_ok());
        let (exprs, rest) = result.unwrap();
        assert_eq!(rest.position(), Position::new(1011, 44, 25));
        assert_eq!(exprs.len(), expected.len());
        for (i, (actual, expected)) in exprs.iter().zip(expected.iter()).enumerate() {
            assert_eq!(actual, expected, "Expression {} does not match", i + 1);
        }
    }

    #[allow(clippy::needless_pass_by_value)]
    fn parse_file_chapter_10(m: MutatorRef<'_>, p: &mut Sexpr<0>) {
        let expected = &sexpr_file!(m, "tests/sexpr/chapter-10.scm");
        let result = p.parse_file("tests/sexpr/chapter-10.scm");
        assert!(result.is_ok());
        let (exprs, rest) = result.unwrap();
        assert_eq!(rest.position(), Position::new(2796, 88, 67));
        assert_eq!(exprs.len(), expected.len());
        for (i, (actual, expected)) in exprs.iter().zip(expected.iter()).enumerate() {
            assert_eq!(actual, expected, "Expression {} does not match", i + 1);
        }
    }

    fn parse_file_the_little_schemer(p: &mut Sexpr<0>) {
        // This file seems too big to compare. The rust-analyzer crashes when
        // trying to analyze the resulting expression. The proc-macro seems to
        // generate a too large expression.
        let result = p.parse_file("tests/sexpr/the-little-schemer.scm");
        assert!(result.is_ok());
        let (exprs, rest) = result.unwrap();
        assert_eq!(rest.position(), Position::new(36810, 1245, 4));
        assert_eq!(exprs.len(), 147);
    }
}