Implement number type

main
Elnu 1 year ago
parent 38948883e4
commit 247ce0ce98

@ -14,6 +14,7 @@ pub enum Token {
Str(String), Str(String),
Array(Vec<Token>), Array(Vec<Token>),
Boolean(bool), Boolean(bool),
Number(f64),
} }
impl Token { impl Token {
@ -23,6 +24,7 @@ impl Token {
Str(_) => "String", Str(_) => "String",
Array(_) => "Array", Array(_) => "Array",
Boolean(_) => "Boolean", Boolean(_) => "Boolean",
Number(_) => "Number",
} }
} }
} }
@ -110,6 +112,7 @@ fn parse_pair(pair: pest::iterators::Pair<Rule>) -> Token {
"False" => false, "False" => false,
_ => unreachable!(), _ => unreachable!(),
}), }),
Rule::number => Token::Number(contents.as_str().parse().unwrap()),
Rule::keyword => Token::Keyword(contents.as_str().to_owned()), Rule::keyword => Token::Keyword(contents.as_str().to_owned()),
__ => unreachable!(), __ => unreachable!(),
} }

@ -8,7 +8,7 @@ char = { !NEWLINE ~ ANY }
// http://pest.rs/book/grammars/syntax.html#atomic // http://pest.rs/book/grammars/syntax.html#atomic
inner = @{ char* } inner = @{ char* }
token = { string | array | boolean | keyword } token = { string | array | boolean | number | keyword }
// KEYWORDS // KEYWORDS
// has to be atomic for no implicit separate (spaces) // has to be atomic for no implicit separate (spaces)
@ -37,6 +37,13 @@ array = {
// BOOLEAN // BOOLEAN
boolean = { "True" | "False" } boolean = { "True" | "False" }
// NUMBER
number = @{
"-"?
~ ("0" | ASCII_NONZERO_DIGIT ~ ASCII_DIGIT*)
~ ("." ~ ASCII_DIGIT*)?
}
// comments are a # followed by // comments are a # followed by
// any number of non-newline characters // any number of non-newline characters
COMMENT = _{ "#" ~ char* } COMMENT = _{ "#" ~ char* }

Loading…
Cancel
Save