surrealdb_core/syn/
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//! Module containing the implementation of the surrealql tokens, lexer, and parser.

use crate::{
	cnf::{MAX_OBJECT_PARSING_DEPTH, MAX_QUERY_PARSING_DEPTH},
	err::Error,
	sql::{Block, Datetime, Duration, Idiom, Query, Range, Subquery, Thing, Value},
};

pub mod error;
pub mod lexer;
pub mod parser;
pub mod token;

#[cfg(test)]
pub trait Parse<T> {
	fn parse(val: &str) -> T;
}

#[cfg(test)]
mod test;

use lexer::{compound, Lexer};
use parser::Parser;
use reblessive::Stack;
use token::t;

const TARGET: &str = "surrealdb::core::syn";

/// Takes a string and returns if it could be a reserved keyword in certain contexts.
pub fn could_be_reserved_keyword(s: &str) -> bool {
	lexer::keywords::could_be_reserved(s)
}

/// Parses a SurrealQL [`Query`]
///
/// During query parsing, the total depth of calls to parse values (including arrays, expressions,
/// functions, objects, sub-queries), Javascript values, and geometry collections count against
/// a computation depth limit. If the limit is reached, parsing will return
/// [`Error::ComputationDepthExceeded`], as opposed to spending more time and potentially
/// overflowing the call stack.
///
/// If you encounter this limit and believe that it should be increased,
/// please [open an issue](https://github.com/surrealdb/surrealdb/issues)!
#[instrument(level = "trace", target = "surrealdb::core::syn", fields(length = input.len()))]
pub fn parse(input: &str) -> Result<Query, Error> {
	trace!(target: TARGET, "Parsing SurrealQL query");

	if input.len() > u32::MAX as usize {
		return Err(Error::QueryTooLarge);
	}

	let mut parser = Parser::new(input.as_bytes())
		.with_object_recursion_limit(*MAX_OBJECT_PARSING_DEPTH as usize)
		.with_query_recursion_limit(*MAX_QUERY_PARSING_DEPTH as usize);
	let mut stack = Stack::new();
	stack
		.enter(|stk| parser.parse_query(stk))
		.finish()
		.map_err(|e| e.render_on(input))
		.map_err(Error::InvalidQuery)
}

/// Parses a SurrealQL [`Value`].
#[instrument(level = "trace", target = "surrealdb::core::syn", fields(length = input.len()))]
pub fn value(input: &str) -> Result<Value, Error> {
	trace!(target: TARGET, "Parsing SurrealQL value");

	if input.len() > u32::MAX as usize {
		return Err(Error::QueryTooLarge);
	}

	let mut parser = Parser::new(input.as_bytes())
		.with_object_recursion_limit(*MAX_OBJECT_PARSING_DEPTH as usize)
		.with_query_recursion_limit(*MAX_QUERY_PARSING_DEPTH as usize);
	let mut stack = Stack::new();
	stack
		.enter(|stk| parser.parse_value_field(stk))
		.finish()
		.and_then(|e| parser.assert_finished().map(|_| e))
		.map_err(|e| e.render_on(input))
		.map_err(Error::InvalidQuery)
}

/// Parses JSON into an inert SurrealQL [`Value`]
#[instrument(level = "trace", target = "surrealdb::core::syn", fields(length = input.len()))]
pub fn json(input: &str) -> Result<Value, Error> {
	trace!(target: TARGET, "Parsing inert JSON value");

	if input.len() > u32::MAX as usize {
		return Err(Error::QueryTooLarge);
	}

	let mut parser = Parser::new(input.as_bytes())
		.with_object_recursion_limit(*MAX_OBJECT_PARSING_DEPTH as usize)
		.with_query_recursion_limit(*MAX_QUERY_PARSING_DEPTH as usize);
	let mut stack = Stack::new();
	stack
		.enter(|stk| parser.parse_json(stk))
		.finish()
		.and_then(|e| parser.assert_finished().map(|_| e))
		.map_err(|e| e.render_on(input))
		.map_err(Error::InvalidQuery)
}

