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 21, 2024
1 parent 2896e1f commit c8d75d8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 21 deletions.
14 changes: 7 additions & 7 deletions jac/jaclang/compiler/jac.lark
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,9 @@ index_slice: LSQUARE expression? COLON expression? (COLON expression?)? RSQUARE
// Function calls
atomic_call: atomic_chain LPAREN param_list? (KW_BY atomic_call)? RPAREN

param_list: expr_list COMMA kw_expr_list
| kw_expr_list
| expr_list
param_list: expr_list COMMA kw_expr_list COMMA?
| kw_expr_list COMMA?
| expr_list COMMA?

// Atom
atom: named_ref
Expand Down 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
44 changes: 30 additions & 14 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 Expand Up @@ -2984,12 +2996,16 @@ def param_list(
) -> ast.SubNodeList[ast.Expr | ast.KWPair]:
"""Grammar rule.
param_list: expr_list COMMA kw_expr_list
| kw_expr_list
| expr_list
param_list: expr_list COMMA kw_expr_list COMMA?
| kw_expr_list COMMA?
| expr_list COMMA?
"""
if len(kid) == 1:
ends_with_comma = (len(kid) > 1 and isinstance(kid[-1], ast.Token)
and kid[-1].name == 'COMMA')
if len(kid) == 1 or (len(kid) == 2 and ends_with_comma):
if isinstance(kid[0], ast.SubNodeList):
if ends_with_comma: # Append the trailing comma to the subnode list.
kid[0].kid.append(kid[1])
return self.nu(kid[0])
else:
raise self.ice()
Expand Down
28 changes: 28 additions & 0 deletions jac/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from jaclang.utils.test import TestCase
from jaclang.vendor.pygls import uris
from jaclang.vendor.pygls.workspace import Workspace
from jaclang.langserve.engine import JacLangServer
import lsprotocol.types as lspt


lsp = JacLangServer()
workspace_path = "."
workspace = Workspace(workspace_path, lsp)
lsp.lsp._workspace = workspace
import_file = uris.from_fs_path(
"c:\\Users\\Thakee\\Desktop\\Home\\jaseci\\jaseci\\jac\\jaclang\\tests\\fixtures\\index_slice.jac"
)
lsp.deep_check(import_file)
positions = [
(23, 20, "index_slice.jac:2:8-2:13"),
(24, 24, "index_slice.jac:2:8-2:13"),
(27, 33, "index_slice.jac:2:8-2:13"),
]

for line, char, expected in positions:
print(str(lsp.get_definition(import_file, lspt.Position(line, char))))
assert (
expected in
str(lsp.get_definition(import_file, lspt.Position(line, char)))
)

0 comments on commit c8d75d8

Please sign in to comment.