use metrale_grammar::CompiledGrammar;
use super::engine::{GrammarEngine, GrammarError};
impl GrammarEngine {
pub(super) fn compile_structural_tag_raw(
&mut self,
triggers: &[String],
tags: &[serde_json::Value],
at_least_one: bool,
stop_after_first: bool,
) -> Result<CompiledGrammar, GrammarError> {
let structural_tag_json = serde_json::json!({
"type": "structural_tag",
"format": {
"type": "triggered_tags",
"triggers": triggers,
"tags": tags,
"at_least_one": at_least_one,
"stop_after_first": stop_after_first,
}
})
.to_string();
let grammar = metrale_grammar::Grammar::from_structural_tag(&structural_tag_json)
.map_err(GrammarError::Compilation)?;
self.compiler
.compile_grammar(&grammar)
.map_err(GrammarError::Compilation)
}
const MAX_JSON_SCHEMA_WHITESPACE: i32 = 8;
pub fn compile_json_schema(&mut self, schema: &str) -> Result<CompiledGrammar, GrammarError> {
self.compiler
.compile_json_schema(
schema,
true,
None,
None::<(&str, &str)>,
true,
Some(Self::MAX_JSON_SCHEMA_WHITESPACE),
)
.map_err(GrammarError::Compilation)
}
pub fn compile_json_grammar(&mut self) -> Result<CompiledGrammar, GrammarError> {
self.compiler
.compile_builtin_json_grammar()
.map_err(GrammarError::Compilation)
}
pub fn compile_ebnf(
&mut self,
ebnf: &str,
root_rule: &str,
) -> Result<CompiledGrammar, GrammarError> {
self.compiler
.compile_grammar_from_ebnf(ebnf, root_rule)
.map_err(GrammarError::Compilation)
}
}