Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of partial float literals #33

Merged
merged 1 commit into from
Aug 21, 2024
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
14 changes: 10 additions & 4 deletions src/spinasm_lsp/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import annotations

import contextlib

import lsprotocol.types as lsp
from asfv1 import fv1parse

Expand Down Expand Up @@ -64,10 +66,14 @@ def __next__(self) -> None:
current_line_txt = self._source[self._current_line]
current_symbol = self.parsed_symbol.txt

# Start at the current column to skip previous duplicates of the symbol
self._current_character = current_line_txt.index(
current_symbol, self._current_character
)
# Update the current parsed character. This can fail under rare circumstances,
# in which case we'll leave _current_character unchanged.
# See https://github.com/aazuspan/spinasm-lsp/issues/31
with contextlib.suppress(ValueError):
# Start at the current column to skip previous duplicates of the symbol
self._current_character = current_line_txt.index(
current_symbol, self._current_character
)


class SPINAsmDiagnosticParser(SPINAsmPositionParser):
Expand Down
23 changes: 23 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,26 @@ def test_example_patches(patch, data_regression):
encoded = serialize_parser_output(parser)

data_regression.check(encoded)


def test_parsing_as_typed():
"""Test that the parser is fault tolerant for partially entered programs."""

# Example program from SPINAsm documentation. Iteratively parsing at each character
# is slow, so we use a small sample program here rather than parameterizing over the
# larger test patches.
source = """
; Example program from SPINAsm documentation
Attn EQU 0.5
Tmp_Reg EQU 63
Tmp_Del EQU $2000

sof 0,0
rda Tmp_Del,Attn
wrax Tmp_Reg,1.0
wrax DACL
"""
source_chars = list(source)
for i in range(len(source_chars)):
partial_source = "".join(source_chars[:i])
assert SPINAsmParser(partial_source)
Loading