2024-08-08 21:54:03 +10:00
|
|
|
|
|
|
|
from typing import Optional
|
|
|
|
|
2024-10-01 11:28:10 +10:00
|
|
|
from ppp_tokens import EofToken, IdentifierToken, Keyword, KeywordToken, NumberToken, StringToken, Symbol, SymbolToken, Token, TokenContents, Location
|
2024-08-08 21:54:03 +10:00
|
|
|
|
|
|
|
class Lexer:
|
2024-10-01 11:28:10 +10:00
|
|
|
def __init__(self, source: str, filename: str) -> None:
|
2024-08-08 21:54:03 +10:00
|
|
|
self._source = source
|
|
|
|
self._location = 0
|
|
|
|
self._line = 1
|
|
|
|
self._col = 0
|
2024-10-01 11:28:10 +10:00
|
|
|
self._filename = filename
|
2024-08-08 21:54:03 +10:00
|
|
|
self._peeked_token: Optional[Token] = None
|
2024-10-01 11:28:10 +10:00
|
|
|
|
|
|
|
def _loc(self) -> Location:
|
|
|
|
return Location(self._filename, self._line, self._col)
|
|
|
|
|
|
|
|
def _token(self, loc: Location, value: str, contents: TokenContents) -> Token:
|
|
|
|
return Token(loc, value, contents)
|
2024-08-08 21:54:03 +10:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_file(cls, path: str) -> 'Lexer':
|
|
|
|
with open(path) as f:
|
2024-10-01 11:28:10 +10:00
|
|
|
return cls(f.read(), path)
|
2024-08-08 21:54:03 +10:00
|
|
|
|
2024-10-01 11:28:10 +10:00
|
|
|
def _advance(self):
|
2024-08-08 21:54:03 +10:00
|
|
|
assert self._location < len(self._source)
|
2024-10-01 11:28:10 +10:00
|
|
|
self._line, self._col = (self._line + 1, 0) if self._source[self._location] == '\n' else (self._line, self._col + 1)
|
2024-08-08 21:54:03 +10:00
|
|
|
self._location += 1
|
|
|
|
|
|
|
|
def next_token(self) -> Token:
|
|
|
|
if self._peeked_token is not None:
|
|
|
|
peeked_token, self._peeked_token = self._peeked_token, None
|
|
|
|
return peeked_token
|
|
|
|
|
|
|
|
while self._location < len(self._source) and self._source[self._location] in ' \t\n': self._advance()
|
|
|
|
|
2024-10-01 11:28:10 +10:00
|
|
|
if self._location >= len(self._source): return self._token(self._loc(), '\0', EofToken())
|
2024-08-08 21:54:03 +10:00
|
|
|
|
|
|
|
match self._source[self._location]:
|
|
|
|
case c if c.isdigit():
|
|
|
|
start_location = self._location
|
2024-10-01 11:28:10 +10:00
|
|
|
loc = self._loc()
|
|
|
|
while self._location < len(self._source) and self._source[self._location].isdigit(): self._advance()
|
2024-08-08 21:54:03 +10:00
|
|
|
number = int(self._source[start_location:self._location])
|
2024-10-01 11:28:10 +10:00
|
|
|
return self._token(loc, self._source[start_location:self._location], NumberToken(number))
|
2024-08-08 21:54:03 +10:00
|
|
|
case c if c.isalpha() or c == "_":
|
|
|
|
start_location = self._location
|
2024-10-01 11:28:10 +10:00
|
|
|
loc = self._loc()
|
|
|
|
while self._location < len(self._source) and (self._source[self._location].isalpha() or self._source[self._location] in '_'): self._advance()
|
2024-08-08 21:54:03 +10:00
|
|
|
word = self._source[start_location:self._location]
|
|
|
|
try:
|
|
|
|
keyword = Keyword(word)
|
2024-10-01 11:28:10 +10:00
|
|
|
return self._token(loc, word, KeywordToken(keyword))
|
2024-08-08 21:54:03 +10:00
|
|
|
except ValueError:
|
|
|
|
try:
|
|
|
|
symbol = Symbol(word)
|
2024-10-01 11:28:10 +10:00
|
|
|
return self._token(loc, word, SymbolToken(symbol))
|
2024-08-08 21:54:03 +10:00
|
|
|
except ValueError:
|
2024-10-01 11:28:10 +10:00
|
|
|
return self._token(loc, word, IdentifierToken(word))
|
2024-08-08 21:54:03 +10:00
|
|
|
case '"':
|
2024-10-01 11:28:10 +10:00
|
|
|
# TODO: Proper escaping
|
2024-10-01 19:02:20 +10:00
|
|
|
loc = self._loc()
|
2024-10-01 11:28:10 +10:00
|
|
|
self._advance()
|
2024-08-08 21:54:03 +10:00
|
|
|
start_location = self._location
|
|
|
|
escaping = False
|
|
|
|
while self._location < len(self._source) and (self._source[self._location] != '"' or escaping):
|
|
|
|
escaping = self._source[self._location] == '\\' if not escaping else False
|
2024-10-01 11:28:10 +10:00
|
|
|
self._advance()
|
2024-08-08 21:54:03 +10:00
|
|
|
string = self._source[start_location:self._location].encode('utf-8').decode('unicode_escape')
|
2024-10-01 11:28:10 +10:00
|
|
|
self._advance()
|
|
|
|
return self._token(loc, self._source[start_location-1:self._location], StringToken(string))
|
2024-08-08 21:54:03 +10:00
|
|
|
# TODO: Make a proper Trie for this.
|
|
|
|
case '|' if self._location < len(self._source)-1 and self._source[self._location+1] == '|':
|
2024-10-01 11:28:10 +10:00
|
|
|
loc = self._loc()
|
|
|
|
self._advance(); self._advance()
|
|
|
|
return self._token(loc, self._source[self._location-2:self._location], SymbolToken(Symbol.Dpipe))
|
2024-08-08 21:54:03 +10:00
|
|
|
case '&' if self._location < len(self._source)-1 and self._source[self._location+1] == '&':
|
2024-10-01 11:28:10 +10:00
|
|
|
loc = self._loc()
|
|
|
|
self._advance(); self._advance()
|
|
|
|
return self._token(loc, self._source[self._location-2:self._location], SymbolToken(Symbol.Dampersand))
|
2024-08-08 21:54:03 +10:00
|
|
|
case '*' if self._location < len(self._source)-1 and self._source[self._location+1] == '*':
|
2024-10-01 11:28:10 +10:00
|
|
|
loc = self._loc()
|
|
|
|
self._advance(); self._advance()
|
|
|
|
return self._token(loc, self._source[self._location-2:self._location], SymbolToken(Symbol.Dasterisk))
|
2024-08-08 21:54:03 +10:00
|
|
|
case '-' if self._location < len(self._source)-1 and self._source[self._location+1] == '>':
|
2024-10-01 11:28:10 +10:00
|
|
|
loc = self._loc()
|
|
|
|
self._advance(); self._advance()
|
|
|
|
return self._token(loc, self._source[self._location-2:self._location], SymbolToken(Symbol.Arrow))
|
2024-08-08 21:54:03 +10:00
|
|
|
case '>' if self._location < len(self._source)-1 and self._source[self._location+1] == '=':
|
2024-10-01 11:28:10 +10:00
|
|
|
loc = self._loc()
|
|
|
|
self._advance(); self._advance()
|
|
|
|
return self._token(loc, self._source[self._location-2:self._location], SymbolToken(Symbol.GreaterEqual))
|
2024-08-08 21:54:03 +10:00
|
|
|
case '<' if self._location < len(self._source)-1 and self._source[self._location+1] == '=':
|
2024-10-01 11:28:10 +10:00
|
|
|
loc = self._loc()
|
|
|
|
self._advance(); self._advance()
|
|
|
|
return self._token(loc, self._source[self._location-2:self._location], SymbolToken(Symbol.LesserEqual))
|
2024-08-08 21:54:03 +10:00
|
|
|
case '=' if self._location < len(self._source)-1 and self._source[self._location+1] == '=':
|
2024-10-01 11:28:10 +10:00
|
|
|
loc = self._loc()
|
|
|
|
self._advance(); self._advance()
|
|
|
|
return self._token(loc, self._source[self._location-2:self._location], SymbolToken(Symbol.Dequal))
|
2024-08-08 21:54:03 +10:00
|
|
|
case '=' if self._location < len(self._source)-1 and self._source[self._location+1] == '>':
|
2024-10-01 11:28:10 +10:00
|
|
|
loc = self._loc()
|
|
|
|
self._advance(); self._advance()
|
|
|
|
return self._token(loc, self._source[self._location-2:self._location], SymbolToken(Symbol.EqualArrow))
|
2024-08-08 21:54:03 +10:00
|
|
|
case '!' if self._location < len(self._source)-1 and self._source[self._location+1] == '=':
|
2024-10-01 11:28:10 +10:00
|
|
|
loc = self._loc()
|
|
|
|
self._advance(); self._advance()
|
|
|
|
return self._token(loc, self._source[self._location-2:self._location], SymbolToken(Symbol.NotEqual))
|
2024-08-08 21:54:03 +10:00
|
|
|
case c if c in Symbol._value2member_map_:
|
2024-10-01 11:28:10 +10:00
|
|
|
loc = self._loc()
|
|
|
|
self._advance()
|
|
|
|
return self._token(loc, self._source[self._location-1], SymbolToken(Symbol(c)))
|
2024-08-08 21:54:03 +10:00
|
|
|
case _:
|
2024-10-01 11:28:10 +10:00
|
|
|
raise SyntaxError(f"{self._loc()}: Unknown character: '{c}'")
|
2024-08-08 21:54:03 +10:00
|
|
|
assert False, "Unreachable"
|
|
|
|
|
|
|
|
def peek_token(self) -> Token:
|
|
|
|
if self._peeked_token is not None: return self._peeked_token
|
|
|
|
self._peeked_token = self.next_token()
|
|
|
|
return self._peeked_token
|
|
|
|
|
|
|
|
def assert_tokenkind(self, kind: type) -> Token:
|
|
|
|
token = self.next_token()
|
2024-10-01 11:28:10 +10:00
|
|
|
if not isinstance(token.contents, kind): raise SyntaxError(f"{token.loc}: Expected {kind} but got {token.contents}!")
|
2024-08-08 21:54:03 +10:00
|
|
|
return token
|
|
|
|
|
|
|
|
def assert_token(self, expected: TokenContents) -> Token:
|
|
|
|
token = self.next_token()
|
2024-10-01 11:28:10 +10:00
|
|
|
if token.contents != expected: raise SyntaxError(f"{token.loc}: Expected {expected} but got {token.contents}!")
|
2024-08-08 21:54:03 +10:00
|
|
|
return token
|
|
|
|
|
|
|
|
def check_token(self, expected: TokenContents) -> bool:
|
|
|
|
token = self.peek_token()
|
|
|
|
return token.contents == expected
|
|
|
|
|
|
|
|
def check_tokens(self, *expected: TokenContents) -> bool:
|
|
|
|
for token in expected:
|
|
|
|
if self.check_token(token):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def check_tokenkind(self, kind: type) -> bool:
|
|
|
|
token = self.peek_token()
|
|
|
|
return isinstance(token.contents, kind)
|
|
|
|
|
|
|
|
def take_tokenkind(self, kind: type) -> Optional[Token]:
|
|
|
|
if self.check_tokenkind(kind):
|
|
|
|
return self.next_token()
|
|
|
|
return None
|
|
|
|
|
|
|
|
def take_token(self, token: TokenContents) -> Optional[Token]:
|
|
|
|
if self.check_token(token):
|
|
|
|
return self.next_token()
|
|
|
|
return None
|
|
|
|
|
|
|
|
def take_tokens(self, *tokens: TokenContents) -> Optional[Token]:
|
|
|
|
for token in tokens:
|
|
|
|
if self.check_token(token):
|
|
|
|
return self.next_token()
|
|
|
|
return None
|