Compare commits

...

2 Commits

Author SHA1 Message Date
NotAFile c1dc524cab clippy fix 2022-02-03 00:55:40 +01:00
NotAFile aadf044f9d prepare for removing module syntax 2022-02-03 00:54:50 +01:00
5 changed files with 81 additions and 47 deletions

30
src/parser/adt.rs Normal file
View File

@ -0,0 +1,30 @@
use super::declaration::{declaration, NetDecl};
use super::tokens::{token, Token, TokenKind as tk, TokenSpan};
use super::IResult;
use nom::multi::many0;
use nom::sequence::tuple;
use nom::{
combinator::{cut, map},
sequence::{delimited, preceded},
};
pub struct StateBlock<'a> {
name: Token<'a>,
items: Vec<NetDecl<'a>>,
}
pub fn state(input: TokenSpan) -> IResult<TokenSpan, StateBlock> {
map(
preceded(
token(tk::State),
cut(tuple((
token(tk::Ident),
delimited(token(tk::LBrace), many0(declaration), token(tk::RBrace)),
))),
),
|(name, items)| StateBlock {
name: name.clone(),
items,
},
)(input)
}

37
src/parser/comb.rs Normal file
View File

@ -0,0 +1,37 @@
use super::module::inputs_list;
use super::tokens::TokenKind as tk;
use crate::parser::tokens::token;
use crate::parser::typename;
use crate::parser::IResult;
use crate::parser::Module;
use crate::parser::TokenSpan;
use nom::combinator::cut;
use nom::combinator::map;
use nom::multi::many0;
use nom::sequence::delimited;
use nom::sequence::preceded;
use nom::sequence::tuple;
pub fn comb_block(input: TokenSpan) -> IResult<TokenSpan, Module> {
map(
preceded(
token(tk::Comb),
cut(tuple((
token(tk::Ident),
delimited(token(tk::LParen), inputs_list, token(tk::RParen)),
preceded(token(tk::RArrow), typename),
delimited(
token(tk::LBrace),
many0(token(tk::Error)),
token(tk::RBrace),
),
))),
),
|(name, inputs, _ret, _items)| Module {
// TODO: bring back returns
name: name.span(),
ports: inputs,
items: todo!(),
},
)(input)
}

View File

@ -1,3 +1,5 @@
pub mod adt;
pub mod comb;
pub mod declaration;
pub mod error;
pub mod expression;

View File

@ -1,16 +1,10 @@
use nom::{
branch::alt,
combinator::{cut, map},
error::context,
multi::{many0, separated_list0},
sequence::{delimited, preceded, tuple},
};
use nom::{combinator::map, multi::separated_list0};
use crate::parser::{
assign_statement, declaration,
proc::{proc_block, ProcBlock},
declaration,
proc::ProcBlock,
tokens::{token, TokenKind as tk, TokenSpan},
typename, Assign, IResult, NetDecl, Span,
Assign, IResult, NetDecl, Span,
};
#[derive(Debug)]
@ -45,46 +39,12 @@ fn port_decl(i: TokenSpan) -> IResult<TokenSpan, PortDecl> {
})(i)
}
fn inputs_list(input: TokenSpan) -> IResult<TokenSpan, Vec<PortDecl>> {
pub fn inputs_list(input: TokenSpan) -> IResult<TokenSpan, Vec<PortDecl>> {
separated_list0(token(tk::Comma), port_decl)(input)
}
fn assign_item(input: TokenSpan) -> IResult<TokenSpan, Assign> {
context(
"assignment",
delimited(
token(tk::Assign),
cut(assign_statement),
cut(token(tk::Semicolon)),
),
)(input)
}
fn module_item(input: TokenSpan) -> IResult<TokenSpan, ModuleItem> {
alt((
map(assign_item, ModuleItem::Assign),
map(proc_block, ModuleItem::Proc),
))(input)
}
pub fn module(input: TokenSpan) -> IResult<TokenSpan, Module> {
map(
preceded(
token(tk::Module),
cut(tuple((
token(tk::Ident),
delimited(token(tk::LParen), inputs_list, token(tk::RParen)),
preceded(token(tk::RArrow), typename),
delimited(token(tk::LBrace), many0(module_item), token(tk::RBrace)),
))),
),
|(name, inputs, _ret, items)| Module {
// TODO: bring back returns
name: name.span(),
ports: inputs,
items,
},
)(input)
pub fn module(_input: TokenSpan) -> IResult<TokenSpan, Module> {
todo!();
}
#[cfg(test)]

View File

@ -14,6 +14,7 @@ use nom::{
};
use std::fmt;
#[derive(Clone)]
pub struct Token<'a> {
span: Span<'a>,
kind: TokenKind,
@ -77,7 +78,9 @@ pub enum TokenKind {
Module,
Assign,
Match,
State,
Proc,
Comb,
// Error
Error,
}
@ -206,6 +209,8 @@ fn lex_keywords(input: Span) -> IResult<Span, Token> {
map(tag("assign"), |_| TokenKind::Assign),
map(tag("match"), |_| TokenKind::Match),
map(tag("proc"), |_| TokenKind::Proc),
map(tag("comb"), |_| TokenKind::Comb),
map(tag("state"), |_| TokenKind::Proc),
))),
|(span, kind)| Token::new(span, kind),
)(input)