This commit is contained in:
NotAFile 2022-01-04 21:14:49 +01:00
parent 93d106d7c3
commit 79e570dc60
1 changed files with 24 additions and 14 deletions

View File

@ -2,7 +2,7 @@ use nom::{
branch::alt, branch::alt,
bytes::complete::tag, bytes::complete::tag,
character::complete::{alpha1, alphanumeric1, char, multispace0, multispace1, u64 as decimal}, character::complete::{alpha1, alphanumeric1, char, multispace0, multispace1, u64 as decimal},
combinator::{map, opt, recognize, consumed}, combinator::{consumed, map, opt, recognize},
error::{context, ParseError, VerboseError}, error::{context, ParseError, VerboseError},
multi::{many0, many1, separated_list0}, multi::{many0, many1, separated_list0},
sequence::{delimited, pair, preceded, separated_pair, terminated, tuple}, sequence::{delimited, pair, preceded, separated_pair, terminated, tuple},
@ -117,16 +117,18 @@ fn declaration(i: Span) -> IResult<Span, NetDecl> {
fn port_decl(i: Span) -> IResult<Span, PortDecl> { fn port_decl(i: Span) -> IResult<Span, PortDecl> {
map( map(
consumed( consumed(tuple((
tuple((
alt(( alt((
map(tag("input"), |_| PortDirection::Input), map(tag("input"), |_| PortDirection::Input),
map(tag("output"), |_| PortDirection::Output), map(tag("output"), |_| PortDirection::Output),
)), )),
declaration, declaration,
)) ))),
), |(pos, (direction, net))| PortDecl {
|(pos, (direction, net))| PortDecl { pos, direction, net }, pos,
direction,
net,
},
)(i) )(i)
} }
@ -139,11 +141,17 @@ fn operation(input: Span) -> IResult<Span, Operation> {
alt(( alt((
map( map(
separated_pair(ws0(identifier), char('&'), ws0(expression)), separated_pair(ws0(identifier), char('&'), ws0(expression)),
|(a, b)| Operation::And { a: (*a.fragment()).into(), b }, |(a, b)| Operation::And {
a: (*a.fragment()).into(),
b,
},
), ),
map( map(
separated_pair(ws0(identifier), char('|'), ws0(expression)), separated_pair(ws0(identifier), char('|'), ws0(expression)),
|(a, b)| Operation::Or { a: (*a.fragment()).into(), b }, |(a, b)| Operation::Or {
a: (*a.fragment()).into(),
b,
},
), ),
))(input) ))(input)
} }
@ -169,7 +177,9 @@ fn expression(input: Span) -> IResult<Span, Expression> {
alt(( alt((
map(ws0(operation), |op| Expression::Operation(Box::new(op))), map(ws0(operation), |op| Expression::Operation(Box::new(op))),
map(ws0(call_item), |call| Expression::Call(Box::new(call))), map(ws0(call_item), |call| Expression::Call(Box::new(call))),
map(ws0(identifier), |ident| Expression::Ident((*ident.fragment()).into())), map(ws0(identifier), |ident| {
Expression::Ident((*ident.fragment()).into())
}),
))(input) ))(input)
} }