Skip to content

Commit

Permalink
remove asserts in Lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
NiumXp committed Feb 13, 2024
1 parent e5b025e commit 19109da
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
11 changes: 4 additions & 7 deletions norminette/lexer/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ def __init__(self, file: File):
self.__line_pos = self.__line = 1

def raw_peek(self, *, offset: int = 0, collect: int = 1):
assert collect > 0 and offset >= 0
if (pos := self.__pos + offset) < len(self.file.source):
return ''.join(self.file.source[pos:pos+collect])
return None
Expand Down Expand Up @@ -122,7 +121,6 @@ def pop(
use_spaces: bool = False,
use_escape: bool = False,
) -> str:
assert times > 0
result = ""
for _ in range(times):
for _ in range(100):
Expand All @@ -147,8 +145,7 @@ def pop(
# BUG It is just considering one `byte` (0x0 to 0xFF), so it not works correctly
# with prefixed strings like `L"\0x1234"`.
peek = self.raw_peek(offset=size, collect=2)
assert peek # TODO Replace it to `raise UnexpectedEOF`
if peek[0] not in hexadecimal_digits:
if peek is None or peek[0] not in hexadecimal_digits:
error = Error.from_name("NO_HEX_DIGITS", level="Notice")
error.add_highlight(self.__line, self.__line_pos + size - 1, length=1)
self.file.errors.add(error)
Expand Down Expand Up @@ -380,9 +377,9 @@ def parse_line_comment(self) -> Optional[Token]:
return
pos = self.line_pos()
val = self.pop(times=2)
while self.peek():
char, _ = self.peek()
if char in ('\n', None):
while result := self.peek():
char, _ = result
if char == '\n':
break
try:
val += self.pop()
Expand Down
6 changes: 5 additions & 1 deletion tests/test_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_lexer_raw_peek(source: str, parameters: Dict[str, Any], expected: Optio
"Newline": ['\n', {}, ('\n', 1)],
"Escaped newline": ["\\\n", {}, ('\\', 1)],
"Times with exact chars": ["abc", {"times": 3}, ("abc", 3)],
"Times with trigraphs": [r"??<a??<b", {"times": 4}, ("{a{b", 6)],
"Times with trigraphs": [r"??<a??<b", {"times": 4}, ("{a{b", 8)],
"Times with trigraphs 2": [r"??<a??<b", {"times": 4}, ("{a{b", 8)],
"Offset with large source": ["heello!s", {"offset": 6}, ('!', 1)],
# "Offset with large source with trigraphs": ["he??<el??/lo!s", {"offset": 8}, ('!', 1)], # teoric BUG
Expand Down Expand Up @@ -98,6 +98,10 @@ def test_lexer_peek(source: str, parameters: Dict[str, Any], expected: Optional[
"Multiples escaped newlines": ["\\\n\\\na\n", {}, 'a'],
"Multiples escaped newlines with trigraphs": ["??/\n??/\na\n", {}, 'a'],
"Bla": ["\\\na\n", {}, 'a'],
"Escape hex followed by EOF": [r"\x", {}, '\\'],
"Escape hex followed by EOF with use_escape": [r"\x", {"use_escape": True}, r"\x"],
"Bad escape hex with use_escape and times": [r"\x\x", {"use_escape": True, "times": 2}, r"\x\x"],
"Ok escape hex followed by a bad with use_escape and times": [r"\xF\x", {"use_escape": True, "times": 2}, r"\xF\x"],
}))
def test_lexer_pop(source: str, parameters: Dict[str, Any], expected: Optional[str]):
lexer = lexer_from_source(source)
Expand Down

0 comments on commit 19109da

Please sign in to comment.