-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.rs
150 lines (124 loc) · 4.35 KB
/
lexer.rs
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
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum LadderTypeToken {
Symbol( String ),
Char( char ),
Num( i64 ),
Open,
Close,
Ladder,
}
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum LexError {
/// found a non-digit character inside a numeric token
InvalidDigit,
/// quoted character token didnt close correctly with '
InvalidChar,
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\
#[derive(PartialEq, Eq, Clone, Debug)]
enum LexerState {
Any,
Sym( String ),
Num( i64 ),
Char( Option<char> )
}
impl LexerState {
fn into_token(self) -> Option<LadderTypeToken> {
match self {
LexerState::Any => None,
LexerState::Sym(s) => Some(LadderTypeToken::Symbol(s)),
LexerState::Num(n) => Some(LadderTypeToken::Num(n)),
LexerState::Char(c) => Some(LadderTypeToken::Char(c?))
}
}
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\
pub struct LadderTypeLexer<It>
where It: std::iter::Iterator<Item = char>
{
chars: std::iter::Peekable<It>,
}
impl<It> From<It> for LadderTypeLexer<It>
where It: Iterator<Item = char>
{
fn from(chars: It) -> Self {
LadderTypeLexer {
chars: chars.peekable()
}
}
}
impl<It> Iterator for LadderTypeLexer<It>
where It: Iterator<Item = char>
{
type Item = Result<LadderTypeToken, LexError>;
fn next(&mut self) -> Option<Self::Item> {
let mut state = LexerState::Any;
while let Some(c) = self.chars.peek() {
match &mut state {
// determine token type
LexerState::Any => {
match c {
'<' => { self.chars.next(); return Some(Ok(LadderTypeToken::Open)); },
'>' => { self.chars.next(); return Some(Ok(LadderTypeToken::Close)); },
'~' => { self.chars.next(); return Some(Ok(LadderTypeToken::Ladder)); },
'\'' => { self.chars.next(); state = LexerState::Char(None); },
c => {
if c.is_whitespace() {
self.chars.next();
} else if c.is_alphabetic() {
state = LexerState::Sym( String::new() );
} else if c.is_digit(10) {
state = LexerState::Num( 0 );
}
}
}
}
LexerState::Char(val) => {
// todo escape characters
*val = self.chars.next();
match self.chars.next() {
Some('\'') => {
if let Some(token) = state.clone().into_token() {
return Some(Ok(token));
}
}
_ => {
return Some(Err(LexError::InvalidChar));
}
}
}
_ => {
if c.is_whitespace() || *c == '>' || *c == '~' {
// finish the current token
if let Some(token) = state.clone().into_token() {
return Some(Ok(token));
}
} else {
// append to the current token
let c = self.chars.next().unwrap();
match &mut state {
LexerState::Sym(s) => {
s.push(c);
}
LexerState::Num(n) => {
if let Some(d) = c.to_digit(10) {
*n = (*n) * 10 + d as i64;
} else {
return Some(Err(LexError::InvalidDigit));
}
}
_ => {}
}
}
}
}
}
if let Some(token) = state.into_token() {
Some(Ok(token))
} else {
None
}
}
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\