diff --git a/src/compiler.rs b/src/compiler.rs index fc227a5..3c96842 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -3,7 +3,7 @@ use std::{collections::HashMap, str::FromStr}; use derive_new::new; use fasteval::{Compiler, Instruction, Slab}; -pub type TokenVec = Vec>; +pub type TokenVec<'a> = Vec>; pub trait Token { fn apply(&self, context: Context) -> Context { @@ -30,7 +30,7 @@ pub struct VariableChange(pub char, pub Expression); impl Token for VariableChange {} -pub struct Loop(pub LoopCount, pub TokenVec); +pub struct Loop<'a>(pub LoopCount, pub TokenVec<'a>); pub enum LoopCount { Litteral(usize), @@ -43,13 +43,13 @@ impl Default for LoopCount { } } -impl Token for Loop {} +impl Token for Loop<'_> {} -pub struct Tuplet(pub TokenVec); +pub struct Tuplet<'a>(pub TokenVec<'a>); -impl Token for Tuplet {} +impl Token for Tuplet<'_> {} -pub struct Slope<'a>(pub &'a VariableChange, pub TokenVec); +pub struct Slope<'a>(pub &'a VariableChange, pub TokenVec<'a>); impl Token for Slope<'_> {} diff --git a/src/parser.rs b/src/parser.rs index cd99dd4..12b384d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -32,10 +32,10 @@ pub struct Parser<'i, 'n, 's, 'v> { variables: &'v [char], } -impl<'i> Parser<'i, '_, '_, '_> { +impl<'i, 's> Parser<'i, '_, 's, '_> { pub fn parse_all( &self, - ) -> Result>, Error>> { + ) -> Result>, Error>> { token_parser(self) .parse_complete(LocatedSpan::new(self.input)) .finish() @@ -43,11 +43,11 @@ impl<'i> Parser<'i, '_, '_, '_> { } } -fn token_parser<'a>( - parser: &Parser, +fn token_parser<'a, 's>( + parser: &Parser<'_, '_, 's, '_>, ) -> impl NomParser< LocatedSpan<&'a str>, - Output = Vec>, + Output = Vec>, Error = nom::error::Error>, > { let space_or_comment = || { @@ -71,7 +71,7 @@ fn token_parser<'a>( )) } -fn into_box(token: impl Token + 'static) -> Box { +fn into_box<'a>(token: impl Token + 'a) -> Box { Box::new(token) } @@ -105,7 +105,7 @@ impl Note { Output = Self, Error = nom::error::Error>, > { - move |input: LocatedSpan<&'a str>| { + |input: LocatedSpan<&'a str>| { let mut parsers: Vec) -> TokenResult<'a, Self>>> = notes .iter() @@ -135,8 +135,8 @@ impl VariableChange { } } -impl Loop { - fn parser<'i, 'n, 's, 'v>( +impl<'s> Loop<'s> { + fn parser<'i, 'n, 'v>( parser: &Parser<'i, 'n, 's, 'v>, ) -> impl Fn(LocatedSpan<&str>) -> TokenResult { |input| { @@ -155,8 +155,8 @@ impl Loop { } } -impl Tuplet { - fn parser<'i, 'n, 's, 'v>( +impl<'s> Tuplet<'s> { + fn parser<'i, 'n, 'v>( parser: &Parser<'i, 'n, 's, 'v>, ) -> impl Fn(LocatedSpan<&str>) -> TokenResult { |input| { @@ -172,18 +172,18 @@ impl<'s> Slope<'s> { parser: &Parser<'i, 'n, 's, 'v>, ) -> impl Fn(LocatedSpan<&'a str>) -> TokenResult<'a, Self> { |input| { + let iter: std::collections::hash_map::Iter<'s, String, VariableChange> = + parser.slopes.iter(); delimited( char('{'), alt( - parser - .slopes - .iter() + iter .map(|(k, v)| { - Box::new(move |input: LocatedSpan<&'a str>| { + Box::new(move|input: LocatedSpan<&'a str>| { value(v, tag(k.as_str())).parse(input) }) as Box< - dyn Fn( + dyn 's + Fn( LocatedSpan<&'a str>, ) -> TokenResult<'a, &'s VariableChange>, @@ -191,7 +191,7 @@ impl<'s> Slope<'s> { }) .collect::) -> TokenResult<'a, &'s VariableChange>, + dyn 's + Fn(LocatedSpan<&'a str>) -> TokenResult<'a, &'s VariableChange>, >, >>() .as_mut_slice(), @@ -209,7 +209,7 @@ impl<'s> Slope<'s> { fn expression_parser + Ord + Display>( variables: &[C], ) -> impl Fn(LocatedSpan<&str>) -> TokenResult { - move |input: LocatedSpan<&str>| { + |input: LocatedSpan<&str>| { let mut end_index = 0; let mut current_expression = None; while input.input_len() > end_index { @@ -232,7 +232,7 @@ fn expression_parser + Ord + Display>( current_expression = Some(e); end_index += 1; } else if let Some(e) = current_expression { - return take(end_index).parse(input).map(move |(r, _)| (r, e)); + return take(end_index).parse(input).map(|(r, _)| (r, e)); } else { return Err(nom::Err::Failure(nom::error::Error::new( input,