Skip to content

Commit 31b1420

Browse files
authored
Allow more flexible interop property access syntax for macros (#276)
1 parent 3e483c6 commit 31b1420

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/basilisp/lang/reader.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,20 @@ def _read_interop(ctx: ReaderContext, end_token: str) -> llist.List:
389389
elif token == '-':
390390
reader.advance()
391391
seq.append(_INTEROP_PROP)
392+
393+
# If whitespace immediately follows, this is the form
394+
# (.- object member ...), otherwise it is (.-member object ...).
395+
# We need to support both, as the former is more commonly
396+
# the format which will appear in macros.
392397
if whitespace_chars.match(reader.peek()):
393-
raise SyntaxError(f"Expected Symbol; found whitespace")
394-
member = _read_next_consuming_comment(ctx)
395-
if not isinstance(member, symbol.Symbol):
396-
raise SyntaxError(f"Expected Symbol; found {type(member)}")
397-
instance = _read_next_consuming_comment(ctx)
398+
instance = _read_next_consuming_comment(ctx)
399+
member = _read_next_consuming_comment(ctx)
400+
else:
401+
member = _read_next_consuming_comment(ctx)
402+
if not isinstance(member, symbol.Symbol):
403+
raise SyntaxError(f"Expected Symbol; found {type(member)}")
404+
instance = _read_next_consuming_comment(ctx)
405+
398406
seq.append(instance)
399407
seq.append(member)
400408
else:

tests/basilisp/reader_test.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -586,13 +586,14 @@ def test_interop_call():
586586

587587

588588
def test_interop_prop():
589-
assert read_str_first("(. sym -name)") == llist.l(
590-
sym.symbol('.-'), sym.symbol('sym'), sym.symbol('name'))
591-
assert read_str_first('(.-algorithm encoder)') == llist.l(
592-
sym.symbol('.-'), sym.symbol('encoder'), sym.symbol('algorithm'))
593-
594-
with pytest.raises(reader.SyntaxError):
595-
read_str_first('(.- name sym)')
589+
assert llist.l(sym.symbol('.-'), sym.symbol('sym'), sym.symbol('name')
590+
) == read_str_first("(. sym -name)")
591+
assert llist.l(sym.symbol('.-'), sym.symbol('encoder'), sym.symbol('algorithm')
592+
) == read_str_first('(.-algorithm encoder)')
593+
assert llist.l(sym.symbol('.-'), sym.symbol('name'), sym.symbol('sym')
594+
) == read_str_first('(.- name sym)')
595+
assert llist.l(sym.symbol('.-'), sym.symbol('name'), "string"
596+
) == read_str_first('(.- name "string")')
596597

597598
with pytest.raises(reader.SyntaxError):
598599
read_str_first('(.-"string" sym)')

0 commit comments

Comments
 (0)