Skip to content

Commit

Permalink
Fix walrus in subscripts
Browse files Browse the repository at this point in the history
Resolves   #820.
  • Loading branch information
evhub committed Jan 18, 2024
1 parent a53a137 commit 17ff2a3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
14 changes: 8 additions & 6 deletions coconut/compiler/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,8 @@ class Grammar(object):
unsafe_fat_arrow = Literal("=>") | fixto(Literal("\u21d2"), "=>")
colon_eq = Literal(":=")
unsafe_dubcolon = Literal("::")
unsafe_colon = Literal(":")
colon = disambiguate_literal(":", ["::", ":="])
indexing_colon = disambiguate_literal(":", [":="]) # same as : but :: is allowed
lt_colon = Literal("<:")
semicolon = Literal(";") | invalid_syntax("\u037e", "invalid Greek question mark instead of semicolon", greedy=True)
multisemicolon = combine(OneOrMore(semicolon))
Expand Down Expand Up @@ -1199,21 +1199,23 @@ class Grammar(object):
| op_item
)

# for .[]
subscript_star = Forward()
subscript_star_ref = star
slicetest = Optional(test_no_chain)
sliceop = condense(unsafe_colon + slicetest)
sliceop = condense(indexing_colon + slicetest)
subscript = condense(
slicetest + sliceop + Optional(sliceop)
| Optional(subscript_star) + test
| Optional(subscript_star) + new_namedexpr_test
)
subscriptlist = itemlist(subscript, comma, suppress_trailing=False) | new_namedexpr_test
subscriptlist = itemlist(subscript, comma, suppress_trailing=False)

# for .$[]
slicetestgroup = Optional(test_no_chain, default="")
sliceopgroup = unsafe_colon.suppress() + slicetestgroup
sliceopgroup = indexing_colon.suppress() + slicetestgroup
subscriptgroup = attach(
slicetestgroup + sliceopgroup + Optional(sliceopgroup)
| test,
| new_namedexpr_test,
subscriptgroup_handle,
)
subscriptgrouplist = itemlist(subscriptgroup, comma)
Expand Down
2 changes: 1 addition & 1 deletion coconut/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
VERSION = "3.0.4"
VERSION_NAME = None
# False for release, int >= 1 for develop
DEVELOP = 14
DEVELOP = 15
ALPHA = False # for pre releases rather than post releases

assert DEVELOP is False or DEVELOP >= 1, "DEVELOP must be False or an int >= 1"
Expand Down
2 changes: 2 additions & 0 deletions coconut/tests/src/cocotest/target_311/py311_test.coco
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ def py311_test() -> bool:
except* ValueError as err:
got_err = err
assert repr(got_err) == repr(multi_err), (got_err, multi_err)
assert [1, 2, 3][x := 1] == 2
assert x == 1
return True

0 comments on commit 17ff2a3

Please sign in to comment.