seam 0.4.3

Symbolic Expressions As Markup.
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
use std::{error::Error, fmt, str::FromStr};
use unicode_width::UnicodeWidthStr;
use descape::UnescapeExt;

use super::{lexer::{LexError, Lexer}, tokens::{Kind, Site, Token}};

/// The [`Node`] type represents what atomic/literals are parsed
/// into; i.e. not compound types (e.g. lists, attributes).
/// These are just a common storage for the literals in [`ParseNode`].
#[derive(Debug, Clone)]
pub struct Node<'a> {
    pub value: String,
    pub site: Site<'a>,
    pub leading_whitespace: String,
}

impl<'a> PartialEq for Node<'a> {
    fn eq(&self, other: &Self) -> bool {
        self.value == other.value
    }
}
impl<'a> Eq for Node<'a> { }

impl<'a> std::hash::Hash for Node<'a> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.value.hash(state)
    }
}

impl<'a> Node<'a> {
    pub fn new(value: &str, site: &Site<'a>, leading_whitespace: &str) -> Self {
        Self {
            site: site.to_owned(),
            value: value.to_owned(),
            leading_whitespace: leading_whitespace.to_owned(),
        }
    }
}

/// Parse nodes are the components of the syntax tree that
/// the source code is translated into.
/// These nodes are also produced at compile-time by the macro expander.
#[derive(Debug, Clone)]
pub enum ParseNode<'a> {
    Symbol(Node<'a>),
    Number(Node<'a>),
    String(Node<'a>),
    Raw(Node<'a>), //< Raw-content strings are not parsed, only expanded by macros.
    List {
        nodes: Box<[ParseNode<'a>]>,
        site: Site<'a>,
        end_token: Token<'a>,
        leading_whitespace: String,
    },
    Attribute {
        keyword: String,
        node: Box<ParseNode<'a>>,
        site: Site<'a>,
        leading_whitespace: String,
    },
}

impl<'a> PartialEq for ParseNode<'a> {
    fn eq(&self, other: &Self) -> bool {
        match self {
            Self::Symbol(node0) => match other {
                Self::Symbol(node1) => node0 == node1,
                _ => false,
            },
            Self::Number(node0) => match other {
                Self::Number(node1) => node0 == node1,
                _ => false,
            },
            Self::String(node0) => match other {
                Self::String(node1) => node0 == node1,
                _ => false,
            },
            Self::Raw(node0) => match other {
                Self::Raw(node1) => node0 == node1,
                _ => false,
            },
            Self::List { nodes: nodes0, .. } => match other {
                Self::List { nodes: nodes1, .. } => nodes0 == nodes1,
                _ => false,
            },
            Self::Attribute { keyword: keyword0, node: node0, .. } => match other {
                Self::Attribute { keyword: keyword1, node: node1, .. } =>
                    keyword0 == keyword1 && node0 == node1,
                _ => false,
            }
        }
    }
}
impl<'a> Eq for ParseNode<'a> { }

impl<'a> std::hash::Hash for ParseNode<'a> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        match self {
            Self::Symbol(node) => {
                state.write_u8(0);
                node.hash(state);
            },
            Self::Number(node) => {
                state.write_u8(1);
                node.hash(state);
            },
            Self::String(node) =>{
                state.write_u8(2);
                node.hash(state);
            },
            Self::Raw(node) => {
                state.write_u8(3);
                node.hash(state);
            },
            Self::List { nodes, .. } => {
                state.write_u8(4);
                nodes.hash(state);
            },
            Self::Attribute { keyword, node, .. } => {
                state.write_u8(5);
                keyword.hash(state);
                node.hash(state);
            },
        }
    }
}

impl<'a> ParseNode<'a> {
    /// Returns true if and only if self is the empty list `()`.
    pub fn null(&self) -> bool {
        match self {
            Self::List { nodes, .. } => nodes.is_empty(),
            _ => false,
        }
    }

    /// Unwrap a literal node if it is a symbol or number.
    pub fn symbolic(&self) -> Option<&Node<'a>> {
        match self {
            Self::Symbol(ref node) | Self::Number(ref node) => Some(node),
            _ => None,
        }
    }

    /// Unwrap string-like nodes.
    pub fn string(&self) -> Option<&Node<'a>> {
        match self {
            Self::String(ref node) | Self::Raw(ref node) => Some(node),
            _ => None,
        }
    }

    /// Unwrap a number node into a [`Node<'a>`].
    pub fn number(&self) -> Option<&Node<'a>> {
        match self {
            Self::Number(ref node) => Some(node),
            _ => None,
        }
    }

    /// Unwrap a symbol node into a [`Node<'a>`].
    pub fn symbol(&self) -> Option<&Node<'a>> {
        match self {
            Self::Symbol(ref node) => Some(node),
            _ => None,
        }
    }

    /// Unwrap literal (atomic) nodes into their underlying [`Node`].
    pub fn atomic(&self) -> Option<&Node<'a>> {
        match self {
            Self::Symbol(ref node)
            | Self::Number(ref node)
            | Self::String(ref node)
            | Self::Raw(ref node) => Some(node),
            _ => None,
        }
    }

    /// Unwrap list node into vector of nodes.
    pub fn list(&self) -> Option<&ParseTree<'a>> {
        match self {
            Self::List { nodes, .. } => Some(nodes),
            _ => None,
        }
    }

    /// Unwrap attribute node into keyword and value.
    pub fn attribute(&self) -> Option<(&str, &Box<ParseNode<'a>>)> {
        match self {
            Self::Attribute { keyword, node, .. } => Some((keyword, node)),
            _ => None,
        }
    }

    /// Same as [`Self::atomic`], but consumes the node,
    /// returning an owned [`Node`].
    pub fn into_atomic(self) -> Option<Node<'a>> {
        match self {
            Self::Symbol(node)
            | Self::Number(node)
            | Self::String(node)
            | Self::Raw(node) => Some(node),
            _ => None,
        }
    }

    /// Get a reference to the parse node's underlying [`Site`].
    pub fn site(&self) -> &Site<'a> {
        match self {
            Self::Symbol(ref node)
            | Self::Number(ref node)
            | Self::String(ref node)
            | Self::Raw(ref node) => &node.site,
            Self::List { ref site, .. } => site,
            Self::Attribute { ref site, .. } => site,
        }
    }

    /// Clone the underlying [`Site`] of this parse node.
    pub fn owned_site(&self) -> Site<'a> {
        match self {
            Self::Symbol(node)
            | Self::Number(node)
            | Self::String(node)
            | Self::Raw(node) => node.site,
            Self::List { site, .. } => *site,
            Self::Attribute { site, .. } => *site,
        }
    }

    /// Get a reference to the underlying leading whitespace string
    /// of this parse node.
    pub fn leading_whitespace(&self) -> &str {
        match self {
            Self::Symbol(ref node)
            | Self::Number(ref node)
            | Self::String(ref node)
            | Self::Raw(ref node) => node.leading_whitespace.as_str(),
            Self::List { leading_whitespace, .. } => leading_whitespace.as_str(),
            Self::Attribute { leading_whitespace, .. } => leading_whitespace.as_str(),
        }
    }

    /// Modify the underlying leading whitespace stored for this parse node.
    pub fn set_leading_whitespace(&mut self, whitespace: String) {
        match self {
            Self::Symbol(ref mut node)
            | Self::Number(ref mut node)
            | Self::String(ref mut node)
            | Self::Raw(ref mut node) => node.leading_whitespace = whitespace,
            Self::List { ref mut leading_whitespace, .. } => *leading_whitespace = whitespace,
            Self::Attribute { ref mut leading_whitespace, .. } => *leading_whitespace = whitespace,
        };
    }

    /// Get a `&'static str` string name of what type of parse node this is.
    pub fn node_type(&self) -> &'static str {
        match self {
            Self::Symbol(..) => "symbol",
            Self::Number(..) => "number",
            Self::String(..) => "string",
            Self::Raw(..) => "raw-content string",
            Self::List { .. } => "list",
            Self::Attribute { .. } => "attribute",
        }
    }

    pub fn is_symbolic(&self) -> bool { self.symbolic().is_some() }
    pub fn is_atomic(&self) -> bool { self.atomic().is_some() }
    pub fn is_symbol(&self) -> bool { self.symbol().is_some() }
    pub fn is_number(&self) -> bool { self.number().is_some() }
    pub fn is_string(&self) -> bool { self.string().is_some() }
    pub fn is_list(&self) -> bool { self.list().is_some() }
    pub fn is_attribute(&self) -> bool { self.attribute().is_some() }
}

