From 4806864e5377a5fef937b3fa02542e620c547969 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Thu, 7 Jul 2022 17:21:38 +0200 Subject: Add basic support for block quotes --- src/util/skip.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/util/skip.rs (limited to 'src/util/skip.rs') diff --git a/src/util/skip.rs b/src/util/skip.rs new file mode 100644 index 0000000..2c4198a --- /dev/null +++ b/src/util/skip.rs @@ -0,0 +1,44 @@ +use crate::tokenizer::{Event, TokenType}; + +/// To do. +pub fn opt(events: &[Event], index: usize, token_types: &[TokenType]) -> usize { + skip_opt_with_direction(events, index, token_types, true) +} + +/// To do. +pub fn opt_back(events: &[Event], index: usize, token_types: &[TokenType]) -> usize { + skip_opt_with_direction(events, index, token_types, false) +} + +/// To do. +fn skip_opt_with_direction( + events: &[Event], + index: usize, + token_types: &[TokenType], + forward: bool, +) -> usize { + let mut index = index; + + while index < events.len() { + let current = &events[index].token_type; + + if !token_types.contains(current) { + break; + } + + // assert_eq!(events[index].event_type, EventType::Enter); + index = if forward { index + 1 } else { index - 1 }; + + loop { + if events[index].token_type == *current { + // assert_eq!(events[index].event_type, EventType::Exit); + index = if forward { index + 1 } else { index - 1 }; + break; + } + + index = if forward { index + 1 } else { index - 1 }; + } + } + + index +} -- cgit