feat: implement storybook DSL with template composition and validation

Add complete domain-specific language for authoring narrative content for
agent simulations.

Features:
- Complete parser using LALRPOP + logos lexer
- Template composition (includes + multiple inheritance)
- Strict mode validation for templates
- Reserved keyword protection
- Semantic validators (trait ranges, schedule overlaps, life arcs, behaviors)
- Name resolution and cross-reference tracking
- CLI tool (validate, inspect, query commands)
- Query API with filtering
- 260 comprehensive tests (unit, integration, property-based)

Implementation phases:
- Phase 1 (Parser): Complete
- Phase 2 (Resolution + Validation): Complete
- Phase 3 (Public API + CLI): Complete

BREAKING CHANGE: Initial implementation
This commit is contained in:
2026-02-08 13:24:35 +00:00
commit 9c20dd4092
59 changed files with 25484 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
// Multiple errors in one file
// This demonstrates non-fail-fast error collection - all errors reported at once!
character Martha {
age: 200
trust: 1.5
bond: -0.3
}
character David {
age: -5
love: 2.0
}
life_arc Growth {
state child {
on age > 18 -> adult
}
state adult {
on age > 65 -> senior
on retired -> elderly
}
}
schedule BadSchedule {
08:00 -> 12:00: work
11:30 -> 13:00: lunch
12:30 -> 17:00: more_work
}

View File

@@ -0,0 +1,6 @@
// Error: Missing colon after field name
// This demonstrates the UnexpectedToken parse error
character Martha {
age 34
}

View File

@@ -0,0 +1,8 @@
// Error: Trait value outside valid range
// Demonstrates TraitOutOfRange validation error
character Martha {
age: 34
trust: 1.5
bond: -0.2
}

View File

@@ -0,0 +1,12 @@
// Error: Transition to undefined state
// Demonstrates UnknownLifeArcState validation error
life_arc Growth {
state child {
on age > 18 -> adult
}
state adult {
on age > 65 -> senior
}
}

View File

@@ -0,0 +1,8 @@
// Error: Schedule blocks overlap in time
// Demonstrates ScheduleOverlap validation error
schedule DailyRoutine {
08:00 -> 12:30: work
12:00 -> 13:00: lunch
13:00 -> 17:00: work
}

View File

@@ -0,0 +1,55 @@
# Compiler Error Examples
This directory contains example `.sb` files that demonstrate each type of error
the Storybook compiler can detect. Each file is intentionally incorrect to showcase
the error messages and helpful hints.
## How to Run
To see all error messages, validate each file individually:
```bash
# From the storybook root directory
cargo build --release
# Run each file to see its error
./target/release/sb validate tests/compiler_errors/01_unexpected_token.sb
./target/release/sb validate tests/compiler_errors/02_unexpected_eof.sb
./target/release/sb validate tests/compiler_errors/03_invalid_token.sb
# ... etc
```
Or use this script to show all errors:
```bash
#!/bin/bash
for file in tests/compiler_errors/*.sb; do
echo "═══════════════════════════════════════════════════════════"
echo "File: $(basename $file)"
echo "═══════════════════════════════════════════════════════════"
cargo run --bin sb -- validate "$file" 2>&1 || true
echo ""
done
```
## Error Categories
### Parse Errors (Syntax)
- `01_unexpected_token.sb` - Missing colon after field name
- `02_unexpected_eof.sb` - Incomplete declaration
- `03_invalid_token.sb` - Invalid character in syntax
- `04_unclosed_prose.sb` - Prose block missing closing `---`
### Validation Errors (Semantics)
- `05_trait_out_of_range.sb` - Trait value outside 0.0-1.0 range
- `06_age_out_of_range.sb` - Age value outside 0-150 range
- `07_unknown_life_arc_state.sb` - Transition to undefined state
- `08_schedule_overlap.sb` - Schedule blocks overlap in time
- `09_unknown_behavior_action.sb` - Undefined behavior tree action
- `10_duplicate_field.sb` - Same field name used twice
- `11_relationship_bond_out_of_range.sb` - Bond value outside 0.0-1.0 range
Each error includes:
- ✓ Clear error message explaining what went wrong
- ✓ Helpful hint on how to fix it
- ✓ Context-specific suggestions

View File

@@ -0,0 +1,26 @@
#!/bin/bash
# Script to run all compiler error examples and see the error messages
cd "$(dirname "$0")/../.."
echo "════════════════════════════════════════════════════════════════"
echo "STORYBOOK COMPILER ERRORS - EXAMPLES"
echo "════════════════════════════════════════════════════════════════"
echo ""
for file in tests/compiler_errors/*.sb; do
if [ -f "$file" ]; then
echo "═══════════════════════════════════════════════════════════════════"
echo "File: $(basename "$file")"
echo "═══════════════════════════════════════════════════════════════════"
cat "$file" | head -3 | tail -2 # Show the comment lines
echo ""
cargo run --quiet --bin sb -- validate "$file" 2>&1 || true
echo ""
echo ""
fi
done
echo "════════════════════════════════════════════════════════════════"
echo "ALL EXAMPLES COMPLETE"
echo "════════════════════════════════════════════════════════════════"