Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
* Fix a bug where the reader was double counting the CRLF newline seq in metadata (#1063)

## [v0.2.3]
### Added
* Added a compiler metadata flag for suppressing warnings when Var indirection is unavoidable (#1052)
Expand Down
4 changes: 3 additions & 1 deletion src/basilisp/lang/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,9 @@ def loc(self) -> Tuple[int, int]:
def _update_loc(self):
"""Update the internal line and column buffers after a new character is
added."""
if newline_chars.match(self._buffer[-2]):
if self._buffer[-2] == "\n" or (
self._buffer[-2] == "\r" and self._buffer[-1] != "\n"
):
self._col.append(0)
self._line.append(self._line[-1] + 1)
else:
Expand Down
29 changes: 29 additions & 0 deletions tests/basilisp/reader_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,35 @@ def test_reader_lines_from_str_other_loc(self, tmp_path):
l3.meta.get(reader.READER_END_COL_KW),
)

@pytest.mark.parametrize(
"evalstr,first,second",
[
("[5]\n(def n 123)", (1, 1, 0, 3), (2, 2, 0, 11)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add cases for buffers starting with \n and \r\n as well since we're looking back in the last 2 characters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the following, is this what you had in mind?

            ("\n[5]\n(def n 123)", (2, 2, 0, 3), (3, 3, 0, 11)),
            ("\r[5]\r(def n 123)", (2, 2, 0, 3), (3, 3, 0, 11)),
            ("\r\n[5]\r\n(def n 123)", (2, 2, 0, 3), (3, 3, 0, 11)),

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. Thanks!

("[5]\r(def n 123)", (1, 1, 0, 3), (2, 2, 0, 11)),
("[5]\r\n(def n 123)", (1, 1, 0, 3), (2, 2, 0, 11)),
("[5]\n\n(def n 123)", (1, 1, 0, 3), (3, 3, 0, 11)),
("[5]\r\r(def n 123)", (1, 1, 0, 3), (3, 3, 0, 11)),
("[5]\r\n\r\n(def n 123)", (1, 1, 0, 3), (3, 3, 0, 11)),
("\n[5]\n(def n 123)", (2, 2, 0, 3), (3, 3, 0, 11)),
("\r[5]\r(def n 123)", (2, 2, 0, 3), (3, 3, 0, 11)),
("\r\n[5]\r\n(def n 123)", (2, 2, 0, 3), (3, 3, 0, 11)),
],
)
def test_reader_newlines_from_str(self, evalstr, first, second):
l0, l1 = list(reader.read_str(evalstr))
assert first == (
l0.meta.get(reader.READER_LINE_KW),
l0.meta.get(reader.READER_END_LINE_KW),
l0.meta.get(reader.READER_COL_KW),
l0.meta.get(reader.READER_END_COL_KW),
)
assert second == (
l1.meta.get(reader.READER_LINE_KW),
l1.meta.get(reader.READER_END_LINE_KW),
l1.meta.get(reader.READER_COL_KW),
l1.meta.get(reader.READER_END_COL_KW),
)

def test_reader_lines_from_file(self, tmp_path):
filename = tmp_path / "test.lpy"

Expand Down
Loading