Annotations

This commit is contained in:
Greg Shuflin 2021-11-18 18:31:33 -08:00
parent 33573bf268
commit 65c745fb30
2 changed files with 21 additions and 4 deletions

View File

@ -129,7 +129,24 @@ fn statement(input: Span) -> ParseResult<Statement> {
} }
fn declaration(input: Span) -> ParseResult<Declaration> { fn declaration(input: Span) -> ParseResult<Declaration> {
alt((binding, type_decl, func, module))(input) alt((binding, type_decl, func, annotation, module))(input)
}
fn annotation(input: Span) -> ParseResult<Declaration> {
map(
tuple((
tok(char('@')),
identifier,
opt(delimited(tok(char('(')), separated_list1(tok(char(',')), expression), tok(char(')')))),
statement_delimiter,
statement,
)),
|(_, name, args, _, inner)| Declaration::Annotation {
name: rc_string(name.fragment()),
arguments: if let Some(args) = args { args } else { vec![] },
inner: Box::new(inner),
},
)(input)
} }
fn func(input: Span) -> ParseResult<Declaration> { fn func(input: Span) -> ParseResult<Declaration> {

View File

@ -999,7 +999,7 @@ fn annotations() {
vec![].into(), vec![].into(),
)); ));
assert_ast! { assert_ast_comb! {
r#" r#"
@test_annotation @test_annotation
fn some_function() { fn some_function() {
@ -1013,7 +1013,7 @@ fn annotations() {
] ]
}; };
assert_ast! { assert_ast_comb! {
r#" r#"
@test_annotation(some,value) @test_annotation(some,value)
@another_annotation @another_annotation
@ -1033,7 +1033,7 @@ fn annotations() {
#[test] #[test]
fn modules() { fn modules() {
assert_ast! { assert_ast_comb! {
r#" r#"
module ephraim { module ephraim {
let mut a = 10 let mut a = 10