MCPcopy Index your code
hub / github.com/PowerShell/DSC / parse_and_execute

Method parse_and_execute

lib/dsc-lib/src/parser/mod.rs:47–100  ·  view source on GitHub ↗

Parse and execute a statement. # Arguments `statement` - The statement to parse and execute. # Errors This function will return an error if the statement fails to parse or execute.

(&mut self, statement: &str, context: &Context)

Source from the content-addressed store, hash-verified

45 ///
46 /// This function will return an error if the statement fails to parse or execute.
47 pub fn parse_and_execute(&mut self, statement: &str, context: &Context) -> Result<Value, DscError> {
48 debug!("{}", t!("parser.parsingStatement", statement = statement));
49 if context.process_mode == ProcessMode::NoExpressionEvaluation {
50 debug!("{}", t!("parser.skippingExpressionProcessing"));
51 return Ok(Value::String(statement.to_string()));
52 }
53
54 let Some(tree) = &mut self.parser.parse(statement, None) else {
55 return Err(DscError::Parser(t!("parser.failedToParse", statement = statement).to_string()));
56 };
57 let root_node = tree.root_node();
58 if root_node.is_error() {
59 return Err(DscError::Parser(t!("parser.failedToParseRoot", statement = statement).to_string()));
60 }
61 if root_node.kind() != "statement" {
62 return Err(DscError::Parser(t!("parser.invalidStatement", statement = statement).to_string()));
63 }
64 let statement_bytes = statement.as_bytes();
65 let mut cursor = root_node.walk();
66 let mut return_value = Value::Null;
67 for child_node in root_node.named_children(&mut cursor) {
68 if child_node.is_error() {
69 return Err(DscError::Parser(t!("parser.failedToParse", statement = statement).to_string()));
70 }
71
72 match child_node.kind() {
73 "stringLiteral" => {
74 let Ok(value) = child_node.utf8_text(statement_bytes) else {
75 return Err(DscError::Parser(t!("parser.failedToParseStringLiteral").to_string()));
76 };
77 debug!("{}", t!("parser.parsingStringLiteral", value = value.to_string()));
78 return_value = Value::String(value.to_string());
79 },
80 "escapedStringLiteral" => {
81 // need to remove the first character: [[ => [
82 let Ok(value) = child_node.utf8_text(statement_bytes) else {
83 return Err(DscError::Parser(t!("parser.failedToParseEscapedStringLiteral").to_string()));
84 };
85 debug!("{}", t!("parser.parsingEscapedStringLiteral", value = value[1..].to_string()));
86 return_value = Value::String(value[1..].to_string());
87 },
88 "expression" => {
89 debug!("{}", t!("parser.parsingExpression"));
90 let expression = Expression::new(statement_bytes, &child_node)?;
91 return_value = expression.invoke(&self.function_dispatcher, context)?;
92 },
93 _ => {
94 return Err(DscError::Parser(t!("parser.unknownExpressionType", kind = child_node.kind()).to_string()));
95 }
96 }
97 }
98
99 Ok(return_value)
100 }
101}
102
103#[cfg(test)]

Callers 15

string_literalFunction · 0.80
bracket_stringFunction · 0.80
bracket_in_stringFunction · 0.80
invalid_functionFunction · 0.80
empty_parameterFunction · 0.80
invalid_conditionFunction · 0.80
condition_trueFunction · 0.80
condition_falseFunction · 0.80
valid_variableFunction · 0.80
invalid_resourceidFunction · 0.80

Calls 3

newFunction · 0.50
parseMethod · 0.45
invokeMethod · 0.45

Tested by 15

string_literalFunction · 0.64
bracket_stringFunction · 0.64
bracket_in_stringFunction · 0.64
invalid_functionFunction · 0.64
empty_parameterFunction · 0.64
invalid_conditionFunction · 0.64
condition_trueFunction · 0.64
condition_falseFunction · 0.64
valid_variableFunction · 0.64
invalid_resourceidFunction · 0.64