From 8e24c6a56f866d5dd19c627fafe67aa3a2afbc45 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= <hugo@lysator.liu.se>
Date: Tue, 19 Sep 2023 12:11:42 +0200
Subject: [PATCH] Allow trailing comma in invoke:

---
 muppet/puppet/format/parser.py |  2 +-
 tests/test_parse_elsif.py      | 39 +++++++++++++++++++++++++++++++---
 2 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/muppet/puppet/format/parser.py b/muppet/puppet/format/parser.py
index 7802286..a6e9de8 100644
--- a/muppet/puppet/format/parser.py
+++ b/muppet/puppet/format/parser.py
@@ -433,7 +433,7 @@ class ParserFormatter(Serializer[ParseDirective]):
                 parser &= ws & self.s(x)
                 for x in xs:
                     parser &= ws & ',' & ws & self.s(x)
-        parser &= optional(ws & ')')
+        parser &= optional(ws & ',') & optional(ws & ')')
         return parser
 
     @override
diff --git a/tests/test_parse_elsif.py b/tests/test_parse_elsif.py
index 3ef6a7f..4e8c532 100644
--- a/tests/test_parse_elsif.py
+++ b/tests/test_parse_elsif.py
@@ -4,6 +4,7 @@ from muppet.puppet.format.parser import ParserFormatter
 from muppet.puppet.ast import build_ast
 from muppet.puppet.parser import puppet_parser
 from muppet.parser_combinator import ParserCombinator
+from pprint import pprint
 
 
 def test_parse_else_if():
@@ -57,9 +58,6 @@ def test_parse_elsif():
     # '}']
 
 
-from pprint import pprint
-
-
 def test_chained():
     s = "x.filter().join()"
     ast = build_ast(puppet_parser(s))
@@ -88,3 +86,38 @@ def test_chained_and_lambda():
     print("parser:\n" + str(parser))
     match_objects = ParserCombinator(s, "s").get(parser)
     pprint(match_objects)
+
+
+def test_invoke():
+    s = """
+    assert_type(
+      Enum['always','daily','weekly','reluctantly'],
+      $update['frequency'],
+    )
+    """
+    ast = build_ast(puppet_parser(s))
+    pprint(ast)
+    parser = ParserFormatter(s, "s").serialize(ast)
+    print("parser:\n" + str(parser))
+    match_objects = ParserCombinator(s, "s").get(parser)
+    pprint(match_objects)
+
+
+def test_invoke_inside_if():
+    # This previously failed due to invoke not handling trailing
+    # commas. The above example (test_invoke) apparently worked,
+    # because reasons.
+    s = """
+    if $update['frequency'] {
+      assert_type(
+        Enum['always','daily','weekly','reluctantly'],
+        $update['frequency'],
+      )
+    }
+    """
+    ast = build_ast(puppet_parser(s))
+    pprint(ast)
+    parser = ParserFormatter(s, "s").serialize(ast)
+    print("parser:\n" + str(parser))
+    match_objects = ParserCombinator(s, "s").get(parser)
+    pprint(match_objects)
-- 
GitLab