feat(lexer): add type system keywords

Added four keywords for new type system:
- concept: Base type definition
- sub_concept: Enum/record sub-type definition
- concept_comparison: Compile-time enum mapping
- any: Universal type for dynamic contexts

Also added:
- CLAUDE.md with project instructions and commit guidelines
- Test coverage for new keywords
- Crate-level deny directives for unused variables and dead code

Fixed pre-existing clippy issues to pass pre-commit hooks.
This commit is contained in:
2026-02-13 22:31:56 +00:00
parent 16deb5d237
commit f258a526e3
14 changed files with 151 additions and 74 deletions

84
CLAUDE.md Normal file
View File

@@ -0,0 +1,84 @@
# Storybook Project - Claude Code Instructions
## Commit Policy (CRITICAL - NEVER BYPASS)
These rules are **MANDATORY** for all commits:
1. **All tests must pass** - Run `cargo test` and verify 0 failures before every commit
2. **All new code must have tests** - No exceptions, no untested code allowed
3. **No unused variables or dead code** - Clean up unused code, don't suppress warnings with underscore prefixes
4. **Commit frequently** at logical milestones
5. **Never use `--no-verify`** flag to bypass pre-commit hooks
## Development Workflow
### Installing the LSP
To reinstall/update the Storybook LSP after making changes:
```bash
cargo install --path . --bin storybook-lsp --force
```
The LSP binary is installed to `~/.cargo/bin/storybook-lsp`. The Zed extension will automatically find it if `~/.cargo/bin` is in your PATH.
### Installing the Zed Extension
To rebuild and install the Zed extension after making changes:
```bash
cd zed-storybook
./build-extension.sh
```
Then in Zed:
1. `Cmd+Shift+P` → "zed: install dev extension"
2. Select: `/Users/sienna/Development/storybook/zed-storybook`
**Updating tree-sitter grammar:**
When the grammar changes, update `zed-storybook/extension.toml` and change the `rev` field under `[grammars.storybook]` to the new commit SHA or branch name.
## Git Workflow
Development happens directly on `main` until the language is stable. Releases are tagged with version numbers (e.g., `v0.2.0`). Branches are only used for external contributors or later development phases.
Pre-commit hooks check: trailing whitespace, rustfmt, clippy.
### Commit Message Guidelines
Keep commit messages clean and focused:
- **DO**: Use conventional commit format (e.g., `feat(lexer):`, `fix(parser):`, `docs:`)
- **DO**: Write clear, concise descriptions of what changed and why
- **DON'T**: Include attribution (no "Co-Authored-By")
- **DON'T**: Include test status (e.g., "all tests pass")
- **DON'T**: Include sprint/milestone markers
- **DON'T**: Include version markers (e.g., "Part of v0.3.0")
Example:
```
feat(lexer): add type system keywords
Added four keywords for new type system:
- concept: Base type definition
- sub_concept: Enum/record sub-type definition
- concept_comparison: Compile-time enum mapping
- any: Universal type for dynamic contexts
```
## Project Structure
- `src/` - Core Storybook compiler and runtime
- `tree-sitter-storybook/` - Tree-sitter grammar for Storybook DSL
- `zed-storybook/` - Zed editor extension
- `storybook-editor/` - LSP server implementation
- `docs/` - Specifications and documentation
- `SBIR-v0.2.0-SPEC.md` - Storybook Intermediate Representation binary format
## Testing Philosophy
- Every feature needs tests
- Every bug fix needs a regression test
- Tests must pass before commits
- Use `cargo test --lib` to run unit tests
- Use `cargo test` to run all tests including integration tests

View File

@@ -21,8 +21,7 @@ async fn main() {
let stdin = tokio::io::stdin(); let stdin = tokio::io::stdin();
let stdout = tokio::io::stdout(); let stdout = tokio::io::stdout();
let (service, socket) = let (service, socket) = LspService::new(storybook::lsp::StorybookLanguageServer::new);
LspService::new(storybook::lsp::StorybookLanguageServer::new);
Server::new(stdin, stdout, socket).serve(service).await; Server::new(stdin, stdout, socket).serve(service).await;
} }

View File

@@ -1,3 +1,6 @@
#![deny(unused_variables)]
#![deny(dead_code)]
//! Storybook - A DSL for authoring narrative content for agent simulations //! Storybook - A DSL for authoring narrative content for agent simulations
//! //!
//! This library provides parsing, resolution, and validation for `.sb` files. //! This library provides parsing, resolution, and validation for `.sb` files.

