Skip to content

Commit 28157e3

Browse files
committed
Fix more --pure errors
1 parent c20a308 commit 28157e3

File tree

6 files changed

+95
-38
lines changed

6 files changed

+95
-38
lines changed

coconut/compiler/compiler.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,7 @@ def bind(cls):
869869
cls.no_partial_trailer_atom <<= attach(cls.no_partial_trailer_atom_ref, cls.method("item_handle"))
870870
cls.simple_assign <<= attach(cls.simple_assign_ref, cls.method("item_handle"))
871871
cls.expr_simple_assign <<= attach(cls.expr_simple_assign_ref, cls.method("item_handle"))
872+
cls.unsafe_simple_assign <<= attach(cls.unsafe_simple_assign_ref, cls.method("item_handle"))
872873

873874
# handle all star assignments with star_assign_item_check
874875
cls.star_assign_item <<= attach(cls.star_assign_item_ref, cls.method("star_assign_item_check"))
@@ -903,6 +904,16 @@ def bind(cls):
903904
cls.name_match_funcdef <<= attach(cls.name_match_funcdef_ref, cls.method("name_match_funcdef_handle"))
904905
cls.op_match_funcdef <<= attach(cls.op_match_funcdef_ref, cls.method("op_match_funcdef_handle"))
905906
cls.base_case_funcdef <<= attach(cls.base_case_funcdef_ref, cls.method("base_case_funcdef_handle"))
907+
cls.case_funcdef_case <<= manage(
908+
cls.case_funcdef_case_ref,
909+
cls.method("case_funcdef_clause_manage"),
910+
include_in_packrat_context=False,
911+
)
912+
cls.case_funcdef_type <<= manage(
913+
cls.case_funcdef_type_ref,
914+
cls.method("case_funcdef_clause_manage"),
915+
include_in_packrat_context=False,
916+
)
906917
cls.yield_from <<= attach(cls.yield_from_ref, cls.method("yield_from_handle"))
907918
cls.typedef <<= attach(cls.typedef_ref, cls.method("typedef_handle"))
908919
cls.typedef_default <<= attach(cls.typedef_default_ref, cls.method("typedef_handle"))
@@ -5276,6 +5287,12 @@ def where_stmt_handle(self, loc, tokens):
52765287

52775288
return self.wrap_passthrough(out, early=True)
52785289

5290+
@property
5291+
def in_method(self):
5292+
"""Determine if currently in a method."""
5293+
cls_context = self.current_parsing_context("class")
5294+
return cls_context is not None and cls_context["name"] is not None and cls_context["in_method"]
5295+
52795296
@contextmanager
52805297
def class_manage(self, original, loc, item):
52815298
"""Manage the class parsing context."""
@@ -5322,11 +5339,13 @@ def func_manage(self, original, loc, item):
53225339
if cls_context is not None:
53235340
cls_context["in_method"] = in_method
53245341

5325-
@property
5326-
def in_method(self):
5327-
"""Determine if currently in a method."""
5328-
cls_context = self.current_parsing_context("class")
5329-
return cls_context is not None and cls_context["name"] is not None and cls_context["in_method"]
5342+
@contextmanager
5343+
def case_funcdef_clause_manage(self, original, loc, item):
5344+
"""Manage the scope for each case clause in a case def."""
5345+
with self.add_to_parsing_context({
5346+
"scope": self.get_empty_scope(),
5347+
}):
5348+
yield
53305349

53315350
@contextmanager
53325351
def has_expr_setname_manage(self, original, loc, item):

coconut/compiler/grammar.py

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,9 +1511,27 @@ class Grammar(object):
15111511
expr_assign_item = expr_base_assign_item | expr_star_assign_item
15121512
expr_assignlist <<= itemlist(expr_assign_item, comma, suppress_trailing=False)
15131513

1514+
# must be kept in sync with assignlist block above (but with unsafe_name)
1515+
unsafe_simple_assign = Forward()
1516+
unsafe_simple_assign_ref = maybeparens(
1517+
lparen,
1518+
(
1519+
# refname if there's a trailer, unsafe_name if not
1520+
(refname | passthrough_atom) + OneOrMore(ZeroOrMore(complex_trailer) + OneOrMore(simple_trailer))
1521+
| unsafe_name
1522+
| passthrough_atom
1523+
),
1524+
rparen,
1525+
)
1526+
15141527
simple_assignlist = maybeparens(lparen, itemlist(simple_assign, comma, suppress_trailing=False), rparen)
15151528
typed_assign_stmt = Forward()
1516-
typed_assign_stmt_ref = simple_assign + colon.suppress() + typedef_test + Optional(equals.suppress() + test_expr)
1529+
typed_assign_stmt_ref = (
1530+
# with assignment value, use simple_assign
1531+
simple_assign + colon.suppress() + typedef_test + equals.suppress() + test_expr
1532+
# bare annotation, use unsafe_simple_assign
1533+
| unsafe_simple_assign + colon.suppress() + typedef_test
1534+
)
15171535
basic_stmt = addspace(ZeroOrMore(assignlist + equals) + test_expr)
15181536

