This commit is contained in:
Greg Shuflin 2024-01-28 12:03:30 -08:00
parent bb06350404
commit 05c9ada7c6
2 changed files with 7 additions and 4 deletions

View File

@ -27,13 +27,13 @@ pub fn one_of<'a>(items: &'static str) -> impl Parser<&'a str, char, ()> {
}
/// Parses a standard identifier in a programming language
pub fn identifier(input: &str) -> ParseResult<&str, String, &str> {
pub fn identifier(input: &str) -> ParseResult<&str, String, ()> {
let mut chars = input.chars();
let mut buf = String::new();
match chars.next() {
Some(ch) if ch.is_alphabetic() => buf.push(ch),
_ => return Err((input, input)),
_ => return Err(((), input)),
}
for next in chars {

View File

@ -22,13 +22,16 @@ fn parse_sexp() {
literal("#f").to(Atom::Bool(false)),
));
let parse_symbol = identifier;
let parse_symbol = identifier.map(Atom::Symbol);
let parse_number = repeated(one_of("1234567890"))
.at_least(1)
.map(|n| Atom::Num(n.iter().collect::<String>().parse::<i64>().unwrap()));
let parser = choice((parse_bool, parse_number));
let parser = choice((parse_symbol, parse_bool, parse_number));
let output = parser.parse("#t").unwrap();
assert_eq!(output.0, Atom::Bool(true));
let output = parser.parse("384").unwrap();
assert_eq!(output.0, Atom::Num(384));
}