feat(parser): parse strings in Midas files
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -43,6 +43,7 @@ class TokenType(Enum):
|
||||
TRUE = auto()
|
||||
FALSE = auto()
|
||||
NONE = auto()
|
||||
STRING = auto()
|
||||
|
||||
# Keywords
|
||||
TYPE = auto()
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user