Skip to content

Commit

Permalink
Check the paragraph break
Browse files Browse the repository at this point in the history
  • Loading branch information
oovm committed Jun 21, 2024
1 parent 74878a5 commit d69f7f3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
11 changes: 11 additions & 0 deletions projects/pex-core/src/helpers/comment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ impl<'i> FnOnce<(ParseState<'i>,)> for CommentLine {
}
}

/// Parse the rest of the line, note this does not catch the newline,
pub fn rest_of_line(input: ParseState) -> ParseResult<StringView> {
let offset = match input.residual.find(&['\r', '\n']) {
Some(s) => s,
None => input.residual.len(),
};
// SAFETY: find offset always valid
let body = unsafe { input.residual.get_unchecked(0..offset) };
input.advance(offset).finish(StringView::new(body, input.start_offset))
}

/// Parse the comment block
///
/// # Patterns
Expand Down
3 changes: 2 additions & 1 deletion projects/pex-core/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ pub use self::{
comment::{CommentBlock, CommentLine},
number::*,
string::{
quotation_pair, quotation_pair_escaped, quotation_pair_nested, surround_pair_with_escaper, unescape_us, UnicodeUnescape,
paragraph_break, quotation_pair, quotation_pair_escaped, quotation_pair_nested, surround_pair_with_escaper,
unescape_us, UnicodeUnescape,
},
surround_pair::{SurroundPair, SurroundPattern},
trie_set::CharactersTrie,
Expand Down
32 changes: 32 additions & 0 deletions projects/pex-core/src/helpers/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,35 @@ pub fn unescape_us(input: ParseState) -> ParseResult<char> {
None => StopBecause::custom_error("Characters must not beyond U+10FFFF", start.start_offset, state.start_offset)?,
}
}

/// A period of whitespace with more than two newlines, and terminated by a newline
pub fn paragraph_break<'i>(input: ParseState<'i>) -> ParseResult<&'i str> {
let mut offset = 0;
// Capture all newlines and spaces
for c in input.residual.chars() {
if c.is_whitespace() {
offset += c.len_utf8();
}
else {
break;
}
}
let text = unsafe { input.residual.get_unchecked(..offset) };
// Fallback for spaces that don't have to be captured
for c in text.chars().rev() {
if c == ' ' {
offset -= c.len_utf8();
}
else {
break;
}
}
if offset == 0 {
StopBecause::missing_string("PARAGRAPH_LINE", input.start_offset)?;
}
let newlines = text.chars().filter(|c| *c == '\n').count();
if newlines <= 1 {
StopBecause::missing_string("PARAGRAPH_BREAK", input.start_offset)?;
}
input.advance_view(offset)
}

0 comments on commit d69f7f3

Please sign in to comment.