fix lifetime issues

This commit is contained in:
Breval Ferrari 2025-05-20 23:18:26 +02:00
parent 8387aa61bd
commit 8563ee3de2
2 changed files with 25 additions and 25 deletions

View file

@ -3,7 +3,7 @@ use std::{collections::HashMap, str::FromStr};
use derive_new::new; use derive_new::new;
use fasteval::{Compiler, Instruction, Slab}; use fasteval::{Compiler, Instruction, Slab};
pub type TokenVec = Vec<Box<dyn Token>>; pub type TokenVec<'a> = Vec<Box<dyn Token + 'a>>;
pub trait Token { pub trait Token {
fn apply(&self, context: Context) -> Context { fn apply(&self, context: Context) -> Context {
@ -30,7 +30,7 @@ pub struct VariableChange(pub char, pub Expression);
impl Token for VariableChange {} impl Token for VariableChange {}
pub struct Loop(pub LoopCount, pub TokenVec); pub struct Loop<'a>(pub LoopCount, pub TokenVec<'a>);
pub enum LoopCount { pub enum LoopCount {
Litteral(usize), 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<'_> {} impl Token for Slope<'_> {}

View file

@ -32,10 +32,10 @@ pub struct Parser<'i, 'n, 's, 'v> {
variables: &'v [char], variables: &'v [char],
} }
impl<'i> Parser<'i, '_, '_, '_> { impl<'i, 's> Parser<'i, '_, 's, '_> {
pub fn parse_all( pub fn parse_all(
&self, &self,
) -> Result<Vec<Box<dyn Token>>, Error<nom_locate::LocatedSpan<&'i str>>> { ) -> Result<Vec<Box<dyn Token + 's>>, Error<nom_locate::LocatedSpan<&'i str>>> {
token_parser(self) token_parser(self)
.parse_complete(LocatedSpan::new(self.input)) .parse_complete(LocatedSpan::new(self.input))
.finish() .finish()
@ -43,11 +43,11 @@ impl<'i> Parser<'i, '_, '_, '_> {
} }
} }
fn token_parser<'a>( fn token_parser<'a, 's>(
parser: &Parser, parser: &Parser<'_, '_, 's, '_>,
) -> impl NomParser< ) -> impl NomParser<
LocatedSpan<&'a str>, LocatedSpan<&'a str>,
Output = Vec<Box<dyn Token>>, Output = Vec<Box<dyn Token + 's>>,
Error = nom::error::Error<LocatedSpan<&'a str>>, Error = nom::error::Error<LocatedSpan<&'a str>>,
> { > {
let space_or_comment = || { let space_or_comment = || {
@ -71,7 +71,7 @@ fn token_parser<'a>(
)) ))
} }
fn into_box(token: impl Token + 'static) -> Box<dyn Token> { fn into_box<'a>(token: impl Token + 'a) -> Box<dyn Token + 'a> {
Box::new(token) Box::new(token)
} }
@ -105,7 +105,7 @@ impl Note {
Output = Self, Output = Self,
Error = nom::error::Error<LocatedSpan<&'a str>>, Error = nom::error::Error<LocatedSpan<&'a str>>,
> { > {
move |input: LocatedSpan<&'a str>| { |input: LocatedSpan<&'a str>| {
let mut parsers: Vec<Box<dyn Fn(LocatedSpan<&'a str>) -> TokenResult<'a, Self>>> = let mut parsers: Vec<Box<dyn Fn(LocatedSpan<&'a str>) -> TokenResult<'a, Self>>> =
notes notes
.iter() .iter()
@ -135,8 +135,8 @@ impl VariableChange {
} }
} }
impl Loop { impl<'s> Loop<'s> {
fn parser<'i, 'n, 's, 'v>( fn parser<'i, 'n, 'v>(
parser: &Parser<'i, 'n, 's, 'v>, parser: &Parser<'i, 'n, 's, 'v>,
) -> impl Fn(LocatedSpan<&str>) -> TokenResult<Self> { ) -> impl Fn(LocatedSpan<&str>) -> TokenResult<Self> {
|input| { |input| {
@ -155,8 +155,8 @@ impl Loop {
} }
} }
impl Tuplet { impl<'s> Tuplet<'s> {
fn parser<'i, 'n, 's, 'v>( fn parser<'i, 'n, 'v>(
parser: &Parser<'i, 'n, 's, 'v>, parser: &Parser<'i, 'n, 's, 'v>,
) -> impl Fn(LocatedSpan<&str>) -> TokenResult<Self> { ) -> impl Fn(LocatedSpan<&str>) -> TokenResult<Self> {
|input| { |input| {
@ -172,18 +172,18 @@ impl<'s> Slope<'s> {
parser: &Parser<'i, 'n, 's, 'v>, parser: &Parser<'i, 'n, 's, 'v>,
) -> impl Fn(LocatedSpan<&'a str>) -> TokenResult<'a, Self> { ) -> impl Fn(LocatedSpan<&'a str>) -> TokenResult<'a, Self> {
|input| { |input| {
let iter: std::collections::hash_map::Iter<'s, String, VariableChange> =
parser.slopes.iter();
delimited( delimited(
char('{'), char('{'),
alt( alt(
parser iter
.slopes
.iter()
.map(|(k, v)| { .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) value(v, tag(k.as_str())).parse(input)
}) })
as Box< as Box<
dyn Fn( dyn 's + Fn(
LocatedSpan<&'a str>, LocatedSpan<&'a str>,
) )
-> TokenResult<'a, &'s VariableChange>, -> TokenResult<'a, &'s VariableChange>,
@ -191,7 +191,7 @@ impl<'s> Slope<'s> {
}) })
.collect::<Vec< .collect::<Vec<
Box< Box<
dyn Fn(LocatedSpan<&'a str>) -> TokenResult<'a, &'s VariableChange>, dyn 's + Fn(LocatedSpan<&'a str>) -> TokenResult<'a, &'s VariableChange>,
>, >,
>>() >>()
.as_mut_slice(), .as_mut_slice(),
@ -209,7 +209,7 @@ impl<'s> Slope<'s> {
fn expression_parser<C: Into<char> + Ord + Display>( fn expression_parser<C: Into<char> + Ord + Display>(
variables: &[C], variables: &[C],
) -> impl Fn(LocatedSpan<&str>) -> TokenResult<Expression> { ) -> impl Fn(LocatedSpan<&str>) -> TokenResult<Expression> {
move |input: LocatedSpan<&str>| { |input: LocatedSpan<&str>| {
let mut end_index = 0; let mut end_index = 0;
let mut current_expression = None; let mut current_expression = None;
while input.input_len() > end_index { while input.input_len() > end_index {
@ -232,7 +232,7 @@ fn expression_parser<C: Into<char> + Ord + Display>(
current_expression = Some(e); current_expression = Some(e);
end_index += 1; end_index += 1;
} else if let Some(e) = current_expression { } 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 { } else {
return Err(nom::Err::Failure(nom::error::Error::new( return Err(nom::Err::Failure(nom::error::Error::new(
input, input,