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
// Copyright 2025 Jonas Forsman
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Project name: neorusticus
// Filename: lib.rs
// Creator: Jonas Forsman
//! Neorusticus - A Prolog implementation in Rust
//!
//! This library provides a complete Prolog interpreter with enhanced error handling,
//! arithmetic operations, list processing, and cut operations.
//!
//! # Quick Start
//!
//! ```rust
//! use neorusticus::parse_term;
//!
//! let term = parse_term("foo(bar, X)").unwrap();
//! println!("Parsed: {}", term);
//! ```
//!
//! # Architecture
//!
//! The library is organized into several modules:
//! - [`ast`] - Abstract syntax tree types
//! - [`lexer`] - Tokenization of Prolog source code
//! - [`parser`] - Parsing tokens into AST
//! - [`unification`] - Unification algorithm
//! - [`error`] - Error types and handling
//! - [`builtins`] - Built-in predicates and functions
//! - [`engine`] - The main Prolog engine for executing queries
//! - [`utils`] - Utility functions for pretty printing, term manipulation, etc.
// Module declarations
// Each module contains a specific aspect of the Prolog implementation
// Error types and position tracking for detailed error reporting
// Converts raw text into tokens (lexical analysis)
// Converts tokens into AST structures (syntactic analysis)
// Defines the abstract syntax tree types (Term, Clause)
// Implements the unification algorithm (pattern matching)
// Built-in predicates like append/3, is/2, etc.
// The main execution engine that resolves queries
// Helper utilities for formatting, analysis, etc.
// Re-export the main types and functions for easy use
// This allows users to import directly from the crate root
// Instead of: use neorusticus::engine::PrologEngine;
// They can: use neorusticus::PrologEngine;
pub use ;
pub use ;
pub use ;
pub use ;
pub use Parser;
pub use ;
pub use BuiltinPredicates;
pub use ;
/// Convenience function for parsing a single term from a string
///
/// This function handles the complete parsing pipeline:
/// 1. Creates a tokenizer to convert the string to tokens
/// 2. Tokenizes the input string
/// 3. Creates a parser with the tokens
/// 4. Parses the tokens into an AST Term
///
/// # Example
/// ```
/// use neorusticus::parse_term;
///
/// let term = parse_term("foo(bar, X)").unwrap();
/// println!("Parsed: {}", term);
/// ```
///
/// # Errors
/// Returns a ParseError if:
/// - The input contains invalid Prolog syntax
/// - The tokenizer encounters invalid characters
/// - The parser cannot construct a valid term
/// Convenience function for creating and running a simple query
///
/// This function creates a new engine, adds the given clauses, and executes the query.
/// Useful for quick one-off queries without managing engine state.
///
/// # Example
/// ```
/// use neorusticus::quick_query;
///
/// let clauses = &[
/// "parent(tom, bob).",
/// "parent(bob, ann).",
/// "grandparent(X, Z) :- parent(X, Y), parent(Y, Z)."
/// ];
///
/// let solutions = quick_query(clauses, "grandparent(tom, X)?").unwrap();
/// println!("Found {} solutions", solutions.len());
/// ```
///
/// # Errors
/// Returns a boxed error if:
/// - Any clause has invalid syntax
/// - The query has invalid syntax
/// - Runtime errors occur during query execution
// Link to the test module
// This includes the test file when compiling in test mode
// The cfg(test) attribute ensures tests are only compiled when running tests