Skip to content

Commit

Permalink
parse tuple literals
Browse files Browse the repository at this point in the history
  • Loading branch information
matzemathics committed Dec 1, 2023
1 parent d98a901 commit 71296e2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
47 changes: 42 additions & 5 deletions nemo/src/io/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)),
)
}
Expand Down Expand Up @@ -631,15 +632,17 @@ 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(
self.parse_map_key(),
self.parse_equals(),
map(self.parse_complex_constant_term(), |term| term),
),
)(s)
)
}

/// Parse an object literal.
Expand All @@ -649,14 +652,29 @@ 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(),
),
)
}

/// Parse a tuple literal.
pub fn parse_tuple_literal(&'a self) -> impl FnMut(Span<'a>) -> IntermediateResult<Tuple> {
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<FileFormat> {
traced("parse_file_format", move |input| {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion nemo/src/model/rule_model/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>(),
Expand Down

0 comments on commit 71296e2

Please sign in to comment.