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
use std::fmt;

use super::{Parse, ParseError, charstream::CharStream};

pub trait Token: Parse + fmt::Display {

}

pub trait Delimiter: Clone {
	type Start: Token;
	type End: Token;

	fn new(start: Self::Start, end: Self::End) -> Self where Self: Sized;
    fn span(&self) -> super::Span;
    fn name() -> String;
}

macro_rules! create_tokens {
    ($($token:tt $id:ident),+) => {
        $(
            #[derive(Clone)]
            pub struct $id {
                span: super::Span
            }
            
            impl Token for $id {}
            
            impl Parse for $id {
                fn parse(value: &mut CharStream) -> Result<Self, ParseError> where Self: Sized {
                    let token = stringify!($token);
                    let len = token.len();
                    let start = value.position();

                    let mut token_value = value.clone();

                    let mut mtch = String::new();
                    while mtch.len() < len {
                        mtch.push(match token_value.next() {
                            Some(value) => value,
                            None => break
                        });
                    }

                    if (token == mtch) {
                        value.goto(token_value.position())?;
                        let end = value.position();
                        return Ok(Self { span: super::Span::new(start, end)});
                    }

                    Err(ParseError(format!("Could not find token '{}'.", stringify!($token)), token_value.position()))
                }

                fn span(&self) -> super::Span {
                    self.span.clone()
                }
            }

            impl fmt::Debug for $id {
                fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result{
                    write!(f, "Token({}, at: {})", stringify!($token), self.span.end)
                }
            }

            impl fmt::Display for $id {
                fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
                    write!(f, "Token({})", stringify!($token))
                }
            }
        )+
    };
}

macro_rules! create_delimiters {
    ($($token:tt $left: ident $right: ident $delim:ident),+) => {
        $(
            #[derive(Clone)]
            pub struct $left {
                span: super::Span
            }

            impl Token for $left {}

            impl Parse for $left {
                fn parse(value: &mut CharStream) -> Result<Self, ParseError> where Self: Sized {
                    let chr = stringify!($token).chars().nth(0).unwrap();
                    let mut token_value = value.clone();
                    let start = value.position();

                    if let Some(token) = token_value.next() {
                        if token == chr {
                            value.goto(token_value.position())?;
                            let end = value.position();
                            return Ok(Self { span: super::Span::new(start, end)})
                        }
                    }

                    Err(ParseError(format!("could not find left side of: '{}'.", stringify!($token)), value.position()))
                }

                fn span(&self) -> super::Span {
                    self.span.clone()
                }
            }

            impl fmt::Debug for $left {
                fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
                    write!(f, "Token({})", stringify!($token).chars().nth(0).unwrap())
                }
            }

            impl fmt::Display for $left {
                fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
                    write!(f, "Token({})", stringify!($token).chars().nth(0).unwrap())
                }
            }

            #[derive(Clone)]
            pub struct $right {
                span: super::Span
            }

            impl Token for $right {}

            impl Parse for $right {
                fn parse(value: &mut CharStream) -> Result<Self, ParseError> where Self: Sized {
                    let chr = stringify!($token).chars().nth(1).unwrap();
                    let mut token_value = value.clone();
                    let start = value.position();

                    if let Some(token) = token_value.next() {
                        if token == chr {
                            value.goto(token_value.position())?;
                            let end = value.position();
                            return Ok(Self { span: super::Span::new(start, end)})
                        }
                    }

                    Err(ParseError(format!("could not find right side of: '{}'.", stringify!($token)), value.position()))
                }

                fn span(&self) -> super::Span {
                    self.span.clone()
                }
            }

            impl fmt::Debug for $right {
                fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
                    write!(f, "Token({})", stringify!($token).chars().nth(1).unwrap())
                }
            }

            impl fmt::Display for $right {
                fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
                    write!(f, "Token({})", stringify!($token).chars().nth(1).unwrap())
                }
            }

            #[derive(Debug, Clone)]
            pub struct $delim {
                start: $left,
                end: $right
            }

            impl Delimiter for $delim {
                type Start = $left;
                type End = $right;

                fn new(start: Self::Start, end: Self::End) -> Self {
                    Self { start, end }
                }

                fn span(&self) -> super::Span {
                    super::Span::new(self.start.span().start, self.end.span().end)
                }

                fn name() -> String {
                    String::from(stringify!($delim))
                }
            }
        )+
    };
}

create_tokens! {
    , Comma,
    . Period,
    ! Bang,
    # Hash,
    _ UnderScore,
    - Hyphen,
    + Plus,
    = Equal,
    == EqualEqual,
    : Colon,
    ; Semicolon,
    | Pipe,
    < Less,
    > Greater,
    / ForwardSlash
}

create_delimiters! {
    () LeftParen RightParen Paren,
    {} LeftBrace RightBrace Brace,
    [] LeftBracket RightBracket Bracket,
    "" LeftQuote RightQuote Quote
}