Skip to content

Commit

Permalink
parser: added include
Browse files Browse the repository at this point in the history
  • Loading branch information
lmittmann committed Oct 17, 2024
1 parent 470c704 commit 2fe13e3
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion crates/ast/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()
.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(),
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2fe13e3

Please sign in to comment.