Skip to content

Commit

Permalink
rename stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Lurk committed Nov 12, 2024
1 parent be5e351 commit acb54b9
Show file tree
Hide file tree
Showing 14 changed files with 25 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/parser/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(crate) fn anchor(p: &mut Parser<'_>) -> Option<Anchor> {
TokenKind::Terminator => break,
TokenKind::LeftSquareBracket if right_square_bracket_pos.is_none() => {
if let Some((_, end)) =
p.advance_until_terminated(|t| t.kind == TokenKind::RightSquareBracket)
p.advance_or_backtrack(|t| t.kind == TokenKind::RightSquareBracket)
{
right_square_bracket_pos.replace(end);
} else {
Expand Down Expand Up @@ -57,7 +57,7 @@ pub(crate) fn anchor(p: &mut Parser<'_>) -> Option<Anchor> {
}

p.flip_to_literal_at(start_pos);
p.move_to(start_pos);
p.backtrack(start_pos);
None
}

Expand Down
2 changes: 1 addition & 1 deletion src/parser/bold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub(crate) fn bold(p: &mut Parser<'_>) -> Option<Bold> {
}
}

p.move_to(start_pos);
p.backtrack(start_pos);
p.flip_to_literal_at(start_pos);
None
}
Expand Down
4 changes: 2 additions & 2 deletions src/parser/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use super::Parser;
pub(crate) fn code(p: &mut Parser<'_>) -> Option<Code> {
let start_pos = p.pos();
let lang = p
.advance_until_terminated(|t| t.kind == TokenKind::Eol)
.advance_or_backtrack(|t| t.kind == TokenKind::Eol)
.map(|(_, end)| end)?;

let Some((start, end)) = p.advance_until(
|t| t.kind == TokenKind::Backtick && t.position.column == 0 && t.slice.len() == 3,
true,
) else {
p.move_to(start_pos);
p.backtrack(start_pos);
p.flip_to_literal_at(start_pos);
return None;
};
Expand Down
2 changes: 1 addition & 1 deletion src/parser/code_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{lexer::TokenKind, nodes::CodeSpan};
use super::Parser;

pub(crate) fn code_span(p: &mut Parser) -> Option<CodeSpan> {
p.advance_until_terminated(|t| t.kind == TokenKind::Backtick && t.slice.len() == 1)
p.advance_or_backtrack(|t| t.kind == TokenKind::Backtick && t.slice.len() == 1)
.map(|(start, end)| CodeSpan::new(p.range_to_string(start + 1..end)))
}

Expand Down
2 changes: 1 addition & 1 deletion src/parser/collapsible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub(crate) fn collapsible(p: &mut Parser) -> Option<Collapsible> {
}
}

p.move_to(start);
p.backtrack(start);
p.flip_to_literal_at(start);
None
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser/embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) fn embed(p: &mut Parser<'_>) -> Option<Embed> {
}
}

p.move_to(start_pos);
p.backtrack(start_pos);
p.flip_to_literal_at(start_pos);
None
}
Expand Down
4 changes: 2 additions & 2 deletions src/parser/heading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ where

if let Some(t) = p.next_token() {
if t.kind != TokenKind::Space {
p.move_to(start);
p.backtrack(start);
p.flip_to_literal_at(start);
return None;
}
Expand Down Expand Up @@ -67,7 +67,7 @@ where
return Some(heading);
}

p.move_to(start);
p.backtrack(start);
p.flip_to_literal_at(start);

