summaryrefslogtreecommitdiffstats
path: root/crates/rebel-parse/src/ast.rs
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2024-04-21 13:57:45 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2024-04-22 17:53:49 +0200
commit767842ab64f601b324b02a0f7f55ebc0b849f251 (patch)
tree71af28ee7b3f5837b60a29d752b7df25843a920d /crates/rebel-parse/src/ast.rs
parent458afaa9b725b295fc634121350da547cf668f25 (diff)
downloadrebel-767842ab64f601b324b02a0f7f55ebc0b849f251.tar
rebel-767842ab64f601b324b02a0f7f55ebc0b849f251.zip
rebel-parse: separate tokenization step
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))
}
}