Skip to content

Commit

Permalink
Code clarity: Use source for input text consistently (#127)
Browse files Browse the repository at this point in the history
Remove uses of `i`, `inp`, or `input`
  • Loading branch information
scouten authored Sep 9, 2024
1 parent ad566c9 commit 8f0a2d8
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 74 deletions.
4 changes: 2 additions & 2 deletions src/attributes/element_attribute.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{primitives::trim_input_for_rem, span::ParseResult, HasSpan, Span};
use crate::{primitives::trim_source_for_rem, span::ParseResult, HasSpan, Span};

/// This struct represents a single element attribute.
///
Expand Down Expand Up @@ -60,7 +60,7 @@ impl<'src> ElementAttribute<'src> {
return None;
}

let source = trim_input_for_rem(source, value.rem);
let source = trim_source_for_rem(source, value.rem);

let shorthand_items = if name.is_none() && parse_shorthand {
parse_shorthand_items(source)
Expand Down
12 changes: 6 additions & 6 deletions src/blocks/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ impl<'src> Block<'src> {
/// Parse a block of any type and return a `Block` that describes it.
///
/// Consumes any blank lines before and after the block.
pub(crate) fn parse(i: Span<'src>) -> Option<ParseResult<'src, Self>> {
let i = i.discard_empty_lines();
pub(crate) fn parse(source: Span<'src>) -> Option<ParseResult<'src, Self>> {
let source = source.discard_empty_lines();

// Try to discern the block type by scanning the first line.
let line = i.take_normalized_line();
let line = source.take_normalized_line();
if line.t.contains("::") {
if let Some(macro_block) = MacroBlock::parse(i) {
if let Some(macro_block) = MacroBlock::parse(source) {
return Some(ParseResult {
t: Self::Macro(macro_block.t),
rem: macro_block.rem,
Expand All @@ -56,7 +56,7 @@ impl<'src> Block<'src> {
// A line containing `::` might be some other kind of block, so we
// don't automatically error out on a parse failure.
} else if line.t.starts_with('=') {
if let Some(section_block) = SectionBlock::parse(i) {
if let Some(section_block) = SectionBlock::parse(source) {
return Some(ParseResult {
t: Self::Section(section_block.t),
rem: section_block.rem,
Expand All @@ -68,7 +68,7 @@ impl<'src> Block<'src> {
}

// If no other block kind matches, we can always use SimpleBlock.
SimpleBlock::parse(i).map(|pr| ParseResult {
SimpleBlock::parse(source).map(|pr| ParseResult {
t: Self::Simple(pr.t),
rem: pr.rem,
})
Expand Down
17 changes: 10 additions & 7 deletions src/blocks/parse_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@ use crate::{blocks::Block, span::ParseResult, Span};
/// Parse blocks until end of input or a pre-determined stop condition is
/// reached.
pub(crate) fn parse_blocks_until<'src, F>(
mut i: Span<'src>,
mut source: Span<'src>,
f: F,
) -> Option<ParseResult<'src, Vec<Block<'src>>>>
where
F: Fn(&Span<'src>) -> bool,
{
let mut blocks: Vec<Block<'src>> = vec![];
i = i.discard_empty_lines();
source = source.discard_empty_lines();

while !i.data().is_empty() {
if f(&i) {
while !source.data().is_empty() {
if f(&source) {
break;
}

let pr = Block::parse(i)?;
i = pr.rem;
let pr = Block::parse(source)?;
source = pr.rem;
blocks.push(pr.t);
}

Some(ParseResult { t: blocks, rem: i })
Some(ParseResult {
t: blocks,
rem: source,
})
}
8 changes: 4 additions & 4 deletions src/blocks/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::slice::Iter;

use crate::{
blocks::{parse_utils::parse_blocks_until, Block, ContentModel, IsBlock},
primitives::trim_input_for_rem,
primitives::trim_source_for_rem,
span::ParseResult,
strings::CowStr,
HasSpan, Span,
Expand All @@ -28,7 +28,7 @@ impl<'src> SectionBlock<'src> {
let source = source.discard_empty_lines();
let level = parse_title_line(source)?;
let blocks = parse_blocks_until(level.rem, |i| peer_or_ancestor_section(*i, level.t.0))?;
let source = trim_input_for_rem(source, blocks.rem);
let source = trim_source_for_rem(source, blocks.rem);

Some(ParseResult {
t: Self {
Expand Down Expand Up @@ -103,8 +103,8 @@ fn parse_title_line(source: Span<'_>) -> Option<ParseResult<(usize, Span)>> {
})
}

fn peer_or_ancestor_section(i: Span<'_>, level: usize) -> bool {
if let Some(pr) = parse_title_line(i) {
fn peer_or_ancestor_section(source: Span<'_>, level: usize) -> bool {
if let Some(pr) = parse_title_line(source) {
pr.t.0 <= level
} else {
false
Expand Down
8 changes: 4 additions & 4 deletions src/document/attribute.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{primitives::trim_input_for_rem, span::ParseResult, strings::CowStr, HasSpan, Span};
use crate::{primitives::trim_source_for_rem, span::ParseResult, strings::CowStr, HasSpan, Span};

/// Document attributes are effectively document-scoped variables for the
/// AsciiDoc language. The AsciiDoc language defines a set of built-in
Expand All @@ -12,8 +12,8 @@ pub struct Attribute<'src> {
}

impl<'src> Attribute<'src> {
pub(crate) fn parse(i: Span<'src>) -> Option<ParseResult<'src, Self>> {
let attr_line = i.take_line_with_continuation()?;
pub(crate) fn parse(source: Span<'src>) -> Option<ParseResult<'src, Self>> {
let attr_line = source.take_line_with_continuation()?;
let colon = attr_line.t.take_prefix(":")?;

let mut unset = false;
Expand Down Expand Up @@ -45,7 +45,7 @@ impl<'src> Attribute<'src> {
RawAttributeValue::Value(value.rem)
};

let source = trim_input_for_rem(i, attr_line.rem);
let source = trim_source_for_rem(source, attr_line.rem);
Some(ParseResult {
t: Self {
name: name.t,
Expand Down
12 changes: 6 additions & 6 deletions src/document/header.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::slice::Iter;

use crate::{
document::Attribute, primitives::trim_input_for_rem, span::ParseResult, HasSpan, Span,
document::Attribute, primitives::trim_source_for_rem, span::ParseResult, HasSpan, Span,
};

/// An AsciiDoc document may begin with a document header. The document header
Expand All @@ -15,8 +15,8 @@ pub struct Header<'src> {
}

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

// TEMPORARY: Titles are optional, but we're not prepared for that yet.
let title = parse_title(source)?;
Expand All @@ -29,7 +29,7 @@ impl<'src> Header<'src> {
rem = attr.rem;
}

let source = trim_input_for_rem(source, rem);
let source = trim_source_for_rem(source, rem);

// Header must be followed by an empty line or EOF.
let pr = rem.take_empty_line()?;
Expand Down Expand Up @@ -61,8 +61,8 @@ impl<'src> HasSpan<'src> for Header<'src> {
}
}

fn parse_title(i: Span<'_>) -> Option<ParseResult<Span>> {
let line = i.take_non_empty_line()?;
fn parse_title(source: Span<'_>) -> Option<ParseResult<Span>> {
let line = source.take_non_empty_line()?;
let equal = line.t.take_prefix("=")?;
let ws = equal.rem.take_required_whitespace()?;

Expand Down
32 changes: 16 additions & 16 deletions src/inlines/inline.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
inlines::InlineMacro, primitives::trim_input_for_rem, span::ParseResult, HasSpan, Span,
inlines::InlineMacro, primitives::trim_source_for_rem, span::ParseResult, HasSpan, Span,
};

/// An inline element is a phrase (i.e., span of content) within a block element
Expand All @@ -23,8 +23,8 @@ impl<'src> Inline<'src> {
/// describes it.
///
/// Returns `None` if input doesn't start with a non-empty line.
pub(crate) fn parse(i: Span<'src>) -> Option<ParseResult<'src, Self>> {
let line = i.take_non_empty_line()?;
pub(crate) fn parse(source: Span<'src>) -> Option<ParseResult<'src, Self>> {
let line = source.take_non_empty_line()?;
let mut span = line.t;

// Special-case optimization: If the entire span is one
Expand Down Expand Up @@ -63,7 +63,7 @@ impl<'src> Inline<'src> {
}

Some(ParseResult {
t: Self::Sequence(inlines, trim_input_for_rem(i, line.rem)),
t: Self::Sequence(inlines, trim_source_for_rem(source, line.rem)),
rem: line.rem,
})
}
Expand All @@ -73,9 +73,9 @@ impl<'src> Inline<'src> {
///
/// Returns `None` if there is not at least one non-empty line at
/// beginning of input.
pub(crate) fn parse_lines(i: Span<'src>) -> Option<ParseResult<'src, Self>> {
pub(crate) fn parse_lines(source: Span<'src>) -> Option<ParseResult<'src, Self>> {
let mut inlines: Vec<Inline<'src>> = vec![];
let mut next = i;
let mut next = source;

while let Some(inline) = Self::parse(next) {
next = inline.rem;
Expand All @@ -88,7 +88,7 @@ impl<'src> Inline<'src> {
rem: next,
})
} else {
let source = trim_input_for_rem(i, next);
let source = trim_source_for_rem(source, next);
Some(ParseResult {
t: Self::Sequence(inlines, source),
rem: next,
Expand All @@ -100,8 +100,8 @@ impl<'src> Inline<'src> {
impl<'src> HasSpan<'src> for Inline<'src> {
fn span(&'src self) -> &'src Span<'src> {
match self {
Self::Uninterpreted(i) => i,
Self::Sequence(_, i) => i,
Self::Uninterpreted(source) => source,
Self::Sequence(_, source) => source,
Self::Macro(m) => m.span(),
}
}
Expand All @@ -110,15 +110,15 @@ impl<'src> HasSpan<'src> for Inline<'src> {
// Parse the largest possible block of "uninterpreted" text.
// Remainder is either empty span or first span that requires
// special interpretation.
fn parse_uninterpreted(i: Span<'_>) -> ParseResult<Span> {
fn parse_uninterpreted(source: Span<'_>) -> ParseResult<Span> {
// Optimization: If line doesn't contain special markup chars,
// then it's all uninterpreted.

if !i.contains(':') {
return i.into_parse_result(i.len());
if !source.contains(':') {
return source.into_parse_result(source.len());
}

let mut rem = i;
let mut rem = source;

while !rem.is_empty() {
if InlineMacro::parse(rem).is_some() {
Expand All @@ -131,14 +131,14 @@ fn parse_uninterpreted(i: Span<'_>) -> ParseResult<Span> {
}

ParseResult {
t: trim_input_for_rem(i, rem),
t: trim_source_for_rem(source, rem),
rem,
}
}

// Parse the block as a special "interpreted" inline sequence or error out.
fn parse_interpreted(i: Span<'_>) -> Option<ParseResult<Inline<'_>>> {
InlineMacro::parse(i).map(|inline| ParseResult {
fn parse_interpreted(source: Span<'_>) -> Option<ParseResult<Inline<'_>>> {
InlineMacro::parse(source).map(|inline| ParseResult {
t: Inline::Macro(inline.t),
rem: inline.rem,
})
Expand Down
4 changes: 2 additions & 2 deletions src/inlines/macro.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{primitives::trim_input_for_rem, span::ParseResult, HasSpan, Span};
use crate::{primitives::trim_source_for_rem, span::ParseResult, HasSpan, Span};

/// An inline macro can be used in an inline context to create new inline
/// content.
Expand All @@ -24,7 +24,7 @@ impl<'src> InlineMacro<'src> {
let open_brace = target.rem.take_prefix("[")?;
let attrlist = open_brace.rem.take_while(|c| c != ']');
let close_brace = attrlist.rem.take_prefix("]")?;
let source = trim_input_for_rem(source, close_brace.rem);
let source = trim_source_for_rem(source, close_brace.rem);

Some(ParseResult {
t: Self {
Expand Down
14 changes: 7 additions & 7 deletions src/primitives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use crate::Span;
/// of the first, return the first input trimmed to exclude the second.
///
/// Note that the trailing remainder condition is not enforced.
pub(crate) fn trim_input_for_rem<'src>(inp: Span<'src>, rem: Span<'src>) -> Span<'src> {
// Sanity check: If rem is longer than inp, we can't trim.
pub(crate) fn trim_source_for_rem<'src>(source: Span<'src>, rem: Span<'src>) -> Span<'src> {
// Sanity check: If rem is longer than source, we can't trim.
let rlen = rem.len();
let ilen = inp.len();
let slen = source.len();

if rlen >= ilen {
inp.slice(0..0)
if rlen >= slen {
source.slice(0..0)
} else {
let trim_len = ilen - rlen;
inp.slice(0..trim_len)
let trim_len = slen - rlen;
source.slice(0..trim_len)
}
}
24 changes: 12 additions & 12 deletions src/tests/primitives/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod trim_input_for_rem {
mod trim_source_for_rem {
use pretty_assertions_sorted::assert_eq;

use crate::{primitives::trim_input_for_rem, tests::fixtures::TSpan, Span};
use crate::{primitives::trim_source_for_rem, tests::fixtures::TSpan, Span};

fn advanced_span(source: &'static str, skip: usize) -> Span<'static> {
let span = Span::new(source);
Expand All @@ -10,11 +10,11 @@ mod trim_input_for_rem {

#[test]
fn empty_spans() {
let inp = advanced_span("abcdef", 6);
let source = advanced_span("abcdef", 6);
let rem = Span::new("");

assert_eq!(
trim_input_for_rem(inp, rem),
trim_source_for_rem(source, rem),
TSpan {
data: "",
line: 1,
Expand All @@ -25,12 +25,12 @@ mod trim_input_for_rem {
}

#[test]
fn rem_equals_inp() {
let inp = advanced_span("abcdef", 6);
fn rem_equals_source() {
let source = advanced_span("abcdef", 6);
let rem = Span::new("abcdef");

assert_eq!(
trim_input_for_rem(inp, rem),
trim_source_for_rem(source, rem),
TSpan {
data: "",
line: 1,
Expand All @@ -44,11 +44,11 @@ mod trim_input_for_rem {
fn rem_too_long() {
// This is nonsense input, but we should at least not panic in this case.

let inp = advanced_span("abcdef", 6);
let source = advanced_span("abcdef", 6);
let rem = Span::new("abcdef_bogus_bogus");

assert_eq!(
trim_input_for_rem(inp, rem),
trim_source_for_rem(source, rem),
TSpan {
data: "",
line: 1,
Expand All @@ -59,12 +59,12 @@ mod trim_input_for_rem {
}

#[test]
fn rem_is_subset_of_inp() {
let inp = advanced_span("abcdef", 2);
fn rem_is_subset_of_source() {
let source = advanced_span("abcdef", 2);
let rem = advanced_span("abcdef", 4);

assert_eq!(
trim_input_for_rem(inp, rem),
trim_source_for_rem(source, rem),
TSpan {
data: "cd",
line: 1,
Expand Down
Loading

0 comments on commit 8f0a2d8

Please sign in to comment.