15191537
type_param = Forward()
@@ -2369,6 +2387,41 @@ class Grammar(object):
23692387
)
23702388

23712389
base_case_funcdef = Forward()
2390+
case_funcdef_case = Forward()
2391+
case_funcdef_case_ref = (
2392+
keyword("case").suppress()
2393+
+ lparen.suppress()
2394+
+ match_args_list
2395+
+ match_guard
2396+
+ rparen.suppress()
2397+
+ (
2398+
colon.suppress()
2399+
+ (
2400+
newline.suppress()
2401+
+ indent.suppress()
2402+
+ attach(condense(OneOrMore(stmt)), make_suite_handle)
2403+
+ dedent.suppress()
2404+
| attach(simple_stmt, make_suite_handle)
2405+
)
2406+
| equals.suppress()
2407+
+ (
2408+
(
2409+
newline.suppress()
2410+
+ indent.suppress()
2411+
+ attach(math_funcdef_body, make_suite_handle)
2412+
+ dedent.suppress()
2413+
)
2414+
| attach(implicit_return_stmt, make_suite_handle)
2415+
)
2416+
)
2417+
)
2418+
case_funcdef_type = Forward()
2419+
case_funcdef_type_ref = (
2420+
keyword("type").suppress()
2421+
+ parameters
2422+
+ return_typedef
2423+
+ newline.suppress()
2424+
)
23722425
base_case_funcdef_ref = (
23732426
keyword("def").suppress()
23742427
+ Group(
@@ -2381,38 +2434,11 @@ class Grammar(object):
23812434
- Optional(docstring)
23822435
- Group(OneOrMore(
23832436
labeled_group(
2384-
keyword("case").suppress()
2385-
+ lparen.suppress()
2386-
+ match_args_list
2387-
+ match_guard
2388-
+ rparen.suppress()
2389-
+ (
2390-
colon.suppress()
2391-
+ (
2392-
newline.suppress()
2393-
+ indent.suppress()
2394-
+ attach(condense(OneOrMore(stmt)), make_suite_handle)
2395-
+ dedent.suppress()
2396-
| attach(simple_stmt, make_suite_handle)
2397-
)
2398-
| equals.suppress()
2399-
+ (
2400-
(
2401-
newline.suppress()
2402-
+ indent.suppress()
2403-
+ attach(math_funcdef_body, make_suite_handle)
2404-
+ dedent.suppress()
2405-
)
2406-
| attach(implicit_return_stmt, make_suite_handle)
2407-
)
2408-
),
2437+
case_funcdef_case,
24092438
"match",
24102439
)
24112440
| labeled_group(
2412-
keyword("type").suppress()
2413-
+ parameters
2414-
+ return_typedef
2415-
+ newline.suppress(),
2441+
case_funcdef_type,
24162442
"type",
24172443
)
24182444
))

coconut/compiler/util.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,10 @@ def __repr__(self):
13931393

13941394

13951395
def manage(item, manager, include_in_packrat_context, greedy=True):
1396-
"""Attach a manager to the given parse item."""
1396+
"""Attach a manager to the given parse item.
1397+
1398+
include_in_packrat_context should be True if the manager changes whether a parse can succeed and False otherwise.
1399+
"""
13971400
return Wrap(item, manager, include_in_packrat_context=include_in_packrat_context, greedy=greedy)
13981401

13991402

coconut/root.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
VERSION = "3.2.0"
2727
VERSION_NAME = None
2828
# False for release, int >= 1 for develop
29-
DEVELOP = 5
29+
DEVELOP = 6
3030
ALPHA = False # for pre releases rather than post releases
3131

3232
assert DEVELOP is False or DEVELOP >= 1, "DEVELOP must be False or an int >= 1"

coconut/tests/main_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ class TestExternal(unittest.TestCase):
11131113
if not PYPY or PY2:
11141114
def test_prelude(self):
11151115
with using_paths(prelude):
1116-
comp_prelude(expect_retcode=None)
1116+
comp_prelude(["--strict", "--pure"], expect_retcode=None)
11171117
if MYPY and PY38:
11181118
run_prelude()
11191119

coconut/tests/src/extras.coco

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,15 @@ class A:
572572
class A:
573573
def mk_int(x) = int(x)
574574
""".strip())
575+
assert parse("""
576+
x: int
577+
x = 1
578+
""".strip())
579+
assert parse("""
580+
case def f:
581+
case(x) = x
582+
case(x, y) = x + y
583+
""".strip())
575584
assert_raises(-> parse("""
576585
def f():
577586
global x

0 commit comments

Comments
 (0)