surrealdb-core 3.2.0

A scalable, distributed, collaborative, document-graph database, for the realtime web
Documentation
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
//! This module defines the pratt parser for operators.

use reblessive::Stk;
use surrealdb_types::ToSql;

use super::enter_query_recursion;
use super::mac::unexpected;
use crate::sql::operator::{BindingPower, BooleanOperator, MatchesOperator, NearestNeighbor};
use crate::sql::{BinaryOperator, Expr, Literal, Part, PostfixOperator, PrefixOperator};
use crate::syn::error::bail;
use crate::syn::lexer::compound::Numeric;
use crate::syn::parser::mac::expected;
use crate::syn::parser::{ParseResult, Parser};
use crate::syn::token::{Span, Token, TokenKind, t};
use crate::types::PublicDuration;

impl Parser<'_> {
	/// Parse a generic expression without triggering the query depth and
	/// setting table_as_field.
	///
	/// Meant to be used when parsing an expression the first time to avoid
	/// having the depth limit be lowered unnecessarily.
	pub(crate) async fn parse_expr_start(&mut self, stk: &mut Stk) -> ParseResult<Expr> {
		self.table_as_field = true;
		self.pratt_parse_expr(stk, BindingPower::Base).await
	}

	/// Parsers a generic value.
	///
	/// A generic loose ident like `foo` in for example `foo.bar` can be two
	/// different values depending on context: a table or a field the current
	/// document. This function parses loose idents as a table, see
	/// [`parse_expr_field`] for parsing loose idents as fields
	pub(crate) async fn parse_expr_table(&mut self, stk: &mut Stk) -> ParseResult<Expr> {
		let old = self.table_as_field;
		self.table_as_field = false;
		let res = enter_query_recursion!(this = self => {
			this.pratt_parse_expr(stk, BindingPower::Base).await
		});
		self.table_as_field = old;
		res
	}

	/// Parsers a generic value.
	///
	/// A generic loose ident like `foo` in for example `foo.bar` can be two
	/// different values depending on context: a table or a field the current
	/// document. This function parses loose idents as a field, see
	/// [`parse_expr_table`] for parsing loose idents as table
	pub(crate) async fn parse_expr_field(&mut self, stk: &mut Stk) -> ParseResult<Expr> {
		let old = self.table_as_field;
		self.table_as_field = true;
		let res = enter_query_recursion!(this = self => {
			this.pratt_parse_expr(stk, BindingPower::Base).await
		});
		self.table_as_field = old;
		res
	}

	/// Parsers a generic value.
	///
	/// Inherits how loose identifiers are parsed from it's caller.
	pub(super) async fn parse_expr_inherit(&mut self, stk: &mut Stk) -> ParseResult<Expr> {
		enter_query_recursion!(this = self => {
			this.pratt_parse_expr(stk, BindingPower::Base).await
		})
	}

	/// Returns the binding power of an infix operator.
	///
	/// Binding power is the opposite of precedence: a higher binding power
	/// means that a token is more like to operate directly on it's neighbours.
	/// Example `*` has a higher binding power than `-` resulting in 1 - 2 * 3
	/// being parsed as 1 - (2 * 3).
	///
	/// All operators in SurrealQL which are parsed by the functions in this
	/// module are left associative or have no defined associativity.
	fn infix_binding_power(&mut self, token: TokenKind) -> Option<BindingPower> {
		match token {
			// assigment operators have the lowest binding power.
			//t!("+=") | t!("-=") | t!("+?=") => Some((2, 1)),
			t!("||") | t!("OR") => Some(BindingPower::Or),
			t!("&&") | t!("AND") => Some(BindingPower::And),

			// Equality operators have same binding power.
			t!("=") | t!("IS") | t!("==") | t!("!=") | t!("*=") | t!("?=") | t!("@") => {
				Some(BindingPower::Equality)
			}

			t!("<") => {
				if let Some(peek) = self.peek_whitespace1()
					&& let t!("-") | t!("~") | t!("->") | t!("..") = peek.kind
				{
					return None;
				}
				Some(BindingPower::Relation)
			}

			t!(">") => {
				if let Some(t!("..")) = self.peek_whitespace1().map(|x| x.kind) {
					return Some(BindingPower::Range);
				}
				Some(BindingPower::Relation)
			}

			t!("..") => Some(BindingPower::Range),

			t!("<=")
			| t!(">=")
			| t!("∋")
			| t!("CONTAINS")
			| t!("∌")
			| t!("CONTAINSNOT")
			| t!("∈")
			| t!("INSIDE")
			| t!("∉")
			| t!("NOTINSIDE")
			| t!("⊇")
			| t!("CONTAINSALL")
			| t!("⊃")
			| t!("CONTAINSANY")
			| t!("⊅")
			| t!("CONTAINSNONE")
			| t!("⊆")
			| t!("ALLINSIDE")
			| t!("⊂")
			| t!("ANYINSIDE")
			| t!("⊄")
			| t!("NONEINSIDE")
			| t!("OUTSIDE")
			| t!("INTERSECTS")
			| t!("NOT")
			| t!("IN")
			| t!("<|") => Some(BindingPower::Relation),

			t!("+") | t!("-") => Some(BindingPower::AddSub),
			t!("*") | t!("×") | t!("/") | t!("÷") | t!("%") => Some(BindingPower::MulDiv),
			t!("**") => Some(BindingPower::Power),
			t!("?:") | t!("?") => Some(BindingPower::Nullish),
			_ => None,
		}
	}

	fn prefix_binding_power(&mut self, token: TokenKind) -> Option<BindingPower> {
		match token {
			t!("!") | t!("+") | t!("-") => Some(BindingPower::Prefix),
			t!("..") => Some(BindingPower::Range),
			t!("<") => {
				if let Some(peek) = self.peek_whitespace1() {
					if peek.kind == t!("-") {
						let recover = self.last_span();
						if self.peek2().kind == TokenKind::Digits {
							self.backup_after(recover);
							return Some(BindingPower::Prefix);
						}
						return None;
					}
					if let t!("~") | t!("->") = peek.kind {
						return None;
					}
				}
				Some(BindingPower::Prefix)
			}
			_ => None,
		}
	}

	fn postfix_binding_power(&mut self, token: TokenKind) -> Option<BindingPower> {
		match token {
			t!(">") => {
				if let Some(peek) = self.peek_whitespace1()
					&& let t!("..") = peek.kind
				{
					if let Some(peek) = self.peek_whitespace2()
						&& (t!("=") == peek.kind || Self::kind_starts_expression(peek.kind))
					{
						return None;
					} else {
						return Some(BindingPower::Range);
					}
				}
				None
			}
			t!("..") => match self.peek_whitespace1().map(|x| x.kind) {
				Some(t!("=")) => None,
				Some(x) if Self::kind_starts_expression(x) => None,
				_ => Some(BindingPower::Range),
			},
			t!("(") => Some(BindingPower::Call),
			_ => None,
		}
	}

	async fn parse_prefix_op(&mut self, stk: &mut Stk, min_bp: BindingPower) -> ParseResult<Expr> {
		let token = self.peek();
		let operator = match token.kind {
			t!("+") => {
				// +123 is a single number token, so parse it as such
				if let Some(TokenKind::Digits) = self.peek_whitespace1().map(|x| x.kind) {
					// This is a bit of an annoying special case.
					// The problem is that `+` and `-` can be an prefix operator and a the start
					// of a number token.
					// To figure out which it is we need to peek the next whitespace token,
					// This eats the digits that the lexer needs to lex the number. So we we need
					// to backup before the digits token was consumed, clear the digits token from
					// the token buffer so it isn't popped after parsing the number and then lex the
					// number.
					self.pop_peek();
					let expr = match self.next_token_value::<Numeric>()? {
						Numeric::Float(f) => Expr::Literal(Literal::Float(f)),
						Numeric::Integer(i) => {
							Expr::Literal(Literal::Integer(i.into_int(self.recent_span())?))
						}
						Numeric::Decimal(d) => Expr::Literal(Literal::Decimal(d)),
						Numeric::Duration(d) => Expr::Prefix {
							op: PrefixOperator::Positive,
							expr: Box::new(Expr::Literal(Literal::Duration(PublicDuration::from(
								d,
							)))),
						},
					};
					if self.peek_continues_idiom() {
						return self
							.parse_remaining_value_idiom(stk, vec![Part::Start(expr)])
							.await;
					} else {
						return Ok(expr);
					}
				}
				self.pop_peek();

				PrefixOperator::Positive
			}
			t!("-") => {
				// -123 is a single number token, so parse it as such
				if let Some(TokenKind::Digits) = self.peek_whitespace1().map(|x| x.kind) {
					// This is a bit of an annoying special case.
					// The problem is that `+` and `-` can be an prefix operator and a the start
					// of a number token.
					// To figure out which it is we need to peek the next whitespace token,
					// This eats the digits that the lexer needs to lex the number. So we we need
					// to backup before the digits token was consumed, clear the digits token from
					// the token buffer so it isn't popped after parsing the number and then lex the
					// number.
					self.pop_peek();
					let expr = match self.next_token_value::<Numeric>()? {
						Numeric::Float(f) => Expr::Literal(Literal::Float(-f)),
						Numeric::Integer(i) => {
							Expr::Literal(Literal::Integer(i.into_neg_int(self.recent_span())?))
						}
						Numeric::Decimal(d) => Expr::Literal(Literal::Decimal(-d)),
						Numeric::Duration(d) => Expr::Prefix {
							op: PrefixOperator::Negate,
							expr: Box::new(Expr::Literal(Literal::Duration(PublicDuration::from(
								d,
							)))),
						},
					};
					if self.peek_continues_idiom() {
						return self
							.parse_remaining_value_idiom(stk, vec![Part::Start(expr)])
							.await;
					} else {
						return Ok(expr);
					}
				}

				self.pop_peek();

				PrefixOperator::Negate
			}
			t!("!") => {
				self.pop_peek();
				PrefixOperator::Not
			}
			t!("<") => {
				self.pop_peek();
				let kind = self.parse_kind(stk, token.span).await?;
				PrefixOperator::Cast(kind)
			}
			t!("..") => {
				self.pop_peek();
				if let Some(x) = self.peek_whitespace() {
					if let t!("=") = x.kind {
						self.pop_peek();
						PrefixOperator::RangeInclusive
					} else if !Self::kind_starts_prime_value(x.kind) {
						// unbounded range.
						return Ok(Expr::Literal(Literal::UnboundedRange));
					} else {
						PrefixOperator::Range
					}
				} else {
					return Ok(Expr::Literal(Literal::UnboundedRange));
				}
			}
			// should be unreachable as we previously check if the token was a prefix op.
			_ => unreachable!(),
		};

		let v = stk.run(|stk| self.pratt_parse_expr(stk, min_bp)).await?;

		Ok(Expr::Prefix {
			op: operator,
			expr: Box::new(v),
		})
	}

	pub(super) fn parse_nearest_neighbor(&mut self, token: Token) -> ParseResult<NearestNeighbor> {
		let amount = self.next_token_value()?;
		let res = if self.eat(t!(",")) {
			let token = self.peek();
			match token.kind {
				TokenKind::Distance(_) => {
					let d = self.parse_distance()?;
					NearestNeighbor::K(amount, d)
				}
				TokenKind::Digits => {
					let ef = self.next_token_value()?;
					NearestNeighbor::Approximate(amount, ef)
				}
				_ => {
					bail!("Unexpected token {} expected a distance of an integer", token.kind,
						@token.span => "The NN operator accepts either a distance or an EF value (integer)")
				}
			}
		} else {
			NearestNeighbor::KTree(amount)
		};
		if !self.eat(t!("|")) || !self.eat_whitespace(t!(">")) {
			bail!("Unexpected token `{}` expected delimiter `|>`",
				self.peek().kind,
				@self.recent_span(),
				@token.span=> "expected this delimiter to close"
			);
		}

		Ok(res)
	}

	/// Returns if an operator has a defined associativity.
	/// For example: `a - b - c == (a - b) - c` so `-` is left associative.
	/// However `a == b == c` is not defined to be either `(a == b) == c` nor `a == (b == c)`.
	fn operator_has_associativity(operator: &BinaryOperator) -> bool {
		!matches!(
			operator,
			BinaryOperator::Equal
				| BinaryOperator::NotEqual
				| BinaryOperator::AllEqual
				| BinaryOperator::AnyEqual
				| BinaryOperator::LessThan
				| BinaryOperator::LessThanEqual
				| BinaryOperator::MoreThan
				| BinaryOperator::MoreThanEqual
				| BinaryOperator::Matches(_)
				| BinaryOperator::Contain
				| BinaryOperator::NotContain
				| BinaryOperator::ContainAll
				| BinaryOperator::ContainAny
				| BinaryOperator::ContainNone
				| BinaryOperator::Inside
				| BinaryOperator::NotInside
				| BinaryOperator::AllInside
				| BinaryOperator::AnyInside
				| BinaryOperator::NoneInside
				| BinaryOperator::Outside
				| BinaryOperator::Intersects
				| BinaryOperator::NearestNeighbor(_)
		)
	}

	fn expr_is_range(expr: &Expr) -> bool {
		match expr {
			Expr::Binary {
				op,
				..
			} => matches!(
				op,
				BinaryOperator::Range
					| BinaryOperator::RangeSkipInclusive
					| BinaryOperator::RangeSkip
					| BinaryOperator::RangeInclusive
			),
			Expr::Prefix {
				op,
				..
			} => matches!(op, PrefixOperator::Range | PrefixOperator::RangeInclusive),
			Expr::Postfix {
				op,
				..
			} => matches!(op, PostfixOperator::Range | PostfixOperator::RangeSkip),
			_ => false,
		}
	}

	async fn parse_infix_op(
		&mut self,
		stk: &mut Stk,
		min_bp: BindingPower,
		lhs: Expr,
		lhs_prime: bool, /* if lhs was a prime expression, required for ensuring (a..b)..c does
		                  * not fail. */
	) -> ParseResult<Expr> {
		let token = self.next();
		let operator = match token.kind {
			t!("||") | t!("OR") => BinaryOperator::Or,
			t!("&&") | t!("AND") => BinaryOperator::And,
			t!("?:") => BinaryOperator::TenaryCondition,
			t!("?") => {
				if !self.eat_whitespace(t!("?")) {
					unexpected!(self, token, "`??`")
				}
				BinaryOperator::NullCoalescing
			}
			t!("==") => BinaryOperator::ExactEqual,
			t!("!=") => BinaryOperator::NotEqual,
			t!("*=") => BinaryOperator::AllEqual,
			t!("?=") => BinaryOperator::AnyEqual,
			t!("=") => BinaryOperator::Equal,
			t!("@") => {
				let op = self.parse_matches()?;
				BinaryOperator::Matches(op)
			}
			t!("<=") => BinaryOperator::LessThanEqual,
			t!("<") => BinaryOperator::LessThan,
			t!(">=") => BinaryOperator::MoreThanEqual,
			t!("**") => BinaryOperator::Power,
			t!("+") => BinaryOperator::Add,
			t!("-") => BinaryOperator::Subtract,
			t!("*") | t!("×") => BinaryOperator::Multiply,
			t!("/") | t!("÷") => BinaryOperator::Divide,
			t!("%") => BinaryOperator::Remainder,
			t!("∋") | t!("CONTAINS") => BinaryOperator::Contain,
			t!("∌") | t!("CONTAINSNOT") => BinaryOperator::NotContain,
			t!("∈") | t!("INSIDE") => BinaryOperator::Inside,
			t!("∉") | t!("NOTINSIDE") => BinaryOperator::NotInside,
			t!("⊇") | t!("CONTAINSALL") => BinaryOperator::ContainAll,
			t!("⊃") | t!("CONTAINSANY") => BinaryOperator::ContainAny,
			t!("⊅") | t!("CONTAINSNONE") => BinaryOperator::ContainNone,
			t!("⊆") | t!("ALLINSIDE") => BinaryOperator::AllInside,
			t!("⊂") | t!("ANYINSIDE") => BinaryOperator::AnyInside,
			t!("⊄") | t!("NONEINSIDE") => BinaryOperator::NoneInside,
			t!("IS") => {
				if self.eat(t!("NOT")) {
					BinaryOperator::NotEqual
				} else {
					BinaryOperator::Equal
				}
			}
			t!("OUTSIDE") => BinaryOperator::Outside,
			t!("INTERSECTS") => BinaryOperator::Intersects,
			t!("NOT") => {
				expected!(self, t!("IN"));
				BinaryOperator::NotInside
			}
			t!("IN") => BinaryOperator::Inside,
			t!("<|") => {
				BinaryOperator::NearestNeighbor(Box::new(self.parse_nearest_neighbor(token)?))
			}

			t!(">") => {
				if let Some(t!("..")) = self.peek_whitespace().map(|x| x.kind) {
					self.pop_peek();
					if let Some(t!("=")) = self.peek_whitespace().map(|x| x.kind) {
						self.pop_peek();
						BinaryOperator::RangeSkipInclusive
					} else {
						BinaryOperator::RangeSkip
					}
				} else {
					BinaryOperator::MoreThan
				}
			}
			t!("..") => {
				if let Some(t!("=")) = self.peek_whitespace().map(|x| x.kind) {
					self.pop_peek();
					BinaryOperator::RangeInclusive
				} else {
					BinaryOperator::Range
				}
			}

			// should be unreachable as we previously check if the token was a prefix op.
			x => unreachable!("found non-operator token {x:?}"),
		};
		let rhs_covered = self.peek().kind == t!("(");
		let rhs = stk.run(|ctx| self.pratt_parse_expr(ctx, min_bp)).await?;

		let has_associatitivity = Self::operator_has_associativity(&operator);
		if !lhs_prime
			&& !has_associatitivity
			&& BindingPower::for_expr(&lhs) == BindingPower::for_binary_operator(&operator)
		{
			let span = token.span.covers(self.recent_span());
			if matches!(
				operator,
				BinaryOperator::Range
					| BinaryOperator::RangeSkipInclusive
					| BinaryOperator::RangeSkip
					| BinaryOperator::RangeInclusive
			) {
				bail!("Chained range operators has no specified associativity",
				@span => "use parens, '()', to specify which operator must be evaluated first")
			} else {
				bail!("Chained relational operators have no defined associativity.",
				@span => "Use parens, '()', to specify which operator must be evaluated first")
			}
		}

		if !rhs_covered
			&& !has_associatitivity
			&& BindingPower::for_expr(&rhs) == BindingPower::for_binary_operator(&operator)
		{
			let span = token.span.covers(self.recent_span());
			if matches!(
				operator,
				BinaryOperator::Range
					| BinaryOperator::RangeSkipInclusive
					| BinaryOperator::RangeSkip
					| BinaryOperator::RangeInclusive
			) {
				bail!("Chained range operators have no defined associativity.",
				@span => "Use parens, '()', to specify which operator must be evaluated first")
			} else {
				bail!("Chained relational operators have no defined associativity.",
				@span => "Use parens, '()', to specify which operator must be evaluated first")
			}
		}

		Ok(Expr::Binary {
			left: Box::new(lhs),
			op: operator,
			right: Box::new(rhs),
		})
	}

	fn parse_matches(&mut self) -> ParseResult<MatchesOperator> {
		let peek = self.peek();
		match peek.kind {
			TokenKind::Digits => {
				let number = self.next_token_value()?;
				let op = if self.eat(t!(",")) {
					let peek = self.next();
					let op = match peek.kind {
						t!("AND") => BooleanOperator::And,
						t!("OR") => BooleanOperator::Or,
						_ => unexpected!(self, peek, "either `AND` or `OR`"),
					};
					Some(op)
				} else {
					None
				};
				expected!(self, t!("@"));
				Ok(MatchesOperator {
					operator: op,
					rf: Some(number),
				})
			}
			t!("AND") => {
				self.pop_peek();
				expected!(self, t!("@"));
				Ok(MatchesOperator {
					operator: Some(BooleanOperator::And),
					rf: None,
				})
			}
			t!("OR") => {
				self.pop_peek();
				expected!(self, t!("@"));
				Ok(MatchesOperator {
					operator: Some(BooleanOperator::Or),
					rf: None,
				})
			}
			t!("@") => {
				self.pop_peek();
				Ok(MatchesOperator {
					operator: None,
					rf: None,
				})
			}
			_ => unexpected!(self, peek, "a match reference, operator or `@`"),
		}
	}

	async fn parse_postfix(
		&mut self,
		stk: &mut Stk,
		lhs: Expr,
		lhs_prime: bool,
	) -> ParseResult<Expr> {
		let token = self.next();
		let op = match token.kind {
			t!(">") => {
				assert!(self.eat_whitespace(t!("..")));
				if !lhs_prime && Self::expr_is_range(&lhs) {
					bail!("Chaining range operators has no specified associativity",
						@token.span => "use parens, '()', to specify which operator must be evaluated first")
				}
				PostfixOperator::RangeSkip
			}
			t!("..") => {
				if !lhs_prime && Self::expr_is_range(&lhs) {
					bail!("Chaining range operators has no specified associativity",
						@token.span => "use parens, '()', to specify which operator must be evaluated first")
				}
				PostfixOperator::Range
			}
			t!("(") => {
				let mut args = Vec::new();
				loop {
					if self.eat(t!(")")) {
						break;
					}

					let arg = stk.run(|ctx| self.parse_expr_inherit(ctx)).await?;
					args.push(arg);

					if !self.eat(t!(",")) {
						self.expect_closing_delimiter(t!(")"), token.span)?;
						break;
					}
				}
				PostfixOperator::Call(args)
			}
			t!(".") => {
				let name = self.parse_ident()?;
				expected!(self, t!("("));

				let mut args = Vec::new();
				loop {
					if self.eat(t!(")")) {
						break;
					}

					let arg = stk.run(|ctx| self.parse_expr_inherit(ctx)).await?;
					args.push(arg);

					if !self.eat(t!(",")) {
						self.expect_closing_delimiter(t!(")"), token.span)?;
						break;
					}
				}
				PostfixOperator::MethodCall(name.into_string(), args)
			}
			// should be unreachable as we previously check if the token was a postfix op.
			x => unreachable!("found non-operator token {x:?}"),
		};

		Ok(Expr::Postfix {
			expr: Box::new(lhs),
			op,
		})
	}

	/// Account for one additional level of expression-operator nesting against
	/// the dedicated depth budget, returning a syntax error when it is
	/// exhausted.
	///
	/// This guards the otherwise-unbounded depth of the `Expr` tree built by
	/// [`Self::pratt_parse_expr`]; see `ParserSettings::expr_recursion_limit`
	/// for why an unbounded operator chain is a denial-of-service vector.
	fn enter_expr_depth(&mut self) -> ParseResult<()> {
		if self.settings.expr_recursion_limit == 0 {
			bail!("Exceeded expression recursion depth limit",
				@self.last_span() => "this expression nests or chains operators too deeply");
		}
		self.settings.expr_recursion_limit -= 1;
		Ok(())
	}

	/// The pratt parsing loop.
	/// Parses expression according to binding power.
	///
	/// This is a thin wrapper around [`Self::pratt_parse_expr_inner`] that
	/// charges the expression-depth budget for the level introduced by this
	/// call (so prefix chains, which recurse through here, are bounded) and
	/// restores it on return so that sibling expressions are not charged for
	/// one another. The inner loop charges the budget again for every operator
	/// it appends to the left-associative spine.
	async fn pratt_parse_expr(&mut self, stk: &mut Stk, min_bp: BindingPower) -> ParseResult<Expr> {
		let restore_to = self.settings.expr_recursion_limit;
		self.enter_expr_depth()?;
		let res = self.pratt_parse_expr_inner(stk, min_bp).await;
		// Restore everything this call consumed, including the levels charged by
		// the spine loop. Nested `pratt_parse_expr` calls restore themselves, so
		// `restore_to` is exactly this call's entry value.
		self.settings.expr_recursion_limit = restore_to;
		res
	}

	async fn pratt_parse_expr_inner(
		&mut self,
		stk: &mut Stk,
		min_bp: BindingPower,
	) -> ParseResult<Expr> {
		let peek = self.peek();
		let (mut lhs, mut lhs_prime) = if let Some(bp) = self.prefix_binding_power(peek.kind) {
			(self.parse_prefix_op(stk, bp).await?, false)
		} else {
			(self.parse_prime_expr(stk).await?, true)
		};

		loop {
			let token = self.peek();

			if let Some(bp) = self.postfix_binding_power(token.kind) {
				if bp <= min_bp {
					break;
				}

				// Appending a postfix operator deepens the spine by one level.
				self.enter_expr_depth()?;
				lhs = self.parse_postfix(stk, lhs, lhs_prime).await?;
				lhs_prime = false;
				continue;
			}

			// explain that assignment operators can't be used in normal expressions.
			if let t!("+=") | t!("-=") | t!("+?=") = token.kind {
				unexpected!(self,token,"an operator",
					=> "assignment operators are only allowed in SET and DUPLICATE KEY UPDATE clauses")
			}

			let Some(bp) = self.infix_binding_power(token.kind) else {
				break;
			};

			if bp <= min_bp {
				break;
			}

			// Appending an infix operator deepens the spine by one level.
			self.enter_expr_depth()?;
			lhs = self.parse_infix_op(stk, bp, lhs, lhs_prime).await?;
			lhs_prime = false;
		}

		Ok(lhs)
	}

	pub(crate) fn reject_letless_let(expr: &Expr, span: Span) -> ParseResult<()> {
		let Expr::Binary {
			left,
			op,
			..
		} = expr
		else {
			return Ok(());
		};
		let Expr::Param(p) = &**left else {
			return Ok(());
		};
		let BinaryOperator::Equal = op else {
			return Ok(());
		};
		bail!("Parameter declarations without `let` are deprecated.",
			@span => "Replace with `let {} = ...` to keep the previous behavior.", p.to_sql())
	}
}

