Introduce a sexp_with_padding parser.
This commit is contained in:
parent
7de72cab23
commit
6e406d71b6
2
build.rs
2
build.rs
@ -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;
|
||||
|
||||
"#
|
||||
)
|
||||
|
@ -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 }))
|
||||
|
@ -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,
|
||||
|
@ -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 =
|
||||
|
Loading…
Reference in New Issue
Block a user