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
use crate::codegen_prelude::{GenerateAstInfo, ParsePairSort};
use crate::parser::ast::from_pairs::FromPairs;
use crate::sources::span::Span;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;

pub mod from_pairs;
pub mod generate_ast;

pub trait SpannedAstInfo: AstInfo {
    fn span(&self) -> &Span;
}

#[derive(Hash, Eq, PartialEq, Copy, Clone, Serialize, Deserialize, Debug)]
pub struct NodeId(u64);

impl NodeId {
    pub(crate) fn new(value: u64) -> NodeId {
        Self(value)
    }
}

pub trait AstInfo: Debug + PartialEq {
    fn node_id(&self) -> NodeId;
}

pub trait AstNode<M: AstInfo>: FromPairs<M> {
    fn ast_info(&self) -> &M;
    fn sort(&self) -> &'static str;
    fn constructor(&self) -> &'static str;

    fn traverse<F>(&self, _f: F)
    where
        Self: Sized,
        F: FnMut(&dyn AstNode<M>),
    {
        todo!()
    }
}

impl<M: AstInfo, T> FromPairs<M> for Box<T>
where
    T: AstNode<M>,
{
    fn from_pairs<G: GenerateAstInfo<Result = M>>(pair: &ParsePairSort, generator: &mut G) -> Self
    where
        Self: Sized,
    {
        Box::new(T::from_pairs(pair, generator))
    }
}

impl<M: AstInfo, T> AstNode<M> for Box<T>
where
    T: AstNode<M>,
{
    fn ast_info(&self) -> &M {
        T::ast_info(self)
    }

    fn sort(&self) -> &'static str {
        T::sort(self)
    }

    fn constructor(&self) -> &'static str {
        T::constructor(self)
    }
}