/// Parses a SurrealQL Subquery [`Subquery`]
#[instrument(level = "trace", target = "surrealdb::core::syn", fields(length = input.len()))]
pub fn subquery(input: &str) -> Result<Subquery, Error> {
	trace!(target: TARGET, "Parsing SurrealQL subquery");

	if input.len() > u32::MAX as usize {
		return Err(Error::QueryTooLarge);
	}

	let mut parser = Parser::new(input.as_bytes())
		.with_object_recursion_limit(*MAX_OBJECT_PARSING_DEPTH as usize)
		.with_query_recursion_limit(*MAX_QUERY_PARSING_DEPTH as usize);
	let mut stack = Stack::new();
	stack
		.enter(|stk| parser.parse_full_subquery(stk))
		.finish()
		.and_then(|e| parser.assert_finished().map(|_| e))
		.map_err(|e| e.render_on(input))
		.map_err(Error::InvalidQuery)
}

/// Parses a SurrealQL [`Idiom`]
#[instrument(level = "trace", target = "surrealdb::core::syn", fields(length = input.len()))]
pub fn idiom(input: &str) -> Result<Idiom, Error> {
	trace!(target: TARGET, "Parsing SurrealQL idiom");

	if input.len() > u32::MAX as usize {
		return Err(Error::QueryTooLarge);
	}

	let mut parser = Parser::new(input.as_bytes())
		.with_object_recursion_limit(*MAX_OBJECT_PARSING_DEPTH as usize)
		.with_query_recursion_limit(*MAX_QUERY_PARSING_DEPTH as usize);
	parser.table_as_field = true;
	let mut stack = Stack::new();
	stack
		.enter(|stk| parser.parse_plain_idiom(stk))
		.finish()
		.and_then(|e| parser.assert_finished().map(|_| e))
		.map_err(|e| e.render_on(input))
		.map_err(Error::InvalidQuery)
}

/// Parse a datetime without enclosing delimiters from a string.
#[instrument(level = "trace", target = "surrealdb::core::syn", fields(length = input.len()))]
pub fn datetime(input: &str) -> Result<Datetime, Error> {
	trace!(target: TARGET, "Parsing SurrealQL datetime");

	if input.len() > u32::MAX as usize {
		return Err(Error::QueryTooLarge);
	}

	let mut lexer = Lexer::new(input.as_bytes());
	let res = compound::datetime_inner(&mut lexer);
	if let Err(e) = lexer.assert_finished() {
		return Err(Error::InvalidQuery(e.render_on(input)));
	}
	res.map(Datetime).map_err(|e| e.render_on(input)).map_err(Error::InvalidQuery)
}

/// Parse a duration from a string.
#[instrument(level = "trace", target = "surrealdb::core::syn", fields(length = input.len()))]
pub fn duration(input: &str) -> Result<Duration, Error> {
	trace!(target: TARGET, "Parsing SurrealQL duration");

	if input.len() > u32::MAX as usize {
		return Err(Error::QueryTooLarge);
	}

	let mut parser = Parser::new(input.as_bytes());
	parser
		.next_token_value::<Duration>()
		.and_then(|e| parser.assert_finished().map(|_| e))
		.map_err(|e| e.render_on(input))
		.map_err(Error::InvalidQuery)
}

/// Parse a range.
#[instrument(level = "trace", target = "surrealdb::core::syn", fields(length = input.len()))]
pub fn range(input: &str) -> Result<Range, Error> {
	trace!(target: TARGET, "Parsing SurrealQL range");

	if input.len() > u32::MAX as usize {
		return Err(Error::QueryTooLarge);
	}

	let mut parser = Parser::new(input.as_bytes());
	let mut stack = Stack::new();
	stack
		.enter(|stk| parser.parse_range(stk))
		.finish()
		.and_then(|e| parser.assert_finished().map(|_| e))
		.map_err(|e| e.render_on(input))
		.map_err(Error::InvalidQuery)
}

/// Parse a record id.
#[instrument(level = "trace", target = "surrealdb::core::syn", fields(length = input.len()))]
pub fn thing(input: &str) -> Result<Thing, Error> {
	trace!(target: TARGET, "Parsing SurrealQL thing");

	if input.len() > u32::MAX as usize {
		return Err(Error::QueryTooLarge);
	}

	let mut parser = Parser::new(input.as_bytes())
		.with_object_recursion_limit(*MAX_OBJECT_PARSING_DEPTH as usize)
		.with_query_recursion_limit(*MAX_QUERY_PARSING_DEPTH as usize);
	let mut stack = Stack::new();
	stack
		.enter(|stk| parser.parse_thing(stk))
		.finish()
		.and_then(|e| parser.assert_finished().map(|_| e))
		.map_err(|e| e.render_on(input))
		.map_err(Error::InvalidQuery)
}

