From 71296e2a153ac75ce9d79dc0802c4c3394a6a662 Mon Sep 17 00:00:00 2001 From: Matthias Meissner Date: Fri, 1 Dec 2023 15:32:47 +0100 Subject: [PATCH] parse tuple literals --- nemo/src/io/parser.rs | 47 ++++++++++++++++++++++++++---- nemo/src/model/rule_model/tuple.rs | 2 +- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/nemo/src/io/parser.rs b/nemo/src/io/parser.rs index f6e96793d..ec3d1342e 100644 --- a/nemo/src/io/parser.rs +++ b/nemo/src/io/parser.rs @@ -326,7 +326,8 @@ impl<'a> RuleParser<'a> { "parse_complex_constant_term", alt(( parse_constant_term(&self.prefixes), - map(self.parse_map_literal(), Constant::MapLiteral), + map(|s| self.parse_tuple_literal()(s), Constant::TupleLiteral), + map(|s| self.parse_map_literal()(s), Constant::MapLiteral), )), ) } @@ -631,7 +632,9 @@ impl<'a> RuleParser<'a> { } /// Parse an entry in a [Map], i.e., a [Key]--[Term] pair. - pub fn parse_map_entry(&'a self, s: Span<'a>) -> IntermediateResult<(Key, Constant)> { + pub fn parse_map_entry( + &'a self, + ) -> impl FnMut(Span<'a>) -> IntermediateResult<(Key, Constant)> { traced( "parse_map_entry", separated_pair( @@ -639,7 +642,7 @@ impl<'a> RuleParser<'a> { self.parse_equals(), map(self.parse_complex_constant_term(), |term| term), ), - )(s) + ) } /// Parse an object literal. @@ -649,7 +652,7 @@ impl<'a> RuleParser<'a> { delimited( self.parse_open_brace(), map( - separated_list0(self.parse_comma(), |s| self.parse_map_entry(s)), + separated_list0(self.parse_comma(), self.parse_map_entry()), Map::from_iter, ), self.parse_close_brace(), @@ -657,6 +660,21 @@ impl<'a> RuleParser<'a> { ) } + /// Parse a tuple literal. + pub fn parse_tuple_literal(&'a self) -> impl FnMut(Span<'a>) -> IntermediateResult { + traced( + "parse_tuple_literal", + delimited( + self.parse_open_parenthesis(), + map( + separated_list0(self.parse_comma(), self.parse_complex_constant_term()), + Tuple::from_iter, + ), + self.parse_close_parenthesis(), + ), + ) + } + /// Parse a file format name. pub fn parse_file_format(&'a self) -> impl FnMut(Span<'a>) -> IntermediateResult { traced("parse_file_format", move |input| { @@ -2351,7 +2369,7 @@ mod test { let entry = format!("{ident}=23"); assert_parse!( - |s| parser.parse_map_entry(s), + parser.parse_map_entry(), &entry, ( key.clone(), @@ -2393,6 +2411,25 @@ mod test { ); } + #[test] + fn tuple_literal() { + let parser = RuleParser::new(); + + let expected: Tuple = [ + Constant::Abstract(Identifier("something".to_string())), + Constant::NumericLiteral(NumericLiteral::Integer(42)), + Constant::TupleLiteral([].into_iter().collect()), + ] + .into_iter() + .collect(); + + assert_parse!( + parser.parse_tuple_literal(), + r#"(something, 42, ())"#, + expected + ); + } + #[test] fn qualified_predicate_name() { let parser = RuleParser::new(); diff --git a/nemo/src/model/rule_model/tuple.rs b/nemo/src/model/rule_model/tuple.rs index 2dec69690..48cf7fc9e 100644 --- a/nemo/src/model/rule_model/tuple.rs +++ b/nemo/src/model/rule_model/tuple.rs @@ -45,7 +45,7 @@ impl std::fmt::Display for Tuple { f.write_str( &self .iter() - .map(|x| ToString::to_string(x)) + .map(ToString::to_string) .by_ref() .intersperse(", ".into()) .collect::(),