sui_sql_macro/parser.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 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
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use std::borrow::Cow;
use crate::lexer::{Lexeme, Token};
/// Recursive descent parser for format strings. This struct is intended to be lightweight to
/// support efficient backtracking. Backtracking is implemented by operating on a copy of the
/// parser's state, and only updating the original when the copy has reached a known good position.
#[derive(Copy, Clone)]
pub(crate) struct Parser<'l, 's> {
lexemes: &'l [Lexeme<'s>],
}
/// Structured representation of a format string, starting with a text prefix (the head), followed
/// by interleaved binders followed by text suffixes (the tail).
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Format<'s> {
pub head: Cow<'s, str>,
pub tail: Vec<(syn::Type, Cow<'s, str>)>,
}
#[derive(thiserror::Error, Debug)]
pub(crate) enum Error {
#[error("Unexpected {actual} at offset {offset}, expected {expect}")]
Unexpected {
offset: usize,
actual: Token,
expect: Token,
},
#[error("Unexpected end of format string, expected: {expect:?}")]
UnexpectedEos { expect: Token },
#[error("Error parsing type for binder at offset {offset}: {source}")]
TypeParse { offset: usize, source: syn::Error },
}
/// Recursive descent parser recognizing the following grammar:
///
/// format ::= text? (bind text?)*
/// text ::= part +
/// part ::= lcurly_escape | rcurly_escape | TEXT
/// bind ::= '{' TEXT '}'
/// lcurly_escape ::= '{' '{'
/// rcurly_escape ::= '}' '}'
///
/// The parser reads tokens from the front, consuming them when it is able to successfully parse a
/// non-terminal. When parsing fails, the parser may be left in an indeterminate state.
/// Backtracking requires explicitly copying the parser state.
impl<'l, 's> Parser<'l, 's> {
/// Constructs a new parser instance that will consume the given `lexemes`.
pub(crate) fn new(lexemes: &'l [Lexeme<'s>]) -> Self {
Self { lexemes }
}
/// Entrypoint to the parser. Consumes the entire remaining output (and therefore also the
/// parser).
pub(crate) fn format(mut self) -> Result<Format<'s>, Error> {
let head = self.text_opt();
let mut tail = vec![];
while let Some(bind) = self.bind()? {
let suffix = self.text_opt();
tail.push((bind, suffix));
}
Ok(Format { head, tail })
}
/// Parse a strand of text. The parser is left in its initial state and an empty string is
/// returned if there was no text to parse.
fn text_opt(&mut self) -> Cow<'s, str> {
let mut copy = *self;
copy.text().map_or(Cow::Borrowed(""), |t| {
*self = copy;
t
})
}
/// Parse a strand of text by gathering together as many `part`s as possible. Errors if there
/// is not at least one `part` to consume.
fn text(&mut self) -> Result<Cow<'s, str>, Error> {
let mut text = self.part()?;
let mut copy = *self;
while let Ok(part) = copy.part() {
text += part;
*self = copy;
}
Ok(text)
}
/// Parses any of the three possible `part`s of a text strand: a string of text, or an escaped
/// curly brace. Errors if the token stream starts with a binder.
fn part(&mut self) -> Result<Cow<'s, str>, Error> {
let mut copy = *self;
if let Ok(s) = copy.lcurly_escape() {
*self = copy;
return Ok(s);
}
let mut copy = *self;
if let Ok(s) = copy.rcurly_escape() {
*self = copy;
return Ok(s);
}
let Lexeme(_, _, text) = self.eat(Token::Text)?;
Ok(Cow::Borrowed(text))
}
/// Parses as an escaped left curly brace (two curly brace tokens).
fn lcurly_escape(&mut self) -> Result<Cow<'s, str>, Error> {
use Token as T;
self.eat(T::LCurl)?;
self.eat(T::LCurl)?;
Ok(Cow::Borrowed("{"))
}
/// Parses as an escaped right curly brace (two curly brace tokens).
fn rcurly_escape(&mut self) -> Result<Cow<'s, str>, Error> {
use Token as T;
self.eat(T::RCurl)?;
self.eat(T::RCurl)?;
Ok(Cow::Borrowed("}"))
}
/// Parses a binding (a text token surrounded by curly braces).
fn bind(&mut self) -> Result<Option<syn::Type>, Error> {
if self.lexemes.is_empty() {
return Ok(None);
}
self.eat(Token::LCurl)?;
let Lexeme(_, offset, bind) = self.eat(Token::Text)?;
self.eat(Token::RCurl)?;
let bind = syn::parse_str(bind).map_err(|source| Error::TypeParse { offset, source })?;
Ok(Some(bind))
}
/// Consume the next token (returning it) as long as it matches `expect`.
fn eat(&mut self, expect: Token) -> Result<Lexeme<'s>, Error> {
let &lexeme = self
.lexemes
.first()
.ok_or(Error::UnexpectedEos { expect })?;
if lexeme.0 == expect {
self.lexemes = &self.lexemes[1..];
Ok(lexeme)
} else {
Err(Error::Unexpected {
offset: lexeme.1,
actual: lexeme.0,
expect,
})
}
}
}
#[cfg(test)]
mod tests {
use crate::Lexer;
use super::*;
/// Test helper for lexing and parsing a format string.
fn parse(s: &str) -> Result<Format, Error> {
let lexemes: Vec<_> = Lexer::new(s).collect();
Parser::new(&lexemes).format()
}
/// Parse a Rust type from a string, to compare against in tests.
fn type_(s: &str) -> syn::Type {
syn::parse_str(s).unwrap()
}
#[test]
fn test_no_binds() {
// A simple format string with no binders will gather everything into the head.
assert_eq!(
parse("foo").unwrap(),
Format {
head: "foo".into(),
tail: vec![]
}
);
}
#[test]
fn test_single_bind() {
// A binder is parsed without its surrounding binders as a type, and splits the format
// string in two.
assert_eq!(
parse("foo = {Text} AND bar = 42").unwrap(),
Format {
head: "foo = ".into(),
tail: vec![(type_("Text"), " AND bar = 42".into())]
},
);
}
#[test]
fn test_multiple_binds() {
// When there are multiple binders the parser needs to detect the gap between binders.
assert_eq!(
parse("foo = {Text} AND (bar < {BigInt} OR bar > 5)").unwrap(),
Format {
head: "foo = ".into(),
tail: vec![
(type_("Text"), " AND (bar < ".into()),
(type_("BigInt"), " OR bar > 5)".into()),
],
},
);
}
#[test]
fn test_ends_with_a_bind() {
// If the format string ends with a binder, the parser still needs to find an empty suffix
// binder.
assert_eq!(
parse("bar BETWEEN {BigInt} AND {BigInt}").unwrap(),
Format {
head: "bar BETWEEN ".into(),
tail: vec![
(type_("BigInt"), " AND ".into()),
(type_("BigInt"), "".into()),
],
},
);
}
#[test]
fn test_escaped_curlies() {
// Escaped curlies are de-duplicated in the parsed output, but the parser does not break up
// strands of format string on them.
assert_eq!(
parse("foo LIKE '{{bar%'").unwrap(),
Format {
head: "foo LIKE '{bar%'".into(),
tail: vec![],
},
);
}
#[test]
fn test_curly_nest() {
// This input can be tricky to parse if the lexer treats escaped curlies as a single token.
assert_eq!(
parse("{{{Bool}}}").unwrap(),
Format {
head: "{".into(),
tail: vec![(type_("Bool"), "}".into())],
},
);
}
#[test]
fn test_bind_unexpected_token() {
// Error if the binder is not properly closed.
assert!(matches!(
parse("{Bool{").unwrap_err(),
Error::Unexpected {
offset: 5,
actual: Token::LCurl,
expect: Token::RCurl
},
));
}
#[test]
fn test_bind_no_type() {
// Error if the binder does not contain a type.
assert!(matches!(
parse("foo = {").unwrap_err(),
Error::UnexpectedEos {
expect: Token::Text,
},
));
}
#[test]
fn test_bind_no_rcurly() {
// Error if the binder ends before it is closed.
assert!(matches!(
parse("foo = {Text").unwrap_err(),
Error::UnexpectedEos {
expect: Token::RCurl,
},
));
}
#[test]
fn test_bind_bad_type() {
// Failure to parse the binder as a Rust type is also an error for this parser.
assert!(matches!(
parse("foo = {not a type}").unwrap_err(),
Error::TypeParse { offset: 7, .. },
));
}
}