79 lines
2.0 KiB
Rust
79 lines
2.0 KiB
Rust
use super::declaration::{declaration, NetDecl};
|
|
use super::tokens::{token, Token, TokenKind as tk, TokenSpan};
|
|
use super::IResult;
|
|
use nom::multi::separated_list0;
|
|
use nom::sequence::tuple;
|
|
use nom::{
|
|
combinator::{cut, map, opt},
|
|
sequence::{delimited, preceded},
|
|
};
|
|
|
|
#[derive(Debug)]
|
|
pub struct StateBlock<'a> {
|
|
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>>>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct StructBlock<'a> {
|
|
pub name: Token<'a>,
|
|
pub members: Vec<NetDecl<'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 {
|
|
name,
|
|
params: param,
|
|
},
|
|
)(input)
|
|
}
|
|
|
|
pub fn state(input: TokenSpan) -> IResult<TokenSpan, StateBlock> {
|
|
map(
|
|
preceded(
|
|
token(tk::State),
|
|
cut(tuple((
|
|
token(tk::Ident),
|
|
delimited(
|
|
token(tk::LBrace),
|
|
separated_list0(token(tk::Comma), state_variant),
|
|
token(tk::RBrace),
|
|
),
|
|
))),
|
|
),
|
|
|(name, variants)| StateBlock { name, variants },
|
|
)(input)
|
|
}
|
|
|
|
pub fn struct_def(input: TokenSpan) -> IResult<TokenSpan, StructBlock> {
|
|
map(
|
|
preceded(
|
|
token(tk::Struct),
|
|
cut(tuple((
|
|
token(tk::Ident),
|
|
delimited(
|
|
token(tk::LBrace),
|
|
separated_list0(token(tk::Comma), declaration),
|
|
token(tk::RBrace),
|
|
),
|
|
))),
|
|
),
|
|
|(name, members)| StructBlock { name, members },
|
|
)(input)
|
|
}
|