None
Expand Down
8 changes: 3 additions & 5 deletions src/parser/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ pub(crate) fn highlight(p: &mut Parser) -> Option<Highlight> {
p.next_token();
}
TokenKind::Space if state == State::TitleCommit && title.is_none() => {
if let Some((start, end)) = p.advance_until_terminated(|t| t.kind == TokenKind::Eol)
{
if let Some((start, end)) = p.advance_or_backtrack(|t| t.kind == TokenKind::Eol) {
state = State::Icon;
title.replace(start + 1..end);
} else {
Expand All @@ -46,8 +45,7 @@ pub(crate) fn highlight(p: &mut Parser) -> Option<Highlight> {
p.next_token();
}
TokenKind::Space if state == State::IconCommit && icon.is_none() => {
if let Some((start, end)) = p.advance_until_terminated(|t| t.kind == TokenKind::Eol)
{
if let Some((start, end)) = p.advance_or_backtrack(|t| t.kind == TokenKind::Eol) {
state = State::Body;
icon.replace(start + 1..end);
} else {
Expand Down Expand Up @@ -76,7 +74,7 @@ pub(crate) fn highlight(p: &mut Parser) -> Option<Highlight> {
}
}

p.move_to(start);
p.backtrack(start);
p.flip_to_literal_at(start);

None
Expand Down
2 changes: 1 addition & 1 deletion src/parser/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ where
}

if images.is_empty() || state == State::Fail {
p.move_to(start);
p.backtrack(start);
p.flip_to_literal_at(start);
return None;
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser/italic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{lexer::TokenKind, nodes::Italic};
use super::Parser;

pub(crate) fn italic(p: &mut Parser) -> Option<Italic> {
p.advance_until_terminated(|t| t.kind == TokenKind::Underscore && t.slice.len() == 1)
p.advance_or_backtrack(|t| t.kind == TokenKind::Underscore && t.slice.len() == 1)
.map(|(start, end)| Italic::new(p.range_to_string(start + 1..end)))
}

Expand Down
8 changes: 4 additions & 4 deletions src/parser/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn parse_list(p: &mut Parser<'_>, list_type: &ListTypes, level: usize) -> Option
list_item = ListItem::new(vec![], None);
} else {
p.flip_to_literal_at(pos - 2);
p.move_to(pos - 3);
p.backtrack(pos - 3);
}
}
TokenKind::Space if state == State::NextLevelOrdered => {
Expand All @@ -112,7 +112,7 @@ fn parse_list(p: &mut Parser<'_>, list_type: &ListTypes, level: usize) -> Option
list_item = ListItem::new(vec![], None);
} else {
p.flip_to_literal_at(pos - 2);
p.move_to(pos - 3);
p.backtrack(pos - 3);
}
}
TokenKind::Space if state == State::SameLevelCommit => {
Expand All @@ -125,7 +125,7 @@ fn parse_list(p: &mut Parser<'_>, list_type: &ListTypes, level: usize) -> Option
}
TokenKind::Space if state == State::PreviousLevelCommit => {
// back to new line so Previous level can decide what to do.
p.move_to(if level == 1 { pos - 1 } else { pos - 2 });
p.backtrack(if level == 1 { pos - 1 } else { pos - 2 });
break;
}
_ => {
Expand All @@ -146,7 +146,7 @@ fn parse_list(p: &mut Parser<'_>, list_type: &ListTypes, level: usize) -> Option
}

if list.body.is_empty() {
p.move_to(start_pos);
p.backtrack(start_pos);
p.flip_to_literal_at(start_pos);
return None;
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) fn metadata(p: &mut Parser) -> Option<String> {
}
}

p.move_to(start);
p.backtrack(start);
p.flip_to_literal_at(start);

None
Expand Down
11 changes: 4 additions & 7 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl<'input> Parser<'input> {
.collect()
}

pub fn move_to(&mut self, index: usize) {
pub fn backtrack(&mut self, index: usize) {
if index < self.stack.len() {
self.stack_pos = index;
}
Expand Down Expand Up @@ -118,14 +118,14 @@ impl<'input> Parser<'input> {
None
}

pub fn advance_until_terminated<Callback>(&mut self, f: Callback) -> Option<(usize, usize)>
pub fn advance_or_backtrack<Callback>(&mut self, f: Callback) -> Option<(usize, usize)>
where
Callback: Fn(&Token) -> bool,
{
let start = self.pos();

let Some(result) = self.advance_until(f, false) else {
self.move_to(start);
self.backtrack(start);
self.flip_to_literal_at(start);
return None;
};
Expand Down Expand Up @@ -204,10 +204,7 @@ mod tests {
fn advance_until() {
let mut p = Parser::new("!test");

assert_eq!(
p.advance_until_terminated(|t| t.kind == TokenKind::Space),
None
);
assert_eq!(p.advance_or_backtrack(|t| t.kind == TokenKind::Space), None);
assert_eq!(
p.peek(),
Some((&Token::new(TokenKind::Literal, "!", Position::default()), 0))
Expand Down
2 changes: 1 addition & 1 deletion src/parser/strikethrough.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{lexer::TokenKind, nodes::Strikethrough};
use super::Parser;

pub(crate) fn strikethrough(p: &mut Parser) -> Option<Strikethrough> {
p.advance_until_terminated(|t| t.kind == TokenKind::Tilde && t.slice.len() == 2)
p.advance_or_backtrack(|t| t.kind == TokenKind::Tilde && t.slice.len() == 2)
.map(|(start, end)| Strikethrough::new(p.range_to_string(start + 1..end)))
}

Expand Down

0 comments on commit acb54b9

Please sign in to comment.