Skip to content
This repository was archived by the owner on Apr 25, 2023. It is now read-only.

Commit c092db8

Browse files
committed
Finally fixed #5 for real, all the tests passed, everything works (I hope) I'm so glad thank god
1 parent b763c2f commit c092db8

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

ilanguage/Main/lexer.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -229,28 +229,31 @@ def gettoken(string: str, line: int, column: int) -> Optional[LexerToken]:
229229
LexerToken: Token from the specified string.
230230
"""
231231

232-
result = None
232+
try:
233+
result = None
233234

234-
if string in list(KEYWORDS):
235-
result = LexerToken(KEYWORDS[string], string)
235+
if string in list(KEYWORDS):
236+
result = LexerToken(KEYWORDS[string], string)
236237

237-
if len(string) > 1 and string[0] == "_":
238-
result = LexerToken("BUILTIN_CONST", string)
238+
if len(string) > 1 and string[0] == "_" and result is None:
239+
result = LexerToken("BUILTIN_CONST", string)
239240

240-
if string in ["true", "false"]:
241-
result = LexerToken("BOOL", string)
241+
if string in ["true", "false"] and result is None:
242+
result = LexerToken("BOOL", string)
242243

243-
if string in BASE_TYPES:
244-
result = LexerToken("BASETYPE", string)
244+
if string in BASE_TYPES and result is None:
245+
result = LexerToken("BASETYPE", string)
245246

246-
if validate_integer(string) and len(string) > 0:
247-
result = LexerToken("INT", string)
247+
if validate_integer(string) and result is None:
248+
result = LexerToken("INT", string)
248249

249-
if string[0] not in DIGITS_AS_STRINGS and len(string) > 0:
250-
result = LexerToken("NAME", string)
250+
if string[0] not in DIGITS_AS_STRINGS and result is None:
251+
result = LexerToken("NAME", string)
251252

252-
if not len(string) > 0:
253-
LexerError(f"Unrecognized Pattern: {string!r}", line, column)
253+
if not len(string) > 0 and result is None:
254+
LexerError(f"Unrecognized Pattern: {string!r}", line, column)
255+
except IndexError:
256+
pass
254257

255258
return result
256259

@@ -259,7 +262,6 @@ def gettoken(string: str, line: int, column: int) -> Optional[LexerToken]:
259262
# MAIN LEXER #
260263
##############
261264

262-
263265
def lex( # pylint: disable=R0912, R0915, R1260
264266
text: Optional[str] = None,
265267
) -> Optional[List[LexerToken]]:
@@ -269,7 +271,7 @@ def lex( # pylint: disable=R0912, R0915, R1260
269271
text (str): Text to lex.
270272
271273
Returns:
272-
List of lexed tokens.
274+
List[LexerToken]: List of lexed tokens.
273275
"""
274276

275277
tokens = []
@@ -346,19 +348,24 @@ def lex( # pylint: disable=R0912, R0915, R1260
346348
helper = 2
347349

348350
elif character in list(MARKS):
349-
tokens.append(gettoken(buffer.getvalue(), line, column))
351+
if gettoken(buffer.getvalue(), line, column) is not None:
352+
tokens.append(gettoken(buffer.getvalue(), line, column))
350353
tokens.append(LexerToken(MARKS[character], character))
351354
buffer.close()
352355
buffer = io.StringIO()
353356

354357
else:
355358
buffer.write(character)
359+
356360
if append_newline:
357361
append_newline = False
358362
tokens.append(LexerToken("NEWLINE", "\n"))
359363

360364
except IndexError:
361365
pass
366+
367+
if buffer.getvalue():
368+
tokens.append(gettoken(buffer.getvalue(), line, column))
362369
finally:
363370
buffer.close()
364371

0 commit comments

Comments
 (0)