Skip to content

Commit

Permalink
Fix parsing of partial float literals
Browse files Browse the repository at this point in the history
  • Loading branch information
aazuspan committed Aug 21, 2024
1 parent da2d1fa commit dfc05bb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
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)

0 comments on commit dfc05bb

Please sign in to comment.