Skip to content

Commit

Permalink
Rename ParseResult to MatchedItem (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
scouten committed Sep 10, 2024
1 parent 141a281 commit af07839
Show file tree
Hide file tree
Showing 40 changed files with 1,033 additions and 1,026 deletions.
28 changes: 14 additions & 14 deletions src/attributes/attrlist.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{ops::Deref, slice::Iter};

use crate::{attributes::ElementAttribute, span::ParseResult, HasSpan, Span};
use crate::{attributes::ElementAttribute, span::MatchedItem, HasSpan, Span};

/// The source text that’s used to define attributes for an element is referred
/// to as an attrlist. An attrlist is always enclosed in a pair of square
Expand All @@ -20,8 +20,8 @@ impl<'src> Attrlist<'src> {
/// the opening or closing square brackets for the attrlist. This is because
/// the rules for closing brackets differ when parsing inline, macro, and
/// block elements.
pub(crate) fn parse(source: Span<'src>) -> Option<ParseResult<'src, Self>> {
let mut rem = source;
pub(crate) fn parse(source: Span<'src>) -> Option<MatchedItem<'src, Self>> {
let mut after = source;
let mut attributes: Vec<ElementAttribute> = vec![];
let mut parse_shorthand_items = true;

Expand All @@ -31,39 +31,39 @@ impl<'src> Attrlist<'src> {

loop {
let maybe_attr = if parse_shorthand_items {
ElementAttribute::parse_with_shorthand(rem)
ElementAttribute::parse_with_shorthand(after)
} else {
ElementAttribute::parse(rem)
ElementAttribute::parse(after)
};

let Some(attr) = maybe_attr else {
break;
};

if attr.t.name().is_none() {
if attr.item.name().is_none() {
parse_shorthand_items = false;
}

attributes.push(attr.t);
attributes.push(attr.item);

rem = attr.rem.take_whitespace().rem;
match rem.take_prefix(",") {
after = attr.after.take_whitespace().after;
match after.take_prefix(",") {
Some(comma) => {
rem = comma.rem.take_whitespace().rem;
after = comma.after.take_whitespace().after;
}
None => {
break;
}
}
}

if !rem.is_empty() {
if !after.is_empty() {
return None;
}

Some(ParseResult {
t: Self { attributes, source },
rem,
Some(MatchedItem {
item: Self { attributes, source },
after,
})
}

Expand Down
48 changes: 24 additions & 24 deletions src/attributes/element_attribute.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{span::ParseResult, HasSpan, Span};
use crate::{span::MatchedItem, HasSpan, Span};

/// This struct represents a single element attribute.
///
Expand All @@ -16,28 +16,28 @@ pub struct ElementAttribute<'src> {
}

impl<'src> ElementAttribute<'src> {
pub(crate) fn parse(source: Span<'src>) -> Option<ParseResult<'src, Self>> {
pub(crate) fn parse(source: Span<'src>) -> Option<MatchedItem<'src, Self>> {
Self::parse_internal(source, false)
}

pub(crate) fn parse_with_shorthand(source: Span<'src>) -> Option<ParseResult<'src, Self>> {
pub(crate) fn parse_with_shorthand(source: Span<'src>) -> Option<MatchedItem<'src, Self>> {
Self::parse_internal(source, true)
}

fn parse_internal(
source: Span<'src>,
parse_shorthand: bool,
) -> Option<ParseResult<'src, Self>> {
let (name, rem): (Option<Span>, Span) = match source.take_attr_name() {
) -> Option<MatchedItem<'src, Self>> {
let (name, after): (Option<Span>, Span) = match source.take_attr_name() {
Some(name) => {
let space = name.rem.take_whitespace();
match space.rem.take_prefix("=") {
let space = name.after.take_whitespace();
match space.after.take_prefix("=") {
Some(equals) => {
let space = equals.rem.take_whitespace();
if space.rem.is_empty() || space.rem.starts_with(',') {
let space = equals.after.take_whitespace();
if space.after.is_empty() || space.after.starts_with(',') {
(None, source)
} else {
(Some(name.t), space.rem)
(Some(name.item), space.after)
}
}
None => (None, source),
Expand All @@ -46,36 +46,36 @@ impl<'src> ElementAttribute<'src> {
None => (None, source),
};

let value = match rem.data().chars().next() {
Some('\'') | Some('"') => match rem.take_quoted_string() {
let value = match after.data().chars().next() {
Some('\'') | Some('"') => match after.take_quoted_string() {
Some(v) => v,
None => {
return None;
}
},
_ => rem.take_while(|c| c != ','),
_ => after.take_while(|c| c != ','),
};

if value.t.is_empty() {
if value.item.is_empty() {
return None;
}

let source = source.trim_remainder(value.rem);
let source = source.trim_remainder(value.after);

let shorthand_items = if name.is_none() && parse_shorthand {
parse_shorthand_items(source)
} else {
vec![]
};

Some(ParseResult {
t: Self {
Some(MatchedItem {
item: Self {
name,
shorthand_items,
value: value.t,
value: value.item,
source,
},
rem: value.rem,
after: value.after,
})
}

Expand Down Expand Up @@ -149,8 +149,8 @@ fn parse_shorthand_items<'src>(span: Span<'src>) -> Vec<Span<'src>> {

// Look for block style selector.
if let Some(block_style_pr) = span.split_at_match_non_empty(is_shorthand_delimiter) {
shorthand_items.push(block_style_pr.t);
span = block_style_pr.rem;
shorthand_items.push(block_style_pr.item);
span = block_style_pr.after;
}

while !span.is_empty() {
Expand All @@ -169,9 +169,9 @@ fn parse_shorthand_items<'src>(span: Span<'src>) -> Vec<Span<'src>> {
todo!("Flag warning for duplicate shorthand delimiter (issue #121)");
}
Some(index) => {
let pr: ParseResult<Span> = span.into_parse_result(index + 1);
shorthand_items.push(pr.t);
span = pr.rem;
let mi: MatchedItem<Span> = span.into_parse_result(index + 1);
shorthand_items.push(mi.item);
span = mi.after;
}
}
}
Expand Down
26 changes: 13 additions & 13 deletions src/blocks/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::slice::Iter;

use crate::{
blocks::{ContentModel, IsBlock, MacroBlock, SectionBlock, SimpleBlock},
span::ParseResult,
span::MatchedItem,
strings::CowStr,
HasSpan, Span,
};
Expand Down Expand Up @@ -40,26 +40,26 @@ 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(source: Span<'src>) -> Option<ParseResult<'src, Self>> {
pub(crate) fn parse(source: Span<'src>) -> Option<MatchedItem<'src, Self>> {
let source = source.discard_empty_lines();

// Try to discern the block type by scanning the first line.
let line = source.take_normalized_line();
if line.t.contains("::") {
if line.item.contains("::") {
if let Some(macro_block) = MacroBlock::parse(source) {
return Some(ParseResult {
t: Self::Macro(macro_block.t),
rem: macro_block.rem,
return Some(MatchedItem {
item: Self::Macro(macro_block.item),
after: macro_block.after,
});
}

// 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('=') {
} else if line.item.starts_with('=') {
if let Some(section_block) = SectionBlock::parse(source) {
return Some(ParseResult {
t: Self::Section(section_block.t),
rem: section_block.rem,
return Some(MatchedItem {
item: Self::Section(section_block.item),
after: section_block.after,
});
}

Expand All @@ -68,9 +68,9 @@ impl<'src> Block<'src> {
}

// If no other block kind matches, we can always use SimpleBlock.
SimpleBlock::parse(source).map(|pr| ParseResult {
t: Self::Simple(pr.t),
rem: pr.rem,
SimpleBlock::parse(source).map(|mi| MatchedItem {
item: Self::Simple(mi.item),
after: mi.after,
})
}
}
Expand Down
32 changes: 16 additions & 16 deletions src/blocks/macro.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
attributes::Attrlist,
blocks::{ContentModel, IsBlock},
span::ParseResult,
span::MatchedItem,
strings::CowStr,
HasSpan, Span,
};
Expand All @@ -22,34 +22,34 @@ pub struct MacroBlock<'src> {
}

impl<'src> MacroBlock<'src> {
pub(crate) fn parse(source: Span<'src>) -> Option<ParseResult<'src, Self>> {
pub(crate) fn parse(source: Span<'src>) -> Option<MatchedItem<'src, Self>> {
let line = source.take_normalized_line();

// Line must end with `]`; otherwise, it's not a block macro.
if !line.t.ends_with(']') {
if !line.item.ends_with(']') {
return None;
}

let line_wo_brace = line.t.slice(0..line.t.len() - 1);
let line_wo_brace = line.item.slice(0..line.item.len() - 1);
let name = line_wo_brace.take_ident()?;
let colons = name.rem.take_prefix("::")?;
let target = colons.rem.take_while(|c| c != '[');
let open_brace = target.rem.take_prefix("[")?;
let attrlist = Attrlist::parse(open_brace.rem)?;
let colons = name.after.take_prefix("::")?;
let target = colons.after.take_while(|c| c != '[');
let open_brace = target.after.take_prefix("[")?;
let attrlist = Attrlist::parse(open_brace.after)?;

Some(ParseResult {
t: Self {
name: name.t,
target: if target.t.is_empty() {
Some(MatchedItem {
item: Self {
name: name.item,
target: if target.item.is_empty() {
None
} else {
Some(target.t)
Some(target.item)
},
attrlist: attrlist.t,
source: line.t,
attrlist: attrlist.item,
source: line.item,
},

rem: line.rem.discard_empty_lines(),
after: line.after.discard_empty_lines(),
})
}

Expand Down
16 changes: 8 additions & 8 deletions src/blocks/parse_utils.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{blocks::Block, span::ParseResult, Span};
use crate::{blocks::Block, span::MatchedItem, Span};

/// Parse blocks until end of input or a pre-determined stop condition is
/// reached.
pub(crate) fn parse_blocks_until<'src, F>(
mut source: Span<'src>,
f: F,
) -> Option<ParseResult<'src, Vec<Block<'src>>>>
) -> Option<MatchedItem<'src, Vec<Block<'src>>>>
where
F: Fn(&Span<'src>) -> bool,
{
Expand All @@ -17,13 +17,13 @@ where
break;
}

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

Some(ParseResult {
t: blocks,
rem: source,
Some(MatchedItem {
item: blocks,
after: source,
})
}
Loading

0 comments on commit af07839

Please sign in to comment.