/// Parse a record id including ranges.
#[instrument(level = "trace", target = "surrealdb::core::syn", fields(length = input.len()))]
pub fn thing_with_range(input: &str) -> Result<Thing, Error> {
	trace!(target: TARGET, "Parsing SurrealQL thing");

	if input.len() > u32::MAX as usize {
		return Err(Error::QueryTooLarge);
	}

	let mut parser = Parser::new(input.as_bytes())
		.with_object_recursion_limit(*MAX_OBJECT_PARSING_DEPTH as usize)
		.with_query_recursion_limit(*MAX_QUERY_PARSING_DEPTH as usize);
	let mut stack = Stack::new();
	stack
		.enter(|stk| parser.parse_thing_with_range(stk))
		.finish()
		.and_then(|e| parser.assert_finished().map(|_| e))
		.map_err(|e| e.render_on(input))
		.map_err(Error::InvalidQuery)
}

/// Parse a block, expects the value to be wrapped in `{}`.
#[instrument(level = "trace", target = "surrealdb::core::syn", fields(length = input.len()))]
pub fn block(input: &str) -> Result<Block, Error> {
	trace!(target: TARGET, "Parsing SurrealQL block");

	if input.len() > u32::MAX as usize {
		return Err(Error::QueryTooLarge);
	}

	let mut parser = Parser::new(input.as_bytes())
		.with_object_recursion_limit(*MAX_OBJECT_PARSING_DEPTH as usize)
		.with_query_recursion_limit(*MAX_QUERY_PARSING_DEPTH as usize);
	let mut stack = Stack::new();
	let token = parser.peek();
	match token.kind {
		t!("{") => {
			let start = parser.pop_peek().span;
			stack
				.enter(|stk| parser.parse_block(stk, start))
				.finish()
				.and_then(|e| parser.assert_finished().map(|_| e))
				.map_err(|e| e.render_on(input))
				.map_err(Error::InvalidQuery)
		}
		found => Err(Error::InvalidQuery(
			error::SyntaxError::new(format_args!("Unexpected token `{found}` expected `{{`"))
				.with_span(token.span, error::MessageKind::Error)
				.render_on(input),
		)),
	}
}

/// Parses a SurrealQL [`Value`] and parses values within strings.
#[instrument(level = "trace", target = "surrealdb::core::syn", fields(length = input.len()))]
pub fn value_legacy_strand(input: &str) -> Result<Value, Error> {
	trace!(target: TARGET, "Parsing SurrealQL value, with legacy strings");

	if input.len() > u32::MAX as usize {
		return Err(Error::QueryTooLarge);
	}

	let mut parser = Parser::new(input.as_bytes())
		.with_object_recursion_limit(*MAX_OBJECT_PARSING_DEPTH as usize)
		.with_query_recursion_limit(*MAX_QUERY_PARSING_DEPTH as usize);
	let mut stack = Stack::new();
	parser.allow_legacy_strand(true);
	stack
		.enter(|stk| parser.parse_value_field(stk))
		.finish()
		.and_then(|e| parser.assert_finished().map(|_| e))
		.map_err(|e| e.render_on(input))
		.map_err(Error::InvalidQuery)
}

/// Parses JSON into an inert SurrealQL [`Value`] and parses values within strings.
#[instrument(level = "trace", target = "surrealdb::core::syn", fields(length = input.len()))]
pub fn json_legacy_strand(input: &str) -> Result<Value, Error> {
	trace!(target: TARGET, "Parsing inert JSON value, with legacy strings");

	if input.len() > u32::MAX as usize {
		return Err(Error::QueryTooLarge);
	}

	let mut parser = Parser::new(input.as_bytes())
		.with_object_recursion_limit(*MAX_OBJECT_PARSING_DEPTH as usize)
		.with_query_recursion_limit(*MAX_QUERY_PARSING_DEPTH as usize);
	let mut stack = Stack::new();
	parser.allow_legacy_strand(true);
	stack
		.enter(|stk| parser.parse_json(stk))
		.finish()
		.and_then(|e| parser.assert_finished().map(|_| e))
		.map_err(|e| e.render_on(input))
		.map_err(Error::InvalidQuery)
}