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
use super::errors::LexerError;
use super::token::Token;
use super::{AirPos, LexerResult};
use std::iter::Peekable;
use std::str::CharIndices;
pub type Spanned<Token, Loc, Error> = Result<(Loc, Token, Loc), Error>;
pub struct AIRLexer<'input> {
input: &'input str,
chars: Peekable<CharIndices<'input>>,
}
impl<'input> Iterator for AIRLexer<'input> {
type Item = Spanned<Token<'input>, AirPos, LexerError>;
fn next(&mut self) -> Option<Self::Item> {
self.next_token()
}
}
impl<'input> AIRLexer<'input> {
pub fn new(input: &'input str) -> Self {
Self {
input,
chars: input.char_indices().peekable(),
}
}
pub fn next_token(&mut self) -> Option<Spanned<Token<'input>, AirPos, LexerError>> {
while let Some((start_pos, ch)) = self.chars.next() {
let start_pos = AirPos::from(start_pos);
match ch {
'(' => return Some(Ok((start_pos, Token::OpenRoundBracket, start_pos + 1))),
')' => return Some(Ok((start_pos, Token::CloseRoundBracket, start_pos + 1))),
'[' => return Some(Ok((start_pos, Token::OpenSquareBracket, start_pos + 1))),
']' => return Some(Ok((start_pos, Token::CloseSquareBracket, start_pos + 1))),
';' => self.skip_comment(),
ch if ch.is_whitespace() => {}
'"' => return self.tokenize_string_literal(start_pos),
_ => return self.tokenize_string(start_pos, false),
}
}
None
}
fn skip_comment(&mut self) {
const NEW_LINE: char = '\n'; for (_, ch) in &mut self.chars {
if ch == NEW_LINE {
break;
}
}
}
#[allow(clippy::unnecessary_wraps)]
fn tokenize_string_literal(
&mut self,
start_pos: AirPos,
) -> Option<Spanned<Token<'input>, AirPos, LexerError>> {
for (pos, ch) in &mut self.chars {
let pos = AirPos::from(pos);
if ch == '"' {
let string_size = pos - start_pos + 1;
return Some(Ok((
start_pos,
Token::StringLiteral(&self.input[(start_pos + 1).into()..pos.into()]),
start_pos + string_size,
)));
}
}
Some(Err(LexerError::unclosed_quote(
start_pos..self.input.len().into(),
)))
}
#[allow(clippy::unnecessary_wraps)]
fn tokenize_string(
&mut self,
start_pos: AirPos,
open_square_bracket_met: bool,
) -> Option<Spanned<Token<'input>, AirPos, LexerError>> {
let end_pos = self.advance_to_token_end(start_pos, open_square_bracket_met);
let token_str = &self.input[start_pos.into()..end_pos.into()];
let token = match string_to_token(token_str, start_pos) {
Ok(token) => token,
Err(e) => return Some(Err(e)),
};
let token_str_len = end_pos - start_pos;
Some(Ok((start_pos, token, start_pos + token_str_len)))
}
fn advance_to_token_end(&mut self, start_pos: AirPos, square_met: bool) -> AirPos {
let mut end_pos = start_pos;
let mut round_brackets_balance: i64 = 0;
let mut square_brackets_balance = i64::from(square_met);
while let Some((pos, ch)) = self.chars.peek() {
end_pos = (*pos).into();
let ch = *ch;
update_brackets_count(
ch,
&mut round_brackets_balance,
&mut square_brackets_balance,
);
if should_stop(ch, round_brackets_balance, square_brackets_balance) {
break;
}
self.chars.next();
}
self.advance_end_pos(&mut end_pos);
end_pos
}
fn advance_end_pos(&mut self, end_pos: &mut AirPos) {
if self.chars.peek().is_none() {
*end_pos = self.input.len().into();
}
}
}
fn update_brackets_count(
ch: char,
round_brackets_balance: &mut i64,
square_brackets_balance: &mut i64,
) {
if ch == '(' {
*round_brackets_balance += 1;
} else if ch == ')' {
*round_brackets_balance -= 1;
} else if ch == '[' {
*square_brackets_balance += 1;
} else if ch == ']' {
*square_brackets_balance -= 1;
}
}
fn should_stop(ch: char, round_brackets_balance: i64, open_square_brackets_balance: i64) -> bool {
ch.is_whitespace() || round_brackets_balance < 0 || open_square_brackets_balance < 0
}
fn string_to_token(input: &str, start_pos: AirPos) -> LexerResult<Token> {
match input {
"" => Err(LexerError::empty_string(start_pos..start_pos)),
CALL_INSTR => Ok(Token::Call),
CANON_INSTR => Ok(Token::Canon),
AP_INSTR => Ok(Token::Ap),
SEQ_INSTR => Ok(Token::Seq),
PAR_INSTR => Ok(Token::Par),
FAIL_INSTR => Ok(Token::Fail),
FOLD_INSTR => Ok(Token::Fold),
XOR_INSTR => Ok(Token::Xor),
NEVER_INSTR => Ok(Token::Never),
NEW_INSTR => Ok(Token::New),
NEXT_INSTR => Ok(Token::Next),
NULL_INSTR => Ok(Token::Null),
MATCH_INSTR => Ok(Token::Match),
MISMATCH_INSTR => Ok(Token::MisMatch),
INIT_PEER_ID => Ok(Token::InitPeerId),
_ if input.starts_with(LAST_ERROR) => parse_last_error(input, start_pos),
TIMESTAMP => Ok(Token::Timestamp),
TTL => Ok(Token::TTL),
TRUE_VALUE => Ok(Token::Boolean(true)),
FALSE_VALUE => Ok(Token::Boolean(false)),
str => super::call_variable_parser::try_parse_call_variable(str, start_pos),
}
}
fn parse_last_error(input: &str, start_pos: AirPos) -> LexerResult<Token<'_>> {
let last_error_size = LAST_ERROR.len();
if input.len() == last_error_size {
return Ok(Token::LastError);
}
if input.len() <= last_error_size {
return Err(LexerError::lambda_parser_error(
start_pos + last_error_size..start_pos + input.len(),
"lambda AST applied to last error has not enough size",
));
}
let last_error_accessor = crate::parse_lambda(&input[last_error_size..]).map_err(|e| {
LexerError::lambda_parser_error(
start_pos + last_error_size..start_pos + input.len(),
e.to_string(),
)
})?;
let last_error_token = Token::LastErrorWithLambda(last_error_accessor);
Ok(last_error_token)
}
const CALL_INSTR: &str = "call";
const CANON_INSTR: &str = "canon";
const AP_INSTR: &str = "ap";
const SEQ_INSTR: &str = "seq";
const PAR_INSTR: &str = "par";
const FAIL_INSTR: &str = "fail";
const FOLD_INSTR: &str = "fold";
const XOR_INSTR: &str = "xor";
const NEVER_INSTR: &str = "never";
const NEW_INSTR: &str = "new";
const NEXT_INSTR: &str = "next";
const NULL_INSTR: &str = "null";
const MATCH_INSTR: &str = "match";
const MISMATCH_INSTR: &str = "mismatch";
const INIT_PEER_ID: &str = "%init_peer_id%";
const LAST_ERROR: &str = "%last_error%";
const TIMESTAMP: &str = "%timestamp%";
const TTL: &str = "%ttl%";
const TRUE_VALUE: &str = "true";
const FALSE_VALUE: &str = "false";