-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtoken.rs
196 lines (180 loc) · 5.51 KB
/
token.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
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
//! Tokens.
/// What we expected to get, but did not get.
#[derive(Debug, PartialEq, Eq)]
pub enum Expect {
/// ` ` or `]` or `,`
Value,
/// `[` or `{`
ValueOrEnd,
/// `[1` or `[1 2`
CommaOrEnd,
/// `{0: 1}`
String,
/// `{"a" 1}`
Colon,
/// `true false` (when parsing exactly one value)
Eof,
}
impl core::fmt::Display for Expect {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
use Expect::*;
match self {
Value => "value".fmt(f),
ValueOrEnd => "value or end of sequence".fmt(f),
CommaOrEnd => "comma or end of sequence".fmt(f),
String => "string".fmt(f),
Colon => "colon".fmt(f),
Eof => "end of file".fmt(f),
}
}
}
/// JSON lexer token.
#[derive(Debug, PartialEq, Eq)]
pub enum Token {
/// `null`
Null,
/// `true`
True,
/// `false`
False,
/// `,`
Comma,
/// `:`
Colon,
/// `[`
LSquare,
/// `]`
RSquare,
/// `{`
LCurly,
/// `}`
RCurly,
/// `"`
Quote,
/// a digit (0-9) or a minus (`-`)
DigitOrMinus,
/// anything else
Error,
}
impl core::fmt::Display for Token {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
use Token::*;
match self {
Null => "null".fmt(f),
True => "true".fmt(f),
False => "false".fmt(f),
Comma => ",".fmt(f),
Colon => ":".fmt(f),
LSquare => "[".fmt(f),
RSquare => "]".fmt(f),
LCurly => "{".fmt(f),
RCurly => "}".fmt(f),
Quote => '"'.fmt(f),
DigitOrMinus => "number".fmt(f),
Error => "unknown token".fmt(f),
}
}
}
impl Token {
/// Return `Ok(())` if `self` equals `token`, else return `Err(err)`.
pub fn equals_or<E>(&self, token: Token, err: E) -> Result<(), E> {
if *self == token {
Ok(())
} else {
Err(err)
}
}
}
/// Lexing that does not require allocation.
pub trait Lex: crate::Read {
/// Skip input until the earliest non-whitespace character.
fn eat_whitespace(&mut self) {
self.skip_next_until(|c| !matches!(c, b' ' | b'\t' | b'\r' | b'\n'))
}
/// Skip potential whitespace and return the following token if there is some.
fn ws_token(&mut self) -> Option<Token> {
self.eat_whitespace();
Some(self.token(*self.peek_next()?))
}
/// Return `out` if the input matches `s`, otherwise return an error.
fn exact<const N: usize>(&mut self, s: [u8; N], out: Token) -> Token {
// we are calling this function without having advanced before
self.take_next();
if self.strip_prefix(s) {
out
} else {
Token::Error
}
}
/// Convert a character to a token, such as '`:`' to `Token::Colon`.
///
/// When the token consists of several characters, such as
/// `null`, `true`, or `false`,
/// also consume the following characters.
fn token(&mut self, c: u8) -> Token {
let token = match c {
// it is important to `return` here in order not to read a byte,
// like we do for the regular, single-character tokens
b'n' => return self.exact([b'u', b'l', b'l'], Token::Null),
b't' => return self.exact([b'r', b'u', b'e'], Token::True),
b'f' => return self.exact([b'a', b'l', b's', b'e'], Token::False),
b'0'..=b'9' | b'-' => return Token::DigitOrMinus,
b'"' => Token::Quote,
b'[' => Token::LSquare,
b']' => Token::RSquare,
b'{' => Token::LCurly,
b'}' => Token::RCurly,
b',' => Token::Comma,
b':' => Token::Colon,
_ => Token::Error,
};
self.take_next();
token
}
/// Parse a string with given function, followed by a colon.
fn str_colon<T, E: From<Expect>, F>(&mut self, token: Token, f: F) -> Result<T, E>
where
F: FnOnce(&mut Self) -> Result<T, E>,
{
token.equals_or(Token::Quote, Expect::String)?;
let key = f(self)?;
let colon = self.ws_token().filter(|t| *t == Token::Colon);
colon.ok_or(Expect::Colon)?;
Ok(key)
}
/// Execute `f` for every item in the comma-separated sequence until `end`.
fn seq<E: From<Expect>, F>(&mut self, end: Token, mut f: F) -> Result<(), E>
where
F: FnMut(Token, &mut Self) -> Result<(), E>,
{
let mut token = self.ws_token().ok_or(Expect::ValueOrEnd)?;
if token == end {
return Ok(());
};
loop {
f(token, self)?;
token = self.ws_token().ok_or(Expect::CommaOrEnd)?;
if token == end {
return Ok(());
} else if token == Token::Comma {
token = self.ws_token().ok_or(Expect::Value)?;
} else {
return Err(Expect::CommaOrEnd)?;
}
}
}
/// Parse once using given function and assure that the function has consumed all tokens.
fn exactly_one<T, E: From<Expect>, F>(&mut self, f: F) -> Result<T, E>
where
F: FnOnce(Token, &mut Self) -> Result<T, E>,
{
let token = self.ws_token().ok_or(Expect::Value)?;
let v = f(token, self)?;
self.eat_whitespace();
match self.peek_next() {
None => Ok(v),
Some(_) => Err(Expect::Eof)?,
}
}
}
impl<T> Lex for T where T: crate::Read {}