-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
33 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
0.6.2 | ||
----- | ||
Make decorators work like functions | ||
|
||
0.6.1 | ||
----- | ||
Remove debug output | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,26 +1,34 @@ | ||
use lavendeux_parser::{ParserState, ParserError, Token, Value}; | ||
use lavendeux_parser::{FunctionDefinition, FunctionArgument}; | ||
use lavendeux_parser::{DecoratorDefinition, FunctionDefinition, FunctionArgument}; | ||
use lavendeux_parser::errors::*; | ||
|
||
const ECHO : FunctionDefinition = FunctionDefinition { | ||
name: "echo", | ||
description: "Echo back the provided input", | ||
arguments: || vec![ | ||
FunctionArgument::new_required("input", ExpectedTypes::String), | ||
], | ||
handler: |_, args: &[Value]| { | ||
Ok(Value::String(args[0].as_string())) | ||
} | ||
}; | ||
|
||
fn main() -> Result<(), ParserError> { | ||
// Load the extensions into our parser | ||
let mut state : ParserState = ParserState::new(); | ||
state.functions.register(ECHO); | ||
|
||
// Register a new function | ||
state.functions.register(FunctionDefinition { | ||
name: "echo", | ||
description: "Echo back the provided input", | ||
arguments: || vec![ | ||
FunctionArgument::new_required("input", ExpectedTypes::String), | ||
], | ||
handler: |_, args: &[Value]| { | ||
Ok(Value::String(args[0].as_string())) | ||
} | ||
}); | ||
|
||
// Register a new decorator | ||
state.decorators.register(DecoratorDefinition { | ||
name: &["upper", "uppercase"], | ||
description: "Outputs an uppercase version of the input", | ||
argument: ExpectedTypes::Any, | ||
handler: |_, input| Ok(input.as_string().to_uppercase()) | ||
}); | ||
|
||
// Now we can use the new functions and @decorators | ||
let token = Token::new("echo(5)", &mut state)?; | ||
assert_eq!(token.text(), "5"); | ||
let token = Token::new("echo('test') @upper", &mut state)?; | ||
assert_eq!(token.text(), "TEST"); | ||
|
||
Ok(()) | ||
} |
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