Skip to content

Commit f5141d7

Browse files
authored
support any char in character literals (#817)
Hi, could you please consider patch to accept any character by the reader as character literal. It fixes #816. I've updated the test to include the new cases. Thanks Co-authored-by: ikappaki <[email protected]>
1 parent be3bb4d commit f5141d7

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2626
* Allow `case` forms with only a default expression (#807)
2727
* Make `pr` a dynamic variable (#820)
2828
* Emit OS specific line endings for the `println` and `prn` fns (#810)
29+
* Support any character in character literals (#816)
2930

3031
### Fixed
3132
* Fix issue with `(count nil)` throwing an exception (#759)

src/basilisp/lang/reader.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,7 @@ def _read_character(ctx: ReaderContext) -> str:
12931293
"""Read a character literal from the input stream.
12941294
12951295
Character literals may appear as:
1296-
- \\a \\b \\c etc will yield 'a', 'b', and 'c' respectively
1296+
- \\a \\$ \\[ etc will yield 'a', '$', and '[' respectively
12971297
12981298
- \\newline, \\space, \\tab, \\formfeed, \\backspace, \\return yield
12991299
the named characters
@@ -1306,13 +1306,13 @@ def _read_character(ctx: ReaderContext) -> str:
13061306
s: List[str] = []
13071307
reader = ctx.reader
13081308
token = reader.peek()
1309+
is_first_char = True
13091310
while True:
1310-
if token == "" or whitespace_chars.match(token):
1311-
break
1312-
if not alphanumeric_chars.match(token):
1311+
if token == "" or (not is_first_char and not alphanumeric_chars.match(token)):
13131312
break
13141313
s.append(token)
13151314
token = reader.next_token()
1315+
is_first_char = False
13161316

13171317
char = "".join(s)
13181318
special = _SPECIAL_CHARS.get(char, None)

tests/basilisp/reader_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,10 @@ def test_deref():
12881288

12891289
def test_character_literal():
12901290
assert "a" == read_str_first("\\a")
1291+
assert "[" == read_str_first("\\[")
1292+
assert "," == read_str_first("\\,")
1293+
assert "^" == read_str_first("\\^")
1294+
assert " " == read_str_first("\\ ")
12911295
assert "Ω" == read_str_first("\\Ω")
12921296

12931297
assert "Ω" == read_str_first("\\u03A9")
@@ -1300,6 +1304,7 @@ def test_character_literal():
13001304
assert "\r" == read_str_first("\\return")
13011305

13021306
assert vec.v("a") == read_str_first("[\\a]")
1307+
assert vec.v("]") == read_str_first("[\\]]")
13031308
assert vec.v("Ω") == read_str_first("[\\Ω]")
13041309

13051310
assert llist.l(sym.symbol("str"), "Ω") == read_str_first("(str \\u03A9)")

0 commit comments

Comments
 (0)