Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move take_empty_line and discard_empty_lines into Span #86

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/blocks/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use nom::IResult;

use crate::{
blocks::{ContentModel, IsBlock, MacroBlock, SectionBlock, SimpleBlock},
primitives::{consume_empty_lines, normalized_line},
primitives::normalized_line,
strings::CowStr,
HasSpan, Span,
};
Expand Down Expand Up @@ -43,7 +43,7 @@ impl<'a> Block<'a> {
///
/// Consumes any blank lines before and after the block.
pub(crate) fn parse(i: Span<'a>) -> IResult<Span, Self> {
let i = consume_empty_lines(i);
let i = i.discard_empty_lines();

// Try to discern the block type by scanning the first line.
let line = normalized_line(i);
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use nom::{
use crate::{
attributes::Attrlist,
blocks::{ContentModel, IsBlock},
primitives::{consume_empty_lines, ident, normalized_line},
primitives::{ident, normalized_line},
strings::CowStr,
HasSpan, Span,
};
Expand Down Expand Up @@ -45,7 +45,7 @@ impl<'a> MacroBlock<'a> {
let (_, attrlist) = Attrlist::parse(attrlist)?;

Ok((
consume_empty_lines(line.rem),
line.rem.discard_empty_lines(),
Self {
name,
target: if target.is_empty() {
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/parse_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use nom::IResult;

use crate::{blocks::Block, primitives::consume_empty_lines, Span};
use crate::{blocks::Block, Span};

/// Parse blocks until end of input or a pre-determined stop condition is
/// reached.
Expand All @@ -9,7 +9,7 @@ where
F: Fn(&Span<'a>) -> bool,
{
let mut blocks: Vec<Block<'a>> = vec![];
i = consume_empty_lines(i);
i = i.discard_empty_lines();

while !i.data().is_empty() {
if f(&i) {
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use nom::{bytes::complete::tag, character::complete::space1, multi::many1_count,

use crate::{
blocks::{parse_utils::parse_blocks_until, Block, ContentModel, IsBlock},
primitives::{consume_empty_lines, non_empty_line, trim_input_for_rem},
primitives::{non_empty_line, trim_input_for_rem},
strings::CowStr,
HasSpan, Span,
};
Expand All @@ -27,7 +27,7 @@ pub struct SectionBlock<'a> {
impl<'a> SectionBlock<'a> {
#[allow(dead_code)]
pub(crate) fn parse(source: Span<'a>) -> IResult<Span, Self> {
let source = consume_empty_lines(source);
let source = source.discard_empty_lines();

let (rem, (level, title)) = parse_title_line(source)?;

Expand Down
4 changes: 2 additions & 2 deletions src/blocks/simple.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use nom::IResult;

use super::{ContentModel, IsBlock};
use crate::{inlines::Inline, primitives::consume_empty_lines, strings::CowStr, HasSpan, Span};
use crate::{inlines::Inline, strings::CowStr, HasSpan, Span};

/// A block that's treated as contiguous lines of paragraph text (and subject to
/// normal substitutions) (e.g., a paragraph block).
Expand All @@ -14,7 +14,7 @@ impl<'a> SimpleBlock<'a> {
source,
nom::error::ErrorKind::TakeTill1,
)))?;
Ok((consume_empty_lines(inline.rem), Self(inline.t)))
Ok((inline.rem.discard_empty_lines(), Self(inline.t)))
}

/// Return the inline content of this block.
Expand Down
3 changes: 1 addition & 2 deletions src/document/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::slice::Iter;
use crate::{
blocks::{parse_utils::parse_blocks_until, Block, ContentModel, IsBlock},
document::Header,
primitives::consume_empty_lines,
strings::CowStr,
Error, HasSpan, Span,
};
Expand Down Expand Up @@ -39,7 +38,7 @@ impl<'a> Document<'a> {
// TO DO: Add option for best-guess parsing?

let source = Span::new(source);
let i = consume_empty_lines(source);
let i = source.discard_empty_lines();

let (i, header) = if i.starts_with("= ") {
let (i, header) = Header::parse(i)?;
Expand Down
8 changes: 4 additions & 4 deletions src/document/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use nom::{

use crate::{
document::Attribute,
primitives::{consume_empty_lines, empty_line, non_empty_line, trim_input_for_rem},
primitives::{non_empty_line, trim_input_for_rem},
HasSpan, Span,
};

Expand All @@ -26,20 +26,20 @@ pub struct Header<'a> {

impl<'a> Header<'a> {
pub(crate) fn parse(i: Span<'a>) -> IResult<Span, Self> {
let source = consume_empty_lines(i);
let source = i.discard_empty_lines();

// TEMPORARY: Titles are optional, but we're not prepared for that yet.
let (rem, title) = parse_title(source)?;
let (rem, attributes) = many0(Attribute::parse)(rem)?;

// Header must be followed by an empty line.
if empty_line(rem).is_none() {
if rem.take_empty_line().is_none() {
return Err(Err::Error(Error::new(rem, ErrorKind::NonEmpty)));
}

let source = trim_input_for_rem(source, rem);
Ok((
consume_empty_lines(rem),
rem.discard_empty_lines(),
Self {
title: Some(title),
attributes,
Expand Down
29 changes: 0 additions & 29 deletions src/primitives/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,32 +114,3 @@ fn one_line_with_continuation(input: Span<'_>) -> IResult<Span, Span> {
Err(Err::Error(Error::new(input, ErrorKind::NonEmpty)))
}
}

/// Consumes an empty line.
///
/// An empty line may contain any number of white space characters.
///
/// Returns `None` if the line contains any non-white-space characters.
pub(crate) fn empty_line(i: Span<'_>) -> Option<ParseResult<Span>> {
let l = line(i);

if l.t.data().bytes().all(nom::character::is_space) {
Some(l)
} else {
None
}
}

/// Consumes zero or more empty lines.
///
/// Returns the original input if any error occurs or no empty lines are found.
pub(crate) fn consume_empty_lines(mut i: Span<'_>) -> Span {
while !i.data().is_empty() {
match empty_line(i) {
Some(line) => i = line.rem,
None => break,
}
}

i
}
4 changes: 1 addition & 3 deletions src/primitives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ use crate::Span;

mod line;
#[allow(unused_imports)]
pub(crate) use line::{
consume_empty_lines, empty_line, line, line_with_continuation, non_empty_line, normalized_line,
};
pub(crate) use line::{line, line_with_continuation, non_empty_line, normalized_line};

/// Given two [`Span`]s, the second of which must be a trailing remainder
/// of the first, return the first input trimmed to exclude the second.
Expand Down
38 changes: 38 additions & 0 deletions src/span/line.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#![allow(dead_code)] // TEMPORARY while refactoring

use super::{ParseResult, Span};
use crate::primitives::line;

impl<'a> Span<'a> {
/// Split the span, assuming the span begins with an empty line.
///
/// An empty line may contain any number of white space characters.
///
/// Returns `None` if the first line of the span contains any
/// non-white-space characters.
pub(crate) fn take_empty_line(self) -> Option<ParseResult<'a, Self>> {
let l = line(self);

if l.t.data().bytes().all(nom::character::is_space) {
Some(l)
} else {
None
}
}

/// Discard zero or more empty lines.
///
/// Return the original span if no empty lines are found.
pub(crate) fn discard_empty_lines(self) -> Span<'a> {
let mut i = self;

while !i.data().is_empty() {
match i.take_empty_line() {
Some(line) => i = line.rem,
None => break,
}
}

i
}
}
1 change: 1 addition & 0 deletions src/span/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ impl<'a> Deref for Span<'a> {
// The other modules referenced below implement additional APIs that are only
// available inside this crate only.

mod line;
mod nom_traits;
mod parse_result;
mod split;
Expand Down
Loading
Loading