diff options
Diffstat (limited to 'src/poem/read/parse.rs')
-rw-r--r-- | src/poem/read/parse.rs | 52 |
1 files changed, 34 insertions, 18 deletions
diff --git a/src/poem/read/parse.rs b/src/poem/read/parse.rs index 27c4728..fc1979f 100644 --- a/src/poem/read/parse.rs +++ b/src/poem/read/parse.rs @@ -15,29 +15,45 @@ use std::str::Chars; /// /// # Examples: /// ``` -/// next(&mut chars, &mut i, Rune::Write, vec![('>', Rune::Addendum)]) -/// next(&mut chars, &mut i, Rune::Quiet, vec![('&', Rune::And)]) +/// next(&mut chars, &mut i, Rune::Write, vec![(">", Rune::Addendum)]) +/// next(&mut chars, &mut i, Rune::Quiet, vec![("&", Rune::And)]) /// ``` -pub fn next(chars: &mut Chars, i: &mut usize, otherwise: Rune, ahead: Vec<(char, Rune)>) -> Rune { - // Try to get the next character in the poem - let next = match chars.peekable().peek() { - Some(c) => *c, - None => { - return otherwise; - } - }; +pub fn next(chars: &mut Chars, i: &mut usize, otherwise: Rune, ahead: Vec<(&str, Rune)>) -> Rune { + // Initialize rune (the return value) with the default + let mut rune = otherwise; + + // We need to peek the iterator + let mut peekable = chars.clone().peekable(); + + // Help keep track of matched characters + let mut j = 0; - // Check if that next character matches any characters in ahead - for (c, rune) in ahead.iter() { - if next == *c { - chars.next(); - *i += 1; - return *rune; + // For each tuple pair (string, rune)... + for (s, r) in ahead.iter() { + // For each character in in the string, starting at length j... + for c in s[j..].chars().into_iter() { + // If the next char matches... + match peekable.peek() { + Some(next) if next == &c => { + // Increment counters + chars.next(); + peekable.next(); + *i += 1; + j += 1; + + // Only update the rune if j equals the length of the string + if j == s.len() { + rune = *r; + } + } + Some(_) => {} + None => {} + } } } - // If it doesn't match, return the default - otherwise + // Return whatever the rune was determined to be + rune } /// Keep pushing to the [Word][super::super::elements::word::Word] stack |