aboutsummaryrefslogtreecommitdiffstats
path: root/src/util
diff options
context:
space:
mode:
authorLibravatar Titus Wormer <tituswormer@gmail.com>2022-08-11 14:53:42 +0200
committerLibravatar Titus Wormer <tituswormer@gmail.com>2022-08-11 14:53:42 +0200
commitd87dc75500a2e73d9521135054b42c18e6eae987 (patch)
tree4bc3f4a72894db3ec5ed4069841c6be5be69713d /src/util
parent6eb2f644057f371841fe25330a57ee185f91c7af (diff)
downloadmarkdown-rs-d87dc75500a2e73d9521135054b42c18e6eae987.tar.gz
markdown-rs-d87dc75500a2e73d9521135054b42c18e6eae987.tar.bz2
markdown-rs-d87dc75500a2e73d9521135054b42c18e6eae987.zip
Refactor to move some code to `event.rs`
Diffstat (limited to '')
-rw-r--r--src/util/edit_map.rs2
-rw-r--r--src/util/skip.rs50
-rw-r--r--src/util/slice.rs8
3 files changed, 25 insertions, 35 deletions
diff --git a/src/util/edit_map.rs b/src/util/edit_map.rs
index 4d9f557..59adfca 100644
--- a/src/util/edit_map.rs
+++ b/src/util/edit_map.rs
@@ -8,7 +8,7 @@
//! And, in other cases, it’s needed to parse subcontent: pass some events
//! through another tokenizer and inject the result.
-use crate::tokenizer::Event;
+use crate::event::Event;
/// Shift `previous` and `next` links according to `jumps`.
///
diff --git a/src/util/skip.rs b/src/util/skip.rs
index d2ad914..371418f 100644
--- a/src/util/skip.rs
+++ b/src/util/skip.rs
@@ -1,31 +1,30 @@
//! Utilities to deal with lists of events.
-use crate::token::Token;
-use crate::tokenizer::{Event, EventType};
+use crate::event::{Event, Kind, Name};
-/// Skip from `index`, optionally past `token_types`.
-pub fn opt(events: &[Event], index: usize, token_types: &[Token]) -> usize {
- skip_opt_impl(events, index, token_types, true)
+/// Skip from `index`, optionally past `names`.
+pub fn opt(events: &[Event], index: usize, names: &[Name]) -> usize {
+ skip_opt_impl(events, index, names, true)
}
-/// Skip from `index`, optionally past `token_types`, backwards.
-pub fn opt_back(events: &[Event], index: usize, token_types: &[Token]) -> usize {
- skip_opt_impl(events, index, token_types, false)
+/// Skip from `index`, optionally past `names`, backwards.
+pub fn opt_back(events: &[Event], index: usize, names: &[Name]) -> usize {
+ skip_opt_impl(events, index, names, false)
}
-pub fn to_back(events: &[Event], index: usize, token_types: &[Token]) -> usize {
- to_impl(events, index, token_types, false)
+pub fn to_back(events: &[Event], index: usize, names: &[Name]) -> usize {
+ to_impl(events, index, names, false)
}
-pub fn to(events: &[Event], index: usize, token_types: &[Token]) -> usize {
- to_impl(events, index, token_types, true)
+pub fn to(events: &[Event], index: usize, names: &[Name]) -> usize {
+ to_impl(events, index, names, true)
}
-pub fn to_impl(events: &[Event], mut index: usize, token_types: &[Token], forward: bool) -> usize {
+pub fn to_impl(events: &[Event], mut index: usize, names: &[Name], forward: bool) -> usize {
while index < events.len() {
- let current = &events[index].token_type;
+ let current = &events[index].name;
- if token_types.contains(current) {
+ if names.contains(current) {
break;
}
@@ -36,23 +35,14 @@ pub fn to_impl(events: &[Event], mut index: usize, token_types: &[Token], forwar
}
/// Skip internals.
-fn skip_opt_impl(
- events: &[Event],
- mut index: usize,
- token_types: &[Token],
- forward: bool,
-) -> usize {
+fn skip_opt_impl(events: &[Event], mut index: usize, names: &[Name], forward: bool) -> usize {
let mut balance = 0;
- let open = if forward {
- EventType::Enter
- } else {
- EventType::Exit
- };
+ let open = if forward { Kind::Enter } else { Kind::Exit };
while index < events.len() {
- let current = &events[index].token_type;
+ let current = &events[index].name;
- if !token_types.contains(current) || events[index].event_type != open {
+ if !names.contains(current) || events[index].kind != open {
break;
}
@@ -60,13 +50,13 @@ fn skip_opt_impl(
balance += 1;
loop {
- balance = if events[index].event_type == open {
+ balance = if events[index].kind == open {
balance + 1
} else {
balance - 1
};
- if events[index].token_type == *current && balance == 0 {
+ if events[index].name == *current && balance == 0 {
index = if forward { index + 1 } else { index - 1 };
break;
}
diff --git a/src/util/slice.rs b/src/util/slice.rs
index f287978..34adf32 100644
--- a/src/util/slice.rs
+++ b/src/util/slice.rs
@@ -1,7 +1,7 @@
//! Utilities to deal with characters.
use crate::constant::TAB_SIZE;
-use crate::tokenizer::{Event, EventType, Point};
+use crate::event::{Event, Kind, Point};
use std::str;
/// A range between two places.
@@ -24,15 +24,15 @@ impl<'a> Position<'a> {
pub fn from_exit_event(events: &'a [Event], index: usize) -> Position<'a> {
let exit = &events[index];
debug_assert_eq!(
- exit.event_type,
- EventType::Exit,
+ exit.kind,
+ Kind::Exit,
"expected `from_exit_event` to be called on `exit` event"
);
let mut enter_index = index - 1;
loop {
let enter = &events[enter_index];
- if enter.event_type == EventType::Enter && enter.token_type == exit.token_type {
+ if enter.kind == Kind::Enter && enter.name == exit.name {
return Position {
start: &enter.point,
end: &exit.point,