Skip to content

Commit 69a140a

Browse files
committed
create Chars struct instead of using Peekable
1 parent 7ff4133 commit 69a140a

File tree

5 files changed

+64
-59
lines changed

5 files changed

+64
-59
lines changed

examples/acme_parser.rs

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,40 +35,30 @@ impl SQLTokenizer<AcmeToken> for AcmeTokenizer {
3535
unimplemented!()
3636
}
3737

38-
fn peek_token(&mut self) -> Result<Option<SQLToken<AcmeToken>>, TokenizerError<AcmeToken>> {
38+
fn next_token(&mut self) -> Result<Option<SQLToken<AcmeToken>>, TokenizerError> {
39+
// let mut arc = self.ansi_tokenizer.lock().unwrap();
40+
// match arc.peek_char() {
41+
// Some(&ch) => match ch {
42+
// '!' => {
43+
// arc.next_char(); // consume the first `!`
44+
// match arc.peek_char() {
45+
// Some(&ch) => match ch {
46+
// '!' => {
47+
// arc.next_char(); // consume the second `!`
48+
// Ok(Some(SQLToken::Custom(AcmeToken::Factorial)))
49+
// },
50+
// _ => Err(TokenizerError::UnexpectedChar(ch,Position::new(0,0)))
51+
// },
52+
// None => Ok(Some(SQLToken::Not))
53+
// }
54+
// }
55+
// _ => arc.next_token()
56+
// }
57+
// _ => arc.next_token()
58+
// }
3959
unimplemented!()
4060
}
4161

42-
fn next_token(&mut self) -> Result<Option<SQLToken<AcmeToken>>, TokenizerError<AcmeToken>> {
43-
let mut arc = self.ansi_tokenizer.lock().unwrap();
44-
match arc.peek_char() {
45-
Some(&ch) => match ch {
46-
'!' => {
47-
arc.next_char(); // consume the first `!`
48-
match arc.peek_char() {
49-
Some(&ch) => match ch {
50-
'!' => {
51-
arc.next_char(); // consume the second `!`
52-
Ok(Some(SQLToken::Custom(AcmeToken::Factorial)))
53-
},
54-
_ => Err(TokenizerError::UnexpectedChar(ch,Position::new(0,0)))
55-
},
56-
None => Ok(Some(SQLToken::Not))
57-
}
58-
}
59-
_ => arc.next_token()
60-
}
61-
_ => arc.next_token()
62-
}
63-
}
64-
65-
fn peek_char(&mut self) -> Option<&char> {
66-
unimplemented!()
67-
}
68-
69-
fn next_char(&mut self) -> Option<&char> {
70-
unimplemented!()
71-
}
7262
}
7363

