1#![allow(dead_code)]
2
3use sea_query::{Token, Tokenizer};
4
5pub struct Parser {
6 pub tokens: Tokenizer,
7 pub curr: Option<Token>,
8 pub last: Option<Token>,
9}
10
11impl Parser {
12 pub fn new(string: &str) -> Self {
13 Self {
14 tokens: Tokenizer::new(string),
15 curr: None,
16 last: None,
17 }
18 }
19
20 pub fn curr(&mut self) -> Option<&Token> {
21 if self.curr.is_some() {
22 self.curr.as_ref()
23 } else {
24 self.next()
25 }
26 }
27
28 pub fn last(&mut self) -> Option<&Token> {
29 self.last.as_ref()
30 }
31
32 #[allow(clippy::should_implement_trait)]
33 pub fn next(&mut self) -> Option<&Token> {
34 if self.curr.is_some() {
35 self.last = std::mem::take(&mut self.curr);
36 }
37
38 if let Some(tok) = self.tokens.next() {
39 if tok.is_space() {
40 if let Some(tok) = self.tokens.next() {
41 self.curr = Some(tok);
42 }
43 } else {
44 self.curr = Some(tok);
45 }
46 }
47 self.curr.as_ref()
48 }
49
50 pub fn next_if_unquoted(&mut self, word: &str) -> bool {
51 if let Some(tok) = self.curr() {
52 if tok.is_unquoted() && tok.as_str().to_lowercase() == word.to_lowercase() {
53 self.next();
54 return true;
55 }
56 }
57 false
58 }
59
60 pub fn next_if_quoted_any(&mut self) -> Option<&Token> {
61 if let Some(tok) = self.curr() {
62 if tok.is_quoted() {
63 self.next();
64 return self.last();
65 }
66 }
67 None
68 }
69
70 pub fn next_if_unquoted_any(&mut self) -> Option<&Token> {
71 if let Some(tok) = self.curr() {
72 if tok.is_unquoted() {
73 self.next();
74 return self.last();
75 }
76 }
77 None
78 }
79
80 pub fn next_if_punctuation(&mut self, word: &str) -> bool {
81 if let Some(tok) = self.curr() {
82 if tok.is_punctuation() && tok.as_str() == word {
83 self.next();
84 return true;
85 }
86 }
87 false
88 }
89
90 pub fn curr_is_unquoted(&mut self) -> bool {
91 self.curr().is_some() && self.curr().unwrap().is_unquoted()
92 }
93
94 pub fn curr_as_str(&mut self) -> &str {
95 self.curr().unwrap().as_str()
96 }
97}