// Try to convert a [`ParseNode`] enum value into
// its underlying type (e.g. [`Node`], `Box<[Node]>`, etc.).

impl<'a> TryFrom<ParseNode<'a>> for Node<'a> {
    type Error = ();

    fn try_from(value: ParseNode<'a>) -> Result<Self, Self::Error> {
        match value.into_atomic() {
            Some(node) => Ok(node),
            None => Err(()),
        }
    }
}

impl<'a> TryFrom<ParseNode<'a>> for Box<[ParseNode<'a>]> {
    type Error = ();

    fn try_from(value: ParseNode<'a>) -> Result<Self, Self::Error> {
        match value {
            ParseNode::List { nodes, .. } => Ok(nodes),
            _ => Err(()),
        }
    }
}

impl<'a> TryFrom<ParseNode<'a>> for Vec<ParseNode<'a>> {
    type Error = ();

    fn try_from(value: ParseNode<'a>) -> Result<Self, Self::Error> {
        let into: Result<Box<[ParseNode<'a>]>, Self::Error> = value.try_into();
        into.map(|b| b.to_vec())
    }
}

/// Trait determining if a [`ParseNode`] can be converted into
/// a value of a given (usually inferred) type.
pub trait IntoValue<'a, T>: Sized {
    fn into_value(&'a self) -> Option<T> { None }
}

