Skip to content

Commit

Permalink
change to use statement as byte array where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveL-MSFT committed Oct 22, 2023
1 parent 17f0344 commit 875a6f3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
6 changes: 3 additions & 3 deletions dsc_lib/src/parser/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, DscError> {
pub fn new(function_dispatcher: &'a FunctionDispatcher, statement_bytes: &[u8], expression: &Node) -> Result<Self, DscError> {
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()));
Expand All @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions dsc_lib/src/parser/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, DscError> {
pub fn new(function_dispatcher: &'a FunctionDispatcher, statement_bytes: &[u8], function: &Node) -> Result<Self, DscError> {
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})
}

Expand Down Expand Up @@ -81,7 +81,7 @@ impl<'a> Function<'a> {
}
}

fn convert_args_node<'a>(function_dispatcher: &'a FunctionDispatcher, statement: &str, args: &Option<Node>) -> Result<Option<Vec<FunctionArg<'a>>>, DscError> {
fn convert_args_node<'a>(function_dispatcher: &'a FunctionDispatcher, statement_bytes: &[u8], args: &Option<Node>) -> Result<Option<Vec<FunctionArg<'a>>>, DscError> {
let Some(args) = args else {
return Ok(None);
};
Expand All @@ -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::<i32>()?));
},
"boolean" => {
let value = arg.utf8_text(statement.as_bytes())?;
let value = arg.utf8_text(statement_bytes)?;
result.push(FunctionArg::Boolean(value.parse::<bool>()?));
},
"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));
},
_ => {
Expand Down
7 changes: 4 additions & 3 deletions dsc_lib/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?)
},
_ => {
Expand Down

0 comments on commit 875a6f3

Please sign in to comment.