-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.rs
More file actions
232 lines (206 loc) · 6.97 KB
/
token.rs
File metadata and controls
232 lines (206 loc) · 6.97 KB
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
use enum_as_inner::EnumAsInner;
use serde::{Deserialize, Serialize};
#[derive(Clone, PartialEq, Serialize, Deserialize, Eq)]
pub struct Token {
pub kind: TokenKind,
pub span: std::ops::Range<usize>,
}
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
pub enum TokenKind {
NewLine,
Ident(String),
Keyword(String),
#[cfg_attr(
feature = "serde_yaml",
serde(with = "serde_yaml::with::singleton_map")
)]
Literal(Literal),
Param(String),
Range {
/// Whether the left side of the range is bound by the previous token
/// (but it's not contained in this token)
bind_left: bool,
bind_right: bool,
},
Interpolation(char, String),
/// single-char control tokens
Control(char),
ArrowThin, // ->
ArrowFat, // =>
Eq, // ==
Ne, // !=
Gte, // >=
Lte, // <=
RegexSearch, // ~=
And, // &&
Or, // ||
Coalesce, // ??
DivInt, // //
// Pow, // **
Annotate, // @
// Aesthetics only
Comment(String),
DocComment(String),
/// Vec containing comments between the newline and the line wrap
// Currently we include the comments with the LineWrap token. This isn't
// ideal, but I'm not sure of an easy way of having them be separate.
// - The line wrap span technically includes the comments — on a newline,
// we need to look ahead to _after_ the comments to see if there's a
// line wrap, and exclude the newline if there is.
// - We can only pass one token back
//
// Alternatives:
// - Post-process the stream, removing the newline prior to a line wrap.
// But requires a whole extra pass.
// - Change the functionality. But it's very nice to be able to comment
// something out and have line-wraps still work.
LineWrap(Vec<TokenKind>),
}
#[derive(Debug, EnumAsInner, PartialEq, Clone, Serialize, Deserialize, strum::AsRefStr)]
pub enum Literal {
Null,
Integer(i64),
Float(f64),
Boolean(bool),
String(String),
Date(String),
Time(String),
Timestamp(String),
ValueAndUnit(ValueAndUnit),
}
impl TokenKind {
pub fn range(bind_left: bool, bind_right: bool) -> Self {
TokenKind::Range {
bind_left,
bind_right,
}
}
}
// Compound units, such as "2 days 3 hours" can be represented as `2days + 3hours`
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ValueAndUnit {
pub n: i64, // Do any DBs use floats or decimals for this?
pub unit: String, // Could be an enum IntervalType,
}
impl std::fmt::Display for Literal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Literal::Null => write!(f, "null")?,
Literal::Integer(i) => write!(f, "{i}")?,
Literal::Float(i) => write!(f, "{i}")?,
Literal::String(s) => {
quote_string(s, f)?;
}
Literal::Boolean(b) => {
f.write_str(if *b { "true" } else { "false" })?;
}
Literal::Date(inner) | Literal::Time(inner) | Literal::Timestamp(inner) => {
write!(f, "@{inner}")?;
}
Literal::ValueAndUnit(i) => {
write!(f, "{}{}", i.n, i.unit)?;
}
}
Ok(())
}
}
fn quote_string(s: &str, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = escape_all_except_quotes(s);
if !s.contains('"') {
return write!(f, r#""{s}""#);
}
if !s.contains('\'') {
return write!(f, "'{s}'");
}
// when string contains both single and double quotes
// find minimum number of double quotes
let mut quotes = "\"\"".to_string();
while s.contains("es) {
quotes += "\"";
}
write!(f, "{quotes}{s}{quotes}")
}
fn escape_all_except_quotes(s: &str) -> String {
let mut result = String::new();
for ch in s.chars() {
if ch == '"' || ch == '\'' {
result.push(ch);
} else {
result.extend(ch.escape_default());
}
}
result
}
// This is here because Literal::Float(f64) does not implement Hash, so we cannot simply derive it.
// There are reasons for that, but chumsky::Error needs Hash for the TokenKind, so it can deduplicate
// tokens in error.
// So this hack could lead to duplicated tokens in error messages. Oh no.
#[allow(clippy::derived_hash_with_manual_eq)]
impl std::hash::Hash for TokenKind {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
core::mem::discriminant(self).hash(state);
}
}
impl std::cmp::Eq for TokenKind {}
impl std::fmt::Display for TokenKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TokenKind::NewLine => write!(f, "new line"),
TokenKind::Ident(s) => {
if s.is_empty() {
// FYI this shows up in errors
write!(f, "an identifier")
} else {
write!(f, "{s}")
}
}
TokenKind::Keyword(s) => write!(f, "keyword {s}"),
TokenKind::Literal(lit) => write!(f, "{}", lit),
TokenKind::Control(c) => write!(f, "{c}"),
TokenKind::ArrowThin => f.write_str("->"),
TokenKind::ArrowFat => f.write_str("=>"),
TokenKind::Eq => f.write_str("=="),
TokenKind::Ne => f.write_str("!="),
TokenKind::Gte => f.write_str(">="),
TokenKind::Lte => f.write_str("<="),
TokenKind::RegexSearch => f.write_str("~="),
TokenKind::And => f.write_str("&&"),
TokenKind::Or => f.write_str("||"),
TokenKind::Coalesce => f.write_str("??"),
TokenKind::DivInt => f.write_str("//"),
// TokenKind::Pow => f.write_str("**"),
TokenKind::Annotate => f.write_str("@{"),
TokenKind::Param(id) => write!(f, "${id}"),
TokenKind::Range {
bind_left,
bind_right,
} => write!(
f,
"'{}..{}'",
if *bind_left { "" } else { " " },
if *bind_right { "" } else { " " }
),
TokenKind::Interpolation(c, s) => {
write!(f, "{c}\"{}\"", s)
}
TokenKind::Comment(s) => {
writeln!(f, "#{}", s)
}
TokenKind::DocComment(s) => {
writeln!(f, "#!{}", s)
}
TokenKind::LineWrap(comments) => {
write!(f, "\n\\ ")?;
for comment in comments {
write!(f, "{}", comment)?;
}
Ok(())
}
}
}
}
impl std::fmt::Debug for Token {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}..{}: {:?}", self.span.start, self.span.end, self.kind)
}
}