futilehdl/src/parser.rs

229 lines
5.9 KiB
Rust
Raw Normal View History

2022-02-01 18:46:06 +00:00
pub mod error;
mod literals;
2022-01-17 00:15:27 +00:00
pub mod module;
pub mod proc;
2022-02-01 22:14:11 +00:00
pub mod tokens;
2022-01-16 21:06:52 +00:00
2022-01-01 21:43:38 +00:00
use nom::{
branch::alt,
bytes::complete::tag,
2022-02-02 00:03:03 +00:00
character::complete::{alpha1, alphanumeric1, multispace0},
2022-01-16 21:10:52 +00:00
combinator::{map, opt, recognize},
2022-02-01 18:46:06 +00:00
error::{ErrorKind, ParseError},
2022-01-16 21:10:52 +00:00
multi::{many0, separated_list0},
2022-02-02 00:03:03 +00:00
sequence::{delimited, pair, preceded, separated_pair, tuple},
2022-01-01 21:43:38 +00:00
};
2022-02-01 01:00:27 +00:00
use nom_greedyerror::GreedyError;
2022-01-14 14:32:00 +00:00
use nom_locate::LocatedSpan;
2022-01-04 16:28:43 +00:00
// custom span type for nom_locate
2022-01-04 19:05:10 +00:00
pub type Span<'a> = LocatedSpan<&'a str>;
2022-01-04 16:24:49 +00:00
2022-02-01 01:00:27 +00:00
pub type IErr<I> = GreedyError<I, ErrorKind>;
2022-01-04 16:28:43 +00:00
// custom IResult type for VerboseError
2022-02-01 01:00:27 +00:00
pub type IResult<I, O, E = IErr<I>> = nom::IResult<I, O, E>;
2022-01-04 16:28:43 +00:00
2022-01-16 21:06:52 +00:00
pub use crate::parser::module::{module, Module, ModuleItem, PortDirection};
2022-02-02 00:00:11 +00:00
use crate::parser::tokens::{token, TokenKind as tk, TokenSpan};
2022-01-01 21:43:38 +00:00
2022-01-04 19:05:10 +00:00
fn ws0<'a, F: 'a, O, E: ParseError<Span<'a>>>(
2022-01-01 21:43:38 +00:00
inner: F,
2022-01-04 19:05:10 +00:00
) -> impl FnMut(Span<'a>) -> IResult<Span<'a>, O, E>
2022-01-01 21:43:38 +00:00
where
2022-01-04 19:05:10 +00:00
F: FnMut(Span<'a>) -> IResult<Span<'a>, O, E>,
2022-01-01 21:43:38 +00:00
{
delimited(multispace0, inner, multispace0)
}
2022-01-04 19:05:10 +00:00
fn identifier(input: Span) -> IResult<Span, Span> {
2022-01-01 21:43:38 +00:00
recognize(pair(
alt((alpha1, tag("_"))),
many0(alt((alphanumeric1, tag("_")))),
))(input)
}
2022-01-23 23:10:09 +00:00
// TODO: allow recursive generics
2022-02-02 00:00:11 +00:00
// TODO: allow expressions again
fn typename(input: TokenSpan) -> IResult<TokenSpan, TypeName> {
2022-01-23 23:10:09 +00:00
map(
tuple((
2022-02-02 00:00:11 +00:00
token(tk::Ident),
opt(delimited(token(tk::LAngle), expression, token(tk::RAngle))),
2022-01-23 23:10:09 +00:00
)),
2022-02-01 18:46:06 +00:00
|(ident, _)| TypeName {
2022-02-02 00:00:11 +00:00
name: ident.span(),
2022-02-01 18:46:06 +00:00
generics: (),
},
2022-01-23 23:10:09 +00:00
)(input)
}
#[derive(Debug)]
pub struct TypeName<'a> {
name: Span<'a>,
generics: (),
}
2022-01-01 21:43:38 +00:00
#[derive(Debug)]
pub struct NetDecl<'a> {
2022-01-23 23:10:09 +00:00
pub name: Span<'a>,
pub typ: TypeName<'a>,
2022-02-02 00:00:11 +00:00
pub value: Option<Span<'a>>,
2022-01-01 21:43:38 +00:00
}
#[derive(Debug)]
pub struct Assign<'a> {
pub lhs: &'a str,
pub expr: Expression<'a>,
2022-01-01 21:43:38 +00:00
}
2022-01-17 18:20:51 +00:00
#[derive(Debug, Clone)]
pub enum Operation<'a> {
2022-01-17 16:37:15 +00:00
And {
a: Expression<'a>,
b: Expression<'a>,
},
Or {
a: Expression<'a>,
b: Expression<'a>,
},
Xor {
a: Expression<'a>,
b: Expression<'a>,
},
2022-01-17 00:15:27 +00:00
Not(Expression<'a>),
2022-01-01 21:43:38 +00:00
}
2022-01-17 18:20:51 +00:00
#[derive(Debug, Clone)]
pub struct Call<'a> {
pub name: Span<'a>,
pub args: Vec<Expression<'a>>,
2022-01-01 21:43:38 +00:00
}
2022-01-17 18:20:51 +00:00
#[derive(Debug, Clone)]
pub enum Expression<'a> {
Ident(&'a str),
2022-01-17 00:15:27 +00:00
Literal(u64),
Call(Box<Call<'a>>),
Operation(Box<Operation<'a>>),
2022-01-01 21:43:38 +00:00
}
2022-02-02 00:00:11 +00:00
// TODO: reallow assignments
fn declaration(i: TokenSpan) -> IResult<TokenSpan, NetDecl> {
2022-01-01 21:43:38 +00:00
map(
tuple((
2022-02-02 00:00:11 +00:00
separated_pair(token(tk::Ident), token(tk::Colon), typename),
opt(preceded(token(tk::Assign), token(tk::Number))),
2022-01-01 21:43:38 +00:00
)),
2022-02-02 00:03:03 +00:00
|((ident, typ), _value)| NetDecl {
2022-02-02 00:00:11 +00:00
name: ident.span(),
2022-01-23 23:10:09 +00:00
typ,
2022-02-02 00:00:11 +00:00
value: None,
2022-01-01 21:43:38 +00:00
},
)(i)
}
2022-02-02 00:00:11 +00:00
fn operation(input: TokenSpan) -> IResult<TokenSpan, Operation> {
2022-01-01 21:43:38 +00:00
// temporarily given up on before I learn the shunting yard algorithm
alt((
map(
2022-02-02 00:00:11 +00:00
separated_pair(expression_nonrecurse, token(tk::BitAnd), expression),
2022-01-17 16:37:15 +00:00
|(a, b)| Operation::And { a, b },
2022-01-01 21:43:38 +00:00
),
map(
2022-02-02 00:00:11 +00:00
separated_pair(expression_nonrecurse, token(tk::BitOr), expression),
2022-01-17 16:37:15 +00:00
|(a, b)| Operation::Or { a, b },
2022-01-01 21:43:38 +00:00
),
2022-01-17 16:36:24 +00:00
map(
2022-02-02 00:00:11 +00:00
separated_pair(expression_nonrecurse, token(tk::BitXor), expression),
2022-01-17 16:37:15 +00:00
|(a, b)| Operation::Xor { a, b },
2022-01-17 00:15:27 +00:00
),
2022-02-02 00:00:11 +00:00
map(preceded(token(tk::BitNot), expression), Operation::Not),
2022-01-01 21:43:38 +00:00
))(input)
}
2022-02-02 00:00:11 +00:00
fn call_item(input: TokenSpan) -> IResult<TokenSpan, Call> {
2022-01-01 21:43:38 +00:00
map(
tuple((
2022-02-02 00:00:11 +00:00
token(tk::Ident),
2022-01-04 00:38:35 +00:00
delimited(
2022-02-02 00:00:11 +00:00
token(tk::LParen),
separated_list0(token(tk::Comma), expression),
token(tk::RParen),
2022-01-04 00:38:35 +00:00
),
2022-01-01 21:43:38 +00:00
)),
2022-02-02 00:00:11 +00:00
|(name, args)| Call {
name: name.span(),
args,
},
2022-01-04 00:38:35 +00:00
)(input)
2022-01-01 21:43:38 +00:00
}
2022-01-17 16:29:00 +00:00
/// parser combinators can not parse left-recursive grammars. To work around this, we split
/// expressions into a recursive and non-recursive portion.
/// Parsers reachable from this point must call expression_nonrecurse instead
2022-02-02 00:00:11 +00:00
fn expression(input: TokenSpan) -> IResult<TokenSpan, Expression> {
2022-01-01 21:43:38 +00:00
alt((
2022-02-02 00:00:11 +00:00
map(operation, |op| Expression::Operation(Box::new(op))),
2022-01-17 16:37:15 +00:00
expression_nonrecurse,
2022-01-17 16:29:00 +00:00
))(input)
}
/// the portion of the expression grammar that can be parsed without left recursion
2022-02-02 00:00:11 +00:00
fn expression_nonrecurse(input: TokenSpan) -> IResult<TokenSpan, Expression> {
2022-01-17 16:29:00 +00:00
alt((
2022-02-02 00:00:11 +00:00
map(token(tk::Number), |_| Expression::Literal(42)),
map(call_item, |call| Expression::Call(Box::new(call))),
map(token(tk::Ident), |ident| {
Expression::Ident(*ident.span().fragment())
2022-01-04 20:14:49 +00:00
}),
2022-02-02 00:00:11 +00:00
delimited(token(tk::LParen), expression, token(tk::RParen)),
2022-01-01 21:43:38 +00:00
))(input)
}
2022-02-02 00:00:11 +00:00
fn assign_statement(input: TokenSpan) -> IResult<TokenSpan, Assign> {
2022-01-17 00:15:27 +00:00
map(
2022-02-02 00:00:11 +00:00
separated_pair(token(tk::Ident), token(tk::EqAssign), expression),
2022-01-17 00:15:27 +00:00
|(lhs, expr)| Assign {
2022-02-02 00:00:11 +00:00
lhs: (*lhs.span().fragment()),
2022-01-17 00:15:27 +00:00
expr,
},
2022-01-01 21:43:38 +00:00
)(input)
}
2022-02-02 00:00:11 +00:00
pub fn parse(input: TokenSpan) -> IResult<TokenSpan, Module> {
module(input)
2022-01-04 16:24:21 +00:00
}
2022-01-01 21:43:38 +00:00
#[cfg(test)]
mod test {
use super::*;
2022-01-17 00:15:27 +00:00
use nom::combinator::all_consuming;
2022-01-01 21:43:38 +00:00
#[test]
fn test_operation() {
2022-01-04 19:05:10 +00:00
operation(" a | b ".into()).unwrap();
operation(" a & b ".into()).unwrap();
2022-01-01 21:43:38 +00:00
}
#[test]
fn test_expression() {
2022-01-04 19:05:10 +00:00
expression(" a ".into()).unwrap();
expression(" a | b ".into()).unwrap();
expression(" a | b | c ".into()).unwrap();
2022-01-01 21:43:38 +00:00
}
#[test]
fn test_assignment() {
2022-01-17 00:15:27 +00:00
// TODO: make wrapper and use for all tests
all_consuming(assign_statement)(" a = b ".into()).unwrap();
all_consuming(assign_statement)(" a = b | c ".into()).unwrap();
2022-01-01 21:43:38 +00:00
}
#[test]
fn test_call() {
2022-01-04 19:05:10 +00:00
call_item("thing ( )".into()).unwrap();
call_item("thing ( a , b , c )".into()).unwrap();
call_item("thing(a,b,c)".into()).unwrap();
2022-01-01 21:43:38 +00:00
}
}