View File

@@ -659,11 +659,7 @@ fn add_missing_template_fields(
character: insert_position.1 as u32, character: insert_position.1 as u32,
}, },
}, },
new_text: if character.fields.is_empty() { new_text: format!("\n{}", field_text),
format!("\n{}", field_text)
} else {
format!("\n{}", field_text)
},
}], }],
); );

View File

@@ -290,17 +290,12 @@ fn get_contextual_field_completions(doc: &Document, offset: usize) -> Option<Vec
| Declaration::Template(template) => { | Declaration::Template(template) => {
// Check if cursor is inside this template block // Check if cursor is inside this template block
if offset >= template.span.start && offset <= template.span.end { if offset >= template.span.start && offset <= template.span.end {
let mut items = Vec::new(); // Templates can suggest common field patterns
return Some(vec![simple_item(
// Add special keywords for templates
items.push(simple_item(
"include", "include",
"Include a template", "Include a template",
"include ${1:TemplateName}", "include ${1:TemplateName}",
)); )]);
// Templates can suggest common field patterns
return Some(items);
} }
}, },
| _ => {}, | _ => {},
@@ -494,16 +489,18 @@ fn determine_context(text: &str, offset: usize) -> CompletionContext {
for (_offset, token, _end) in &tokens { for (_offset, token, _end) in &tokens {
match token { match token {
| Token::LBrace => nesting_level += 1, | Token::LBrace => {
nesting_level += 1;
if seen_colon_without_brace {
// Opening brace after colon - we've entered the block
seen_colon_without_brace = false;
}
},
| Token::RBrace => nesting_level = nesting_level.saturating_sub(1), | Token::RBrace => nesting_level = nesting_level.saturating_sub(1),
| Token::Colon => { | Token::Colon => {
// Mark that we've seen a colon // Mark that we've seen a colon
seen_colon_without_brace = true; seen_colon_without_brace = true;
}, },
| Token::LBrace if seen_colon_without_brace => {
// Opening brace after colon - we've entered the block
seen_colon_without_brace = false;
},
| Token::Ident(keyword) | Token::Ident(keyword)
if matches!( if matches!(
keyword.as_str(), keyword.as_str(),

View File

@@ -91,25 +91,6 @@ fn try_parse(text: &str) -> Result<(), Vec<Diagnostic>> {
} }
} }
fn byte_offset_to_line(text: &str, offset: usize) -> usize {
let mut line = 0;
let mut current_offset = 0;
for ch in text.chars() {
if current_offset >= offset {
break;
}
if ch == '\n' {
line += 1;
}
current_offset += ch.len_utf8();
}
line
}
/// Convert a byte offset to line/column position /// Convert a byte offset to line/column position
/// This is a placeholder - will be replaced when we have proper Span tracking /// This is a placeholder - will be replaced when we have proper Span tracking
pub fn byte_offset_to_position(text: &str, offset: usize) -> Position { pub fn byte_offset_to_position(text: &str, offset: usize) -> Position {

View File

@@ -172,9 +172,8 @@ invalid syntax here
#[test] #[test]
fn test_byte_offset_to_position_beyond_text() { fn test_byte_offset_to_position_beyond_text() {
let text = "short"; let text = "short";
let pos = diagnostics::byte_offset_to_position(text, 1000); // Should not panic when offset is beyond text length
// Should not panic, returns position at end (line is always valid u32) let _ = diagnostics::byte_offset_to_position(text, 1000);
assert!(pos.line == 0 || pos.line > 0);
} }
#[test] #[test]

View File

@@ -39,13 +39,11 @@ pub fn get_hover_info(text: &str, line: usize, character: usize) -> Option<Hover
} }
// Add the character offset (assuming UTF-8) // Add the character offset (assuming UTF-8)
let mut char_count = 0; for (char_count, (byte_pos, _)) in line_text.char_indices().enumerate() {
for (byte_pos, _) in line_text.char_indices() {
if char_count == character { if char_count == character {
byte_offset += byte_pos; byte_offset += byte_pos;
break; break;
} }
char_count += 1;
} }
break; break;
} }
@@ -123,13 +121,11 @@ pub fn get_semantic_hover_info(doc: &Document, line: usize, character: usize) ->
return None; return None;
} }
let mut char_count = 0; for (char_count, (byte_pos, _)) in line_text.char_indices().enumerate() {
for (byte_pos, _) in line_text.char_indices() {
if char_count == character { if char_count == character {
byte_offset += byte_pos; byte_offset += byte_pos;
break; break;
} }
char_count += 1;
} }
break; break;
} }
@@ -156,7 +152,7 @@ pub fn get_semantic_hover_info(doc: &Document, line: usize, character: usize) ->
let word = target_ident?; let word = target_ident?;
// Look up the symbol in the name table // Look up the symbol in the name table
let symbol_info = doc.name_table.lookup(&[word.clone()])?; let symbol_info = doc.name_table.lookup(std::slice::from_ref(&word))?;
// Find the declaration in the AST // Find the declaration in the AST
for decl in &ast.declarations { for decl in &ast.declarations {

View File

@@ -84,10 +84,7 @@ pub fn get_rename_edits(
new_text: params.new_name.clone(), new_text: params.new_name.clone(),
}; };
all_changes all_changes.entry(url.clone()).or_default().push(edit);
.entry(url.clone())
.or_default()
.push(edit);
} }
} }
} }

