Skip to content

Commit

Permalink
trailing comma for tuple implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
ThakeeNathees committed Sep 20, 2024
1 parent 2896e1f commit a19e00b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
8 changes: 4 additions & 4 deletions jac/jaclang/compiler/jac.lark
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,11 @@ kv_pair: expression COLON expression | STAR_POW expression
expr_list: (expr_list COMMA)? expression

// Tuples and Jac Tuples
tuple_list: expression COMMA expr_list COMMA kw_expr_list
| expression COMMA kw_expr_list
| expression COMMA expr_list
tuple_list: expression COMMA expr_list COMMA kw_expr_list COMMA?
| expression COMMA kw_expr_list COMMA?
| expression COMMA expr_list COMMA?
| expression COMMA
| kw_expr_list
| kw_expr_list COMMA?

kw_expr_list: (kw_expr_list COMMA)? kw_expr
kw_expr: named_ref EQ expression | STAR_POW expression
Expand Down
32 changes: 22 additions & 10 deletions jac/jaclang/compiler/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2813,25 +2813,37 @@ def tuple_list(
) -> ast.SubNodeList[ast.Expr | ast.KWPair]:
"""Grammar rule.
tuple_list: expression COMMA expr_list COMMA kw_expr_list
| expression COMMA kw_expr_list
| expression COMMA expr_list
| expression COMMA
| kw_expr_list
tuple_list: expression COMMA expr_list COMMA kw_expr_list COMMA?
| expression COMMA kw_expr_list COMMA?
| expression COMMA expr_list COMMA?
| expression COMMA
| kw_expr_list COMMA?
"""
chomp = [*kid]
first_expr = None
if isinstance(chomp[0], ast.SubNodeList):
# The chomp will be like this:
# kw_expr_list, [COMMA]
if len(chomp) > 1:
# Add the comma to the subnode list if it exists, otherwise the last comma will not be a part of
# the ast, we need it for formatting.
chomp[0].kid.append(chomp[1])
return self.nu(chomp[0])
else:
first_expr = chomp[0]
chomp = chomp[2:]
# The chomp will be like this:
# expression, COMMA, [subnode_list, [COMMA, [kw_expr_list, [COMMA]]]]
# Pop the first expression from chomp.
first_expr = chomp[0] # Get the first expression.
chomp = chomp[2:] # Get rid of expr and comma.

# The chomp will be like this:
# [subnode_list, [COMMA, [kw_expr_list, [COMMA]]]]
expr_list = []
if len(chomp):
expr_list = chomp[0].kid
chomp = chomp[1:]
expr_list = chomp[0].kid # Get the kids subnode list.
chomp = chomp[2:] # Get rid of the subnode list and a comma if exists.
if len(chomp):
chomp = chomp[1:]
# The chomp will be like this: [kw_expr_list, [COMMA]]
expr_list = [*expr_list, *chomp[0].kid]
expr_list = [first_expr, *expr_list]
valid_kid = [i for i in expr_list if isinstance(i, (ast.Expr, ast.KWPair))]
Expand Down

0 comments on commit a19e00b

Please sign in to comment.