diff --git a/calc-example/calc/src/ir.rs b/calc-example/calc/src/ir.rs index 7b422df5..773b8b67 100644 --- a/calc-example/calc/src/ir.rs +++ b/calc-example/calc/src/ir.rs @@ -1,6 +1,14 @@ use ordered_float::OrderedFloat; use salsa::debug::DebugWithDb; +// ANCHOR: input +#[salsa::input] +pub struct SourceProgram { + #[return_ref] + text: String, +} +// ANCHOR_END: input + // ANCHOR: interned_ids #[salsa::interned] #[derive(Eq, PartialEq, Clone, Hash)] diff --git a/calc-example/calc/src/main.rs b/calc-example/calc/src/main.rs index a75e3a00..905d3d5a 100644 --- a/calc-example/calc/src/main.rs +++ b/calc-example/calc/src/main.rs @@ -1,6 +1,7 @@ // ANCHOR: jar_struct #[salsa::jar(db = Db)] pub struct Jar( + crate::ir::SourceProgram, crate::ir::VariableId, crate::ir::FunctionId, crate::ir::Expression, @@ -8,7 +9,6 @@ pub struct Jar( crate::ir::Function, crate::ir::Diagnostics, crate::parser::parse_statements, - crate::parser::source_text, ); // ANCHOR_END: jar_struct diff --git a/calc-example/calc/src/parser.rs b/calc-example/calc/src/parser.rs index 032e1fe3..1a2e78b7 100644 --- a/calc-example/calc/src/parser.rs +++ b/calc-example/calc/src/parser.rs @@ -1,22 +1,15 @@ use ordered_float::OrderedFloat; use crate::ir::{ - Diagnostic, Diagnostics, Expression, ExpressionData, Function, FunctionId, Op, Statement, - StatementData, VariableId, + Diagnostic, Diagnostics, Expression, ExpressionData, Function, FunctionId, Op, SourceProgram, + Statement, StatementData, VariableId, }; -// ANCHOR: source_text -#[salsa::memoized(return_ref)] -pub fn source_text(_db: &dyn crate::Db) -> String { - panic!("input") -} -// ANCHOR_END: source_text - // ANCHOR: parse_statements #[salsa::memoized(return_ref)] -pub fn parse_statements(db: &dyn crate::Db) -> Vec { +pub fn parse_statements(db: &dyn crate::Db, source: SourceProgram) -> Vec { // Get the source text from the database - let source_text = source_text(db); + let source_text = source.text(db); // Create the parser let mut parser = Parser { @@ -339,14 +332,14 @@ fn parse_string(source_text: &str) -> String { // Create the database let mut db = crate::db::Database::default(); - // Set the source_text value - source_text::set(&mut db, source_text.to_string()); + // Create the source program + let source_program = SourceProgram::new(&mut db, source_text.to_string()); // Invoke the parser - let statements = parse_statements(&db); + let statements = parse_statements(&db, source_program); // Read out any diagnostics - let accumulated = parse_statements::accumulated::(&db); + let accumulated = parse_statements::accumulated::(&db, source_program); // Format the result as a string and return it format!("{:#?}", (statements, accumulated).debug(&db))