From 0d01692c97ea8ca6fc4b229e5b9678cb252bceda Mon Sep 17 00:00:00 2001 From: Greg Brown Date: Sat, 9 Jan 2021 14:31:02 +0000 Subject: Introduce chomp as a procedural macro. Add a bunch of tests. Fix chomp and chewed so autochomp compiles. --- src/chomp/check/inline.rs | 119 +++++++++++++++++++++++++++++++++++++++++++++- src/chomp/error.rs | 17 ++++++- 2 files changed, 132 insertions(+), 4 deletions(-) (limited to 'src/chomp') diff --git a/src/chomp/check/inline.rs b/src/chomp/check/inline.rs index 43a2eb8..da501f1 100644 --- a/src/chomp/check/inline.rs +++ b/src/chomp/check/inline.rs @@ -158,7 +158,7 @@ mod tests { None, ) .into(), - None + None, ); let inlined = expr.fold(&mut InlineCalls::new(function)); assert_eq!( @@ -208,7 +208,7 @@ mod tests { None, ) .into(), - None + None, ); let inlined = expr.fold(&mut InlineCalls::new(function)); assert_eq!( @@ -231,4 +231,119 @@ mod tests { .into()) ) } + + #[test] + fn test_inline_double_subst() { + let expr = Call::new( + Name::Spanless("opt".to_string()), + vec![Call::new( + Name::Spanless("opt".to_string()), + vec![Literal::Spanless("x".to_string()).into()], + None, + ) + .into()], + None, + ); + let inlined = expr.fold(&mut InlineCalls::new(opt())); + assert_eq!( + inlined, + Ok(Alt::new( + Epsilon::default().into(), + None, + Alt::new( + Epsilon::default().into(), + None, + Literal::Spanless("x".to_string()).into() + ) + .into() + ) + .into()) + ) + } + + #[test] + fn test_inline_call_args() { + let expr = Fix::new( + Name::Spanless("rec".to_string()), + Cat::new( + Literal::Spanless("a".to_string()).into(), + None, + Call::new( + Name::Spanless("opt".to_string()), + vec![Cat::new( + Cat::new( + Literal::Spanless("a".to_string()).into(), + None, + Fix::new( + Name::Spanless("star".to_string()), + Call::new( + Name::Spanless("opt".to_string()), + vec![Cat::new( + Literal::Spanless(" ".to_string()).into(), + None, + Variable::new(Name::Spanless("star".to_string()), 0).into(), + ) + .into()], + None, + ) + .into(), + None, + ) + .into(), + ) + .into(), + None, + Variable::new(Name::Spanless("rec".to_string()), 0).into(), + ) + .into()], + None, + ) + .into(), + ) + .into(), + None, + ); + let inlined = expr.fold(&mut InlineCalls::new(opt())); + assert_eq!(inlined, + Ok(Fix::new( + Name::Spanless("rec".to_string()), + Cat::new( + Literal::Spanless("a".to_string()).into(), + None, + Alt::new( + Epsilon::default().into(), + None, + Cat::new( + Cat::new( + Literal::Spanless("a".to_string()).into(), + None, + Fix::new( + Name::Spanless("star".to_string()), + Alt::new( + Epsilon::default().into(), + None, + Cat::new( + Literal::Spanless(" ".to_string()).into(), + None, + Variable::new(Name::Spanless("star".to_string()), 0).into(), + ) + .into() + ) + .into(), + None, + ) + .into(), + ) + .into(), + None, + Variable::new(Name::Spanless("rec".to_string()), 0).into(), + ) + .into(), + ) + .into(), + ) + .into(), + None, + ).into())) + } } diff --git a/src/chomp/error.rs b/src/chomp/error.rs index ffcbdd0..e7e4660 100644 --- a/src/chomp/error.rs +++ b/src/chomp/error.rs @@ -358,6 +358,19 @@ pub enum SubstituteError { WrongArgCount { call: Call, expected: usize }, } +impl From for syn::Error { + fn from(e: SubstituteError) -> Self { + match e { + SubstituteError::FreeParameter(param) => { + Self::new(param.name().span().unwrap_or_else(Span::call_site), format!("undeclared variable `{}'", param.name())) + } + SubstituteError::WrongArgCount { call, expected } => { + Self::new(call.span().unwrap_or_else(Span::call_site), format!("wrong number of arguments. Expected {}, got {}", expected, call.args().len())) + } + } + } +} + impl Display for SubstituteError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { @@ -378,8 +391,8 @@ impl Display for SubstituteError { "{}:{}: wrong number of arguments. Expected {}, got {}", start.line, start.column, - call.args().len(), - expected + expected, + call.args().len() ) } } -- cgit v1.2.3