surrealdb_core/syn/lexer/compound/
strand.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
use std::ops::RangeInclusive;
use std::{char, mem};

use crate::syn::{
	error::{bail, syntax_error, SyntaxError},
	lexer::{unicode::chars, Lexer},
	token::{t, Token},
};

pub fn strand(lexer: &mut Lexer, start: Token) -> Result<String, SyntaxError> {
	let is_double = match start.kind {
		t!("\"") => true,
		t!("'") => false,
		_ => panic!("Invalid start of strand compound token"),
	};

	loop {
		let Some(x) = lexer.reader.next() else {
			lexer.scratch.clear();
			let err = syntax_error!("Unexpected end of file, expected strand to end",@lexer.current_span());
			return Err(err.with_data_pending());
		};

		if x.is_ascii() {
			match x {
				b'\'' if !is_double => {
					let res = mem::take(&mut lexer.scratch);
					return Ok(res);
				}
				b'"' if is_double => {
					let res = mem::take(&mut lexer.scratch);
					return Ok(res);
				}
				b'\0' => {
					bail!("Invalid null byte in source, null bytes are not valid SurrealQL characters",@lexer.current_span());
				}
				b'\\' => {
					// Handle escape sequences.
					let Some(next) = lexer.reader.next() else {
						lexer.scratch.clear();
						let err = syntax_error!("Unexpected end of file, expected strand to end",@lexer.current_span());
						return Err(err.with_data_pending());
					};
					match next {
						b'\\' => {
							lexer.scratch.push('\\');
						}
						b'\'' if !is_double => {
							lexer.scratch.push('\'');
						}
						b'\"' if is_double => {
							lexer.scratch.push('\"');
						}
						b'/' => {
							lexer.scratch.push('/');
						}
						b'b' => {
							lexer.scratch.push(chars::BS);
						}
						b'f' => {
							lexer.scratch.push(chars::FF);
						}
						b'n' => {
							lexer.scratch.push(chars::LF);
						}
						b'r' => {
							lexer.scratch.push(chars::CR);
						}
						b't' => {
							lexer.scratch.push(chars::TAB);
						}
						b'u' => {
							let c = lex_unicode_sequence(lexer)?;
							lexer.scratch.push(c);
						}
						x => match lexer.reader.convert_to_char(x) {
							Ok(char) => {
								let valid_escape = if is_double {
									'"'
								} else {
									'\''
								};
								bail!("Invalid escape character `{char}`, valid characters are `\\`, `{valid_escape}`, `/`, `b`, `f`, `n`, `r`, `t`, or `u` ", @lexer.current_span());
							}
							Err(e) => {
								lexer.scratch.clear();
								return Err(e.into());
							}
						},
					}
				}
				x => lexer.scratch.push(x as char),
			}
		} else {
			match lexer.reader.complete_char(x) {
				Ok(x) => lexer.scratch.push(x),
				Err(e) => {
					lexer.scratch.clear();
					return Err(e.into());
				}
			}
		}
	}
}

const LEADING_SURROGATES: RangeInclusive<u16> = 0xD800..=0xDBFF;
const TRAILING_SURROGATES: RangeInclusive<u16> = 0xDC00..=0xDFFF;

fn lex_unicode_sequence(lexer: &mut Lexer) -> Result<char, SyntaxError> {
	if let Some(b'{') = lexer.reader.peek() {
		lexer.reader.next();
		return lex_bracket_unicode_sequence(lexer);
	}

	let leading = lex_bare_unicode_sequence(lexer)?;
	if LEADING_SURROGATES.contains(&leading) {
		if !(lexer.reader.next() == Some(b'\\') && lexer.reader.next() == Some(b'u')) {
			bail!("Unicode escape sequence encoding a leading surrogate needs to be followed by a trailing surrogate", @lexer.current_span());
		}
		let trailing = lex_bare_unicode_sequence(lexer)?;
		// Compute the codepoint.
		// https://datacadamia.com/data/type/text/surrogate#from_surrogate_to_character_code
		let codepoint = 0x10000
			+ ((leading as u32 - *LEADING_SURROGATES.start() as u32) << 10)
			+ trailing as u32
			- *TRAILING_SURROGATES.start() as u32;

		return char::from_u32(codepoint).ok_or_else(|| {
			syntax_error!("Unicode escape sequences encode invalid character codepoint", @lexer.current_span())
		});
	}

	char::from_u32(leading as u32)
		.ok_or_else(|| syntax_error!("Unicode escape sequences encode invalid character codepoint", @lexer.current_span()))
}

fn lex_bracket_unicode_sequence(lexer: &mut Lexer) -> Result<char, SyntaxError> {
	let mut accum = 0u32;
	for _ in 0..6 {
		let c = lexer.reader.peek().ok_or_else(
			|| syntax_error!("Unexpected end of file, expected strand to end", @lexer.current_span()),
		)?;

		match c {
			b'a'..=b'f' => {
				accum <<= 4;
				accum += (c - b'a') as u32 + 10;
			}
			b'A'..=b'F' => {
				accum <<= 4;
				accum += (c - b'A') as u32 + 10;
			}
			b'0'..=b'9' => {
				accum <<= 4;
				accum += (c - b'0') as u32;
			}
			_ => break,
		}
		lexer.reader.next();
	}

	let Some(b'}') = lexer.reader.next() else {
		bail!("Missing end brace `}}` of unicode escape sequence", @lexer.current_span())
	};

	char::from_u32(accum)
		.ok_or_else(|| syntax_error!("Unicode escape sequences encode invalid character codepoint", @lexer.current_span()))
}

fn lex_bare_unicode_sequence(lexer: &mut Lexer) -> Result<u16, SyntaxError> {
	let mut accum: u16 = 0;
	for _ in 0..4 {
		let Some(c) = lexer.reader.next() else {
			bail!("Missing characters after unicode escape sequence", @lexer.current_span());
		};

		accum <<= 4;
		match c {
			b'a'..=b'f' => {
				accum += (c - b'a') as u16 + 10;
			}
			b'A'..=b'F' => {
				accum += (c - b'A') as u16 + 10;
			}
			b'0'..=b'9' => {
				accum += (c - b'0') as u16;
			}
			_ => {
				bail!("Invalid character `{}` in unicode escape sequence, must be a hex digit.",c as char, @lexer.current_span());
			}
		}
	}
	Ok(accum)
}