futilehdl/src/parser/adt.rs

55 lines
1.3 KiB
Rust
Raw Normal View History

2022-02-02 23:54:50 +00:00
use super::tokens::{token, Token, TokenKind as tk, TokenSpan};
use super::IResult;
2022-02-03 00:55:12 +00:00
use nom::multi::separated_list0;
2022-02-02 23:54:50 +00:00
use nom::sequence::tuple;
use nom::{
2022-02-03 00:55:12 +00:00
combinator::{cut, map, opt},
2022-02-02 23:54:50 +00:00
sequence::{delimited, preceded},
};
2022-02-03 00:55:12 +00:00
#[derive(Debug)]
2022-02-02 23:54:50 +00:00
pub struct StateBlock<'a> {
2022-02-03 00:55:12 +00:00
pub name: Token<'a>,
pub variants: Vec<StateVariant<'a>>,
}
#[derive(Debug)]
pub struct StateVariant<'a> {
pub name: Token<'a>,
pub params: Option<Vec<Token<'a>>>,
}
fn state_variant(input: TokenSpan) -> IResult<TokenSpan, StateVariant> {
map(
tuple((
token(tk::Ident),
opt(delimited(
token(tk::LParen),
separated_list0(token(tk::Comma), token(tk::Ident)),
token(tk::RParen),
)),
)),
|(name, param)| StateVariant {
2022-04-05 11:36:00 +00:00
name,
2022-02-03 00:55:12 +00:00
params: param,
},
)(input)
2022-02-02 23:54:50 +00:00
}
pub fn state(input: TokenSpan) -> IResult<TokenSpan, StateBlock> {
map(
preceded(
token(tk::State),
cut(tuple((
token(tk::Ident),
2022-02-03 00:55:12 +00:00
delimited(
token(tk::LBrace),
separated_list0(token(tk::Comma), state_variant),
token(tk::RBrace),
),
2022-02-02 23:54:50 +00:00
))),
),
2022-02-03 00:55:12 +00:00
|(name, variants)| StateBlock { name, variants },
2022-02-02 23:54:50 +00:00
)(input)
}