From 247ce0ce987f420068a5fcbe74036d7c6121691c Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Wed, 24 May 2023 09:23:57 -0700 Subject: [PATCH] Implement number type --- renrs/src/lib.rs | 3 +++ renrs/src/rpy.pest | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/renrs/src/lib.rs b/renrs/src/lib.rs index 9b84807..d451396 100644 --- a/renrs/src/lib.rs +++ b/renrs/src/lib.rs @@ -14,6 +14,7 @@ pub enum Token { Str(String), Array(Vec), Boolean(bool), + Number(f64), } impl Token { @@ -23,6 +24,7 @@ impl Token { Str(_) => "String", Array(_) => "Array", Boolean(_) => "Boolean", + Number(_) => "Number", } } } @@ -110,6 +112,7 @@ fn parse_pair(pair: pest::iterators::Pair) -> Token { "False" => false, _ => unreachable!(), }), + Rule::number => Token::Number(contents.as_str().parse().unwrap()), Rule::keyword => Token::Keyword(contents.as_str().to_owned()), __ => unreachable!(), } diff --git a/renrs/src/rpy.pest b/renrs/src/rpy.pest index a49b9e2..da8e3c3 100644 --- a/renrs/src/rpy.pest +++ b/renrs/src/rpy.pest @@ -8,7 +8,7 @@ char = { !NEWLINE ~ ANY } // http://pest.rs/book/grammars/syntax.html#atomic inner = @{ char* } -token = { string | array | boolean | keyword } +token = { string | array | boolean | number | keyword } // KEYWORDS // has to be atomic for no implicit separate (spaces) @@ -37,6 +37,13 @@ array = { // BOOLEAN boolean = { "True" | "False" } +// NUMBER +number = @{ + "-"? + ~ ("0" | ASCII_NONZERO_DIGIT ~ ASCII_DIGIT*) + ~ ("." ~ ASCII_DIGIT*)? +} + // comments are a # followed by // any number of non-newline characters COMMENT = _{ "#" ~ char* }