Add Decimal number type.

This commit is contained in:
Stephen Chung
2021-02-13 20:57:56 +08:00
parent 4fdd58f220
commit 3650b04c38
15 changed files with 477 additions and 16 deletions

View File

@@ -17,6 +17,9 @@ use crate::{Engine, LexError, StaticVec, INT};
#[cfg(not(feature = "no_float"))]
use crate::ast::FloatWrapper;
#[cfg(feature = "decimal")]
use rust_decimal::Decimal;
type LERR = LexError;
pub type TokenStream<'a, 't> = Peekable<TokenIterator<'a, 't>>;
@@ -162,6 +165,11 @@ pub enum Token {
/// Reserved under the `no_float` feature.
#[cfg(not(feature = "no_float"))]
FloatConstant(FloatWrapper),
/// A [`Decimal`] constant.
///
/// Requires the `decimal` feature.
#[cfg(feature = "decimal")]
DecimalConstant(Decimal),
/// An identifier.
Identifier(String),
/// A character constant.
@@ -348,6 +356,8 @@ impl Token {
IntegerConstant(i) => i.to_string().into(),
#[cfg(not(feature = "no_float"))]
FloatConstant(f) => f.to_string().into(),
#[cfg(feature = "decimal")]
DecimalConstant(d) => d.to_string().into(),
StringConstant(_) => "string".into(),
CharConstant(c) => c.to_string().into(),
Identifier(s) => s.clone().into(),
@@ -1073,7 +1083,7 @@ fn get_next_token_inner(
result.push(next_char);
eat_next(stream, pos);
}
#[cfg(not(feature = "no_float"))]
#[cfg(any(not(feature = "no_float"), feature = "decimal"))]
'.' => {
stream.get_next().unwrap();
@@ -1183,6 +1193,10 @@ fn get_next_token_inner(
let num =
num.or_else(|_| FloatWrapper::from_str(&out).map(Token::FloatConstant));
// Then try decimal
#[cfg(feature = "decimal")]
let num = num.or_else(|_| Decimal::from_str(&out).map(Token::DecimalConstant));
return Some((
num.unwrap_or_else(|_| {
Token::LexError(LERR::MalformedNumber(result.into_iter().collect()))