/// A number type.
trait Num<Rhs = Self, Output = Self>:
    std::ops::Add<Rhs, Output = Output>
  + std::ops::Sub<Rhs, Output = Output>
  + std::ops::Mul<Rhs, Output = Output>
  + std::ops::Div<Rhs, Output = Output>
  + std::ops::Rem<Rhs, Output = Output> { }
impl Num for usize { }
impl Num for isize { }
impl Num for u32 { }
impl Num for i32 { }
impl Num for u64 { }
impl Num for i64 { }
impl Num for f32 { }
impl Num for f64 { }

/// Convert parse-node into value if said value is a number type.
impl<'a, T: Num + FromStr> IntoValue<'a, T> for ParseNode<'a> {
    fn into_value(&self) -> Option<T> {
        match self {
            ParseNode::Number(node) => node.value.parse().ok(),
            _ => None,
        }
    }
}

/// Convert parse-node into value if said value is a symbol/string type.
impl<'a> IntoValue<'a, &'a str> for ParseNode<'a> {
    fn into_value(&'a self) -> Option<&'a str> {
        match self {
            ParseNode::Symbol(node)
          | ParseNode::String(node)
          | ParseNode::Raw(node) => Some(node.value.as_ref()),
            _ => None,
        }
    }
}

/// TODO: Convert parse-node into value if said value is a list type.
/*
impl<'a, V> IntoValue<'a, &'a [V]> for ParseNode<'a> {
    fn into_value(&'a self) -> Option<&'a [V]> {
        match self {
            ParseNode::List { nodes, .. } => {
                let mut values = Vec::with_capacity(nodes.len());
                for node in nodes {
                    let Some(value) = node.into_value() else {
                        return None;
                    };
                    let value: V = value;
                    values.push(value)
                }
                // TODO: fix this.
                let values: &[V] = &*Box::leak(values.into_boxed_slice());
                Some(values)
            },
            _ => None,
        }
    }
}
*/

/// An array of parse nodes, like in a [`ParseNode::List`], never grows.
/// Hence we prefer the `Box<[...]>` representation over a `Vec<...>`.
pub type ParseTree<'a> = Box<[ParseNode<'a>]>;

#[derive(Debug, Clone)]
pub struct ParseError<'a>(pub String, pub Site<'a>);

impl<'a> fmt::Display for ParseError<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let ParseError(msg, site) = self;
        let line_prefix = format!("  {} |", site.line);
        let line_view = site.line_slice();
        writeln!(f, "{} {}", line_prefix, line_view)?;
        writeln!(f, "{:>prefix_offset$} {:~>text_offset$}{:^>length$}", "|", "", "",
            prefix_offset=UnicodeWidthStr::width(line_prefix.as_str()),
            text_offset=site.line_column() - 1,
            length=site.width())?;
        write!(f, "[**] Parse Error ({}:{}:{}): {}",
            site.source, site.line, site.line_column(), msg)
    }
}

