Skip to content

Commit dd7d239

Browse files
authored
Fix a bug where the reader was double counting the CRLF newline seq (#1064)
Hi, can you please a fix for the line number in the metadata generated by the reader. It fixes #1063. We now check the char at -2 only if it’s: - `\n` (for `\nC` or `\r\nC` case) - `\r` (for the `\rC`, but not `\r\n`, case). where `C` is any other char. Added tests for the same. Thanks --------- Co-authored-by: ikappaki <[email protected]>
1 parent 49fa1d1 commit dd7d239

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Fixed
10+
* Fix a bug where the reader was double counting the CRLF newline seq in metadata (#1063)
11+
912
## [v0.2.3]
1013
### Added
1114
* Added a compiler metadata flag for suppressing warnings when Var indirection is unavoidable (#1052)

src/basilisp/lang/reader.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,9 @@ def loc(self) -> Tuple[int, int]:
277277
def _update_loc(self):
278278
"""Update the internal line and column buffers after a new character is
279279
added."""
280-
if newline_chars.match(self._buffer[-2]):
280+
if self._buffer[-2] == "\n" or (
281+
self._buffer[-2] == "\r" and self._buffer[-1] != "\n"
282+
):
281283
self._col.append(0)
282284
self._line.append(self._line[-1] + 1)
283285
else:

tests/basilisp/reader_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,35 @@ def test_reader_lines_from_str_other_loc(self, tmp_path):
198198
l3.meta.get(reader.READER_END_COL_KW),
199199
)
200200

201+
@pytest.mark.parametrize(
202+
"evalstr,first,second",
203+
[
204+
("[5]\n(def n 123)", (1, 1, 0, 3), (2, 2, 0, 11)),
205+
("[5]\r(def n 123)", (1, 1, 0, 3), (2, 2, 0, 11)),
206+
("[5]\r\n(def n 123)", (1, 1, 0, 3), (2, 2, 0, 11)),
207+
("[5]\n\n(def n 123)", (1, 1, 0, 3), (3, 3, 0, 11)),
208+
("[5]\r\r(def n 123)", (1, 1, 0, 3), (3, 3, 0, 11)),
209+
("[5]\r\n\r\n(def n 123)", (1, 1, 0, 3), (3, 3, 0, 11)),
210+
("\n[5]\n(def n 123)", (2, 2, 0, 3), (3, 3, 0, 11)),
211+
("\r[5]\r(def n 123)", (2, 2, 0, 3), (3, 3, 0, 11)),
212+
("\r\n[5]\r\n(def n 123)", (2, 2, 0, 3), (3, 3, 0, 11)),
213+
],
214+
)
215+
def test_reader_newlines_from_str(self, evalstr, first, second):
216+
l0, l1 = list(reader.read_str(evalstr))
217+
assert first == (
218+
l0.meta.get(reader.READER_LINE_KW),
219+
l0.meta.get(reader.READER_END_LINE_KW),
220+
l0.meta.get(reader.READER_COL_KW),
221+
l0.meta.get(reader.READER_END_COL_KW),
222+
)
223+
assert second == (
224+
l1.meta.get(reader.READER_LINE_KW),
225+
l1.meta.get(reader.READER_END_LINE_KW),
226+
l1.meta.get(reader.READER_COL_KW),
227+
l1.meta.get(reader.READER_END_COL_KW),
228+
)
229+
201230
def test_reader_lines_from_file(self, tmp_path):
202231
filename = tmp_path / "test.lpy"
203232

0 commit comments

Comments
 (0)