Refactor numbers
This commit is contained in:
parent
4b4b78be49
commit
0e88bda969
@ -96,7 +96,7 @@ pub struct ClockingDrive<'a> {
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum CycleDelay<'a> {
|
||||
Number(Number<'a>),
|
||||
IntegralNumber(IntegralNumber<'a>),
|
||||
Identifier(Identifier<'a>),
|
||||
Expression(Expression<'a>),
|
||||
}
|
||||
@ -280,7 +280,7 @@ pub fn clocking_drive(s: &str) -> IResult<&str, ClockingDrive> {
|
||||
pub fn cycle_delay(s: &str) -> IResult<&str, CycleDelay> {
|
||||
alt((
|
||||
map(preceded(symbol("##"), integral_number), |x| {
|
||||
CycleDelay::Number(x)
|
||||
CycleDelay::IntegralNumber(x)
|
||||
}),
|
||||
map(preceded(symbol("##"), identifier), |x| {
|
||||
CycleDelay::Identifier(x)
|
||||
|
@ -39,8 +39,8 @@ pub enum RsProductionList<'a> {
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum WeightSpecification<'a> {
|
||||
Number(Number<'a>),
|
||||
Identifier(PsIdentifier<'a>),
|
||||
IntegralNumber(IntegralNumber<'a>),
|
||||
PsIdentifier(PsIdentifier<'a>),
|
||||
Expression(Expression<'a>),
|
||||
}
|
||||
|
||||
@ -165,8 +165,8 @@ pub fn rs_production_list_join(s: &str) -> IResult<&str, RsProductionList> {
|
||||
|
||||
pub fn weight_specification(s: &str) -> IResult<&str, WeightSpecification> {
|
||||
alt((
|
||||
map(integral_number, |x| WeightSpecification::Number(x)),
|
||||
map(ps_identifier, |x| WeightSpecification::Identifier(x)),
|
||||
map(integral_number, |x| WeightSpecification::IntegralNumber(x)),
|
||||
map(ps_identifier, |x| WeightSpecification::PsIdentifier(x)),
|
||||
map(paren(expression), |x| WeightSpecification::Expression(x)),
|
||||
))(s)
|
||||
}
|
||||
|
@ -218,7 +218,7 @@ pub enum ModulePathMintypmaxExpression<'a> {
|
||||
#[derive(Debug)]
|
||||
pub enum PartSelectRange<'a> {
|
||||
Range((ConstantExpression<'a>, ConstantExpression<'a>)),
|
||||
IndexedRange((Expression<'a>, &'a str, ConstantExpression<'a>)),
|
||||
IndexedRange((Expression<'a>, Symbol<'a>, ConstantExpression<'a>)),
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
@ -21,241 +21,392 @@ pub enum IntegralNumber<'a> {
|
||||
OctalNumber(OctalNumber<'a>),
|
||||
BinaryNumber(BinaryNumber<'a>),
|
||||
HexNumber(HexNumber<'a>),
|
||||
UnsignedNumber(&'a str),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum DecimalNumber<'a> {
|
||||
UnsignedNumber(UnsignedNumber<'a>),
|
||||
BaseUnsigned(DecimalNumberBaseUnsigned<'a>),
|
||||
BaseXNumber(DecimalNumberBaseXNumber<'a>),
|
||||
BaseZNumber(DecimalNumberBaseZNumber<'a>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DecimalNumberBaseUnsigned<'a> {
|
||||
pub nodes: (Option<Size<'a>>, DecimalBase<'a>, UnsignedNumber<'a>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DecimalNumberBaseXNumber<'a> {
|
||||
pub nodes: (Option<Size<'a>>, DecimalBase<'a>, XNumber<'a>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DecimalNumberBaseZNumber<'a> {
|
||||
pub nodes: (Option<Size<'a>>, DecimalBase<'a>, ZNumber<'a>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BinaryNumber<'a> {
|
||||
pub nodes: (Option<Size<'a>>, BinaryBase<'a>, BinaryValue<'a>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct OctalNumber<'a> {
|
||||
pub nodes: (Option<Size<'a>>, OctalBase<'a>, OctalValue<'a>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct HexNumber<'a> {
|
||||
pub nodes: (Option<Size<'a>>, HexBase<'a>, HexValue<'a>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Sign<'a> {
|
||||
Plus(Symbol<'a>),
|
||||
Minus(Symbol<'a>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Size<'a> {
|
||||
pub nodes: (NonZeroUnsignedNumber<'a>,),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct NonZeroUnsignedNumber<'a> {
|
||||
pub nodes: (&'a str,),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum RealNumber<'a> {
|
||||
FixedPointNumber(FixedPointNumber<'a>),
|
||||
FloatingPointNumber(FloatingPointNumber<'a>),
|
||||
Floating(RealNumberFloating<'a>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DecimalNumber<'a> {
|
||||
pub nodes: (Option<&'a str>, &'a str, &'a str),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BinaryNumber<'a> {
|
||||
pub nodes: (Option<&'a str>, &'a str, &'a str),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct OctalNumber<'a> {
|
||||
pub nodes: (Option<&'a str>, &'a str, &'a str),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct HexNumber<'a> {
|
||||
pub nodes: (Option<&'a str>, &'a str, &'a str),
|
||||
pub struct RealNumberFloating<'a> {
|
||||
pub nodes: (
|
||||
UnsignedNumber<'a>,
|
||||
Option<(Symbol<'a>, UnsignedNumber<'a>)>,
|
||||
Exp<'a>,
|
||||
Option<Sign<'a>>,
|
||||
UnsignedNumber<'a>,
|
||||
),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FixedPointNumber<'a> {
|
||||
pub nodes: (&'a str, &'a str),
|
||||
pub nodes: (UnsignedNumber<'a>, Symbol<'a>, UnsignedNumber<'a>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FloatingPointNumber<'a> {
|
||||
pub nodes: (&'a str, Option<&'a str>, &'a str, Option<&'a str>, &'a str),
|
||||
pub struct Exp<'a> {
|
||||
pub nodes: (Symbol<'a>,),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct UnsignedNumber<'a> {
|
||||
pub nodes: (&'a str,),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BinaryValue<'a> {
|
||||
pub nodes: (&'a str,),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct OctalValue<'a> {
|
||||
pub nodes: (&'a str,),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct HexValue<'a> {
|
||||
pub nodes: (&'a str,),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DecimalBase<'a> {
|
||||
pub nodes: (&'a str,),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BinaryBase<'a> {
|
||||
pub nodes: (&'a str,),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct OctalBase<'a> {
|
||||
pub nodes: (&'a str,),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct HexBase<'a> {
|
||||
pub nodes: (&'a str,),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct XNumber<'a> {
|
||||
pub nodes: (&'a str,),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ZNumber<'a> {
|
||||
pub nodes: (&'a str,),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct UnbasedUnsizedLiteral<'a> {
|
||||
pub nodes: (Symbol<'a>,),
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
pub fn number(s: &str) -> IResult<&str, Number> {
|
||||
alt((real_number, integral_number))(s)
|
||||
alt((
|
||||
map(real_number, |x| Number::RealNumber(x)),
|
||||
map(integral_number, |x| Number::IntegralNumber(x)),
|
||||
))(s)
|
||||
}
|
||||
|
||||
pub fn integral_number(s: &str) -> IResult<&str, Number> {
|
||||
let (s, x) = alt((
|
||||
octal_number,
|
||||
binary_number,
|
||||
hex_number,
|
||||
decimal_number,
|
||||
integral_unsigned_number,
|
||||
))(s)?;
|
||||
Ok((s, Number::IntegralNumber(x)))
|
||||
pub fn integral_number(s: &str) -> IResult<&str, IntegralNumber> {
|
||||
alt((
|
||||
map(octal_number, |x| IntegralNumber::OctalNumber(x)),
|
||||
map(binary_number, |x| IntegralNumber::BinaryNumber(x)),
|
||||
map(hex_number, |x| IntegralNumber::HexNumber(x)),
|
||||
map(decimal_number, |x| IntegralNumber::DecimalNumber(x)),
|
||||
))(s)
|
||||
}
|
||||
|
||||
pub fn decimal_number(s: &str) -> IResult<&str, IntegralNumber> {
|
||||
let (s, (x, y, z)) = tuple((
|
||||
opt(size),
|
||||
decimal_base,
|
||||
alt((unsigned_number, x_number, z_number)),
|
||||
))(s)?;
|
||||
pub fn decimal_number(s: &str) -> IResult<&str, DecimalNumber> {
|
||||
alt((
|
||||
map(unsigned_number, |x| DecimalNumber::UnsignedNumber(x)),
|
||||
decimal_number_base_unsigned,
|
||||
decimal_number_base_x_number,
|
||||
decimal_number_base_z_number,
|
||||
))(s)
|
||||
}
|
||||
|
||||
pub fn decimal_number_base_unsigned(s: &str) -> IResult<&str, DecimalNumber> {
|
||||
let (s, a) = opt(size)(s)?;
|
||||
let (s, b) = decimal_base(s)?;
|
||||
let (s, c) = unsigned_number(s)?;
|
||||
Ok((
|
||||
s,
|
||||
IntegralNumber::DecimalNumber(DecimalNumber { nodes: (x, y, z) }),
|
||||
DecimalNumber::BaseUnsigned(DecimalNumberBaseUnsigned { nodes: (a, b, c) }),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn integral_unsigned_number(s: &str) -> IResult<&str, IntegralNumber> {
|
||||
let (s, x) = unsigned_number(s)?;
|
||||
Ok((s, IntegralNumber::UnsignedNumber(x)))
|
||||
}
|
||||
|
||||
pub fn binary_number(s: &str) -> IResult<&str, IntegralNumber> {
|
||||
let (s, (x, y, z)) = tuple((opt(size), binary_base, binary_value))(s)?;
|
||||
pub fn decimal_number_base_x_number(s: &str) -> IResult<&str, DecimalNumber> {
|
||||
let (s, a) = opt(size)(s)?;
|
||||
let (s, b) = decimal_base(s)?;
|
||||
let (s, c) = x_number(s)?;
|
||||
Ok((
|
||||
s,
|
||||
IntegralNumber::BinaryNumber(BinaryNumber { nodes: (x, y, z) }),
|
||||
DecimalNumber::BaseXNumber(DecimalNumberBaseXNumber { nodes: (a, b, c) }),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn octal_number(s: &str) -> IResult<&str, IntegralNumber> {
|
||||
let (s, (x, y, z)) = tuple((opt(size), octal_base, octal_value))(s)?;
|
||||
pub fn decimal_number_base_z_number(s: &str) -> IResult<&str, DecimalNumber> {
|
||||
let (s, a) = opt(size)(s)?;
|
||||
let (s, b) = decimal_base(s)?;
|
||||
let (s, c) = z_number(s)?;
|
||||
Ok((
|
||||
s,
|
||||
IntegralNumber::OctalNumber(OctalNumber { nodes: (x, y, z) }),
|
||||
DecimalNumber::BaseZNumber(DecimalNumberBaseZNumber { nodes: (a, b, c) }),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn hex_number(s: &str) -> IResult<&str, IntegralNumber> {
|
||||
let (s, (x, y, z)) = tuple((opt(size), hex_base, hex_value))(s)?;
|
||||
Ok((s, IntegralNumber::HexNumber(HexNumber { nodes: (x, y, z) })))
|
||||
pub fn binary_number(s: &str) -> IResult<&str, BinaryNumber> {
|
||||
let (s, a) = opt(size)(s)?;
|
||||
let (s, b) = binary_base(s)?;
|
||||
let (s, c) = binary_value(s)?;
|
||||
Ok((s, BinaryNumber { nodes: (a, b, c) }))
|
||||
}
|
||||
|
||||
pub fn size(s: &str) -> IResult<&str, &str> {
|
||||
ws(size_impl)(s)
|
||||
pub fn octal_number(s: &str) -> IResult<&str, OctalNumber> {
|
||||
let (s, a) = opt(size)(s)?;
|
||||
let (s, b) = octal_base(s)?;
|
||||
let (s, c) = octal_value(s)?;
|
||||
Ok((s, OctalNumber { nodes: (a, b, c) }))
|
||||
}
|
||||
|
||||
pub fn size_impl(s: &str) -> IResult<&str, &str> {
|
||||
let (s, x) = is_a("123456789")(s)?;
|
||||
fold_many0(alt((tag("_"), digit1)), x, |acc, item| {
|
||||
pub fn hex_number(s: &str) -> IResult<&str, HexNumber> {
|
||||
let (s, a) = opt(size)(s)?;
|
||||
let (s, b) = hex_base(s)?;
|
||||
let (s, c) = hex_value(s)?;
|
||||
Ok((s, HexNumber { nodes: (a, b, c) }))
|
||||
}
|
||||
|
||||
pub fn sign(s: &str) -> IResult<&str, Sign> {
|
||||
alt((
|
||||
map(symbol("+"), |x| Sign::Plus(x)),
|
||||
map(symbol("-"), |x| Sign::Minus(x)),
|
||||
))(s)
|
||||
}
|
||||
|
||||
pub fn size(s: &str) -> IResult<&str, Size> {
|
||||
let (s, a) = non_zero_unsigned_number(s)?;
|
||||
Ok((s, Size { nodes: (a,) }))
|
||||
}
|
||||
|
||||
pub fn non_zero_unsigned_number(s: &str) -> IResult<&str, NonZeroUnsignedNumber> {
|
||||
let (s, a) = ws(non_zero_unsigned_number_impl)(s)?;
|
||||
Ok((s, NonZeroUnsignedNumber { nodes: (a,) }))
|
||||
}
|
||||
|
||||
pub fn non_zero_unsigned_number_impl(s: &str) -> IResult<&str, &str> {
|
||||
let (s, a) = is_a("123456789")(s)?;
|
||||
fold_many0(alt((tag("_"), digit1)), a, |acc, item| {
|
||||
str_concat::concat(acc, item).unwrap()
|
||||
})(s)
|
||||
}
|
||||
|
||||
pub fn real_number(s: &str) -> IResult<&str, Number> {
|
||||
let (s, x) = alt((floating_point_number, fixed_point_number))(s)?;
|
||||
Ok((s, Number::RealNumber(x)))
|
||||
pub fn real_number(s: &str) -> IResult<&str, RealNumber> {
|
||||
alt((
|
||||
real_number_floating,
|
||||
map(fixed_point_number, |x| RealNumber::FixedPointNumber(x)),
|
||||
))(s)
|
||||
}
|
||||
|
||||
pub fn fixed_point_number(s: &str) -> IResult<&str, RealNumber> {
|
||||
let (s, (x, _, y)) = tuple((unsigned_number, symbol("."), unsigned_number))(s)?;
|
||||
pub fn real_number_floating(s: &str) -> IResult<&str, RealNumber> {
|
||||
let (s, a) = unsigned_number(s)?;
|
||||
let (s, b) = opt(pair(symbol("."), unsigned_number))(s)?;
|
||||
let (s, c) = exp(s)?;
|
||||
let (s, d) = opt(sign)(s)?;
|
||||
let (s, e) = unsigned_number(s)?;
|
||||
Ok((
|
||||
s,
|
||||
RealNumber::FixedPointNumber(FixedPointNumber { nodes: (x, y) }),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn floating_point_number(s: &str) -> IResult<&str, RealNumber> {
|
||||
let (s, x) = unsigned_number(s)?;
|
||||
let (s, y) = opt(preceded(symbol("."), unsigned_number))(s)?;
|
||||
let (s, z) = alt((symbol("e"), symbol("E")))(s)?;
|
||||
let (s, v) = opt(alt((symbol("+"), symbol("-"))))(s)?;
|
||||
let (s, w) = unsigned_number(s)?;
|
||||
|
||||
Ok((
|
||||
s,
|
||||
RealNumber::FloatingPointNumber(FloatingPointNumber {
|
||||
nodes: (x, y, z, v, w),
|
||||
RealNumber::Floating(RealNumberFloating {
|
||||
nodes: (a, b, c, d, e),
|
||||
}),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn unsigned_number(s: &str) -> IResult<&str, &str> {
|
||||
ws(unsigned_number_impl)(s)
|
||||
pub fn fixed_point_number(s: &str) -> IResult<&str, FixedPointNumber> {
|
||||
let (s, a) = unsigned_number(s)?;
|
||||
let (s, b) = map(tag("."), |x| Symbol { nodes: (x,) })(s)?;;
|
||||
let (s, c) = unsigned_number(s)?;
|
||||
Ok((s, FixedPointNumber { nodes: (a, b, c) }))
|
||||
}
|
||||
|
||||
pub fn exp(s: &str) -> IResult<&str, Exp> {
|
||||
let (s, a) = alt((symbol("e"), symbol("E")))(s)?;
|
||||
Ok((s, Exp { nodes: (a,) }))
|
||||
}
|
||||
|
||||
pub fn unsigned_number(s: &str) -> IResult<&str, UnsignedNumber> {
|
||||
let (s, a) = ws(unsigned_number_impl)(s)?;
|
||||
Ok((s, UnsignedNumber { nodes: (a,) }))
|
||||
}
|
||||
|
||||
pub fn unsigned_number_impl(s: &str) -> IResult<&str, &str> {
|
||||
let (s, x) = digit1(s)?;
|
||||
fold_many0(alt((tag("_"), digit1)), x, |acc, item| {
|
||||
let (s, a) = digit1(s)?;
|
||||
fold_many0(alt((tag("_"), digit1)), a, |acc, item| {
|
||||
str_concat::concat(acc, item).unwrap()
|
||||
})(s)
|
||||
}
|
||||
|
||||
pub fn binary_value(s: &str) -> IResult<&str, &str> {
|
||||
ws(binary_value_impl)(s)
|
||||
pub fn binary_value(s: &str) -> IResult<&str, BinaryValue> {
|
||||
let (s, a) = ws(binary_value_impl)(s)?;
|
||||
Ok((s, BinaryValue { nodes: (a,) }))
|
||||
}
|
||||
|
||||
pub fn binary_value_impl(s: &str) -> IResult<&str, &str> {
|
||||
let (s, x) = is_a("01xXzZ?")(s)?;
|
||||
fold_many0(alt((tag("_"), is_a("01xXzZ?"))), x, |acc, item| {
|
||||
let (s, a) = is_a("01xXzZ?")(s)?;
|
||||
fold_many0(alt((tag("_"), is_a("01xXzZ?"))), a, |acc, item| {
|
||||
str_concat::concat(acc, item).unwrap()
|
||||
})(s)
|
||||
}
|
||||
|
||||
pub fn octal_value(s: &str) -> IResult<&str, &str> {
|
||||
ws(octal_value_impl)(s)
|
||||
pub fn octal_value(s: &str) -> IResult<&str, OctalValue> {
|
||||
let (s, a) = ws(octal_value_impl)(s)?;
|
||||
Ok((s, OctalValue { nodes: (a,) }))
|
||||
}
|
||||
|
||||
pub fn octal_value_impl(s: &str) -> IResult<&str, &str> {
|
||||
let (s, x) = is_a("01234567xXzZ?")(s)?;
|
||||
fold_many0(alt((tag("_"), is_a("01234567xXzZ?"))), x, |acc, item| {
|
||||
let (s, a) = is_a("01234567xXzZ?")(s)?;
|
||||
fold_many0(alt((tag("_"), is_a("01234567xXzZ?"))), a, |acc, item| {
|
||||
str_concat::concat(acc, item).unwrap()
|
||||
})(s)
|
||||
}
|
||||
|
||||
pub fn hex_value(s: &str) -> IResult<&str, &str> {
|
||||
ws(hex_value_impl)(s)
|
||||
pub fn hex_value(s: &str) -> IResult<&str, HexValue> {
|
||||
let (s, a) = ws(hex_value_impl)(s)?;
|
||||
Ok((s, HexValue { nodes: (a,) }))
|
||||
}
|
||||
|
||||
pub fn hex_value_impl(s: &str) -> IResult<&str, &str> {
|
||||
let (s, x) = is_a("0123456789abcdefABCDEFxXzZ?")(s)?;
|
||||
let (s, a) = is_a("0123456789abcdefABCDEFxXzZ?")(s)?;
|
||||
fold_many0(
|
||||
alt((tag("_"), is_a("0123456789abcdefABCDEFxXzZ?"))),
|
||||
x,
|
||||
a,
|
||||
|acc, item| str_concat::concat(acc, item).unwrap(),
|
||||
)(s)
|
||||
}
|
||||
|
||||
pub fn decimal_base(s: &str) -> IResult<&str, &str> {
|
||||
ws(decimal_base_impl)(s)
|
||||
pub fn decimal_base(s: &str) -> IResult<&str, DecimalBase> {
|
||||
let (s, a) = ws(decimal_base_impl)(s)?;
|
||||
Ok((s, DecimalBase { nodes: (a,) }))
|
||||
}
|
||||
|
||||
pub fn decimal_base_impl(s: &str) -> IResult<&str, &str> {
|
||||
alt((tag_no_case("'d"), tag_no_case("'sd")))(s)
|
||||
}
|
||||
|
||||
pub fn binary_base(s: &str) -> IResult<&str, &str> {
|
||||
ws(binary_base_impl)(s)
|
||||
pub fn binary_base(s: &str) -> IResult<&str, BinaryBase> {
|
||||
let (s, a) = ws(binary_base_impl)(s)?;
|
||||
Ok((s, BinaryBase { nodes: (a,) }))
|
||||
}
|
||||
|
||||
pub fn binary_base_impl(s: &str) -> IResult<&str, &str> {
|
||||
alt((tag_no_case("'b"), tag_no_case("'sb")))(s)
|
||||
}
|
||||
|
||||
pub fn octal_base(s: &str) -> IResult<&str, &str> {
|
||||
ws(octal_base_impl)(s)
|
||||
pub fn octal_base(s: &str) -> IResult<&str, OctalBase> {
|
||||
let (s, a) = ws(octal_base_impl)(s)?;
|
||||
Ok((s, OctalBase { nodes: (a,) }))
|
||||
}
|
||||
|
||||
pub fn octal_base_impl(s: &str) -> IResult<&str, &str> {
|
||||
alt((tag_no_case("'o"), tag_no_case("'so")))(s)
|
||||
}
|
||||
|
||||
pub fn hex_base(s: &str) -> IResult<&str, &str> {
|
||||
ws(hex_base_impl)(s)
|
||||
pub fn hex_base(s: &str) -> IResult<&str, HexBase> {
|
||||
let (s, a) = ws(hex_base_impl)(s)?;
|
||||
Ok((s, HexBase { nodes: (a,) }))
|
||||
}
|
||||
|
||||
pub fn hex_base_impl(s: &str) -> IResult<&str, &str> {
|
||||
alt((tag_no_case("'h"), tag_no_case("'sh")))(s)
|
||||
}
|
||||
|
||||
pub fn x_number(s: &str) -> IResult<&str, &str> {
|
||||
ws(x_number_impl)(s)
|
||||
pub fn x_number(s: &str) -> IResult<&str, XNumber> {
|
||||
let (s, a) = ws(x_number_impl)(s)?;
|
||||
Ok((s, XNumber { nodes: (a,) }))
|
||||
}
|
||||
|
||||
pub fn x_number_impl(s: &str) -> IResult<&str, &str> {
|
||||
let (s, x) = tag_no_case("x")(s)?;
|
||||
fold_many0(alt((tag("_"), is_a("_"))), x, |acc, item| {
|
||||
let (s, a) = tag_no_case("x")(s)?;
|
||||
fold_many0(alt((tag("_"), is_a("_"))), a, |acc, item| {
|
||||
str_concat::concat(acc, item).unwrap()
|
||||
})(s)
|
||||
}
|
||||
|
||||
pub fn z_number(s: &str) -> IResult<&str, &str> {
|
||||
ws(z_number_impl)(s)
|
||||
pub fn z_number(s: &str) -> IResult<&str, ZNumber> {
|
||||
let (s, a) = ws(z_number_impl)(s)?;
|
||||
Ok((s, ZNumber { nodes: (a,) }))
|
||||
}
|
||||
|
||||
pub fn z_number_impl(s: &str) -> IResult<&str, &str> {
|
||||
let (s, x) = alt((tag_no_case("z"), tag("?")))(s)?;
|
||||
fold_many0(alt((tag("_"), is_a("_"))), x, |acc, item| {
|
||||
let (s, a) = alt((tag_no_case("z"), tag("?")))(s)?;
|
||||
fold_many0(alt((tag("_"), is_a("_"))), a, |acc, item| {
|
||||
str_concat::concat(acc, item).unwrap()
|
||||
})(s)
|
||||
}
|
||||
|
||||
pub fn unbased_unsized_literal(s: &str) -> IResult<&str, &str> {
|
||||
alt((symbol("'0"), symbol("'1"), symbol("'z"), symbol("'x")))(s)
|
||||
pub fn unbased_unsized_literal(s: &str) -> IResult<&str, UnbasedUnsizedLiteral> {
|
||||
let (s, a) = alt((symbol("'0"), symbol("'1"), symbol("'z"), symbol("'x")))(s)?;
|
||||
Ok((s, UnbasedUnsizedLiteral { nodes: (a,) }))
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
@ -6,13 +6,13 @@ use nom::IResult;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Operator<'a> {
|
||||
pub nodes: (&'a str,),
|
||||
pub nodes: (Symbol<'a>,),
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
pub fn unary_operator(s: &str) -> IResult<&str, Operator> {
|
||||
let (s, x) = alt((
|
||||
let (s, a) = alt((
|
||||
symbol("+"),
|
||||
symbol("-"),
|
||||
symbol("!"),
|
||||
@ -25,11 +25,11 @@ pub fn unary_operator(s: &str) -> IResult<&str, Operator> {
|
||||
symbol("^"),
|
||||
symbol("~"),
|
||||
))(s)?;
|
||||
Ok((s, Operator { nodes: (x,) }))
|
||||
Ok((s, Operator { nodes: (a,) }))
|
||||
}
|
||||
|
||||
pub fn binary_operator(s: &str) -> IResult<&str, Operator> {
|
||||
let (s, x) = alt((
|
||||
let (s, a) = alt((
|
||||
alt((
|
||||
symbol("+"),
|
||||
symbol("-"),
|
||||
@ -64,16 +64,16 @@ pub fn binary_operator(s: &str) -> IResult<&str, Operator> {
|
||||
symbol(">"),
|
||||
)),
|
||||
))(s)?;
|
||||
Ok((s, Operator { nodes: (x,) }))
|
||||
Ok((s, Operator { nodes: (a,) }))
|
||||
}
|
||||
|
||||
pub fn inc_or_dec_operator(s: &str) -> IResult<&str, Operator> {
|
||||
let (s, x) = alt((symbol("++"), symbol("--")))(s)?;
|
||||
Ok((s, Operator { nodes: (x,) }))
|
||||
let (s, a) = alt((symbol("++"), symbol("--")))(s)?;
|
||||
Ok((s, Operator { nodes: (a,) }))
|
||||
}
|
||||
|
||||
pub fn unary_module_path_operator(s: &str) -> IResult<&str, Operator> {
|
||||
let (s, x) = alt((
|
||||
let (s, a) = alt((
|
||||
symbol("!"),
|
||||
symbol("&"),
|
||||
symbol("|"),
|
||||
@ -84,11 +84,11 @@ pub fn unary_module_path_operator(s: &str) -> IResult<&str, Operator> {
|
||||
symbol("^"),
|
||||
symbol("~"),
|
||||
))(s)?;
|
||||
Ok((s, Operator { nodes: (x,) }))
|
||||
Ok((s, Operator { nodes: (a,) }))
|
||||
}
|
||||
|
||||
pub fn binary_module_path_operator(s: &str) -> IResult<&str, Operator> {
|
||||
let (s, x) = alt((
|
||||
let (s, a) = alt((
|
||||
symbol("=="),
|
||||
symbol("!="),
|
||||
symbol("&&"),
|
||||
@ -99,7 +99,7 @@ pub fn binary_module_path_operator(s: &str) -> IResult<&str, Operator> {
|
||||
symbol("^"),
|
||||
symbol("~^"),
|
||||
))(s)?;
|
||||
Ok((s, Operator { nodes: (x,) }))
|
||||
Ok((s, Operator { nodes: (a,) }))
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
@ -134,7 +134,7 @@ pub enum RangeExpression<'a> {
|
||||
pub enum PrimaryLiteral<'a> {
|
||||
Number(Number<'a>),
|
||||
TimeLiteral(TimeLiteral<'a>),
|
||||
UnbasedUnsizedLiteral(&'a str),
|
||||
UnbasedUnsizedLiteral(UnbasedUnsizedLiteral<'a>),
|
||||
StringLiteral(StringLiteral<'a>),
|
||||
}
|
||||
|
||||
@ -145,13 +145,13 @@ pub enum TimeLiteral<'a> {
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum TimeUnit {
|
||||
S,
|
||||
MS,
|
||||
US,
|
||||
NS,
|
||||
PS,
|
||||
FS,
|
||||
pub enum TimeUnit<'a> {
|
||||
S(Symbol<'a>),
|
||||
MS(Symbol<'a>),
|
||||
US(Symbol<'a>),
|
||||
NS(Symbol<'a>),
|
||||
PS(Symbol<'a>),
|
||||
FS(Symbol<'a>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -168,12 +168,12 @@ pub struct BitSelect<'a> {
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct UnsignedTimeLiteral<'a> {
|
||||
pub nodes: (&'a str, TimeUnit),
|
||||
pub nodes: (UnsignedNumber<'a>, TimeUnit<'a>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FixedPointTimeLiteral<'a> {
|
||||
pub nodes: (RealNumber<'a>, TimeUnit),
|
||||
pub nodes: (FixedPointNumber<'a>, TimeUnit<'a>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -445,24 +445,14 @@ pub fn fixed_point_time_literal(s: &str) -> IResult<&str, TimeLiteral> {
|
||||
}
|
||||
|
||||
pub fn time_unit(s: &str) -> IResult<&str, TimeUnit> {
|
||||
let (s, x) = alt((
|
||||
symbol("s"),
|
||||
symbol("ms"),
|
||||
symbol("us"),
|
||||
symbol("ns"),
|
||||
symbol("ps"),
|
||||
symbol("fs"),
|
||||
))(s)?;
|
||||
let unit = match x {
|
||||
"s" => TimeUnit::S,
|
||||
"ms" => TimeUnit::MS,
|
||||
"us" => TimeUnit::US,
|
||||
"ns" => TimeUnit::NS,
|
||||
"ps" => TimeUnit::PS,
|
||||
"fs" => TimeUnit::FS,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
Ok((s, unit))
|
||||
alt((
|
||||
map(symbol("s"), |x| TimeUnit::S(x)),
|
||||
map(symbol("ms"), |x| TimeUnit::MS(x)),
|
||||
map(symbol("us"), |x| TimeUnit::US(x)),
|
||||
map(symbol("ns"), |x| TimeUnit::NS(x)),
|
||||
map(symbol("ps"), |x| TimeUnit::PS(x)),
|
||||
map(symbol("fs"), |x| TimeUnit::FS(x)),
|
||||
))(s)
|
||||
}
|
||||
|
||||
pub fn implicit_class_handle(s: &str) -> IResult<&str, ImplicitClassHandle> {
|
||||
|
@ -1,6 +1,7 @@
|
||||
use crate::parser::*;
|
||||
use nom::bytes::complete::*;
|
||||
use nom::character::complete::*;
|
||||
use nom::combinator::*;
|
||||
use nom::IResult;
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
@ -17,8 +18,8 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
pub fn symbol<'a>(t: &'a str) -> impl Fn(&'a str) -> IResult<&'a str, &'a str> {
|
||||
move |s: &'a str| ws(tag(t.clone()))(s)
|
||||
pub fn symbol<'a>(t: &'a str) -> impl Fn(&'a str) -> IResult<&'a str, Symbol<'a>> {
|
||||
move |s: &'a str| ws(map(tag(t.clone()), |x| Symbol { nodes: (x,) }))(s)
|
||||
}
|
||||
|
||||
pub fn paren<'a, O, F>(f: F) -> impl Fn(&'a str) -> IResult<&'a str, O>
|
||||
@ -69,6 +70,11 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Symbol<'a> {
|
||||
pub nodes: (&'a str,),
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#[derive(Debug)]
|
||||
|
Loading…
x
Reference in New Issue
Block a user