impl<'a> Error for ParseError<'a> { }

/// Parser structure walks through source using lexer.
#[derive(Debug, Clone)]
pub struct Parser {
    lexer: Lexer, //< Parser owns a lexer.
}

impl<'a> Parser {
    pub fn new(lexer: Lexer) -> Self {
        Self { lexer }
    }

    pub fn get_source(&self) -> &str {
        self.lexer.get_source()
    }

    /// Parse whole source code, finishing off the lexer.
    pub fn parse(&'a self) -> Result<ParseTree<'a>, Box<dyn Error + 'a>> {
        let mut root: Vec<ParseNode> = Vec::new();
        while !self.lexer.eof() {
            let expr = self.parse_expr()?;
            root.push(expr);
        }
        return Ok(root.into_boxed_slice());
    }

    /// Produce a parse node from the current position in the lexer.
    pub fn parse_expr(&'a self) -> Result<ParseNode<'a>, Box<dyn Error + 'a>> {
        let token = self.lexer.peek()?;
        match token.kind {
            Kind::LParen => self.parse_list(),
            Kind::RParen => Err(ParseError(
                "Unexpected `)' closing parenthesis.".to_owned(),
                token.site.to_owned()))?,
            Kind::Keyword => self.parse_keyword(),
            Kind::Symbol => Ok(ParseNode::Symbol(self.parse_atomic()?)),
            // TODO: Parse (escpae) string-literals.
            Kind::String => Ok(ParseNode::String(self.parse_atomic()?)),
            Kind::Number => Ok(ParseNode::Number(self.parse_atomic()?)),
        }
    }

    /// Parse keyword-attribute pair.
    fn parse_keyword(&'a self) -> Result<ParseNode<'a>, Box<dyn Error + 'a>> {
        // Consume :keyword token.
        let token = self.lexer.consume()?;
        assert_eq!(token.kind, Kind::Keyword);
        // Check we are able to consume next expression for keyword's value.
        {
            let no_expr_error = ParseError(
                format!("Keyword `:{}' expects an expression following it.", token.value),
                token.site.to_owned());
            if self.lexer.eof() { Err(no_expr_error.clone())? ;}
            match self.lexer.peek()? {
                Token { kind: Kind::RParen, .. } => Err(no_expr_error)?,
                _ => ()
            }
        }
        // Otherwise, parse the value and combine the node.
        let value = self.parse_expr()?;
        Ok(ParseNode::Attribute {
            keyword: token.value.to_owned(),
            node: Box::new(value),
            site: token.site.to_owned(),
            leading_whitespace: token.leading_whitespace.to_owned(),
        })
    }

    /// Parse a literal node.
    /// This is where escapes in symbols and strings are handled.
    fn parse_atomic(&'a self) -> Result<Node<'a>, LexError<'a>> {
        let token = self.lexer.consume()?;
        let value = match token.kind {
            Kind::Symbol | Kind::Number | Kind::Keyword => escape_sanitize(token.value),
            Kind::String => escape_string(token.value, &token.site)?,
            _ => unreachable!("called `parse_atomic` on non-atomic token."),
        };
        Ok(Node {
            value,
            site: token.site.clone(),
            leading_whitespace: token.leading_whitespace.to_string(),
        })
    }

    /// Parse a list `( [...] )'.
    fn parse_list(&'a self) -> Result<ParseNode<'a>, Box<dyn Error + 'a>> {
        // Consumed the `(' token.
        let lparen = self.lexer.consume()?;
        assert_eq!(lparen.kind, Kind::LParen);
        // Collect list elements.
        let mut elements = Vec::new();
        let mut rparen: Option<Token> = None;
        while !self.lexer.eof() {
            // Keep parsing expressions until `)' is reached.
            let token = self.lexer.peek()?;
            if token.kind == Kind::RParen {
                rparen = Some(self.lexer.consume()?); // Swallow up `)'.
                break;
            }
            let expr = self.parse_expr()?;
            elements.push(expr);
        }
        // Closing parenthesis was never found.
        let Some(rparen) = rparen else {
            return Err(ParseError(
                "Expected `)' closing parenthesis.".to_owned(),
                lparen.site.to_owned()))?;
        };
        Ok(ParseNode::List {
            nodes: elements.into_boxed_slice(),
            site: lparen.site.to_owned(),
            end_token: rparen.to_owned(),
            leading_whitespace: lparen.leading_whitespace.to_owned(),
        })
    }
}

