pub struct Digital<'lex> { /* private fields */ }
Expand description
A digital literal.
See rule::Digital
for an explanation on what a “digital” is (briefly,
they are generalized number literals).
Note that this crate does not provide general number parsing services;
parsing numbers is a complex and subtle undertaking, particularly when it
comes to floats. For that, consider reading the source code of the
rustc_apfloat
library, or another softfloat library.
However, we do provide parsing for a few common cases: decimal, binary, octal, and hexadecimal. For floats, this means that the radix of the mantissa is decimal, binary, octal, or hex, the radix of the exponent is decimal, and the base of the exponent is 10 (for decimal) or 2 (for the others).
Implementations§
source§impl<'lex> Digital<'lex>
impl<'lex> Digital<'lex>
sourcepub fn sign(self) -> Option<Sign>
pub fn sign(self) -> Option<Sign>
Returns the leading sign of this digital literal, if it had any.
sourcepub fn is_positive(self) -> bool
pub fn is_positive(self) -> bool
Checks if there was an explicit positive sign.
sourcepub fn is_negative(self) -> bool
pub fn is_negative(self) -> bool
Checks if there was an explicit positive sign.
sourcepub fn sign_span(self) -> Option<SpanId>
pub fn sign_span(self) -> Option<SpanId>
Returns the span corresponding to Digital::sign()
.
sourcepub fn digit_blocks(self) -> impl Iterator<Item = SpanId> + 'lex
pub fn digit_blocks(self) -> impl Iterator<Item = SpanId> + 'lex
Returns the point-separated digit chunks of this digital literal.
sourcepub fn exponents(self) -> impl Iterator<Item = Digital<'lex>>
pub fn exponents(self) -> impl Iterator<Item = Digital<'lex>>
Returns the exponents of this digital literal, if it any.
Calling exponents()
on any of the returned tokens will yield all
exponents that follow.
sourcepub fn has_prefix(&self, expected: &str) -> bool
pub fn has_prefix(&self, expected: &str) -> bool
Checks whether this identifier has a particular prefix.
sourcepub fn has_suffix(&self, expected: &str) -> bool
pub fn has_suffix(&self, expected: &str) -> bool
Checks whether this identifier has a particular prefix.
sourcepub fn to_int<N>(self, range: impl RangeBounds<N>, report: &Report) -> N
pub fn to_int<N>(self, range: impl RangeBounds<N>, report: &Report) -> N
Parses this token as an integer.
More than one digit block, or any exponents, will be diagnosed as an error.
Parse failures become diagnostics, and an unspecified value is provided for a failed integer.
sourcepub fn to_ints<N>(self, range: impl RangeBounds<N>, report: &Report) -> Vec<N>
pub fn to_ints<N>(self, range: impl RangeBounds<N>, report: &Report) -> Vec<N>
Parses the blocks of this digital literal as a sequence of integers; this ignores any exponents that follow.
Parse failures become diagnostics, and an unspecified value is provided for a failed integer.
sourcepub fn to_float<Fp: Parse>(
self,
range: impl RangeBounds<Fp>,
report: &Report,
) -> Result<Fp, Exotic>
pub fn to_float<Fp: Parse>( self, range: impl RangeBounds<Fp>, report: &Report, ) -> Result<Fp, Exotic>
Parses this token as a float. Fp
can be any of the float types defined
in ilex::fp
.
This function supports floats with a mantissa radix that is either 10 or a power of two. In the former case, the base for the exponent is 10; otherwise, it is 2.
Any other radix will produce Err
. It is highly recommended to
unwrap()
the result, since a return value of Exotic
is almost
certainly a bug.
Parse failures and overflow to infinity will generate diagnostics.
sourcepub fn to_float_exact<Fp: Parse>(
self,
range: impl RangeBounds<Fp>,
report: &Report,
) -> Result<Fp, Exotic>
pub fn to_float_exact<Fp: Parse>( self, range: impl RangeBounds<Fp>, report: &Report, ) -> Result<Fp, Exotic>
Parses this token as a float, with no rounding. Fp
can be any of the
float types defined in token::fp
.
This function is like Digital::to_float()
, except that it also
generates a diagnostic if rounding while converting to base 2.
Trait Implementations§
source§impl Spanned for Digital<'_>
impl Spanned for Digital<'_>
source§fn comments<'ctx>(&self, ctx: &'ctx Context) -> Comments<'ctx>
fn comments<'ctx>(&self, ctx: &'ctx Context) -> Comments<'ctx>
SpanId::comments()
.impl<'lex> Copy for Digital<'lex>
Auto Trait Implementations§
impl<'lex> Freeze for Digital<'lex>
impl<'lex> RefUnwindSafe for Digital<'lex>
impl<'lex> Send for Digital<'lex>
impl<'lex> Sync for Digital<'lex>
impl<'lex> Unpin for Digital<'lex>
impl<'lex> UnwindSafe for Digital<'lex>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)