From 2fe13e36375f70729150acc4a2aca545695d602d Mon Sep 17 00:00:00 2001 From: lmittmann Date: Thu, 17 Oct 2024 23:27:06 +0200 Subject: [PATCH] parser: added include --- crates/ast/src/parser.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/crates/ast/src/parser.rs b/crates/ast/src/parser.rs index 75a7074..308a75e 100644 --- a/crates/ast/src/parser.rs +++ b/crates/ast/src/parser.rs @@ -57,12 +57,20 @@ impl<'tokens, 'src: 'tokens, P, T> Parser<'tokens, 'src, T> for P where } fn root<'tokens, 'src: 'tokens>() -> impl Parser<'tokens, 'src, ast::Root<'src>> { - definition() + root_section() .repeated() .collect::>() .map(|defs| ast::Root(defs.into_boxed_slice())) } +fn root_section<'tokens, 'src: 'tokens>() -> impl Parser<'tokens, 'src, ast::RootSection<'src>> { + let include = just(Keyword("include")) + .ignore_then(select! {String(s) => s}.map_with(|s, ex| (s, ex.span()))) + .map(ast::RootSection::Include); + + choice((definition().map(ast::RootSection::Definition), include)) +} + fn definition<'tokens, 'src: 'tokens>() -> impl Parser<'tokens, 'src, ast::Definition<'src>> { just(Keyword("define")).ignore_then(choice(( r#macro(), @@ -472,6 +480,25 @@ mod tests { assert_err!(code(), vec![Hex("0x0")], "odd length"); } + #[test] + fn parse_root_section() { + let span: Span = SimpleSpan::new(0, 0); + + assert_ok!( + root_section(), + vec![Keyword("include"), String("test")], + ast::RootSection::Include(("test", span)) + ); + assert_ok!( + root_section(), + vec![Keyword("define"), Ident("constant"), Ident("TEST"), Punct('='), Hex("0x1")], + ast::RootSection::Definition(ast::Definition::Constant { + name: ("TEST", span), + value: uint!(1_U256) + }) + ); + } + #[test] fn parse_macro() { let span: Span = SimpleSpan::new(0, 0);