Convert all functions to using the wrapped input type.
This commit is contained in:
@@ -50,7 +50,9 @@ pub fn immediate_in_section<'r, 's, 'x>(context: Context<'r, 's>, section_name:
|
||||
}
|
||||
|
||||
/// Get one character from before the current position.
|
||||
pub fn get_one_before<'s>(document: &'s str, current_position: &'s str) -> Option<&'s str> {
|
||||
pub fn get_one_before<'s>(document: &'s str, current_position: OrgSource<'s>) -> Option<&'s str> {
|
||||
// TODO: This should be replaced with new logic now that we are wrapping the input type.
|
||||
let current_position = Into::<&str>::into(¤t_position);
|
||||
assert!(is_slice_of(document, current_position));
|
||||
if document.as_ptr() as usize == current_position.as_ptr() as usize {
|
||||
return None;
|
||||
@@ -63,8 +65,10 @@ pub fn get_one_before<'s>(document: &'s str, current_position: &'s str) -> Optio
|
||||
/// Get the line current_position is on up until current_position
|
||||
pub fn get_current_line_before_position<'s>(
|
||||
document: &'s str,
|
||||
current_position: &'s str,
|
||||
) -> Option<&'s str> {
|
||||
current_position: OrgSource<'s>,
|
||||
) -> Option<OrgSource<'s>> {
|
||||
// TODO: This should be replaced with new logic now that we are wrapping the input type.
|
||||
let current_position = Into::<&str>::into(¤t_position);
|
||||
assert!(is_slice_of(document, current_position));
|
||||
if document.as_ptr() as usize == current_position.as_ptr() as usize {
|
||||
return None;
|
||||
@@ -83,7 +87,7 @@ pub fn get_current_line_before_position<'s>(
|
||||
}
|
||||
previous_character_offset = new_offset;
|
||||
}
|
||||
Some(&document[previous_character_offset..offset])
|
||||
Some((&document[previous_character_offset..offset]).into())
|
||||
}
|
||||
|
||||
/// Check if the child string slice is a slice of the parent string slice.
|
||||
@@ -96,29 +100,23 @@ fn is_slice_of(parent: &str, child: &str) -> bool {
|
||||
}
|
||||
|
||||
/// Get a slice of the string that was consumed in a parser using the original input to the parser and the remaining input after the parser.
|
||||
pub fn get_consumed<'s>(input: &'s str, remaining: &'s str) -> &'s str {
|
||||
pub fn get_consumed<'s>(input: OrgSource<'s>, remaining: OrgSource<'s>) -> OrgSource<'s> {
|
||||
// TODO: This should be replaced with new logic now that we are wrapping the input type.
|
||||
let input = Into::<&str>::into(&input);
|
||||
let remaining = Into::<&str>::into(&remaining);
|
||||
assert!(is_slice_of(input, remaining));
|
||||
let source = {
|
||||
let offset = remaining.as_ptr() as usize - input.as_ptr() as usize;
|
||||
&input[..offset]
|
||||
};
|
||||
source
|
||||
source.into()
|
||||
}
|
||||
|
||||
/// A line containing only whitespace and then a line break
|
||||
///
|
||||
/// It is up to the caller to ensure this is called at the start of a line.
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn blank_line(input: &str) -> Res<&str, &str> {
|
||||
not(eof)(input)?;
|
||||
recognize(tuple((space0, alt((line_ending, eof)))))(input)
|
||||
}
|
||||
|
||||
/// A line containing only whitespace and then a line break
|
||||
///
|
||||
/// It is up to the caller to ensure this is called at the start of a line.
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn new_blank_line<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
pub fn blank_line(input: OrgSource<'_>) -> Res<OrgSource<'_>, OrgSource<'_>> {
|
||||
not(eof)(input)?;
|
||||
recognize(tuple((space0, alt((line_ending, eof)))))(input)
|
||||
}
|
||||
@@ -126,8 +124,8 @@ pub fn new_blank_line<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn element_trailing_whitespace<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
input: &'s str,
|
||||
) -> Res<&'s str, &'s str> {
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
start_of_line(context, input)?;
|
||||
alt((eof, recognize(many0(blank_line))))(input)
|
||||
}
|
||||
@@ -135,8 +133,8 @@ pub fn element_trailing_whitespace<'r, 's>(
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn maybe_consume_trailing_whitespace_if_not_exiting<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
input: &'s str,
|
||||
) -> Res<&'s str, Option<&'s str>> {
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Option<OrgSource<'s>>> {
|
||||
if context.should_consume_trailing_whitespace() && exit_matcher_parser(context, input).is_err()
|
||||
{
|
||||
Ok(opt(parser_with_context!(element_trailing_whitespace)(
|
||||
@@ -150,8 +148,8 @@ pub fn maybe_consume_trailing_whitespace_if_not_exiting<'r, 's>(
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn maybe_consume_trailing_whitespace<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
input: &'s str,
|
||||
) -> Res<&'s str, Option<&'s str>> {
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Option<OrgSource<'s>>> {
|
||||
if context.should_consume_trailing_whitespace() {
|
||||
Ok(opt(parser_with_context!(element_trailing_whitespace)(
|
||||
context,
|
||||
@@ -162,13 +160,16 @@ pub fn maybe_consume_trailing_whitespace<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn trailing_whitespace(input: &str) -> Res<&str, &str> {
|
||||
pub fn trailing_whitespace(input: OrgSource<'_>) -> Res<OrgSource<'_>, OrgSource<'_>> {
|
||||
alt((eof, recognize(tuple((line_ending, many0(blank_line))))))(input)
|
||||
}
|
||||
|
||||
/// Check that we are at the start of a line
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn start_of_line<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> {
|
||||
pub fn start_of_line<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, ()> {
|
||||
let document_root = context.get_document_root().unwrap();
|
||||
let preceding_character = get_one_before(document_root, input)
|
||||
.map(|slice| slice.chars().next())
|
||||
@@ -178,7 +179,7 @@ pub fn start_of_line<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'
|
||||
Some(_) => {
|
||||
// Not at start of line, cannot be a heading
|
||||
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
||||
"Not at start of line",
|
||||
"Not at start of line".into(),
|
||||
))));
|
||||
}
|
||||
// If None, we are at the start of the file which allows for headings
|
||||
@@ -191,8 +192,8 @@ pub fn start_of_line<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn preceded_by_whitespace<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
input: &'s str,
|
||||
) -> Res<&'s str, ()> {
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, ()> {
|
||||
let document_root = context.get_document_root().unwrap();
|
||||
let preceding_character = get_one_before(document_root, input)
|
||||
.map(|slice| slice.chars().next())
|
||||
@@ -202,7 +203,7 @@ pub fn preceded_by_whitespace<'r, 's>(
|
||||
// If None, we are at the start of the file which is not allowed
|
||||
None | Some(_) => {
|
||||
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
||||
"Not preceded by whitespace.",
|
||||
"Not preceded by whitespace.".into(),
|
||||
))));
|
||||
}
|
||||
};
|
||||
@@ -213,7 +214,7 @@ pub fn preceded_by_whitespace<'r, 's>(
|
||||
///
|
||||
/// This function only operates on spaces, tabs, carriage returns, and line feeds. It does not handle fancy unicode whitespace.
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn non_whitespace_character(input: &str) -> Res<&str, char> {
|
||||
pub fn non_whitespace_character(input: OrgSource<'_>) -> Res<OrgSource<'_>, char> {
|
||||
none_of(" \t\r\n")(input)
|
||||
}
|
||||
|
||||
@@ -221,25 +222,31 @@ pub fn non_whitespace_character(input: &str) -> Res<&str, char> {
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn exit_matcher_parser<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
input: &'s str,
|
||||
) -> Res<&'s str, &'s str> {
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
peek(|i| context.check_exit_matcher(i))(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn always_fail<'r, 's>(_context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||||
pub fn always_fail<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
Err(nom::Err::Error(CustomError::MyError(MyError(
|
||||
"Always fail",
|
||||
"Always fail".into(),
|
||||
))))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn whitespace_eof(input: &str) -> Res<&str, &str> {
|
||||
pub fn whitespace_eof(input: OrgSource<'_>) -> Res<OrgSource<'_>, OrgSource<'_>> {
|
||||
recognize(tuple((multispace0, eof)))(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn text_until_exit<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||||
pub fn text_until_exit<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(verify(
|
||||
many_till(anychar, parser_with_context!(exit_matcher_parser)(context)),
|
||||
|(children, _exit_contents)| !children.is_empty(),
|
||||
@@ -247,9 +254,9 @@ pub fn text_until_exit<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn not_yet_implemented() -> Res<&'static str, ()> {
|
||||
pub fn not_yet_implemented() -> Res<OrgSource<'static>, ()> {
|
||||
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
||||
"Not implemented yet.",
|
||||
"Not implemented yet.".into(),
|
||||
))));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user