Implement number type

main
Elnu 1 year ago
parent 38948883e4
commit 247ce0ce98

@ -14,6 +14,7 @@ pub enum Token {
Str(String),
Array(Vec<Token>),
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<Rule>) -> Token {
"False" => false,
_ => unreachable!(),
}),
Rule::number => Token::Number(contents.as_str().parse().unwrap()),
Rule::keyword => Token::Keyword(contents.as_str().to_owned()),
__ => unreachable!(),
}

@ -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* }

Loading…
Cancel
Save