surrealdb_core/syn/lexer/mod.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
mod byte;
mod char;
pub mod compound;
mod ident;
pub mod keywords;
mod reader;
mod unicode;
#[cfg(test)]
mod test;
pub use reader::{BytesReader, CharError};
use crate::syn::{
error::{bail, SyntaxError},
token::{Span, Token, TokenKind},
};
/// The SurrealQL lexer.
/// Takes a slice of bytes and turns it into tokens. The lexer is designed with possible invalid utf-8
/// in mind and will handle bytes which are invalid utf-8 with an error.
///
/// The lexer generates tokens lazily. whenever [`Lexer::next_token`] is called on the lexer it will
/// try to lex the next bytes in the give source as a token. The lexer always returns a token, even
/// if the source contains invalid tokens or as at the end of the source. In both cases a specific
/// type of token is returned.
///
/// Note that SurrealQL syntax cannot be lexed in advance. For example, record strings and regexes,
/// both cannot be parsed correctly without knowledge of previous tokens as they are both ambigious
/// with other tokens.
#[non_exhaustive]
pub struct Lexer<'a> {
/// The reader for reading the source bytes.
pub(super) reader: BytesReader<'a>,
/// The one past the last character of the previous token.
last_offset: u32,
/// A buffer used to build the value of tokens which can't be read straight from the source.
/// like for example strings with escape characters.
scratch: String,
// below are a collection of storage for values produced by tokens.
// For performance reasons we wan't to keep the tokens as small as possible.
// As only some tokens have an additional value associated with them we don't store that value
// in the token itself but, instead, in the lexer ensureing a smaller size for each individual
// token.
//
// This does result in some additional state to keep track of as peeking a token while a token
// value is still in the variables below will overwrite the previous value.
//
// Both numbers and actual strings are stored as a string value.
// The parser can, depending on position in syntax, decide to parse a number in a variety of
// different precisions or formats. The only way to support all is to delay parsing the
// actual number value to when the parser can decide on a format.
pub(super) string: Option<String>,
pub(super) error: Option<SyntaxError>,
}
impl<'a> Lexer<'a> {
/// Create a new lexer.
/// # Panic
/// This function will panic if the source is longer then u32::MAX.
pub fn new(source: &'a [u8]) -> Lexer<'a> {
let reader = BytesReader::new(source);
assert!(reader.len() <= u32::MAX as usize, "source code exceeded maximum size");
Lexer {
reader,
last_offset: 0,
scratch: String::new(),
string: None,
error: None,
}
}
/// Reset the state of the lexer.
///
/// Doesn't change the state of the reader.
pub fn reset(&mut self) {
self.last_offset = 0;
self.scratch.clear();
self.string = None;
self.error = None;
}
/// Change the used source from the lexer to a new buffer.
///
/// Usefull for reusing buffers.
///
/// # Panic
/// This function will panic if the source is longer then u32::MAX.
pub fn change_source<'b>(self, source: &'b [u8]) -> Lexer<'b> {
let reader = BytesReader::<'b>::new(source);
assert!(reader.len() <= u32::MAX as usize, "source code exceeded maximum size");
Lexer {
reader,
last_offset: 0,
scratch: self.scratch,
string: self.string,
error: self.error,
}
}
/// Returns the next token, driving the lexer forward.
///
/// If the lexer is at the end the source it will always return the Eof token.
pub fn next_token(&mut self) -> Token {
let Some(byte) = self.reader.next() else {
return self.eof_token();
};
if byte.is_ascii() {
self.lex_ascii(byte)
} else {
self.lex_char(byte)
}
}
/// Creates the eof token.
///
/// An eof token has tokenkind Eof and an span which points to the last character of the
/// source.
fn eof_token(&mut self) -> Token {
Token {
kind: TokenKind::Eof,
span: Span {
offset: self.last_offset,
len: 0,
},
}
}
/// Return an invalid token.
fn invalid_token(&mut self, error: SyntaxError) -> Token {
self.error = Some(error);
self.finish_token(TokenKind::Invalid)
}
// Returns the span for the current token being lexed.
pub(crate) fn current_span(&self) -> Span {
// We make sure that the source is no longer then u32::MAX so this can't overflow.
let new_offset = self.reader.offset() as u32;
let len = new_offset - self.last_offset;
Span {
offset: self.last_offset,
len,
}
}
pub(crate) fn span_since(&self, offset: usize) -> Span {
let new_offset = self.reader.offset() as u32;
let len = new_offset - offset as u32;
Span {
offset: offset as u32,
len,
}
}
fn advance_span(&mut self) -> Span {
let span = self.current_span();
self.last_offset = self.reader.offset() as u32;
span
}
/// Builds a token from an TokenKind.
///
/// Attaches a span to the token and returns, updates the new offset.
fn finish_token(&mut self, kind: TokenKind) -> Token {
Token {
kind,
span: self.advance_span(),
}
}
/// Moves the lexer state back to before the give span.
///
/// # Warning
/// Moving the lexer into a state where the next byte is within a multibyte character will
/// result in spurious errors.
pub(crate) fn backup_before(&mut self, span: Span) {
self.reader.backup(span.offset as usize);
self.last_offset = span.offset;
}
/// Moves the lexer state to after the give span.
///
/// # Warning
/// Moving the lexer into a state where the next byte is within a multibyte character will
/// result in spurious errors.
pub(crate) fn backup_after(&mut self, span: Span) {
let offset = span.offset + span.len;
self.reader.backup(offset as usize);
self.last_offset = offset;
}
/// Checks if the next byte is the given byte, if it is it consumes the byte and returns true.
/// Otherwise returns false.
///
/// Also returns false if there is no next character.
fn eat(&mut self, byte: u8) -> bool {
if self.reader.peek() == Some(byte) {
self.reader.next();
true
} else {
false
}
}
/// Checks if the closure returns true when given the next byte, if it is it consumes the byte
/// and returns true. Otherwise returns false.
///
/// Also returns false if there is no next character.
fn eat_when<F: FnOnce(u8) -> bool>(&mut self, f: F) -> bool {
let Some(x) = self.reader.peek() else {
return false;
};
if f(x) {
self.reader.next();
true
} else {
false
}
}
fn expect(&mut self, c: char) -> Result<(), SyntaxError> {
match self.reader.peek() {
Some(x) => {
let offset = self.reader.offset() as u32;
self.reader.next();
let char = self.reader.convert_to_char(x)?;
if char == c {
return Ok(());
}
let len = self.reader.offset() as u32 - offset;
bail!(
"Unexpected character `{char}` expected `{c}`",
@Span {
offset,
len
}
)
}
None => {
bail!("Unexpected end of file, expected character `{c}`", @self.current_span())
}
}
}
/// Returns the string for a given span of the source.
/// Will panic if the given span was not valid for the source, or invalid utf8
pub fn span_str(&self, span: Span) -> &'a str {
std::str::from_utf8(self.span_bytes(span)).expect("invalid span segment for source")
}
/// Returns the string for a given span of the source.
/// Will panic if the given span was not valid for the source, or invalid utf8
pub fn span_bytes(&self, span: Span) -> &'a [u8] {
self.reader.span(span)
}
/// Returns an error if not all bytes were consumed.
pub fn assert_finished(&self) -> Result<(), SyntaxError> {
if !self.reader.is_empty() {
let offset = self.reader.offset() as u32;
let len = self.reader.remaining().len() as u32;
let span = Span {
offset,
len,
};
bail!("Trailing characters", @span)
}
Ok(())
}
}
impl Iterator for Lexer<'_> {
type Item = Token;
fn next(&mut self) -> Option<Self::Item> {
let token = self.next_token();
if token.is_eof() {
return None;
}
Some(token)
}
}