use std::collections::HashMap;
use crate::grammar_check;
use crate::peg::{Grammar, Peg, choice, field, node, repeat, rule, rule_ref, seq, token};
use crate::{BuildError, Diagnostic};
use dsl_kit_core::IdGen;
use dsl_kit_schema::{ChildSchema, FieldSchema, Multiplicity, NodeSchema, VariantSchema};
pub mod codes {
pub const UNSUPPORTED_FIELD: &str = "dsl_kit::schema_gen::unsupported_field";
pub const EMPTY_SCHEMA: &str = "dsl_kit::schema_gen::empty_schema";
}
pub const START_RULE: &str = "node";
type PegFactory = Box<dyn Fn(&IdGen) -> Peg>;
#[derive(Default)]
pub struct SyntaxOverrides {
by_type: HashMap<String, PegFactory>,
by_field: HashMap<(String, String), PegFactory>,
}
impl SyntaxOverrides {
pub fn new() -> Self {
Self::default()
}
pub fn for_type(
mut self,
ty: impl AsRef<str>,
value: impl Fn(&IdGen) -> Peg + 'static,
) -> Self {
self.by_type.insert(strip_ws(ty.as_ref()), Box::new(value));
self
}
pub fn for_field(
mut self,
variant: impl Into<String>,
field: impl Into<String>,
value: impl Fn(&IdGen) -> Peg + 'static,
) -> Self {
self.by_field
.insert((variant.into(), field.into()), Box::new(value));
self
}
fn resolve(&self, variant: &str, field: &FieldSchema) -> Option<&PegFactory> {
self.by_field
.get(&(variant.to_string(), field.name.clone()))
.or_else(|| self.by_type.get(&strip_ws(&field.ty)))
}
}
fn strip_ws(ty: &str) -> String {
ty.chars().filter(|c| !c.is_whitespace()).collect()
}
impl Grammar {
pub fn from_schema(schema: &NodeSchema, ids: &IdGen) -> Result<Grammar, BuildError> {
grammar_from_schema(schema, ids)
}
}
pub fn grammar_from_schema(schema: &NodeSchema, ids: &IdGen) -> Result<Grammar, BuildError> {
grammar_from_schema_with(schema, ids, &SyntaxOverrides::default())
}
pub fn grammar_from_schema_with(
schema: &NodeSchema,
ids: &IdGen,
overrides: &SyntaxOverrides,
) -> Result<Grammar, BuildError> {
if schema.variants.is_empty() {
return Err(BuildError::single(Diagnostic::error(
codes::EMPTY_SCHEMA,
format!("schema `{}` declares no variants", schema.name),
)));
}
let mut bad_fields = Vec::new();
for v in &schema.variants {
for f in &v.fields {
if resolved_field_value_peg(&v.name, f, ids, overrides).is_none() {
bad_fields.push(Diagnostic::error(
codes::UNSUPPORTED_FIELD,
format!(
"variant `{}` field `{}`: type `{}` has no canonical-syntax \
mapping (supported: String, bool, the integer types; or \
register a `SyntaxOverrides` value production)",
v.name, f.name, f.ty
),
));
}
}
}
if !bad_fields.is_empty() {
return Err(BuildError::new(bad_fields));
}
let mut rules = Vec::with_capacity(schema.variants.len() + 1);
rules.push(rule(
ids,
START_RULE,
choice(
ids,
schema
.variants
.iter()
.map(|v| rule_ref(ids, v.name.clone()))
.collect(),
),
));
for v in &schema.variants {
rules.push(variant_rule(v, ids, overrides));
}
Ok(Grammar::new(rules, START_RULE))
}
fn variant_rule(v: &VariantSchema, ids: &IdGen, overrides: &SyntaxOverrides) -> Peg {
let has_optional = v.fields.iter().any(|f| f.optional);
let mut args: Vec<Peg> = Vec::new();
for f in &v.fields {
let value = resolved_field_value_peg(&v.name, f, ids, overrides)
.expect("unsupported field types were rejected before rule generation");
args.push(seq(
ids,
vec![
token(ids, format!("%kw:{}", f.name)),
token(ids, ":"),
field(ids, f.name.clone(), value),
],
));
}
for c in &v.children {
args.push(child_arg_peg(c, ids));
}
let mut items = vec![token(ids, format!("%kw:{}", v.name)), token(ids, "(")];
if has_optional && !args.is_empty() {
let arg_choice = choice(ids, args);
let tail_alt = |ids: &IdGen| -> Peg {
let mut alts: Vec<Peg> = Vec::new();
for f in &v.fields {
let value = resolved_field_value_peg(&v.name, f, ids, overrides)
.expect("unsupported field types were rejected before rule generation");
alts.push(seq(
ids,
vec![
token(ids, format!("%kw:{}", f.name)),
token(ids, ":"),
field(ids, f.name.clone(), value),
],
));
}
for c in &v.children {
alts.push(child_arg_peg(c, ids));
}
choice(ids, alts)
};
let tail = repeat(ids, seq(ids, vec![token(ids, ","), tail_alt(ids)]), 0, None);
let full = seq(ids, vec![arg_choice, tail]);
items.push(repeat(ids, full, 0, Some(1)));
} else {
for (i, arg) in args.into_iter().enumerate() {
if i > 0 {
items.push(token(ids, ","));
}
items.push(arg);
}
}
items.push(token(ids, ")"));
rule(
ids,
v.name.clone(),
node(ids, v.name.clone(), seq(ids, items)),
)
}
fn resolved_field_value_peg(
variant: &str,
f: &FieldSchema,
ids: &IdGen,
overrides: &SyntaxOverrides,
) -> Option<Peg> {
match overrides.resolve(variant, f) {
Some(factory) => Some(factory(ids)),
None => field_value_peg(f, ids),
}
}
fn field_value_peg(f: &FieldSchema, ids: &IdGen) -> Option<Peg> {
const INT_TYPES: &[&str] = &[
"i8", "i16", "i32", "i64", "i128", "u8", "u16", "u32", "u64", "u128", "usize", "isize",
];
let ty = strip_ws(&f.ty);
if ty == "String" {
Some(token(ids, "%str"))
} else if INT_TYPES.contains(&ty.as_str()) {
Some(token(ids, "%int"))
} else if ty == "bool" {
Some(choice(
ids,
vec![token(ids, "%kw:true"), token(ids, "%kw:false")],
))
} else if ty == "Option<String>" {
Some(choice(
ids,
vec![token(ids, "%kw:none"), token(ids, "%str")],
))
} else if ty == "Vec<String>" {
let elems = seq(
ids,
vec![
token(ids, "%str_raw"),
repeat(
ids,
seq(ids, vec![token(ids, ","), token(ids, "%str_raw")]),
0,
None,
),
],
);
Some(seq(
ids,
vec![
token(ids, "["),
repeat(ids, elems, 0, Some(1)),
token(ids, "]"),
],
))
} else {
None
}
}
fn child_arg_peg(c: &ChildSchema, ids: &IdGen) -> Peg {
let name_kw = token(ids, format!("%kw:{}", c.name));
let colon = token(ids, ":");
match c.multiplicity {
Multiplicity::One => seq(
ids,
vec![
name_kw,
colon,
field(ids, c.name.clone(), rule_ref(ids, START_RULE)),
],
),
Multiplicity::Optional => seq(
ids,
vec![
name_kw,
colon,
choice(
ids,
vec![
field(ids, c.name.clone(), rule_ref(ids, START_RULE)),
token(ids, "%kw:none"),
],
),
],
),
Multiplicity::Many => {
let elems = seq(
ids,
vec![
rule_ref(ids, START_RULE),
repeat(
ids,
seq(ids, vec![token(ids, ","), rule_ref(ids, START_RULE)]),
0,
None,
),
],
);
seq(
ids,
vec![
name_kw,
colon,
token(ids, "["),
field(ids, c.name.clone(), repeat(ids, elems, 0, Some(1))),
token(ids, "]"),
],
)
}
}
}
pub fn checked_grammar_from_schema(
schema: &NodeSchema,
ids: &IdGen,
) -> Result<Grammar, BuildError> {
checked_grammar_from_schema_with(schema, ids, &SyntaxOverrides::default())
}
pub fn checked_grammar_from_schema_with(
schema: &NodeSchema,
ids: &IdGen,
overrides: &SyntaxOverrides,
) -> Result<Grammar, BuildError> {
let grammar = grammar_from_schema_with(schema, ids, overrides)?;
let mut diags = grammar_check::check_left_recursion(&grammar);
diags.extend(grammar_check::check_nullable_repeat(&grammar));
diags.extend(
grammar_check::check_schema_consistency(&grammar, schema)
.into_iter()
.filter(|d| d.severity == crate::Severity::Error),
);
if diags.is_empty() {
Ok(grammar)
} else {
Err(BuildError::new(diags))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{RawValue, check_conformance};
fn demo_schema() -> NodeSchema {
NodeSchema {
name: "Expr".into(),
variants: vec![
VariantSchema {
name: "Lit".into(),
fields: vec![FieldSchema {
name: "value".into(),
ty: "i64".into(),
optional: false,
}],
children: vec![],
},
VariantSchema {
name: "Name".into(),
fields: vec![
FieldSchema {
name: "text".into(),
ty: "String".into(),
optional: false,
},
FieldSchema {
name: "quoted".into(),
ty: "bool".into(),
optional: false,
},
],
children: vec![],
},
VariantSchema {
name: "Add".into(),
fields: vec![],
children: vec![
ChildSchema {
name: "lhs".into(),
multiplicity: Multiplicity::One,
},
ChildSchema {
name: "rhs".into(),
multiplicity: Multiplicity::One,
},
],
},
VariantSchema {
name: "Neg".into(),
fields: vec![],
children: vec![ChildSchema {
name: "body".into(),
multiplicity: Multiplicity::Optional,
}],
},
VariantSchema {
name: "List".into(),
fields: vec![],
children: vec![ChildSchema {
name: "items".into(),
multiplicity: Multiplicity::Many,
}],
},
VariantSchema {
name: "Unit".into(),
fields: vec![],
children: vec![],
},
],
}
}
fn demo_grammar() -> Grammar {
checked_grammar_from_schema(&demo_schema(), &IdGen::new())
.expect("demo schema generates a clean grammar")
}
fn parse_ok(input: &str) -> crate::ParseTree {
let g = demo_grammar();
let tree = g
.parse(input)
.unwrap_or_else(|e| panic!("parse failed for {input:?}: {:?}", e.diagnostics));
let diags = check_conformance(&tree, &demo_schema());
assert!(
diags.is_empty(),
"conformance clean for {input:?}: {diags:?}"
);
tree
}
#[test]
fn generated_grammar_passes_all_static_checks() {
demo_grammar();
}
#[test]
fn parses_int_field_variant() {
let tree = parse_ok("Lit(value: -42)");
assert_eq!(tree.variant, "Lit");
assert_eq!(tree.field("value"), Some(&RawValue::Text("-42".into())));
}
#[test]
fn parses_string_and_bool_fields_with_escapes() {
let tree = parse_ok(r#"Name(text: "hello \"world\"\n", quoted: true)"#);
assert_eq!(
tree.field("text"),
Some(&RawValue::Text("hello \"world\"\n".into()))
);
assert_eq!(tree.field("quoted"), Some(&RawValue::Text("true".into())));
}
#[test]
fn parses_nested_one_children() {
let tree = parse_ok("Add(lhs: Lit(value: 1), rhs: Neg(body: none))");
let lhs = tree.child_slot("lhs").unwrap();
assert_eq!(lhs.len(), 1);
assert_eq!(lhs[0].variant, "Lit");
let rhs = tree.child_slot("rhs").unwrap();
assert_eq!(rhs[0].variant, "Neg");
}
#[test]
fn optional_child_none_leaves_slot_absent() {
let tree = parse_ok("Neg(body: none)");
assert!(
tree.child_slot("body").is_none(),
"bare `none` = absent slot"
);
}
#[test]
fn optional_child_present_binds_one_tree() {
let tree = parse_ok("Neg(body: Lit(value: 3))");
assert_eq!(tree.child_slot("body").unwrap().len(), 1);
}
#[test]
fn many_child_list_binds_in_order_and_drops_commas() {
let tree = parse_ok("List(items: [Lit(value: 1), Lit(value: 2), Unit()])");
let items = tree.child_slot("items").unwrap();
let variants: Vec<&str> = items.iter().map(|t| t.variant.as_str()).collect();
assert_eq!(variants, ["Lit", "Lit", "Unit"]);
}
#[test]
fn many_child_empty_list_is_conformant() {
let tree = parse_ok("List(items: [])");
assert!(
tree.child_slot("items").is_none_or(<[_]>::is_empty),
"empty list = zero children"
);
}
#[test]
fn zero_argument_variant_parses() {
let tree = parse_ok("Unit()");
assert_eq!(tree.variant, "Unit");
assert!(tree.fields.is_empty() && tree.children.is_empty());
}
#[test]
fn whitespace_and_newlines_are_tolerated() {
let tree = parse_ok("Add(\n lhs: Lit( value : 7 ),\n rhs: Unit()\n)");
assert_eq!(tree.child_slot("lhs").unwrap()[0].variant, "Lit");
}
#[test]
fn unsupported_field_type_fails_generation_with_full_list() {
let schema = NodeSchema {
name: "Bad".into(),
variants: vec![VariantSchema {
name: "Par".into(),
fields: vec![
FieldSchema {
name: "policy".into(),
ty: "Option<JoinPolicy>".into(),
optional: false,
},
FieldSchema {
name: "reducer".into(),
ty: "ReducerId".into(),
optional: false,
},
],
children: vec![],
}],
};
let err = grammar_from_schema(&schema, &IdGen::new()).unwrap_err();
assert_eq!(err.diagnostics.len(), 2, "one diagnostic per bad field");
assert!(
err.diagnostics
.iter()
.all(|d| d.code == codes::UNSUPPORTED_FIELD)
);
}
fn par_schema() -> NodeSchema {
NodeSchema {
name: "Flow".into(),
variants: vec![
VariantSchema {
name: "Par".into(),
fields: vec![
FieldSchema {
name: "policy".into(),
ty: "Option < JoinPolicy >".into(),
optional: false,
},
FieldSchema {
name: "reducer_id".into(),
ty: "Option < String >".into(),
optional: false,
},
],
children: vec![ChildSchema {
name: "children".into(),
multiplicity: Multiplicity::Many,
}],
},
VariantSchema {
name: "Call".into(),
fields: vec![FieldSchema {
name: "label".into(),
ty: "String".into(),
optional: false,
}],
children: vec![],
},
],
}
}
fn par_overrides() -> SyntaxOverrides {
SyntaxOverrides::new()
.for_type("Option<JoinPolicy>", |ids| {
choice(
ids,
vec![
token(ids, "%kw:none"),
token(ids, "%kw:all_failfast"),
token(ids, "%kw:any_failfast"),
],
)
})
.for_type("Option<String>", |ids| {
choice(ids, vec![token(ids, "%kw:none"), token(ids, "%str")])
})
}
#[test]
fn type_overrides_unlock_exotic_payload_fields() {
let g = checked_grammar_from_schema_with(&par_schema(), &IdGen::new(), &par_overrides())
.expect("overridden Flow schema generates a clean grammar");
let tree = g
.parse(
r#"Par(policy: all_failfast, reducer_id: "reduce_all_ordered",
children: [Call(label: "a"), Call(label: "b")])"#,
)
.expect("canonical Par text parses");
let diags = check_conformance(&tree, &par_schema());
assert!(diags.is_empty(), "conformance clean: {diags:?}");
assert_eq!(
tree.field("policy"),
Some(&RawValue::Text("all_failfast".into()))
);
assert_eq!(
tree.field("reducer_id"),
Some(&RawValue::Text("reduce_all_ordered".into()))
);
assert_eq!(tree.child_slot("children").unwrap().len(), 2);
}
#[test]
fn field_override_wins_over_type_override() {
let overrides = par_overrides().for_field("Par", "policy", |ids| token(ids, "%kw:pinned"));
let g = checked_grammar_from_schema_with(&par_schema(), &IdGen::new(), &overrides)
.expect("clean grammar with field-level override");
let tree = g
.parse(r#"Par(policy: pinned, reducer_id: none, children: [])"#)
.expect("field-level syntax parses");
assert_eq!(tree.field("policy"), Some(&RawValue::Text("pinned".into())));
assert!(
g.parse(r#"Par(policy: all_failfast, reducer_id: none, children: [])"#)
.is_err(),
"type-level syntax rejected once the field is pinned"
);
}
#[test]
fn unrelated_override_leaves_builtin_mapping_and_failure_intact() {
let overrides = SyntaxOverrides::new().for_type("Uuid", |ids| token(ids, "%str"));
let err = grammar_from_schema_with(&par_schema(), &IdGen::new(), &overrides).unwrap_err();
assert_eq!(
err.diagnostics.len(),
1,
"policy is the only Par field still unmapped without overrides",
);
assert!(
err.diagnostics
.iter()
.all(|d| d.code == codes::UNSUPPORTED_FIELD)
);
assert!(
err.diagnostics[0].message.contains("policy"),
"unmapped-field diagnostic points at `policy`",
);
let g = checked_grammar_from_schema_with(&demo_schema(), &IdGen::new(), &overrides)
.expect("demo schema still generates");
assert_eq!(g.parse("Lit(value: 7)").unwrap().variant, "Lit");
}
#[test]
fn builtin_option_string_mapping_accepts_none_and_str() {
let schema = NodeSchema {
name: "Meta".into(),
variants: vec![VariantSchema {
name: "Row".into(),
fields: vec![FieldSchema {
name: "description".into(),
ty: "Option<String>".into(),
optional: true,
}],
children: vec![],
}],
};
let g = checked_grammar_from_schema(&schema, &IdGen::new())
.expect("Option<String> is a built-in mapping");
let t1 = g.parse(r#"Row(description: none)"#).expect("none parses");
assert_eq!(
t1.field("description"),
Some(&RawValue::Text("none".into())),
);
let t2 = g.parse(r#"Row(description: "hi")"#).expect("string parses");
assert_eq!(t2.field("description"), Some(&RawValue::Text("hi".into())));
let t3 = g.parse(r#"Row()"#).expect("omitted parses");
assert!(t3.field("description").is_none());
for t in [&t1, &t2, &t3] {
assert!(
check_conformance(t, &schema).is_empty(),
"conformance clean for {t:?}",
);
}
}
#[test]
fn builtin_vec_string_mapping_accepts_bracketed_list() {
let schema = NodeSchema {
name: "Meta".into(),
variants: vec![VariantSchema {
name: "Row".into(),
fields: vec![FieldSchema {
name: "tags".into(),
ty: "Vec<String>".into(),
optional: true,
}],
children: vec![],
}],
};
let g = checked_grammar_from_schema(&schema, &IdGen::new())
.expect("Vec<String> is a built-in mapping");
let t1 = g.parse(r#"Row(tags: [])"#).expect("empty list parses");
assert_eq!(t1.field("tags"), Some(&RawValue::Text("[]".into())));
let t2 = g
.parse(r#"Row(tags: ["a", "b"])"#)
.expect("two items parses");
assert_eq!(
t2.field("tags"),
Some(&RawValue::Text(r#"["a","b"]"#.into())),
);
let t3 = g.parse(r#"Row()"#).expect("omitted parses");
assert!(t3.field("tags").is_none());
for t in [&t1, &t2, &t3] {
assert!(
check_conformance(t, &schema).is_empty(),
"conformance clean for {t:?}",
);
}
}
#[test]
fn empty_schema_fails_generation() {
let schema = NodeSchema {
name: "Void".into(),
variants: vec![],
};
let err = grammar_from_schema(&schema, &IdGen::new()).unwrap_err();
assert_eq!(err.diagnostics[0].code, codes::EMPTY_SCHEMA);
}
#[test]
fn parse_error_reports_expected_set() {
let g = demo_grammar();
let err = g.parse("Lit(value: banana)").unwrap_err();
assert!(!err.diagnostics.is_empty());
}
}