Work
This commit is contained in:
parent
bb06350404
commit
05c9ada7c6
@ -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
|
/// 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 chars = input.chars();
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
|
|
||||||
match chars.next() {
|
match chars.next() {
|
||||||
Some(ch) if ch.is_alphabetic() => buf.push(ch),
|
Some(ch) if ch.is_alphabetic() => buf.push(ch),
|
||||||
_ => return Err((input, input)),
|
_ => return Err(((), input)),
|
||||||
}
|
}
|
||||||
|
|
||||||
for next in chars {
|
for next in chars {
|
||||||
|
@ -22,13 +22,16 @@ fn parse_sexp() {
|
|||||||
literal("#f").to(Atom::Bool(false)),
|
literal("#f").to(Atom::Bool(false)),
|
||||||
));
|
));
|
||||||
|
|
||||||
let parse_symbol = identifier;
|
let parse_symbol = identifier.map(Atom::Symbol);
|
||||||
let parse_number = repeated(one_of("1234567890"))
|
let parse_number = repeated(one_of("1234567890"))
|
||||||
.at_least(1)
|
.at_least(1)
|
||||||
.map(|n| Atom::Num(n.iter().collect::<String>().parse::<i64>().unwrap()));
|
.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();
|
let output = parser.parse("#t").unwrap();
|
||||||
assert_eq!(output.0, Atom::Bool(true));
|
assert_eq!(output.0, Atom::Bool(true));
|
||||||
|
|
||||||
|
let output = parser.parse("384").unwrap();
|
||||||
|
assert_eq!(output.0, Atom::Num(384));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user