feat(parser): parse strings in Midas files

This commit is contained in:
2026-06-19 21:53:35 +02:00
parent 7695d50537
commit db8d88ef35
3 changed files with 17 additions and 0 deletions

View File

@@ -69,6 +69,8 @@ class MidasLexer(Lexer):
):
self.advance()
self.add_token(TokenType.WHITESPACE)
case '"' | "'":
self.scan_string(char)
case _:
if char.isdigit():
self.scan_number()
@@ -78,6 +80,17 @@ class MidasLexer(Lexer):
self.error("Unexpected character")
return None
def scan_string(self, opening: str):
while self.peek() != opening and not self.is_at_end():
self.advance()
if self.is_at_end():
self.error("Unterminated string")
self.advance()
value: str = self.source[self.start + 1 : self.idx - 1]
self.add_token(TokenType.STRING, value)
def scan_number(self):
"""Scan the rest of number and add it as a token

View File

@@ -43,6 +43,7 @@ class TokenType(Enum):
TRUE = auto()
FALSE = auto()
NONE = auto()
STRING = auto()
# Keywords
TYPE = auto()

View File

@@ -418,6 +418,9 @@ class MidasParser(Parser):
if self.match(TokenType.NUMBER):
return LiteralExpr(location=token.get_location(), value=token.value)
if self.match(TokenType.STRING):
return LiteralExpr(location=token.get_location(), value=token.value)
if self.match_identifier():
return VariableExpr(location=token.get_location(), name=token)