Introduce a sexp_with_padding parser.

This commit is contained in:
Tom Alexander 2023-04-21 20:53:55 -04:00
parent 7de72cab23
commit 6e406d71b6
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 24 additions and 5 deletions

View File

@ -61,7 +61,7 @@ fn write_header(test_file: &mut File) {
use organic::compare_document;
use organic::parser::document;
use organic::emacs_parse_org_document;
use organic::parser::sexp::sexp;
use organic::parser::sexp::sexp_with_padding;
"#
)

View File

@ -1,9 +1,16 @@
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::character::complete::line_ending;
use nom::character::complete::space0;
use nom::combinator::eof;
use nom::combinator::recognize;
use nom::sequence::tuple;
use super::sexp::sexp;
use super::Context;
use crate::error::Res;
use crate::parser::util::get_consumed;
use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting;
use crate::parser::util::start_of_line;
use crate::parser::DiarySexp;
@ -13,6 +20,12 @@ pub fn diary_sexp<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s
let (remaining, _leading_whitespace) = space0(input)?;
let (remaining, _clock) = tag("%%")(remaining)?;
let (remaining, _gap_whitespace) = space0(remaining)?;
let (remaining, _sexp) = recognize(sexp)(remaining)?;
let (remaining, _trailing_whitespace) =
recognize(tuple((space0, alt((line_ending, eof)))))(remaining)?;
let (remaining, _trailing_ws) =
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
let source = get_consumed(input, remaining);
Ok((remaining, DiarySexp { source }))

View File

@ -74,13 +74,19 @@ impl<'s> Token<'s> {
}
#[tracing::instrument(ret, level = "debug")]
pub fn sexp<'s>(input: &'s str) -> Res<&'s str, Token<'s>> {
pub fn sexp_with_padding<'s>(input: &'s str) -> Res<&'s str, Token<'s>> {
let (remaining, _) = multispace0(input)?;
let (remaining, tkn) = token(remaining)?;
let (remaining, _) = multispace0(remaining)?;
Ok((remaining, tkn))
}
#[tracing::instrument(ret, level = "debug")]
pub fn sexp<'s>(input: &'s str) -> Res<&'s str, Token<'s>> {
let (remaining, tkn) = token(input)?;
Ok((remaining, tkn))
}
#[tracing::instrument(ret, level = "debug")]
fn token<'s>(input: &'s str) -> Res<&'s str, Token<'s>> {
alt((list, atom))(input)
@ -178,7 +184,7 @@ mod tests {
#[test]
fn simple() {
let input = " (foo bar baz ) ";
let (remaining, parsed) = sexp(input).expect("Parse the input");
let (remaining, parsed) = sexp_with_padding(input).expect("Parse the input");
assert_eq!(remaining, "");
assert!(match parsed {
Token::Atom(_) => false,
@ -190,7 +196,7 @@ mod tests {
#[test]
fn quoted() {
let input = r#" ("foo" bar baz ) "#;
let (remaining, parsed) = sexp(input).expect("Parse the input");
let (remaining, parsed) = sexp_with_padding(input).expect("Parse the input");
assert_eq!(remaining, "");
assert!(match parsed {
Token::Atom(_) => false,

View File

@ -5,7 +5,7 @@ fn {name}() {{
println!("{{}}", org_contents);
let org_sexp = emacs_parse_org_document(todo_org_path).expect("Use emacs to parse org file.");
println!("{{}}", org_sexp);
let (_remaining, parsed_sexp) = sexp(org_sexp.as_str()).expect("Sexp Parse failure");
let (_remaining, parsed_sexp) = sexp_with_padding(org_sexp.as_str()).expect("Sexp Parse failure");
let (remaining, rust_parsed) = document(org_contents.as_str()).expect("Org Parse failure");
println!("{{:#?}}", rust_parsed);
let diff_result =