solidb 1.2.2

A lightweight, high-performance structured database server written in Rust.
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
//! Operator precedence chain for SDBQL expression parsing.
//!
//! Precedence (lowest to highest):
//! 1. Ternary: `?:`
//! 2. Null coalesce: `??`
//! 3. Logical OR: `||`
//! 4. Pipeline: `|>`
//! 5. Boolean OR: `OR`
//! 6. Boolean AND: `AND`
//! 7. Bitwise OR: `|`
//! 8. Bitwise XOR: `^`
//! 9. Bitwise AND: `&`
//! 10. Comparison: `==`, `!=`, `<`, `<=`, `>`, `>=`, `IN`, `LIKE`, etc.
//! 11. Range: `..`
//! 12. Shift: `<<`, `>>`
//! 13. Additive: `+`, `-`
//! 14. Multiplicative: `*`, `/`, `%`
//! 15. Unary: `!`, `-`, `~`
//! 16. Postfix: `.`, `?.`, `[]`
//! 17. Primary: literals, variables, function calls, etc.

use crate::error::{DbError, DbResult};
use crate::sdbql::ast::{ArrayQuantifier, BinaryOperator, Expression, UnaryOperator};
use crate::sdbql::lexer::Token;
use crate::sdbql::parser::Parser;

impl Parser {
    /// Parse ternary expression: condition ? true_expr : false_expr
    /// Lowest precedence, right-associative
    pub(super) fn parse_ternary_expression(&mut self) -> DbResult<Expression> {
        let condition = self.parse_null_coalesce_expression()?;

        if matches!(self.current_token(), Token::Question) {
            self.advance(); // consume '?'
            let true_expr = self.parse_ternary_expression()?; // right-associative
            self.expect(Token::Colon)?;
            let false_expr = self.parse_ternary_expression()?;
            Ok(Expression::Ternary {
                condition: Box::new(condition),
                true_expr: Box::new(true_expr),
                false_expr: Box::new(false_expr),
            })
        } else {
            Ok(condition)
        }
    }

    /// Parse null coalescing expression: left ?? right
    /// Returns left if left is not null, otherwise evaluates and returns right
    fn parse_null_coalesce_expression(&mut self) -> DbResult<Expression> {
        let mut left = self.parse_logical_or_expression()?;

        while matches!(self.current_token(), Token::NullCoalesce) {
            self.advance(); // consume ??
            let right = self.parse_logical_or_expression()?;
            left = Expression::BinaryOp {
                left: Box::new(left),
                op: BinaryOperator::NullCoalesce,
                right: Box::new(right),
            };
        }

        Ok(left)
    }

    /// Parse logical OR expression: left || right
    fn parse_logical_or_expression(&mut self) -> DbResult<Expression> {
        let mut left = self.parse_pipeline_expression()?;

        while matches!(self.current_token(), Token::DoublePipe) {
            self.advance(); // consume ||
            let right = self.parse_pipeline_expression()?;
            left = Expression::BinaryOp {
                left: Box::new(left),
                op: BinaryOperator::LogicalOr,
                right: Box::new(right),
            };
        }

        Ok(left)
    }

    /// Parse pipeline expression: expr |> FUNC(args) |> FUNC2(args)
    fn parse_pipeline_expression(&mut self) -> DbResult<Expression> {
        let mut left = self.parse_or_expression()?;

        while matches!(self.current_token(), Token::PipeRight) {
            self.advance(); // consume |>

            let func_name = self.parse_pipeline_function_name()?;
            self.expect(Token::LeftParen)?;
            let args = self.parse_function_call_args()?;

            let right = Expression::FunctionCall {
                name: func_name,
                args,
            };

            left = Expression::Pipeline {
                left: Box::new(left),
                right: Box::new(right),
            };
        }

        Ok(left)
    }

    /// Parse function name in pipeline context - allows keywords that double as functions
    fn parse_pipeline_function_name(&mut self) -> DbResult<String> {
        let name = match self.current_token() {
            Token::Identifier(name) => name.clone(),
            Token::Filter => "FILTER".to_string(),
            Token::Sort => "SORT".to_string(),
            Token::Count => "COUNT".to_string(),
            Token::Any => "ANY".to_string(),
            Token::Return => "RETURN".to_string(),
            Token::In => "IN".to_string(),
            Token::Replace => "REPLACE".to_string(),
            Token::Like => "LIKE".to_string(),
            Token::Left => "LEFT".to_string(),
            Token::Right => "RIGHT".to_string(),
            Token::Join => "JOIN".to_string(),
            _ => {
                return Err(DbError::ParseError(format!(
                    "Expected function name after |>, got {:?}",
                    self.current_token()
                )));
            }
        };
        self.advance();
        Ok(name.to_uppercase())
    }

