-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #241 from SteveL-MSFT/tree-sitter-parse
Implement use of tree-sitter parser
- Loading branch information
Showing
18 changed files
with
744 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
use base64::{Engine as _, engine::general_purpose}; | ||
|
||
use crate::DscError; | ||
use crate::parser::functions::{FunctionArg, FunctionResult}; | ||
use super::{Function, AcceptedArgKind}; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct Base64 {} | ||
|
||
impl Function for Base64 { | ||
fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> { | ||
vec![AcceptedArgKind::String] | ||
} | ||
|
||
fn min_args(&self) -> usize { | ||
1 | ||
} | ||
|
||
fn max_args(&self) -> usize { | ||
1 | ||
} | ||
|
||
fn invoke(&self, args: &[FunctionArg]) -> Result<FunctionResult, DscError> { | ||
let FunctionArg::String(arg) = args.get(0).unwrap() else { | ||
return Err(DscError::Parser("Invalid argument type".to_string())); | ||
}; | ||
Ok(FunctionResult::String(general_purpose::STANDARD.encode(arg))) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::parser::Statement; | ||
|
||
#[test] | ||
fn strings() { | ||
let mut parser = Statement::new().unwrap(); | ||
let result = parser.parse_and_execute("[base64('hello world')]").unwrap(); | ||
assert_eq!(result, "aGVsbG8gd29ybGQ="); | ||
} | ||
|
||
#[test] | ||
fn numbers() { | ||
let mut parser = Statement::new().unwrap(); | ||
let result = parser.parse_and_execute("[base64(123)]"); | ||
assert!(result.is_err()); | ||
} | ||
|
||
#[test] | ||
fn nested() { | ||
let mut parser = Statement::new().unwrap(); | ||
let result = parser.parse_and_execute("[base64(base64('hello world'))]").unwrap(); | ||
assert_eq!(result, "YUdWc2JHOGdkMjl5YkdRPQ=="); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
use crate::DscError; | ||
use crate::functions::{Function, FunctionArg, FunctionResult, AcceptedArgKind}; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct Concat {} | ||
|
||
impl Function for Concat { | ||
fn min_args(&self) -> usize { | ||
2 | ||
} | ||
|
||
fn max_args(&self) -> usize { | ||
usize::MAX | ||
} | ||
|
||
fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> { | ||
vec![AcceptedArgKind::String, AcceptedArgKind::Integer] | ||
} | ||
|
||
fn invoke(&self, args: &[FunctionArg]) -> Result<FunctionResult, DscError> { | ||
let mut result = String::new(); | ||
for arg in args { | ||
match arg { | ||
FunctionArg::String(value) => { | ||
result.push_str(value); | ||
}, | ||
FunctionArg::Integer(value) => { | ||
result.push_str(&value.to_string()); | ||
}, | ||
_ => { | ||
return Err(DscError::Parser("Invalid argument type".to_string())); | ||
} | ||
} | ||
} | ||
Ok(FunctionResult::String(result)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::parser::Statement; | ||
|
||
#[test] | ||
fn strings() { | ||
let mut parser = Statement::new().unwrap(); | ||
let result = parser.parse_and_execute("[concat('a', 'b')]").unwrap(); | ||
assert_eq!(result, "ab"); | ||
} | ||
|
||
#[test] | ||
fn strings_with_spaces() { | ||
let mut parser = Statement::new().unwrap(); | ||
let result = parser.parse_and_execute("[concat('a ', ' ', ' b')]").unwrap(); | ||
assert_eq!(result, "a b"); | ||
} | ||
|
||
#[test] | ||
fn numbers() { | ||
let mut parser = Statement::new().unwrap(); | ||
let result = parser.parse_and_execute("[concat(1, 2)]").unwrap(); | ||
assert_eq!(result, "12"); | ||
} | ||
|
||
#[test] | ||
fn string_and_numbers() { | ||
let mut parser = Statement::new().unwrap(); | ||
let result = parser.parse_and_execute("[concat('a', 1, 'b', 2)]").unwrap(); | ||
assert_eq!(result, "a1b2"); | ||
} | ||
|
||
#[test] | ||
fn nested() { | ||
let mut parser = Statement::new().unwrap(); | ||
let result = parser.parse_and_execute("[concat('a', concat('b', 'c'), 'd')]").unwrap(); | ||
assert_eq!(result, "abcd"); | ||
} | ||
|
||
#[test] | ||
fn invalid_one_parameter() { | ||
let mut parser = Statement::new().unwrap(); | ||
let result = parser.parse_and_execute("[concat('a')]"); | ||
assert!(result.is_err()); | ||
} | ||
} |
Oops, something went wrong.