diff --git a/dsc_lib/src/parser/expressions.rs b/dsc_lib/src/parser/expressions.rs index 9778e8a51..cefaa2291 100644 --- a/dsc_lib/src/parser/expressions.rs +++ b/dsc_lib/src/parser/expressions.rs @@ -25,11 +25,11 @@ impl<'a> Expression<'a> { /// # Errors /// /// This function will return an error if the expression node is not valid. - pub fn new(function_dispatcher: &'a FunctionDispatcher, statement: &str, expression: &Node) -> Result { + pub fn new(function_dispatcher: &'a FunctionDispatcher, statement_bytes: &[u8], expression: &Node) -> Result { let Some(function) = expression.child_by_field_name("function") else { return Err(DscError::Parser("Function node not found".to_string())); }; - let function = Function::new(function_dispatcher, statement, &function)?; + let function = Function::new(function_dispatcher, statement_bytes, &function)?; let member_access = if let Some(member_access) = expression.child_by_field_name("members") { if member_access.is_error() { return Err(DscError::Parser("Error parsing dot-notation".to_string())); @@ -40,7 +40,7 @@ impl<'a> Expression<'a> { if member.is_error() { return Err(DscError::Parser("Error parsing dot-notation member".to_string())); } - let value = member.utf8_text(statement.as_bytes())?; + let value = member.utf8_text(statement_bytes)?; result.push(value.to_string()); } Some(result) diff --git a/dsc_lib/src/parser/functions.rs b/dsc_lib/src/parser/functions.rs index f5ce42564..25c63c4d4 100644 --- a/dsc_lib/src/parser/functions.rs +++ b/dsc_lib/src/parser/functions.rs @@ -43,15 +43,15 @@ impl<'a> Function<'a> { /// # Errors /// /// This function will return an error if the function node is not valid. - pub fn new(function_dispatcher: &'a FunctionDispatcher, statement: &str, function: &Node) -> Result { + pub fn new(function_dispatcher: &'a FunctionDispatcher, statement_bytes: &[u8], function: &Node) -> Result { let Some(function_name) = function.child_by_field_name("name") else { return Err(DscError::Parser("Function name node not found".to_string())); }; let function_args = function.child_by_field_name("args"); - let args = convert_args_node(function_dispatcher, statement, &function_args)?; + let args = convert_args_node(function_dispatcher, statement_bytes, &function_args)?; Ok(Function{ function_dispatcher, - name: function_name.utf8_text(statement.as_bytes())?.to_string(), + name: function_name.utf8_text(statement_bytes)?.to_string(), args}) } @@ -81,7 +81,7 @@ impl<'a> Function<'a> { } } -fn convert_args_node<'a>(function_dispatcher: &'a FunctionDispatcher, statement: &str, args: &Option) -> Result>>, DscError> { +fn convert_args_node<'a>(function_dispatcher: &'a FunctionDispatcher, statement_bytes: &[u8], args: &Option) -> Result>>, DscError> { let Some(args) = args else { return Ok(None); }; @@ -90,20 +90,20 @@ fn convert_args_node<'a>(function_dispatcher: &'a FunctionDispatcher, statement: for arg in args.named_children(&mut cursor) { match arg.kind() { "string" => { - let value = arg.utf8_text(statement.as_bytes())?; + let value = arg.utf8_text(statement_bytes)?; result.push(FunctionArg::String(value.to_string())); }, "number" => { - let value = arg.utf8_text(statement.as_bytes())?; + let value = arg.utf8_text(statement_bytes)?; result.push(FunctionArg::Integer(value.parse::()?)); }, "boolean" => { - let value = arg.utf8_text(statement.as_bytes())?; + let value = arg.utf8_text(statement_bytes)?; result.push(FunctionArg::Boolean(value.parse::()?)); }, "expression" => { // TODO: this is recursive, we may want to stop at a specific depth - let expression = Expression::new(function_dispatcher, statement, &arg)?; + let expression = Expression::new(function_dispatcher, statement_bytes, &arg)?; result.push(FunctionArg::Expression(expression)); }, _ => { diff --git a/dsc_lib/src/parser/mod.rs b/dsc_lib/src/parser/mod.rs index c144facc7..79faa03f0 100644 --- a/dsc_lib/src/parser/mod.rs +++ b/dsc_lib/src/parser/mod.rs @@ -59,22 +59,23 @@ impl Statement { return Err(DscError::Parser("Error parsing statement child".to_string())); } let kind = child_node.kind(); + let statement_bytes = statement.as_bytes(); match kind { "stringLiteral" | "bracketInStringLiteral" => { - let Ok(value) = child_node.utf8_text(statement.as_bytes()) else { + let Ok(value) = child_node.utf8_text(statement_bytes) else { return Err(DscError::Parser("Error parsing string literal".to_string())); }; Ok(value.to_string()) }, "escapedStringLiteral" => { // need to remove the first character: [[ => [ - let Ok(value) = child_node.utf8_text(statement.as_bytes()) else { + let Ok(value) = child_node.utf8_text(statement_bytes) else { return Err(DscError::Parser("Error parsing escaped string literal".to_string())); }; Ok(value[1..].to_string()) }, "expression" => { - let expression = Expression::new(&self.function_dispatcher, statement, &child_node)?; + let expression = Expression::new(&self.function_dispatcher, statement_bytes, &child_node)?; Ok(expression.invoke()?) }, _ => {