7464
struct AcmeParser {

src/ansi/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl<TokenType, ExprType> SQLParser<TokenType, ExprType> for ANSISQLParser<Token
2424

2525
fn parse_prefix(&mut self) -> Result<Box<SQLExpr<ExprType>>, ParserError<TokenType>> {
2626

27-
match self.tokenizer.lock().unwrap().peek_token()? {
27+
match self.tokenizer.lock().unwrap().next_token()? {
2828
Some(SQLToken::Keyword(ref k)) => match k.to_uppercase().as_ref() {
2929
"INSERT" => unimplemented!(),
3030
"UPDATE" => unimplemented!(),

src/ansi/tokenizer.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ impl<'a, TokenType> SQLTokenizer<TokenType> for ANSISQLTokenizer<'a>
1616
unimplemented!()
1717
}
1818

19-
fn peek_token(&mut self) -> Result<Option<SQLToken<TokenType>>, TokenizerError<TokenType>> {
20-
unimplemented!()
21-
}
22-
23-
fn next_token(&mut self) -> Result<Option<SQLToken<TokenType>>, TokenizerError<TokenType>> {
19+
fn next_token(&mut self) -> Result<Option<SQLToken<TokenType>>, TokenizerError> {
2420
match self.chars.next() {
2521
Some(ch) => match ch {
2622
' ' | '\t' | '\n' => Ok(Some(SQLToken::Whitespace(ch))),
@@ -48,12 +44,5 @@ impl<'a, TokenType> SQLTokenizer<TokenType> for ANSISQLTokenizer<'a>
4844
}
4945
}
5046

51-
fn peek_char(&mut self) -> Option<&char> {
52-
unimplemented!()
53-
}
54-
55-
fn next_char(&mut self) -> Option<&char> {
56-
unimplemented!()
57-
}
5847
}
5948

src/parser.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use std::cmp::PartialEq;
22
use std::fmt::Debug;
33
use std::rc::Rc;
4-
use std::str::Chars;
5-
use std::iter::Peekable;
64
use std::sync::{Arc, Mutex};
75

86
use super::tokenizer::*;
@@ -99,10 +97,10 @@ pub enum ParserError<TokenType>
9997
Custom(String)
10098
}
10199

102-
impl<TokenType> From<TokenizerError<TokenType>> for ParserError<TokenType>
100+
impl<TokenType> From<TokenizerError> for ParserError<TokenType>
103101
where TokenType: Debug + PartialEq {
104102

105-
fn from(_: TokenizerError<TokenType>) -> Self {
103+
fn from(_: TokenizerError) -> Self {
106104
unimplemented!()
107105
}
108106
}

src/tokenizer.rs

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,41 @@ use std::fmt::Debug;
33
//use std::iter::Peekable;
44
//use std::str::Chars;
55

6+
7+
pub struct CharSeq {
8+
chars: Vec<char>,
9+
i: usize,
10+
m: usize
11+
}
12+
13+
impl CharSeq {
14+
15+
pub fn new(sql: &str) -> Self {
16+
CharSeq {
17+
chars: sql.chars().collect(),
18+
i: 0,
19+
m: 0
20+
}
21+
}
22+
23+
pub fn mark(&mut self) {
24+
self.m = self.i;
25+
}
26+
27+
pub fn reset(&mut self) {
28+
self.i = self.m;
29+
}
30+
31+
pub fn next(&mut self) -> Option<char> {
32+
if self.i < self.chars.len() {
33+
self.i += 1;
34+
Some(self.chars[self.i-1])
35+
} else {
36+
None
37+
}
38+
}
39+
}
40+
641
#[derive(Debug)]
742
pub struct Position {
843
line: usize,
@@ -15,11 +50,11 @@ impl Position {
1550
}
1651

1752
#[derive(Debug)]
18-
pub enum TokenizerError<T> {
53+
pub enum TokenizerError {
1954
UnexpectedChar(char,Position),
2055
UnexpectedEof(Position),
2156
UnterminatedStringLiteral(Position),
22-
Custom(T)
57+
Custom(String)
2358
}
2459

2560
/// SQL Tokens
@@ -43,7 +78,7 @@ pub enum SQLToken<T: Debug + PartialEq> {
4378
LParen,
4479
RParen,
4580
Comma,
46-
/// Custom token
81+
/// Custom token (dialect-specific)
4782
Custom(T)
4883
}
4984

@@ -53,15 +88,8 @@ pub trait SQLTokenizer<TokenType>
5388
/// get the precendence of a token
5489
fn precedence(&self, token: &SQLToken<TokenType>) -> usize;
5590

56-
/// return a reference to the next token but do not advance the index
57-
fn peek_token(&mut self) -> Result<Option<SQLToken<TokenType>>, TokenizerError<TokenType>>;
58-
5991
/// return a reference to the next token and advance the index
60-
fn next_token(&mut self) -> Result<Option<SQLToken<TokenType>>, TokenizerError<TokenType>>;
61-
62-
fn peek_char(&mut self) -> Option<&char>;
63-
64-
fn next_char(&mut self) -> Option<&char>;
92+
fn next_token(&mut self) -> Result<Option<SQLToken<TokenType>>, TokenizerError>;
6593
}
6694

6795
//

0 commit comments

Comments
 (0)