Skip to content

Commit fdd515e

Browse files
hudson-aimmoskal
andauthored
Port guidance's JSON implementation (#48)
Port the JSON grammar builder from guidance --------- Co-authored-by: Michal Moskal <[email protected]>
1 parent 7a98040 commit fdd515e

File tree

21 files changed

+2493
-646
lines changed

21 files changed

+2493
-646
lines changed

parser/Cargo.lock

Lines changed: 68 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

parser/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ jsonschema = { version = "0.24.0", default-features = false, optional = true }
1515
url = "2.5.2"
1616
lazy_static = { version = "1.5.0", optional = true }
1717
regex-syntax = "0.8.5"
18+
indexmap = "2.6.0"
19+
referencing = "0.26.1"
1820
rayon = { version = "1.10.0", optional = true }
1921

2022
[features]

parser/build.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ fn main() {
2525
},
2626
|bindings| {
2727
bindings.write_to_file("llguidance.h");
28-
bindings.write_to_file(format!("{}/../../../llguidance.h", env::var("OUT_DIR").unwrap()));
28+
bindings.write_to_file(format!(
29+
"{}/../../../llguidance.h",
30+
env::var("OUT_DIR").unwrap()
31+
));
2932
},
3033
);
3134
}

parser/src/earley/from_guidance.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ fn grammar_from_json(
9393
"cannot have both json_schema/lark_grammar and nodes/rx_nodes"
9494
);
9595

96-
let mut new_grm = if let Some(json_schema) = input.json_schema.as_ref() {
96+
let mut new_grm = if let Some(json_schema) = input.json_schema.take() {
9797
ensure!(
9898
input.lark_grammar.is_none(),
9999
"cannot have both json_schema and lark_grammar"
100100
);
101-
let opts = JsonCompileOptions { compact: false };
101+
let opts: JsonCompileOptions = JsonCompileOptions::default();
102102
opts.json_to_llg(json_schema)?
103103
} else {
104104
lark_to_llguidance(input.lark_grammar.as_ref().unwrap())?

parser/src/ffi.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,9 @@ fn new_constraint_json(init: &LlgConstraintInit, json_schema: *const c_char) ->
356356
.map_err(|_| anyhow::anyhow!("Invalid UTF-8 in json_schema"))?;
357357
let json_schema = serde_json::from_str(json_schema)
358358
.map_err(|e| anyhow::anyhow!("Invalid JSON in json_schema: {e}"))?;
359-
let opts = JsonCompileOptions { compact: false };
359+
let opts = JsonCompileOptions::default();
360360
let grammar = opts
361-
.json_to_llg(&json_schema)
361+
.json_to_llg(json_schema)
362362
.map_err(|e| anyhow::anyhow!("Error compiling JSON schema to LLG: {e}"))?;
363363
init.build_constraint(grammar)
364364
}

parser/src/grammar_builder.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::api::{
77
RegexSpec, TopLevelGrammar,
88
};
99

10-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
10+
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
1111
pub struct NodeRef {
1212
idx: usize,
1313
grammar_id: u32,
@@ -91,6 +91,14 @@ impl RegexBuilder {
9191
self.add_node(RegexNode::Repeat(node, min, max))
9292
}
9393

94+
pub fn not(&mut self, node: RegexId) -> RegexId {
95+
self.add_node(RegexNode::Not(node))
96+
}
97+
98+
pub fn and(&mut self, nodes: Vec<RegexId>) -> RegexId {
99+
self.add_node(RegexNode::And(nodes))
100+
}
101+
94102
fn finalize(&mut self) -> Vec<RegexNode> {
95103
let r = std::mem::take(&mut self.nodes);
96104
*self = Self::new();

0 commit comments

Comments
 (0)