/// Sanitize any escaped characters by removing their leading backslash.
fn escape_sanitize(string: &str) -> String {
    let mut builder = String::with_capacity(string.len());
    let mut chars = string.chars();
    while let Some(c) = chars.next() {
        if c == '\\' { continue; }
        builder.push(c)
    }
    builder
}

/// Parse a string with its escapes.
/// **Note:** Uses the `descape` crate for now.
fn escape_string<'a>(string: &'a str, site: &Site<'a>) -> Result<String, LexError<'a>> {
    string.to_unescaped()
        .map(|s| s.to_string())
        .map_err(|invalid| {
            LexError(
                format!("Invalid escape `\\{}' at byte-index {}.",
                    string.chars().nth(invalid.index).unwrap_or('?'), invalid.index),
                site.clone())
        })
}

pub trait SearchTree<'a> {
    /// Search the parse-tree for a specific node with a specific value.
    fn search_node(&'a self, kind: SearchType,
                   value: &str,
                   case_insensitive: bool,
                   level: usize) -> Option<&'a ParseNode<'a>>;
}

#[derive(Clone, Copy, PartialEq)]
pub enum SearchType {
    ListHead, ListMember,
    Symbol, Number, String,
    Attribute,
    Any,
}

impl SearchType {
    pub fn is_a(self, kind: SearchType) -> bool {
        self == SearchType::Any || self == kind
    }
}

impl<'a> SearchTree<'a> for ParseNode<'a> {
    fn search_node(&'a self, kind: SearchType, value: &str,
                   insensitive: bool, level: usize) -> Option<&'a ParseNode<'a>> {
        if level == 0 {
            return None;
        }

        let is_equal = |string: &str| if insensitive {
            string.to_lowercase() == value.to_lowercase()
        } else {
            string == value
        };

        match self {
            ParseNode::List { nodes, .. } => {
                if kind.is_a(SearchType::ListHead) {
                    if let Some(Some(caller)) = nodes.get(0).map(ParseNode::atomic) {
                        if is_equal(&caller.value) {
                            return Some(self);
                        }
                    }
                }
                nodes.search_node(kind, value, insensitive, level - 1)
            },
            ParseNode::Symbol(name) => {
                if kind.is_a(SearchType::Symbol) && is_equal(&name.value) {
                    Some(self)
                } else {
                    None
                }
            },
            ParseNode::String(name) | ParseNode::Raw(name) => {
                if kind.is_a(SearchType::String) && is_equal(&name.value) {
                    Some(self)
                } else {
                    None
                }
            },
            ParseNode::Number(name) => {
                if kind.is_a(SearchType::Number) && is_equal(&name.value) {
                    Some(self)
                } else {
                    None
                }
            },
            ParseNode::Attribute { node, ref keyword, .. } => {
                if kind.is_a(SearchType::Attribute) {
                    if is_equal(keyword) {
                        return Some(node);
                    }
                }
                node.search_node(kind, value, insensitive, level - 1)
            },
        }
    }
}

impl<'a> SearchTree<'a> for ParseTree<'a> {
    fn search_node(&'a self, kind: SearchType, value: &str,
                   insensitive: bool, level: usize) -> Option<&'a ParseNode<'a>> {
        if level == 0 {
            return None;
        }

        for node in self {
            let found = node.search_node(kind, value, insensitive, level);
            if found.is_some() {
                return found;
            }
        }

        None
    }
}

/// Pretty printing for parse nodes.
impl<'a> fmt::Display for ParseNode<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ParseNode::Symbol(node)
            | ParseNode::Number(node) => write!(f, "{}{}", node.leading_whitespace, node.value),
            ParseNode::String(node)
            | ParseNode::Raw(node) => write!(f, "{}{:?}", node.leading_whitespace, node.value),
            ParseNode::Attribute { keyword, node, leading_whitespace, .. } =>
                write!(f, "{}:{}{}", leading_whitespace, keyword, &*node),
            ParseNode::List { nodes, leading_whitespace, end_token, .. } => {
                write!(f, "{}(", leading_whitespace)?;
                for node in nodes {
                    write!(f, "{}", node)?;
                }
                write!(f, "{})", end_token.leading_whitespace)
            }
        }
    }
}