#[cfg(test)]
mod test {
	use surrealdb_types::ToSql;

	use crate::sql::{BinaryOperator, Expr, Kind, Literal, PrefixOperator};
	use crate::syn;

	#[test]
	fn cast_int() {
		let sql = "<int>1.2345";
		let out = syn::expr(sql).unwrap();
		assert_eq!("<int> 1.2345f", out.to_sql());
		assert_eq!(
			out,
			Expr::Prefix {
				op: PrefixOperator::Cast(Kind::Int),
				expr: Box::new(Expr::Literal(Literal::Float(1.2345)))
			}
		)
	}

	#[test]
	fn cast_string() {
		let sql = "<string>1.2345";
		let out = syn::expr(sql).unwrap();
		assert_eq!("<string> 1.2345f", out.to_sql());
		assert_eq!(
			out,
			Expr::Prefix {
				op: PrefixOperator::Cast(Kind::String),
				expr: Box::new(Expr::Literal(Literal::Float(1.2345)))
			}
		)
	}

	#[test]
	fn expression_statement() {
		let sql = "true AND false";
		let out = syn::expr(sql).unwrap();
		assert_eq!("true AND false", out.to_sql());
	}

	#[test]
	fn expression_left_opened() {
		let sql = "3 * 3 * 3 = 27";
		let out = syn::expr(sql).unwrap();
		assert_eq!("3 * 3 * 3 = 27", out.to_sql());
	}

