use crate::lexer::FloatVal;
use crate::parser::{Cursor, Parse, Parser, Peek, Result};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::str;
use crate::ast::annotation;
#[derive(Copy, Clone, Debug)]
pub struct Span {
pub(crate) offset: usize,
}
impl Span {
pub fn linecol_in(&self, text: &str) -> (usize, usize) {
let mut cur = 0;
for (i, line) in text.split_terminator('\n').enumerate() {
if cur + line.len() + 1 > self.offset {
return (i, self.offset - cur);
}
cur += line.len() + 1;
}
(text.lines().count(), 0)
}
}
#[derive(Copy, Clone)]
pub struct Id<'a> {
name: &'a str,
span: Span,
}
impl<'a> Id<'a> {
pub fn name(&self) -> &'a str {
self.name
}
pub fn span(&self) -> Span {
self.span
}
}
impl<'a> Hash for Id<'a> {
fn hash<H: Hasher>(&self, hasher: &mut H) {
self.name.hash(hasher);
}
}
impl<'a> PartialEq for Id<'a> {
fn eq(&self, other: &Id<'a>) -> bool {
self.name == other.name
}
}
impl<'a> Eq for Id<'a> {}
impl<'a> Parse<'a> for Id<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.step(|c| {
if let Some((name, rest)) = c.id() {
return Ok((
Id {
name,
span: c.cur_span(),
},
rest,
));
}
Err(c.error("expected an identifier"))
})
}
}
impl fmt::Debug for Id<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.name.fmt(f)
}
}
impl Peek for Id<'_> {
fn peek(cursor: Cursor<'_>) -> bool {
cursor.id().is_some()
}
fn display() -> &'static str {
"an identifier"
}
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Index<'a> {
Num(u32),
Id(Id<'a>),
}
impl<'a> Parse<'a> for Index<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
let mut l = parser.lookahead1();
if l.peek::<Id>() {
Ok(Index::Id(parser.parse()?))
} else if l.peek::<u32>() {
Ok(Index::Num(parser.parse()?))
} else {
Err(l.error())
}
}
}
impl Peek for Index<'_> {
fn peek(cursor: Cursor<'_>) -> bool {
u32::peek(cursor) || Id::peek(cursor)
}
fn display() -> &'static str {
"an index"
}
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct NameAnnotation<'a> {
pub name: &'a str,
}
impl<'a> Parse<'a> for NameAnnotation<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.parse::<annotation::name>()?;
let name = parser.parse()?;
Ok(NameAnnotation { name })
}
}
impl<'a> Parse<'a> for Option<NameAnnotation<'a>> {
fn parse(parser: Parser<'a>) -> Result<Self> {
let _r = parser.register_annotation("name");
Ok(if parser.peek2::<annotation::name>() {
Some(parser.parens(|p| p.parse())?)
} else {
None
})
}
}
macro_rules! integers {
($($i:ident($u:ident))*) => ($(
impl<'a> Parse<'a> for $i {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.step(|c| {
if let Some((i, rest)) = c.integer() {
let (s, base) = i.val();
let val = $i::from_str_radix(s, base)
.or_else(|_| {
$u::from_str_radix(s, base).map(|i| i as $i)
});
return match val {
Ok(n) => Ok((n, rest)),
Err(_) => Err(c.error(concat!(
"invalid ",
stringify!($i),
" number: constant out of range",
))),
};
}
Err(c.error(concat!("expected a ", stringify!($i))))
})
}
}
impl Peek for $i {
fn peek(cursor: Cursor<'_>) -> bool {
cursor.integer().is_some()
}
fn display() -> &'static str {
stringify!($i)
}
}
)*)
}
integers! {
u8(u8) u16(u16) u32(u32) u64(u64)
i8(u8) i16(u16) i32(u32) i64(u64)
}
impl<'a> Parse<'a> for &'a [u8] {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.step(|c| {
if let Some((i, rest)) = c.string() {
return Ok((i, rest));
}
Err(c.error("expected a string"))
})
}
}
impl Peek for &'_ [u8] {
fn peek(cursor: Cursor<'_>) -> bool {
cursor.string().is_some()
}
fn display() -> &'static str {
"string"
}
}
impl<'a> Parse<'a> for &'a str {
fn parse(parser: Parser<'a>) -> Result<Self> {
str::from_utf8(parser.parse()?).map_err(|_| parser.error("invalid UTF-8 encoding"))
}
}
impl Parse<'_> for String {
fn parse(parser: Parser<'_>) -> Result<Self> {
Ok(<&str>::parse(parser)?.to_string())
}
}
impl Peek for &'_ str {
fn peek(cursor: Cursor<'_>) -> bool {
<&[u8]>::peek(cursor)
}
fn display() -> &'static str {
<&[u8]>::display()
}
}
macro_rules! float {
($($name:ident => {
bits: $int:ident,
float: $float:ident,
exponent_bits: $exp_bits:tt,
name: $parse:ident,
})*) => ($(
#[derive(Debug)]
pub struct $name {
pub bits: $int,
}
impl<'a> Parse<'a> for $name {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.step(|c| {
let (val, rest) = if let Some((f, rest)) = c.float() {
($parse(f.val()), rest)
} else if let Some((i, rest)) = c.integer() {
let (s, base) = i.val();
(
$parse(&FloatVal::Val {
hex: base == 16,
integral: s.into(),
decimal: None,
exponent: None,
}),
rest,
)
} else {
return Err(c.error("expected a float"));
};
match val {
Some(bits) => Ok(($name { bits }, rest)),
None => Err(c.error("invalid float value: constant out of range")),
}
})
}
}
fn $parse(val: &FloatVal<'_>) -> Option<$int> {
let width = std::mem::size_of::<$int>() * 8;
let neg_offset = width - 1;
let exp_offset = neg_offset - $exp_bits;
let signif_bits = width - 1 - $exp_bits;
let signif_mask = (1 << exp_offset) - 1;
let bias = (1 << ($exp_bits - 1)) - 1;
let (hex, integral, decimal, exponent_str) = match val {
FloatVal::Inf { negative } => {
let exp_bits = (1 << $exp_bits) - 1;
let neg_bit = *negative as $int;
return Some(
(neg_bit << neg_offset) |
(exp_bits << exp_offset)
);
}
FloatVal::Nan { negative, val } => {
let exp_bits = (1 << $exp_bits) - 1;
let neg_bit = *negative as $int;
let signif = val.unwrap_or(1 << (signif_bits - 1)) as $int;
if signif & signif_mask == 0 {
return None;
}
return Some(
(neg_bit << neg_offset) |
(exp_bits << exp_offset) |
(signif & signif_mask)
);
}
FloatVal::Val { hex, integral, decimal, exponent } => {
(hex, integral, decimal, exponent)
}
};
if !*hex {
let mut s = integral.to_string();
if let Some(decimal) = decimal {
s.push_str(".");
s.push_str(&decimal);
}
if let Some(exponent) = exponent_str {
s.push_str("e");
s.push_str(&exponent);
}
let float = s.parse::<$float>().ok()?;
if float.is_infinite() {
return None;
}
return Some(float.to_bits());
}
let decimal = decimal.as_ref().map(|s| &**s).unwrap_or("");
let negative = integral.starts_with('-');
let integral = integral.trim_start_matches('-').trim_start_matches('0');
let decimal_no_leading = decimal.trim_start_matches('0');
let decimal_iter = if integral.is_empty() {
decimal_no_leading.chars()
} else {
decimal.chars()
};
let mut digits = integral.chars()
.map(|c| (to_hex(c) as $int, false))
.chain(decimal_iter.map(|c| (to_hex(c) as $int, true)));
let lead_nonzero_digit = match digits.next() {
Some((c, _)) => c,
None if negative => return Some(1 << (width - 1)),
None => return Some(0),
};
let mut significand = 0 as $int;
let mut exponent = if !integral.is_empty() {
1
} else {
-((decimal.len() - decimal_no_leading.len() + 1) as i32) + 1
};
let lz = (lead_nonzero_digit as u8).leading_zeros() as i32 - 4;
exponent = exponent.checked_mul(4)?.checked_sub(lz + 1)?;
let mut significand_pos = (width - (4 - (lz as usize))) as isize;
assert!(significand_pos >= 0);
significand |= lead_nonzero_digit << significand_pos;
let mut discarded_extra_nonzero = false;
for (digit, decimal) in digits {
if !decimal {
exponent += 4;
}
if significand_pos > -4 {
significand_pos -= 4;
}
if significand_pos >= 0 {
significand |= digit << significand_pos;
} else if significand_pos > -4 {
significand |= digit >> (4 - significand_pos);
discarded_extra_nonzero = (digit & !((!0) >> (4 - significand_pos))) != 0;
} else if digit != 0 {
discarded_extra_nonzero = true;
}
}
exponent = exponent.checked_add(match exponent_str {
Some(s) => s.parse::<i32>().ok()?,
None => 0,
})?;
debug_assert!(significand != 0);
let (encoded_exponent, encoded_significand, discarded_significand) =
if exponent <= -bias {
let shift = exp_offset as i32 + exponent + bias;
if shift == 0 {
(0, 0, significand)
} else if shift < 0 || shift >= width as i32 {
(0, 0, 0)
} else {
(
0,
significand >> (width as i32 - shift),
significand << shift,
)
}
} else if exponent <= bias {
(
((exponent + bias) as $int) << exp_offset,
(significand >> (width - exp_offset - 1)) & signif_mask,
significand << (exp_offset + 1),
)
} else {
(
((1 << $exp_bits) - 1) << exp_offset,
0,
0,
)
};
let bits = encoded_exponent | encoded_significand;
let msb = 1 << (width - 1);
let bits = bits
+ (((discarded_significand & msb != 0)
&& ((discarded_significand & !msb != 0) ||
discarded_extra_nonzero ||
(encoded_significand & 1 != 0))) as $int);
let bits = if negative {
bits | (1 << (width - 1))
} else {
bits
};
if $float::from_bits(bits).is_infinite() {
return None;
}
Some(bits)
}
)*)
}
float! {
Float32 => {
bits: u32,
float: f32,
exponent_bits: 8,
name: strtof,
}
Float64 => {
bits: u64,
float: f64,
exponent_bits: 11,
name: strtod,
}
}
fn to_hex(c: char) -> u8 {
match c {
'a'..='f' => c as u8 - b'a' + 10,
'A'..='F' => c as u8 - b'A' + 10,
_ => c as u8 - b'0',
}
}
pub struct LParen {
_priv: (),
}
impl Peek for LParen {
fn peek(cursor: Cursor<'_>) -> bool {
cursor.lparen().is_some()
}
fn display() -> &'static str {
"left paren"
}
}
#[cfg(test)]
mod tests {
#[test]
fn hex_strtof() {
macro_rules! f {
($a:tt) => (f!(@mk $a, None, None));
($a:tt p $e:tt) => (f!(@mk $a, None, Some($e.into())));
($a:tt . $b:tt) => (f!(@mk $a, Some($b.into()), None));
($a:tt . $b:tt p $e:tt) => (f!(@mk $a, Some($b.into()), Some($e.into())));
(@mk $a:tt, $b:expr, $e:expr) => (crate::lexer::FloatVal::Val {
hex: true,
integral: $a.into(),
decimal: $b,
exponent: $e
});
}
assert_eq!(super::strtof(&f!("0")), Some(0));
assert_eq!(super::strtof(&f!("0" . "0")), Some(0));
assert_eq!(super::strtof(&f!("0" . "0" p "2354")), Some(0));
assert_eq!(super::strtof(&f!("-0")), Some(1 << 31));
assert_eq!(super::strtof(&f!("f32")), Some(0x45732000));
assert_eq!(super::strtof(&f!("0" . "f32")), Some(0x3f732000));
assert_eq!(super::strtof(&f!("1" . "2")), Some(0x3f900000));
assert_eq!(
super::strtof(&f!("0" . "00000100000000000" p "-126")),
Some(0)
);
assert_eq!(
super::strtof(&f!("1" . "fffff4" p "-106")),
Some(0x0afffffa)
);
assert_eq!(super::strtof(&f!("fffff98" p "-133")), Some(0x0afffffa));
assert_eq!(super::strtof(&f!("0" . "081" p "023")), Some(0x48810000));
assert_eq!(
super::strtof(&f!("1" . "00000100000000000" p "-50")),
Some(0x26800000)
);
}
}