    /// Parse boolean OR expression
    pub(super) fn parse_or_expression(&mut self) -> DbResult<Expression> {
        let mut left = self.parse_and_expression()?;

        while matches!(self.current_token(), Token::Or) {
            self.advance();
            let right = self.parse_and_expression()?;
            left = Expression::BinaryOp {
                left: Box::new(left),
                op: BinaryOperator::Or,
                right: Box::new(right),
            };
        }

        Ok(left)
    }

    /// Parse boolean AND expression
    fn parse_and_expression(&mut self) -> DbResult<Expression> {
        let mut left = self.parse_bitwise_or_expression()?;

        while matches!(self.current_token(), Token::And) {
            self.advance();
            let right = self.parse_bitwise_or_expression()?;
            left = Expression::BinaryOp {
                left: Box::new(left),
                op: BinaryOperator::And,
                right: Box::new(right),
            };
        }

        Ok(left)
    }

    /// Parse bitwise OR expression
    fn parse_bitwise_or_expression(&mut self) -> DbResult<Expression> {
        let mut left = self.parse_bitwise_xor_expression()?;

        while matches!(self.current_token(), Token::Pipe) {
            self.advance();
            let right = self.parse_bitwise_xor_expression()?;
            left = Expression::BinaryOp {
                left: Box::new(left),
                op: BinaryOperator::BitwiseOr,
                right: Box::new(right),
            };
        }

        Ok(left)
    }

    /// Parse bitwise XOR expression
    fn parse_bitwise_xor_expression(&mut self) -> DbResult<Expression> {
        let mut left = self.parse_bitwise_and_expression()?;

        while matches!(self.current_token(), Token::Caret) {
            self.advance();
            let right = self.parse_bitwise_and_expression()?;
            left = Expression::BinaryOp {
                left: Box::new(left),
                op: BinaryOperator::BitwiseXor,
                right: Box::new(right),
            };
        }

        Ok(left)
    }

    /// Parse bitwise AND expression
    fn parse_bitwise_and_expression(&mut self) -> DbResult<Expression> {
        let mut left = self.parse_comparison_expression()?;

        while matches!(self.current_token(), Token::Ampersand) {
            self.advance();
            let right = self.parse_comparison_expression()?;
            left = Expression::BinaryOp {
                left: Box::new(left),
                op: BinaryOperator::BitwiseAnd,
                right: Box::new(right),
            };
        }

        Ok(left)
    }

    /// Parse comparison expression
    pub(super) fn parse_comparison_expression(&mut self) -> DbResult<Expression> {
        let mut left = self.parse_range_expression()?;

        loop {
            // Array comparison: `arr ANY == x`, `arr ALL IN y`, `arr NONE > 1`,
            // `arr AT LEAST (n) == x`.
            if let Some(quantifier) = self.parse_array_quantifier()? {
                let Some(op) = self.parse_comparison_operator()? else {
                    return Err(DbError::ParseError(format!(
                        "Expected a comparison operator after the array quantifier, found {:?}",
                        self.current_token()
                    )));
                };
                if !matches!(
                    op,
                    BinaryOperator::Equal
                        | BinaryOperator::NotEqual
                        | BinaryOperator::LessThan
                        | BinaryOperator::LessThanOrEqual
                        | BinaryOperator::GreaterThan
                        | BinaryOperator::GreaterThanOrEqual
                        | BinaryOperator::In
                        | BinaryOperator::NotIn
                ) {
                    return Err(DbError::ParseError(format!(
                        "Array comparison supports ==, !=, <, <=, >, >=, IN and NOT IN, not {:?}",
                        op
                    )));
                }
                let right = self.parse_range_expression()?;
                left = Expression::ArrayComparison {
                    quantifier,
                    left: Box::new(left),
                    op,
                    right: Box::new(right),
                };
                continue;
            }

            let Some(op) = self.parse_comparison_operator()? else {
                break;
            };
            let right = self.parse_range_expression()?;
            left = Expression::BinaryOp {
                left: Box::new(left),
                op,
                right: Box::new(right),
            };
        }

        Ok(left)
    }

    /// Consume an array-comparison quantifier (`ANY`, `ALL`, `NONE`,
    /// `AT LEAST (n)`) if one follows an operand.
    ///
    /// `ANY` / `ALL` / `NONE` only count when a comparison operator comes
    /// next, so a traversal's `... TO @end ANY edges` and other uses of those
    /// words are left alone. `AT LEAST` is unambiguous on its own.
    fn parse_array_quantifier(&mut self) -> DbResult<Option<ArrayQuantifier>> {
        let simple = match self.current_token() {
            Token::Any => Some(ArrayQuantifier::Any),
            Token::Identifier(n) if n.eq_ignore_ascii_case("ALL") => Some(ArrayQuantifier::All),
            Token::Identifier(n) if n.eq_ignore_ascii_case("NONE") => Some(ArrayQuantifier::None),
            _ => None,
        };
        if let Some(quantifier) = simple {
            if self.comparison_operator_at(1) {
                self.advance();
                return Ok(Some(quantifier));
            }
            return Ok(None);
        }

        if self.ident_eq("AT")
            && matches!(self.peek_token(1), Token::Identifier(n) if n.eq_ignore_ascii_case("LEAST"))
        {
            self.advance(); // AT
            self.advance(); // LEAST
            self.expect(Token::LeftParen)?;
            let count = self.with_in_allowed(|p| p.parse_expression())?;
            self.expect(Token::RightParen)?;
            return Ok(Some(ArrayQuantifier::AtLeast(Box::new(count))));
        }
        Ok(None)
    }