	#[test]
	fn expression_left_closed() {
		let sql = "(3 * 3 * 3) = 27";
		let out = syn::expr(sql).unwrap();
		assert_eq!("3 * 3 * 3 = 27", out.to_sql());
	}

	#[test]
	fn expression_right_opened() {
		let sql = "27 = 3 * 3 * 3";
		let out = syn::expr(sql).unwrap();
		assert_eq!("27 = 3 * 3 * 3", out.to_sql());
	}

	#[test]
	fn expression_right_closed() {
		let sql = "27 = (3 * 3 * 3)";
		let out = syn::expr(sql).unwrap();
		assert_eq!("27 = 3 * 3 * 3", out.to_sql());
	}

	#[test]
	fn expression_both_opened() {
		let sql = "3 * 3 * 3 = 3 * 3 * 3";
		let out = syn::expr(sql).unwrap();
		assert_eq!("3 * 3 * 3 = 3 * 3 * 3", out.to_sql());
	}

	#[test]
	fn expression_both_closed() {
		let sql = "(3 * 3 * 3) = (3 * 3 * 3)";
		let out = syn::expr(sql).unwrap();
		assert_eq!("3 * 3 * 3 = 3 * 3 * 3", out.to_sql());
	}

	#[test]
	fn expression_closed_required() {
		let sql = "(3 + 3) * 3";
		let out = syn::expr(sql).unwrap();
		assert_eq!("(3 + 3) * 3", out.to_sql());
	}

	#[test]
	fn range_closed_required() {
		let sql = "(1..2)..3";
		let out = syn::expr(sql).unwrap();
		assert_eq!("(1..2)..3", out.to_sql());
	}

	#[test]
	fn expression_unary() {
		let sql = "-a";
		let out = syn::expr(sql).unwrap();
		assert_eq!(sql, out.to_sql());
	}

	#[test]
	fn expression_with_unary() {
		let sql = "-(5) + 5";
		let out = syn::expr(sql).unwrap();
		assert_eq!("-5 + 5", out.to_sql());
	}

	#[test]
	fn expression_left_associative() {
		let sql = "1 - 1 - 1";
		let out = syn::expr(sql).unwrap();
		let one = Expr::Literal(Literal::Integer(1));

		let expected = Expr::Binary {
			left: Box::new(Expr::Binary {
				left: Box::new(one.clone()),
				op: BinaryOperator::Subtract,
				right: Box::new(one.clone()),
			}),
			op: BinaryOperator::Subtract,
			right: Box::new(one),
		};
		assert_eq!(expected, out);
	}
}