neumann_parser 0.4.0

Hand-written recursive descent parser for the Neumann query language
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
//! Neumann Parser - A hand-written recursive descent parser for the Neumann query language.
//!
//! Supports SQL statements, graph commands, vector operations, and unified queries.
//!
//! # Example
//!
//! ```
//! use neumann_parser::{parse, tokenize};
//!
//! // Parse a SQL statement
//! let stmt = parse("SELECT * FROM users WHERE id = 1").unwrap();
//!
//! // Tokenize source text
//! let tokens = tokenize("SELECT id, name FROM users");
//! ```

pub mod ast;
pub mod cypher;
pub mod error;
pub mod expr;
pub mod lexer;
pub mod parser;
pub mod span;
pub mod token;

pub use ast::*;
pub use cypher::*;
pub use error::{Errors, ParseError, ParseErrorKind, ParseResult};
pub use expr::{parse_expr, ExprParser};
pub use lexer::{tokenize, Lexer};
pub use parser::{parse, parse_all, Parser};
pub use span::{get_line, line_col, line_number, BytePos, Span, Spanned};
pub use token::{Token, TokenKind};

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_public_api_parse() {
        let stmt = parse("SELECT * FROM users").unwrap();
        assert!(matches!(stmt.kind, StatementKind::Select(_)));
    }

    #[test]
    fn test_public_api_tokenize() {
        let tokens = tokenize("SELECT * FROM users");
        assert!(!tokens.is_empty());
        assert!(matches!(tokens[0].kind, TokenKind::Select));
    }

    #[test]
    fn test_public_api_parse_expr() {
        let expr = parse_expr("1 + 2 * 3").unwrap();
        assert!(matches!(expr.kind, ExprKind::Binary(_, BinaryOp::Add, _)));
    }

    #[test]
    fn test_public_api_parse_all() {
        let stmts = parse_all("SELECT 1; SELECT 2").unwrap();
        assert_eq!(stmts.len(), 2);
    }

    #[test]
    fn test_error_formatting() {
        let result = parse("SELCT * FROM users");
        assert!(result.is_err());
        let err = result.unwrap_err();
        let formatted = err.format_with_source("SELCT * FROM users");
        assert!(formatted.contains("error"));
    }

    #[test]
    fn test_span_utilities() {
        let source = "SELECT\nFROM";
        assert_eq!(line_number(source, BytePos(7)), 2);
        assert_eq!(line_col(source, BytePos(7)), (2, 1));
        assert_eq!(get_line(source, BytePos(7)), "FROM");
    }

    #[test]
    fn test_graph_statement() {
        let stmt = parse("NODE CREATE person {name: 'Alice'}").unwrap();
        assert!(matches!(stmt.kind, StatementKind::Node(_)));
    }

    #[test]
    fn test_vector_statement() {
        let stmt = parse("SIMILAR 'query' LIMIT 10").unwrap();
        assert!(matches!(stmt.kind, StatementKind::Similar(_)));
    }

    #[test]
    fn test_unified_statement() {
        let stmt = parse("FIND NODE person WHERE age > 18").unwrap();
        assert!(matches!(stmt.kind, StatementKind::Find(_)));
    }

    #[test]
    fn test_entity_create_statement() {
        let stmt = parse("ENTITY CREATE 'user:1' { name: 'Alice' }").unwrap();
        assert!(matches!(stmt.kind, StatementKind::Entity(_)));
    }

    #[test]
    fn test_entity_create_with_embedding() {
        let stmt = parse("ENTITY CREATE 'doc:1' { title: 'Test' } EMBEDDING [1.0, 0.0]").unwrap();
        if let StatementKind::Entity(EntityStmt {
            operation: EntityOp::Create { embedding, .. },
        }) = stmt.kind
        {
            assert!(embedding.is_some());
            assert_eq!(embedding.unwrap().len(), 2);
        } else {
            panic!("expected ENTITY CREATE");
        }
    }

    #[test]
    fn test_entity_connect_statement() {
        let stmt = parse("ENTITY CONNECT 'from' -> 'to' : follows").unwrap();
        if let StatementKind::Entity(EntityStmt {
            operation: EntityOp::Connect { edge_type, .. },
        }) = stmt.kind
        {
            assert_eq!(edge_type.name, "follows");
        } else {
            panic!("expected ENTITY CONNECT");
        }
    }

    #[test]
    fn test_entity_batch_create() {
        let stmt =
            parse("ENTITY BATCH CREATE [{key: 'u1', name: 'Alice'}, {key: 'u2', name: 'Bob'}]")
                .unwrap();
        if let StatementKind::Entity(EntityStmt {
            operation: EntityOp::Batch { entities },
        }) = stmt.kind
        {
            assert_eq!(entities.len(), 2);
            assert!(entities[0].embedding.is_none());
            assert_eq!(entities[0].properties.len(), 1);
        } else {
            panic!("expected ENTITY BATCH");
        }
    }

    #[test]
    fn test_entity_batch_with_embedding() {
        let stmt = parse(
            "ENTITY BATCH CREATE [{key: 'e1', name: 'A', embedding: [1.0, 2.0]}, {key: 'e2'}]",
        )
        .unwrap();
        if let StatementKind::Entity(EntityStmt {
            operation: EntityOp::Batch { entities },
        }) = stmt.kind
        {
            assert_eq!(entities.len(), 2);
            assert!(entities[0].embedding.is_some());
            assert_eq!(entities[0].embedding.as_ref().unwrap().len(), 2);
            assert!(entities[1].embedding.is_none());
        } else {
            panic!("expected ENTITY BATCH");
        }
    }

    #[test]
    fn test_similar_connected_to() {
        let stmt = parse("SIMILAR 'key' CONNECTED TO 'hub' LIMIT 10").unwrap();
        let StatementKind::Similar(similar) = stmt.kind else {
            panic!("expected SIMILAR")
        };
        assert!(similar.connected_to.is_some());
        assert!(similar.limit.is_some());
    }

    #[test]
    fn test_neighbors_by_similarity() {
        let stmt = parse("NEIGHBORS 'entity' BY SIMILAR [1.0, 0.0] LIMIT 5").unwrap();
        let StatementKind::Neighbors(neighbors) = stmt.kind else {
            panic!("expected NEIGHBORS")
        };
        assert!(neighbors.by_similarity.is_some());
        assert_eq!(neighbors.by_similarity.unwrap().len(), 2);
    }

    #[test]
    fn test_lexer_api() {
        let mut lexer = Lexer::new("SELECT 1");
        let token = lexer.next_token();
        assert!(matches!(token.kind, TokenKind::Select));
        assert_eq!(lexer.source(), "SELECT 1");
    }

    #[test]
    fn test_parser_api() {
        let mut parser = Parser::new("SELECT 1; SELECT 2");
        let stmt1 = parser.parse_statement().unwrap();
        assert!(matches!(stmt1.kind, StatementKind::Select(_)));
        let stmt2 = parser.parse_statement().unwrap();
        assert!(matches!(stmt2.kind, StatementKind::Select(_)));
    }

    #[test]
    fn test_expr_parser_api() {
        let mut parser = ExprParser::new("1 + 2");
        let expr = parser.parse_expr().unwrap();
        assert!(matches!(expr.kind, ExprKind::Binary(_, BinaryOp::Add, _)));
    }

    #[test]
    fn test_complex_sql() {
        let sql = r#"
            SELECT u.name, COUNT(o.id) AS order_count
            FROM users u
            LEFT JOIN orders o ON u.id = o.user_id
            WHERE u.active = TRUE
            GROUP BY u.name
            HAVING COUNT(o.id) > 0
            ORDER BY order_count DESC
            LIMIT 10
        "#;
        let stmt = parse(sql).unwrap();
        let StatementKind::Select(select) = stmt.kind else {
            panic!("expected SELECT")
        };
        assert!(!select.distinct);
        assert!(select.from.is_some());
        assert!(select.where_clause.is_some());
        assert!(!select.group_by.is_empty());
        assert!(select.having.is_some());
        assert!(!select.order_by.is_empty());
        assert!(select.limit.is_some());
    }

    #[test]
    fn test_insert_values() {
        let stmt =
            parse("INSERT INTO users (name, age) VALUES ('Bob', 25), ('Carol', 30)").unwrap();
        let StatementKind::Insert(insert) = stmt.kind else {
            panic!("expected VALUES")
        };
        assert_eq!(insert.table.name, "users");
        if let InsertSource::Values(rows) = insert.source {
            assert_eq!(rows.len(), 2);
        } else {
            panic!("expected INSERT");
        }
    }

    #[test]
    fn test_create_table_full() {
        let sql = r#"
            CREATE TABLE IF NOT EXISTS orders (
                id INT PRIMARY KEY,
                user_id INT NOT NULL REFERENCES users(id),
                total DECIMAL(10, 2) DEFAULT 0.00,
                created_at TIMESTAMP,
                FOREIGN KEY (user_id) REFERENCES users(id)
            )
        "#;
        let stmt = parse(sql).unwrap();
        let StatementKind::CreateTable(create) = stmt.kind else {
            panic!("expected CREATE TABLE")
        };
        assert!(create.if_not_exists);
        assert_eq!(create.columns.len(), 4);
        assert_eq!(create.constraints.len(), 1);
    }

    #[test]
    fn test_edge_create_full() {
        let stmt = parse("EDGE CREATE 1 -> 2 : FOLLOWS {since: 2023, weight: 0.8}").unwrap();
        if let StatementKind::Edge(EdgeStmt {
            operation:
                EdgeOp::Create {
                    edge_type,
                    properties,
                    ..
                },
        }) = stmt.kind
        {
            assert_eq!(edge_type.name, "FOLLOWS");
            assert_eq!(properties.len(), 2);
        } else {
            panic!("expected EDGE CREATE");
        }
    }

    #[test]
    fn test_spanned_type() {
        let spanned = Spanned::new(42, Span::from_offsets(0, 2));
        assert_eq!(spanned.node, 42);
        assert_eq!(spanned.span.len(), 2);
    }

    #[test]
    fn test_errors_collection() {
        let mut errors = Errors::new();
        errors.push(ParseError::invalid("test", Span::from_offsets(0, 1)));
        assert!(!errors.is_empty());
        assert_eq!(errors.len(), 1);
    }

    // Chain statement tests
    #[test]
    fn test_chain_begin() {
        let stmt = parse("BEGIN CHAIN TRANSACTION").unwrap();
        let StatementKind::Chain(chain) = stmt.kind else {
            panic!("expected CHAIN BEGIN")
        };
        assert!(matches!(chain.operation, ChainOp::Begin));
    }

    #[test]
    fn test_chain_commit() {
        let stmt = parse("COMMIT CHAIN").unwrap();
        let StatementKind::Chain(chain) = stmt.kind else {
            panic!("expected CHAIN COMMIT")
        };
        assert!(matches!(chain.operation, ChainOp::Commit));
    }

    #[test]
    fn test_chain_rollback() {
        let stmt = parse("ROLLBACK CHAIN TO 100").unwrap();
        if let StatementKind::Chain(chain) = stmt.kind {
            if let ChainOp::Rollback { height } = chain.operation {
                if let ExprKind::Literal(Literal::Integer(h)) = height.kind {
                    assert_eq!(h, 100);
                } else {
                    panic!("expected integer");
                }
            } else {
                panic!("expected CHAIN ROLLBACK");
            }
        } else {
            panic!("expected CHAIN statement");
        }
    }

    #[test]
    fn test_chain_history() {
        let stmt = parse("CHAIN HISTORY 'users:123'").unwrap();
        if let StatementKind::Chain(chain) = stmt.kind {
            if let ChainOp::History { key } = chain.operation {
                if let ExprKind::Literal(Literal::String(k)) = key.kind {
                    assert_eq!(k, "users:123");
                } else {
                    panic!("expected string");
                }
            } else {
                panic!("expected CHAIN HISTORY");
            }
        } else {
            panic!("expected CHAIN statement");
        }
    }

    #[test]
    fn test_chain_similar() {
        let stmt = parse("CHAIN SIMILAR [1.0, 2.0, 3.0] LIMIT 10").unwrap();
        if let StatementKind::Chain(chain) = stmt.kind {
            if let ChainOp::Similar { embedding, limit } = chain.operation {
                assert_eq!(embedding.len(), 3);
                assert!(limit.is_some());
            } else {
                panic!("expected CHAIN SIMILAR");
            }
        } else {
            panic!("expected CHAIN statement");
        }
    }

    #[test]
    fn test_chain_drift() {
        let stmt = parse("CHAIN DRIFT FROM 0 TO 1000").unwrap();
        if let StatementKind::Chain(chain) = stmt.kind {
            if let ChainOp::Drift {
                from_height,
                to_height,
            } = chain.operation
            {
                if let ExprKind::Literal(Literal::Integer(f)) = from_height.kind {
                    assert_eq!(f, 0);
                }
                if let ExprKind::Literal(Literal::Integer(t)) = to_height.kind {
                    assert_eq!(t, 1000);
                }
            } else {
                panic!("expected CHAIN DRIFT");
            }
        } else {
            panic!("expected CHAIN statement");
        }
    }

    #[test]
    fn test_chain_height() {
        let stmt = parse("CHAIN HEIGHT").unwrap();
        let StatementKind::Chain(chain) = stmt.kind else {
            panic!("expected CHAIN HEIGHT")
        };
        assert!(matches!(chain.operation, ChainOp::Height));
    }

    #[test]
    fn test_chain_tip() {
        let stmt = parse("CHAIN TIP").unwrap();
        let StatementKind::Chain(chain) = stmt.kind else {
            panic!("expected CHAIN TIP")
        };
        assert!(matches!(chain.operation, ChainOp::Tip));
    }

    #[test]
    fn test_chain_block() {
        let stmt = parse("CHAIN BLOCK 42").unwrap();
        if let StatementKind::Chain(chain) = stmt.kind {
            if let ChainOp::Block { height } = chain.operation {
                if let ExprKind::Literal(Literal::Integer(h)) = height.kind {
                    assert_eq!(h, 42);
                }
            } else {
                panic!("expected CHAIN BLOCK");
            }
        } else {
            panic!("expected CHAIN statement");
        }
    }

    #[test]
    fn test_chain_verify() {
        let stmt = parse("CHAIN VERIFY").unwrap();
        let StatementKind::Chain(chain) = stmt.kind else {
            panic!("expected CHAIN VERIFY")
        };
        assert!(matches!(chain.operation, ChainOp::Verify));
    }

    #[test]
    fn test_show_codebook_global() {
        let stmt = parse("SHOW CODEBOOK GLOBAL").unwrap();
        let StatementKind::Chain(chain) = stmt.kind else {
            panic!("expected SHOW CODEBOOK GLOBAL")
        };
        assert!(matches!(chain.operation, ChainOp::ShowCodebookGlobal));
    }

    #[test]
    fn test_show_codebook_local() {
        let stmt = parse("SHOW CODEBOOK LOCAL 'users'").unwrap();
        if let StatementKind::Chain(chain) = stmt.kind {
            if let ChainOp::ShowCodebookLocal { domain } = chain.operation {
                if let ExprKind::Literal(Literal::String(d)) = domain.kind {
                    assert_eq!(d, "users");
                }
            } else {
                panic!("expected SHOW CODEBOOK LOCAL");
            }
        } else {
            panic!("expected CHAIN statement");
        }
    }

    #[test]
    fn test_analyze_codebook_transitions() {
        let stmt = parse("ANALYZE CODEBOOK TRANSITIONS").unwrap();
        let StatementKind::Chain(chain) = stmt.kind else {
            panic!("expected ANALYZE CODEBOOK TRANSITIONS")
        };
        assert!(matches!(chain.operation, ChainOp::AnalyzeTransitions));
    }
}