This commit is contained in:
Patrick Lühne 2020-04-28 02:14:27 +02:00
parent 834194d40a
commit a6edd2e9cc
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF

View File

@ -327,7 +327,7 @@ impl<'i> Tokens<'i, ()>
pub fn new_filter<P>(input: &'i str, mut predicate: P) pub fn new_filter<P>(input: &'i str, mut predicate: P)
-> Tokens<'i, impl FnMut(Token<'i>) -> Option<Token<'i>>> -> Tokens<'i, impl FnMut(Token<'i>) -> Option<Token<'i>>>
where where
P: FnMut(&Token<'i>) -> bool P: FnMut(&Token<'i>) -> bool,
{ {
Tokens::new_filter_map(input, Tokens::new_filter_map(input,
move |x| move |x|
@ -517,158 +517,6 @@ where
} }
} }
/*pub(crate) struct TokenIterator<'i>
{
original_input: &'i str,
input: &'i str,
}
impl<'i> TokenIterator<'i>
{
pub fn new(input: &'i str) -> Self
{
Self
{
original_input: input,
input,
}
}
}
impl<'i> std::iter::Iterator for TokenIterator<'i>
{
type Item = Result<(usize, usize, Token<'i>), crate::parse::Error>;
fn next(&mut self) -> Option<Self::Item>
{
self.input = self.input.trim_start();
let index_left = substring_offset(self.input, self.original_input);
let first_character = match self.input.chars().next()
{
None => return None,
Some(first_character) => first_character,
};
if self.input.starts_with(")")
{
return Some(Err(crate::parse::Error::new_unmatched_parenthesis(
crate::parse::error::Location::new(0, Some(1)))));
}
match parenthesized_expression(self.input)
{
Ok(Some((parenthesized_expression, remaining_input))) =>
{
self.input = remaining_input;
let index_right = substring_offset(self.input, self.original_input);
return Some(Ok((index_left, index_right,
Token::ParenthesizedExpression(parenthesized_expression))));
},
Ok(None) => (),
Err(error) => return Some(Err(error)),
}
match number(self.input)
{
Ok(Some((number, remaining_input))) =>
{
self.input = remaining_input;
let index_right = substring_offset(self.input, self.original_input);
return Some(Ok((index_left, index_right, Token::Number(number))));
},
Ok(None) => (),
Err(error) => return Some(Err(error)),
}
if let Some((identifier, remaining_input)) = identifier(self.input)
{
self.input = remaining_input;
let index_right = substring_offset(self.input, self.original_input);
return Some(Ok((index_left, index_right, Token::Identifier(identifier))));
}
if let Some((symbol, remaining_input)) = symbol(self.input)
{
self.input = remaining_input;
let index_right = substring_offset(self.input, self.original_input);
return Some(Ok((index_left, index_right, Token::Symbol(symbol))));
}
return Some(Err(crate::parse::Error::new_character_not_allowed(first_character,
crate::parse::error::Location::new(0, Some(0)))));
}
}
pub(crate) struct TokenSplit<'i, T, U>
where
T: std::iter::Iterator<Item = Result<(usize, usize, U), crate::parse::Error>>
{
token_iterator: T,
original_input: &'i str,
previous_index: usize,
}
impl<'i, T, U> TokenSplit<'i, T, U>
where
T: std::iter::Iterator<Item = Result<(usize, usize, U), crate::parse::Error>>
{
pub fn new(token_iterator: T, original_input: &'i str) -> Self
{
Self
{
token_iterator,
original_input,
previous_index: 0,
}
}
}
impl<'i, T, U> std::iter::Iterator for TokenSplit<'i, T, U>
where
T: std::iter::Iterator<Item = Result<(usize, usize, U), crate::parse::Error>>
{
type Item = Result<&'i str, crate::parse::Error>;
fn next(&mut self) -> Option<Self::Item>
{
if self.previous_index == self.original_input.len()
{
return None;
}
loop
{
match self.token_iterator.next()
{
Some(Ok((index_left, index_right, token))) =>
{
let input_between = self.original_input[self.previous_index..index_left].trim();
assert!(!input_between.is_empty());
self.previous_index = index_right;
return Some(Ok(input_between));
},
Some(Err(error)) => return Some(Err(error)),
None =>
{
let remaining_input = self.original_input[self.previous_index..].trim();
self.previous_index = self.original_input.len();
return Some(Ok(remaining_input));
},
}
}
}
}*/
#[cfg(test)] #[cfg(test)]
mod tests mod tests
{ {