Fix parsing

This commit is contained in:
Greg Shuflin 2024-01-29 19:37:18 -08:00
parent a9d08a9213
commit 33899ae66e
3 changed files with 5 additions and 9 deletions

View File

@ -49,7 +49,6 @@ pub fn identifier(input: &str) -> ParseResult<&str, String, ()> {
} }
pub fn whitespace(input: &str) -> ParseResult<&str, char, ()> { pub fn whitespace(input: &str) -> ParseResult<&str, char, ()> {
println!("whitespace: `{input}`");
match input.chars().next() { match input.chars().next() {
Some(ch) if ch.is_whitespace() => Ok((ch, &input[1..])), Some(ch) if ch.is_whitespace() => Ok((ch, &input[1..])),
_ => Err(((), input)), _ => Err(((), input)),

View File

@ -12,13 +12,9 @@ pub fn surrounded_by<I, O1, O2, E>(
surrounding: impl Parser<I, O2, E>, surrounding: impl Parser<I, O2, E>,
) -> impl Parser<I, O1, E> { ) -> impl Parser<I, O1, E> {
move |input| { move |input| {
println!("surrounded_by");
let (_result1, rest1) = surrounding.parse(input)?; let (_result1, rest1) = surrounding.parse(input)?;
println!("A");
let (result2, rest2) = main.parse(rest1)?; let (result2, rest2) = main.parse(rest1)?;
println!("B");
let (_result3, rest3) = surrounding.parse(rest2)?; let (_result3, rest3) = surrounding.parse(rest2)?;
println!("C");
Ok((result2, rest3)) Ok((result2, rest3))
} }
} }

View File

@ -48,7 +48,6 @@ fn test_parse_atom() {
} }
fn parse_expr(input: &str) -> ParseResult<&str, Expr, ()> { fn parse_expr(input: &str) -> ParseResult<&str, Expr, ()> {
println!("parse_expr: {input}");
choice((parse_list, parse_atom.map(Expr::Atom))).parse(input) choice((parse_list, parse_atom.map(Expr::Atom))).parse(input)
} }
@ -61,15 +60,17 @@ fn parse_list(input: &str) -> ParseResult<&str, Expr, ()> {
) )
.then_ignore(literal_char(')')) .then_ignore(literal_char(')'))
.map(Expr::List) .map(Expr::List)
.surrounded_by(repeated(whitespace))
.parse(input) .parse(input)
} }
#[test] #[test]
fn test_parse_list() { fn test_parse_list() {
let output = parse_list.parse("\n(1 2 (1 2) 9999 3)").unwrap(); let output = parse_list.parse("(1 2 (1 2) 9999 3)").unwrap();
assert_eq!(output.1, ""); assert_eq!(output.1, "");
}
fn parse_sexp(input: &str) -> ParseResult<&str, Expr, ()> {
parse_list.surrounded_by(repeated(whitespace)).parse(input)
} }
#[test] #[test]
@ -91,6 +92,6 @@ fn test_parse_sexp() {
)"# )"#
.trim(); .trim();
let output = parse_expr(complex_input).unwrap(); let output = parse_sexp(complex_input).unwrap();
assert_eq!(output.1, ""); assert_eq!(output.1, "");
} }