rustql_parser 0.1.0

rustql parser for graphql
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
#[allow(clippy::all)]
use std::str::CharIndices;
use rustql_common::position::Position;
use rustql_common::token::TokenKind;
use crate::{lexer_error, internal_error};

pub struct Lexer<'a> {
    source: &'a str,
    iter: CharIndices<'a>,
    iter_byte_index: usize,
    iter_char: Option<char>,

    tok: TokenKind,
    pos: Position,
    start_pos: Position,
    end_pos: Position,
    start_byte_index: usize,
    end_byte_index: usize,
}

impl<'a> Lexer<'a> {
    pub fn new(source: &'a str) -> Self {
        let mut iter = source.char_indices();
        let frist_tuple = iter.next();
        match frist_tuple {
            Some((index, frist_char)) => {
                Self {
                    source,
                    iter,
                    iter_char: Some(frist_char),
                    iter_byte_index: index,

                    tok: TokenKind::Start,
                    pos: Position::new(),
                    start_pos: Position::new(),
                    end_pos: Position::new(),
                    start_byte_index: 0,
                    end_byte_index: 0,
                }
            }
            None => {
                Self {
                    source,
                    iter,
                    iter_char: None,
                    iter_byte_index: 0,

                    tok: TokenKind::EOFToken,
                    pos: Position::new(),
                    start_pos: Position::new(),
                    end_pos: Position::new(),
                    start_byte_index: 0,
                    end_byte_index: 0,
                }
            }
        }
    }
    fn is_char(&self, target: char) -> bool {
        if let Some(ch) = self.get_char() {
            if ch == target {
                return true;
            }
        }
        false
    }
    fn get_char(&self) -> Option<char> {
        self.iter_char
    }
    fn eat_char(&mut self, mut n: usize) {
        while n != 0 {
            if let Some(code) = self.get_char() {
                match code {
                    '\n' => {
                        self.pos.col = 0;
                        self.pos.row += 1; 
                    }
                    _ => {
                        self.pos.col += 1;
                    }
                }
                self.pos.index += 1;
                n -= 1;
                match self.iter.next() {
                    Some(tuple) => {
                        self.iter_char = Some(tuple.1);
                        self.iter_byte_index = tuple.0;
                    }
                    None => {
                        self.iter_char = None; 
                        self.iter_byte_index = self.source.len();
                    }
                }
            }else {
                break;
            }
        }
    }
    fn start_with(&self, pat: &str) -> bool {
        self.source[self.iter_byte_index..].starts_with(pat)
    }
    fn start_token(&mut self) {
        self.start_byte_index = self.iter_byte_index;
        self.start_pos = self.pos.clone();
    }
    fn finish_token(&mut self) {
        self.end_byte_index = self.iter_byte_index;
        self.end_pos = self.pos.clone();
    }
    fn skip_ignore_token(&mut self) {
        while let Some(code) = self.get_char() {
            match code {
                '\n' | ' ' | '\t' | ',' | '\r'=> self.eat_char(1) ,
                _ => break
            }
        }
    }
    pub fn get_start_pos(&self) -> Position {
        self.start_pos.clone()
    }
    pub fn get_end_pos(&self) -> Position {
        self.end_pos.clone()
    }
    /* this method only used for debug  */
    pub fn get_pos(&self) -> Position {
        self.pos.clone()
    }
    pub fn get_start_byte_index(&self) -> usize {
        self.start_byte_index
    }
    pub fn get_end_byte_index(&self) -> usize {
        self.end_byte_index
    }
    pub fn get_source_string(&self, start: usize, end: usize) -> &'a str {
        &self.source[start..end]
    }
    pub fn get_value(&self)-> &'a str {
        &self.source[self.start_byte_index..self.end_byte_index]
    }
    pub fn get_token(&mut self) -> TokenKind {
        if self.tok == TokenKind::Start {
            self.next_token()
        }else {
            self.tok.clone()
        }
    }
    pub fn next_token(&mut self) -> TokenKind {
        self.skip_ignore_token();
        self.start_token();
        self.tok = match self.get_char() {
            None => {
                self.finish_token();
                TokenKind::EOFToken
            }
            Some(code) => {
                match code  {
                    '!' => {
                        self.eat_char(1);
                        self.finish_token();
                        TokenKind::Point
                    }
                    '|' => {
                        self.eat_char(1);
                        self.finish_token();
                        TokenKind::Pipe
                    }
                    '$' => {
                        self.eat_char(1);
                        self.finish_token();
                        TokenKind::DollarSign
                    }
                    '(' => {
                        self.eat_char(1);
                        self.finish_token();
                        TokenKind::ParenthesesLeft
                    }
                    ')' => {
                        self.eat_char(1);
                        self.finish_token();
                        TokenKind::ParenthesesRight
                    }
                    ':' => {
                        self.eat_char(1);
                        self.finish_token();
                        TokenKind::Colon
                    }
                    '=' => {
                        self.eat_char(1);
                        self.finish_token();
                        TokenKind::Eqal
                    }
                    '@' => {
                        self.eat_char(1);
                        self.finish_token();
                        TokenKind::At
                    }
                    '[' => {
                        self.eat_char(1);
                        self.finish_token();
                        TokenKind::BracketLeft
                    }
                    ']' => {
                        self.eat_char(1);
                        self.finish_token();
                        TokenKind::BracketRight
                    }
                    '{' => {
                        self.eat_char(1);
                        self.finish_token();
                        TokenKind::BracesLeft
                    }
                    '}' => {
                        self.eat_char(1);
                        self.finish_token();
                        TokenKind::BracesRight
                    }
                    '.' => {
                        self.read_dot()
                    }
                    '&' => {
                        self.eat_char(1);
                        self.finish_token();
                        TokenKind::And
                    }
                    '-' | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
                        self.read_number()
                    }
                    '#' => {
                        self.read_comment()
                    }
                    '\"' => {
                        if self.start_with("\"\"\"") {
                            self.read_block_string()
                        } else {
                            self.read_string()
                        }
                    }
                    _ => {
                        if is_name_start(code) {
                            self.read_name()
                        }else {
                            lexer_error!("this char can not be parsed", self);
                        }
                    }
                }
            }
        };
        self.tok.clone()
    }
    fn read_dot(&mut self) -> TokenKind {
        if !self.start_with(".") {
            internal_error!("unreach code, read_dot function must be called when start with .");
        }
        if self.start_with("...") {
            self.eat_char(3);
            self.finish_token();
            return TokenKind::Ellipsis;
        }
        self.read_number()
    }
    fn read_name(&mut self) -> TokenKind {
        match self.get_char() {
            Some(ch) => {
                if !is_name_start(ch) {
                    internal_error!(format!("unreach code, read_name must be called with start name char, but got {:?}", ch));
                }
                self.eat_char(1);
            }
            None => {
                internal_error!("unreach code, rread_name must be called with start name char, but got EOF");
            }
        }
        while let Some(ch) = self.get_char() {
            if is_name_body(ch) {
                self.eat_char(1)
            }else {
                break;
            }
        }
        self.finish_token();
        TokenKind::Name

    }
    fn read_number(&mut self) -> TokenKind {
        let mut is_float = false;
        // Read nagaive
        if self.is_char('-') {
            self.eat_char(1);
        }
        // Read int part
        // if not start with 0, caume util not digital
        // if start with 0, must not start with digial next.
        if !self.is_char('0') {
            self.helper_read_digital();
        }else {
            self.eat_char(1);
            if let Some(ch) = self.get_char() {
                if is_digital(ch) {
                    lexer_error!("0 can not be followed by digial when it in begin of number", self);
                }
            }
        }
        // Read dot and if start with dot
        if self.is_char('.') {
            self.eat_char(1);
            is_float = true;
            self.helper_read_digital();
        }
        if self.is_char('e') || self.is_char('E') {
            self.eat_char(1);
            is_float = true;
            if self.is_char('+') || self.is_char('-') {
                self.eat_char(1);
            }
            self.helper_read_digital();
        }
        self.finish_token();
        // next char can not be any char belong to start with name 
        if let Some(ch) = self.get_char() {
            if is_name_start(ch) {
                lexer_error!("number can not be followed by this char", self);
            }
        }
        if is_float { TokenKind::FloatValue } else { TokenKind::IntValue }
    }
    fn helper_read_digital(&mut self) {
        while let Some(ch) = self.get_char() {
            if is_digital(ch) {
                self.eat_char(1);
            }else {
                break;
            }
        }
    }
    fn read_comment(&mut self) -> TokenKind {
        while let Some(ch) = self.get_char() {
            match ch {
                '\n' => break,
                _ => self.eat_char(1)
            }
        }
        self.finish_token();
        TokenKind::Comment
    }
    fn read_string(&mut self) -> TokenKind {
        if !self.start_with("\"") {
            internal_error!("unreach code, read_block_string must be call when start with '...'");
        }
        self.eat_char(1);

        while !self.start_with("\"")  {
            match self.get_char() {
                Some(code) => {
                    match code {
                        '\n' => { lexer_error!("non block string can not use lineterminator", self); },
                        _ => self.eat_char(1)
                    };
                }
                None => {
                    lexer_error!("unclose string.", self);
                }
            }
        };
        self.eat_char(1);
        self.finish_token();
        TokenKind::StringValue
    }
    fn read_block_string(&mut self) -> TokenKind {
        if !self.start_with("\"\"\"") {
            internal_error!("unreach code, read_block_string must be call when start with '...'");
        }
        self.eat_char(3);

        while !self.start_with("\"\"\"")  {
            if self.start_with("\\\"\"\"") {
                self.eat_char(4);
                continue;
            }
            match self.get_char() {
                Some(_) => {
                    self.eat_char(1);
                }
                None => {
                    lexer_error!("unclose block string.", self);
                }
            }
        }
        self.eat_char(3);
        self.finish_token();
        TokenKind::StringValue
    }
}

fn is_digital(ch: char) -> bool {
    matches!(ch, '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9')
}
fn is_name_start(ch: char) -> bool {
    matches!(ch, 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k' | 'l' | 'm' |
        'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z' |
        'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' |
        'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z' |
        '_')
}
fn is_name_body(ch: char) -> bool {
    if is_digital(ch) || is_name_start(ch) {
        return true
    }
    false
}