diff --git a/README.md b/README.md index a71ca66..8efcbe0 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,8 @@ A parser library for System Verilog. | declaration | port_declarations | x | x | | | declaration | type_declarations | x | x | | | declaration | net_and_variable_types | | | | -| declaration | strengths | | | | -| declaration | delays | x | | | +| declaration | strengths | x | x | x | +| declaration | delays | x | x | | | declaration | declaration_lists | | | | | declaration | declaration_assignments | | | | | declaration | declaration_ranges | | | | diff --git a/src/parser/declarations/delays.rs b/src/parser/declarations/delays.rs index 6287849..a7c6ac2 100644 --- a/src/parser/declarations/delays.rs +++ b/src/parser/declarations/delays.rs @@ -1,9 +1,9 @@ use crate::ast::*; use crate::parser::*; -//use nom::branch::*; -//use nom::combinator::*; -use nom::error::*; -use nom::{Err, IResult}; +use nom::branch::*; +use nom::combinator::*; +use nom::sequence::*; +use nom::IResult; // ----------------------------------------------------------------------------- @@ -26,7 +26,11 @@ pub struct Delay3Mintypmax<'a> { 'a, ( MintypmaxExpression<'a>, - Option<(MintypmaxExpression<'a>, Option>)>, + Option<( + Symbol<'a>, + MintypmaxExpression<'a>, + Option<(Symbol<'a>, MintypmaxExpression<'a>)>, + )>, ), >, ), @@ -47,7 +51,13 @@ pub struct Delay2Single<'a> { pub struct Delay2Mintypmax<'a> { pub nodes: ( Symbol<'a>, - Paren<'a, (MintypmaxExpression<'a>, Option>)>, + Paren< + 'a, + ( + MintypmaxExpression<'a>, + Option<(Symbol<'a>, MintypmaxExpression<'a>)>, + ), + >, ), } @@ -55,7 +65,7 @@ pub struct Delay2Mintypmax<'a> { pub enum DelayValue<'a> { UnsignedNumber(UnsignedNumber<'a>), RealNumber(RealNumber<'a>), - Identifier(Identifier<'a>), + PsIdentifier(PsIdentifier<'a>), TimeLiteral(TimeLiteral<'a>), Step1(Symbol<'a>), } @@ -63,13 +73,53 @@ pub enum DelayValue<'a> { // ----------------------------------------------------------------------------- pub fn delay3(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt((delay3_single, delay3_mintypmax))(s) +} + +pub fn delay3_single(s: Span) -> IResult { + let (s, a) = symbol("#")(s)?; + let (s, b) = delay_value(s)?; + Ok((s, Delay3::Single(Delay3Single { nodes: (a, b) }))) +} + +pub fn delay3_mintypmax(s: Span) -> IResult { + let (s, a) = symbol("#")(s)?; + let (s, b) = paren(pair( + mintypmax_expression, + opt(triple( + symbol(","), + mintypmax_expression, + opt(pair(symbol(","), mintypmax_expression)), + )), + ))(s)?; + Ok((s, Delay3::Mintypmax(Delay3Mintypmax { nodes: (a, b) }))) } pub fn delay2(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt((delay2_single, delay2_mintypmax))(s) +} + +pub fn delay2_single(s: Span) -> IResult { + let (s, a) = symbol("#")(s)?; + let (s, b) = delay_value(s)?; + Ok((s, Delay2::Single(Delay2Single { nodes: (a, b) }))) +} + +pub fn delay2_mintypmax(s: Span) -> IResult { + let (s, a) = symbol("#")(s)?; + let (s, b) = paren(pair( + mintypmax_expression, + opt(pair(symbol(","), mintypmax_expression)), + ))(s)?; + Ok((s, Delay2::Mintypmax(Delay2Mintypmax { nodes: (a, b) }))) } pub fn delay_value(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(unsigned_number, |x| DelayValue::UnsignedNumber(x)), + map(real_number, |x| DelayValue::RealNumber(x)), + map(ps_identifier, |x| DelayValue::PsIdentifier(x)), + map(time_literal, |x| DelayValue::TimeLiteral(x)), + map(symbol("1step"), |x| DelayValue::Step1(x)), + ))(s) } diff --git a/src/parser/declarations/strengths.rs b/src/parser/declarations/strengths.rs index 9634bcf..36eac62 100644 --- a/src/parser/declarations/strengths.rs +++ b/src/parser/declarations/strengths.rs @@ -1,22 +1,52 @@ +use crate::ast::*; use crate::parser::*; -//use nom::branch::*; -//use nom::combinator::*; -use nom::error::*; -use nom::{Err, IResult}; +use nom::branch::*; +use nom::combinator::*; +use nom::IResult; // ----------------------------------------------------------------------------- -#[derive(Debug)] +#[derive(Debug, Node)] pub enum DriveStrength<'a> { - Strength01(Strength0<'a>, Strength1<'a>), - Strength10(Strength1<'a>, Strength0<'a>), - Strength0z(Strength0<'a>), - Strength1z(Strength1<'a>), - Strengthz0(Strength0<'a>), - Strengthz1(Strength1<'a>), + Strength01(DriveStrength01<'a>), + Strength10(DriveStrength10<'a>), + Strength0z(DriveStrength0z<'a>), + Strength1z(DriveStrength1z<'a>), + Strengthz0(DriveStrengthz0<'a>), + Strengthz1(DriveStrengthz1<'a>), } -#[derive(Debug)] +#[derive(Debug, Node)] +pub struct DriveStrength01<'a> { + pub nodes: (Paren<'a, (Strength0<'a>, Symbol<'a>, Strength1<'a>)>,), +} + +#[derive(Debug, Node)] +pub struct DriveStrength10<'a> { + pub nodes: (Paren<'a, (Strength1<'a>, Symbol<'a>, Strength0<'a>)>,), +} + +#[derive(Debug, Node)] +pub struct DriveStrength0z<'a> { + pub nodes: (Paren<'a, (Strength0<'a>, Symbol<'a>, Symbol<'a>)>,), +} + +#[derive(Debug, Node)] +pub struct DriveStrength1z<'a> { + pub nodes: (Paren<'a, (Strength1<'a>, Symbol<'a>, Symbol<'a>)>,), +} + +#[derive(Debug, Node)] +pub struct DriveStrengthz1<'a> { + pub nodes: (Paren<'a, (Symbol<'a>, Symbol<'a>, Strength1<'a>)>,), +} + +#[derive(Debug, Node)] +pub struct DriveStrengthz0<'a> { + pub nodes: (Paren<'a, (Symbol<'a>, Symbol<'a>, Strength0<'a>)>,), +} + +#[derive(Debug, Node)] pub enum Strength0<'a> { Supply0(Symbol<'a>), Strong0(Symbol<'a>), @@ -24,7 +54,7 @@ pub enum Strength0<'a> { Weak0(Symbol<'a>), } -#[derive(Debug)] +#[derive(Debug, Node)] pub enum Strength1<'a> { Supply1(Symbol<'a>), Strong1(Symbol<'a>), @@ -32,27 +62,159 @@ pub enum Strength1<'a> { Weak1(Symbol<'a>), } -#[derive(Debug)] +#[derive(Debug, Node)] pub enum ChargeStrength<'a> { - Small(Symbol<'a>), - Medium(Symbol<'a>), - Large(Symbol<'a>), + Small(ChargeStrengthSmall<'a>), + Medium(ChargeStrengthMedium<'a>), + Large(ChargeStrengthLarge<'a>), +} + +#[derive(Debug, Node)] +pub struct ChargeStrengthSmall<'a> { + pub nodes: (Paren<'a, Symbol<'a>>,), +} + +#[derive(Debug, Node)] +pub struct ChargeStrengthMedium<'a> { + pub nodes: (Paren<'a, Symbol<'a>>,), +} + +#[derive(Debug, Node)] +pub struct ChargeStrengthLarge<'a> { + pub nodes: (Paren<'a, Symbol<'a>>,), } // ----------------------------------------------------------------------------- pub fn drive_strength(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + drive_strength01, + drive_strength10, + drive_strength0z, + drive_strength1z, + drive_strengthz1, + drive_strengthz0, + ))(s) +} + +pub fn drive_strength01(s: Span) -> IResult { + let (s, a) = paren(triple(strength0, symbol(","), strength1))(s)?; + Ok(( + s, + DriveStrength::Strength01(DriveStrength01 { nodes: (a,) }), + )) +} + +pub fn drive_strength10(s: Span) -> IResult { + let (s, a) = paren(triple(strength1, symbol(","), strength0))(s)?; + Ok(( + s, + DriveStrength::Strength10(DriveStrength10 { nodes: (a,) }), + )) +} + +pub fn drive_strength0z(s: Span) -> IResult { + let (s, a) = paren(triple(strength0, symbol(","), symbol("highz1")))(s)?; + Ok(( + s, + DriveStrength::Strength0z(DriveStrength0z { nodes: (a,) }), + )) +} + +pub fn drive_strength1z(s: Span) -> IResult { + let (s, a) = paren(triple(strength1, symbol(","), symbol("highz0")))(s)?; + Ok(( + s, + DriveStrength::Strength1z(DriveStrength1z { nodes: (a,) }), + )) +} + +pub fn drive_strengthz1(s: Span) -> IResult { + let (s, a) = paren(triple(symbol("highz0"), symbol(","), strength1))(s)?; + Ok(( + s, + DriveStrength::Strengthz1(DriveStrengthz1 { nodes: (a,) }), + )) +} + +pub fn drive_strengthz0(s: Span) -> IResult { + let (s, a) = paren(triple(symbol("highz1"), symbol(","), strength0))(s)?; + Ok(( + s, + DriveStrength::Strengthz0(DriveStrengthz0 { nodes: (a,) }), + )) } pub fn strength0(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(symbol("supply0"), |x| Strength0::Supply0(x)), + map(symbol("strong0"), |x| Strength0::Strong0(x)), + map(symbol("pull0"), |x| Strength0::Pull0(x)), + map(symbol("weak0"), |x| Strength0::Weak0(x)), + ))(s) } pub fn strength1(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + map(symbol("supply1"), |x| Strength1::Supply1(x)), + map(symbol("strong1"), |x| Strength1::Strong1(x)), + map(symbol("pull1"), |x| Strength1::Pull1(x)), + map(symbol("weak1"), |x| Strength1::Weak1(x)), + ))(s) } pub fn charge_strength(s: Span) -> IResult { - Err(Err::Error(make_error(s, ErrorKind::Fix))) + alt(( + charge_strength_small, + charge_strength_medium, + charge_strength_large, + ))(s) +} + +pub fn charge_strength_small(s: Span) -> IResult { + let (s, a) = paren(symbol("small"))(s)?; + Ok(( + s, + ChargeStrength::Small(ChargeStrengthSmall { nodes: (a,) }), + )) +} + +pub fn charge_strength_medium(s: Span) -> IResult { + let (s, a) = paren(symbol("medium"))(s)?; + Ok(( + s, + ChargeStrength::Medium(ChargeStrengthMedium { nodes: (a,) }), + )) +} + +pub fn charge_strength_large(s: Span) -> IResult { + let (s, a) = paren(symbol("large"))(s)?; + Ok(( + s, + ChargeStrength::Large(ChargeStrengthLarge { nodes: (a,) }), + )) +} + +// ----------------------------------------------------------------------------- + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_drive_strength() { + parser_test!(drive_strength, "(supply0, strong1)", Ok((_, _))); + parser_test!(drive_strength, "(pull1, weak0)", Ok((_, _))); + parser_test!(drive_strength, "(pull0, highz1)", Ok((_, _))); + parser_test!(drive_strength, "(weak1, highz0)", Ok((_, _))); + parser_test!(drive_strength, "(highz0, supply1)", Ok((_, _))); + parser_test!(drive_strength, "(highz1, strong0)", Ok((_, _))); + } + + #[test] + fn test_charge_strength() { + parser_test!(charge_strength, "( small)", Ok((_, _))); + parser_test!(charge_strength, "( medium )", Ok((_, _))); + parser_test!(charge_strength, "(large)", Ok((_, _))); + } }