    /// True when the token at `offset` starts a comparison operator that
    /// `parse_comparison_operator` would accept here.
    fn comparison_operator_at(&self, offset: usize) -> bool {
        match self.peek_token(offset) {
            Token::Equal
            | Token::NotEqual
            | Token::LessThan
            | Token::LessThanEq
            | Token::GreaterThan
            | Token::GreaterThanEq => true,
            Token::In => self.allow_in_operator,
            Token::Not => {
                self.allow_in_operator && matches!(self.peek_token(offset + 1), Token::In)
            }
            _ => false,
        }
    }

    /// Parse range expressions (e.g., 1..5 produces [1, 2, 3, 4, 5])
    pub(super) fn parse_range_expression(&mut self) -> DbResult<Expression> {
        let left = self.parse_shift_expression()?;

        if matches!(self.current_token(), Token::DotDot) {
            self.advance(); // consume '..'
            let right = self.parse_shift_expression()?;
            Ok(Expression::Range(Box::new(left), Box::new(right)))
        } else {
            Ok(left)
        }
    }

    /// Parse shift expression
    fn parse_shift_expression(&mut self) -> DbResult<Expression> {
        let mut left = self.parse_additive_expression()?;

        while matches!(self.current_token(), Token::LeftShift | Token::RightShift) {
            let op = match self.current_token() {
                Token::LeftShift => BinaryOperator::LeftShift,
                Token::RightShift => BinaryOperator::RightShift,
                _ => unreachable!(),
            };
            self.advance();
            let right = self.parse_additive_expression()?;
            left = Expression::BinaryOp {
                left: Box::new(left),
                op,
                right: Box::new(right),
            };
        }

        Ok(left)
    }

    /// Parse additive expression (+, -)
    fn parse_additive_expression(&mut self) -> DbResult<Expression> {
        let mut left = self.parse_multiplicative_expression()?;

        while matches!(self.current_token(), Token::Plus | Token::Minus) {
            let op = match self.current_token() {
                Token::Plus => BinaryOperator::Add,
                Token::Minus => BinaryOperator::Subtract,
                _ => unreachable!(),
            };
            self.advance();
            let right = self.parse_multiplicative_expression()?;
            left = Expression::BinaryOp {
                left: Box::new(left),
                op,
                right: Box::new(right),
            };
        }

        Ok(left)
    }

    /// Parse multiplicative expression (*, /, %)
    fn parse_multiplicative_expression(&mut self) -> DbResult<Expression> {
        let mut left = self.parse_unary_expression()?;

        while matches!(
            self.current_token(),
            Token::Star | Token::Slash | Token::Percent
        ) {
            let op = match self.current_token() {
                Token::Star => BinaryOperator::Multiply,
                Token::Slash => BinaryOperator::Divide,
                Token::Percent => BinaryOperator::Modulus,
                _ => unreachable!(),
            };
            self.advance();
            let right = self.parse_unary_expression()?;
            left = Expression::BinaryOp {
                left: Box::new(left),
                op,
                right: Box::new(right),
            };
        }

        Ok(left)
    }

    /// Parse unary expression (!, -, ~)
    ///
    /// Chained unary operators recurse *here*, without going back through
    /// `parse_expression`, so this is the one recursive production the
    /// SEC-130 depth guard did not reach: `RETURN` followed by 200k `-`
    /// characters (200 KB, well under the body limit) recursed 200k frames
    /// and overflowed the stack. A Rust stack overflow is not catchable — it
    /// aborts the whole process, taking every other connection with it. The
    /// depth accounting below puts these frames on the same budget as every
    /// other nesting construct.
    pub(super) fn parse_unary_expression(&mut self) -> DbResult<Expression> {
        let op = match self.current_token() {
            Token::Not => UnaryOperator::Not,
            Token::Minus => UnaryOperator::Negate,
            Token::Tilde => UnaryOperator::BitwiseNot,
            _ => return self.parse_postfix_expression(),
        };

        self.advance();
        self.check_depth()?;
        let operand = self.parse_unary_expression();
        // Decrement before propagating: an early `?` here would leak depth
        // and make later sibling expressions in the same query fail.
        self.leave_depth();

        Ok(Expression::UnaryOp {
            op,
            operand: Box::new(operand?),
        })
    }
}