View File

@@ -83,7 +83,6 @@ fn find_identifiers_in_span(
/// Recursively highlight behavior tree nodes /// Recursively highlight behavior tree nodes
fn highlight_behavior_node( fn highlight_behavior_node(
builder: &mut SemanticTokensBuilder, builder: &mut SemanticTokensBuilder,
doc: &Document,
node: &crate::syntax::ast::BehaviorNode, node: &crate::syntax::ast::BehaviorNode,
) { ) {
use crate::syntax::ast::BehaviorNode; use crate::syntax::ast::BehaviorNode;
@@ -91,19 +90,18 @@ fn highlight_behavior_node(
match node { match node {
| BehaviorNode::Selector { children, .. } | BehaviorNode::Sequence { children, .. } => { | BehaviorNode::Selector { children, .. } | BehaviorNode::Sequence { children, .. } => {
for child in children { for child in children {
highlight_behavior_node(builder, doc, child); highlight_behavior_node(builder, child);
} }
}, },
| BehaviorNode::Action(action_name, params) => { | BehaviorNode::Action(_action_name, params) => {
// Action names don't have spans, so we'd need to search for them // Action names don't have spans, so we'd need to search for them
// For now, just highlight the parameters // For now, just highlight the parameters
for param in params { for param in params {
highlight_field(builder, param); highlight_field(builder, param);
} }
let _ = action_name; // Suppress warning
}, },
| BehaviorNode::Decorator { child, .. } => { | BehaviorNode::Decorator { child, .. } => {
highlight_behavior_node(builder, doc, child); highlight_behavior_node(builder, child);
}, },
| BehaviorNode::SubTree(_path) => { | BehaviorNode::SubTree(_path) => {
// SubTree references another behavior by path // SubTree references another behavior by path
@@ -161,7 +159,7 @@ pub fn get_semantic_tokens(doc: &Document) -> Option<SemanticTokensResult> {
&doc.text, &doc.text,
character.span.start, character.span.start,
character.span.end, character.span.end,
&[species.clone()], std::slice::from_ref(species),
); );
for (offset, species_name) in species_positions { for (offset, species_name) in species_positions {
@@ -322,7 +320,7 @@ pub fn get_semantic_tokens(doc: &Document) -> Option<SemanticTokensResult> {
// TODO: Traverse behavior tree to highlight conditions and actions // TODO: Traverse behavior tree to highlight conditions and actions
// Would need recursive function to walk BehaviorNode tree // Would need recursive function to walk BehaviorNode tree
highlight_behavior_node(&mut builder, doc, &behavior.root); highlight_behavior_node(&mut builder, &behavior.root);
}, },
| Declaration::Relationship(relationship) => { | Declaration::Relationship(relationship) => {
// Highlight relationship name as METHOD // Highlight relationship name as METHOD

View File

@@ -481,14 +481,14 @@ mod definition_tests {
}; };
let uri = Url::parse("file:///test.sb").unwrap(); let uri = Url::parse("file:///test.sb").unwrap();
let result = definition::get_definition(&mut doc, &params, &uri); let result = definition::get_definition(&doc, &params, &uri);
assert!(result.is_some()); assert!(result.is_some());
} }
#[test] #[test]
fn test_goto_definition_not_found() { fn test_goto_definition_not_found() {
let mut doc = Document::new("character Alice {}".to_string()); let doc = Document::new("character Alice {}".to_string());
let params = GotoDefinitionParams { let params = GotoDefinitionParams {
text_document_position_params: TextDocumentPositionParams { text_document_position_params: TextDocumentPositionParams {
@@ -505,7 +505,7 @@ mod definition_tests {
}; };
let uri = Url::parse("file:///test.sb").unwrap(); let uri = Url::parse("file:///test.sb").unwrap();
let result = definition::get_definition(&mut doc, &params, &uri); let result = definition::get_definition(&doc, &params, &uri);
assert!(result.is_none()); assert!(result.is_none());
} }
@@ -542,7 +542,7 @@ mod references_tests {
}; };
let uri = Url::parse("file:///test.sb").unwrap(); let uri = Url::parse("file:///test.sb").unwrap();
let result = references::find_references(&mut doc, &params, &uri); let result = references::find_references(&doc, &params, &uri);
assert!(result.is_some()); assert!(result.is_some());
let locations = result.unwrap(); let locations = result.unwrap();
@@ -576,7 +576,7 @@ mod references_tests {
}; };
let uri = Url::parse("file:///test.sb").unwrap(); let uri = Url::parse("file:///test.sb").unwrap();
let result = references::find_references(&mut doc, &params, &uri); let result = references::find_references(&doc, &params, &uri);
let locations = result.unwrap(); let locations = result.unwrap();
// Should only find "Alice", not "Alicia" // Should only find "Alice", not "Alicia"

View File

@@ -105,14 +105,16 @@ pub enum Priority {
Critical, Critical,
} }
impl Priority { impl std::str::FromStr for Priority {
pub fn from_str(s: &str) -> Option<Self> { type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s { match s {
| "low" => Some(Priority::Low), | "low" => Ok(Priority::Low),
| "normal" => Some(Priority::Normal), | "normal" => Ok(Priority::Normal),
| "high" => Some(Priority::High), | "high" => Ok(Priority::High),
| "critical" => Some(Priority::Critical), | "critical" => Ok(Priority::Critical),
| _ => None, | _ => Err(()),
} }
} }
} }
@@ -126,7 +128,7 @@ pub struct Character {
pub template: Option<Vec<String>>, // `from Template1, Template2` pub template: Option<Vec<String>>, // `from Template1, Template2`
pub uses_behaviors: Option<Vec<BehaviorLink>>, // `uses behaviors: [...]` pub uses_behaviors: Option<Vec<BehaviorLink>>, // `uses behaviors: [...]`
pub uses_schedule: Option<Vec<String>>, /* `uses schedule: ScheduleName` or `uses schedules: pub uses_schedule: Option<Vec<String>>, /* `uses schedule: ScheduleName` or `uses schedules:
* [...]` */ * [...]` */
pub span: Span, pub span: Span,
} }

View File

@@ -31,6 +31,14 @@ pub enum Token {
Species, Species,
#[token("enum")] #[token("enum")]
Enum, Enum,
#[token("concept")]
Concept,
#[token("sub_concept")]
SubConcept,
#[token("concept_comparison")]
ConceptComparison,
#[token("any")]
Any,
#[token("state")] #[token("state")]
State, State,
#[token("on")] #[token("on")]
@@ -509,4 +517,21 @@ Second prose block content.
vec![Token::IntLit(20), Token::DotDot, Token::IntLit(40),] vec![Token::IntLit(20), Token::DotDot, Token::IntLit(40),]
); );
} }
#[test]
fn test_type_system_keywords() {
let input = "concept sub_concept concept_comparison any";
let lexer = Lexer::new(input);
let tokens: Vec<Token> = lexer.map(|(_, tok, _)| tok).collect();
assert_eq!(
tokens,
vec![
Token::Concept,
Token::SubConcept,
Token::ConceptComparison,
Token::Any,
]
);
}
} }

View File

@@ -24,7 +24,7 @@ fn load_example(name: &str) -> Project {
assert!(path.exists(), "Example '{}' not found at {:?}", name, path); assert!(path.exists(), "Example '{}' not found at {:?}", name, path);
Project::load(&path).expect(&format!("Failed to load example '{}'", name)) Project::load(&path).unwrap_or_else(|_| panic!("Failed to load example '{}'", name))
} }
// ============================================================================ // ============================================================================