Skip to content

Commit

Permalink
#include
Browse files Browse the repository at this point in the history
* added RootSection

* added string token

* parser: added include

---------

Co-authored-by: lmittmann <[email protected]>
  • Loading branch information
lmittmann and lmittmann authored Oct 17, 2024
1 parent 56d1765 commit b2d11e9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
8 changes: 7 additions & 1 deletion crates/ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ use chumsky::span::SimpleSpan;
use evm_glue::opcodes::Opcode;

#[derive(Debug, PartialEq, Eq)]
pub struct Root<'src>(pub Box<[Definition<'src>]>);
pub struct Root<'src>(pub Box<[RootSection<'src>]>);

#[derive(Debug, PartialEq, Eq)]
pub enum RootSection<'src> {
Definition(Definition<'src>),
Include(Spanned<&'src str>),
}

#[derive(Debug, PartialEq, Eq)]
pub enum Definition<'src> {
Expand Down
21 changes: 19 additions & 2 deletions crates/ast/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub enum Token<'src> {
Dec(&'src str),
Hex(&'src str),
Bin(&'src str),
String(&'src str),

Error(char),
}
Expand All @@ -39,7 +40,8 @@ impl fmt::Display for Token<'_> {
| Token::Ident(s)
| Token::Dec(s)
| Token::Hex(s)
| Token::Bin(s) => write!(f, "{}", s),
| Token::Bin(s)
| Token::String(s) => write!(f, "{}", s),
Token::Punct(c) | Token::Error(c) => write!(f, "{}", c),
}
}
Expand Down Expand Up @@ -83,7 +85,12 @@ fn lexer<'src>(
.to_slice()
.map(Token::Dec);

let token = choice((keyword, ident, punct, hex, bin, dec));
let string = just("\"")
.ignore_then(any().and_is(just("\"").not()).repeated().to_slice())
.then_ignore(just("\""))
.map(Token::String);

let token = choice((keyword, ident, punct, hex, bin, dec, string));

// comments
let single_line_comment = just("//")
Expand Down Expand Up @@ -185,4 +192,14 @@ mod tests {
assert_ok!("0b101", (Token::Bin("0b101"), SimpleSpan::new(0, 5)));
assert_ok!("0b0", (Token::Bin("0b0"), SimpleSpan::new(0, 3)));
}

#[test]
fn lex_string() {
assert_ok!("\"\"", (Token::String(""), SimpleSpan::new(0, 2)));
assert_ok!("\"foo\"", (Token::String("foo"), SimpleSpan::new(0, 5)));
assert_ok!(
"\"foo bar\"",
(Token::String("foo bar"), SimpleSpan::new(0, 9))
);
}
}
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 b2d11e9

Please sign in to comment.