summaryrefslogtreecommitdiffstats
path: root/crates/rebel-parse/src/ast.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/rebel-parse/src/ast.rs')
-rw-r--r--crates/rebel-parse/src/ast.rs15
1 files changed, 12 insertions, 3 deletions
diff --git a/crates/rebel-parse/src/ast.rs b/crates/rebel-parse/src/ast.rs
index d923f2a..1f98f15 100644
--- a/crates/rebel-parse/src/ast.rs
+++ b/crates/rebel-parse/src/ast.rs
@@ -138,9 +138,18 @@ pub enum Literal<'a> {
}
impl<'a> Literal<'a> {
- pub(crate) fn integer(s: &'a str, radix: u32) -> Result<Self, &'static str> {
- let s = s.replace('_', "");
- let value = u64::from_str_radix(&s, radix).or(Err("Failed to parse number"))?;
+ pub(crate) fn number(s: &'a str) -> Result<Self, &'static str> {
+ let (radix, rest) = if let Some(rest) = s.strip_prefix("0x") {
+ (16, rest)
+ } else if let Some(rest) = s.strip_prefix("0o") {
+ (8, rest)
+ } else if let Some(rest) = s.strip_prefix("0b") {
+ (2, rest)
+ } else {
+ (10, s)
+ };
+ let digits = rest.replace('_', "");
+ let value = u64::from_str_radix(&digits, radix).or(Err("number"))?;
Ok(Literal::Integer(value))
}
}