Expand description
gollum-parser
§Examples
//! Demonstrates the `gollum_parser::parse` API.
//!
//! Run with:
//! cargo run --example parse_basics -p gollum-parser
use gollum_ast::item::Item;
use gollum_parser::parse;
fn main() {
let src = r#"
% Type and predicate declarations
:- type Person.
:- pred parent(Person, Person).
% Facts
parent(alice, bob).
parent(bob, charlie).
% Rule
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
% Probabilistic fact
0.8 :: rain.
% Tabling directive
:- table ancestor/2.
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
% Query
?- grandparent(alice, Y).
"#;
let items = parse(src).expect("parse failed");
println!("Parsed {} top-level items:\n", items.len());
for (i, item) in items.iter().enumerate() {
match item {
Item::Fact(f) => {
println!(" [{i}] Fact: {}/{} args={:?}", f.name, f.args.len(), f.args);
}
Item::Rule(r) => {
println!(
" [{i}] Rule: {}/{} :- {} goals",
r.head.name,
r.head.args.len(),
r.body.len()
);
}
Item::Query(q) => {
println!(" [{i}] Query: {} goals", q.goals.len());
}
Item::Directive(d) => {
println!(" [{i}] Directive: {d:?}");
}
Item::Probabilistic(p) => {
println!(" [{i}] Probabilistic: probability={}", p.probability);
}
}
}
}
Re-exports§
pub use lexer::Token;
Modules§
- lexer
- Logos-based lexer for Gollum source text.
Enums§
- Error
- Error type for
gollum-parser - Item
- A top-level item in a Gollum program.
- Term
- A term in Gollum logic.
Functions§
- parse
- Parse Gollum source text into a list of top-level items.
- parse_
term - Parse a single term from source text and return a
Term. Accepts the same tokenisation rules asparsebut expects a single term and no trailing dot.
Type Aliases§
- Result
- Result type for
gollum-parser