feat(parser): parse strings in Midas files
This commit is contained in:
@@ -69,6 +69,8 @@ class MidasLexer(Lexer):
|
|||||||
):
|
):
|
||||||
self.advance()
|
self.advance()
|
||||||
self.add_token(TokenType.WHITESPACE)
|
self.add_token(TokenType.WHITESPACE)
|
||||||
|
case '"' | "'":
|
||||||
|
self.scan_string(char)
|
||||||
case _:
|
case _:
|
||||||
if char.isdigit():
|
if char.isdigit():
|
||||||
self.scan_number()
|
self.scan_number()
|
||||||
@@ -78,6 +80,17 @@ class MidasLexer(Lexer):
|
|||||||
self.error("Unexpected character")
|
self.error("Unexpected character")
|
||||||
return None
|
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):
|
def scan_number(self):
|
||||||
"""Scan the rest of number and add it as a token
|
"""Scan the rest of number and add it as a token
|
||||||
|
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ class TokenType(Enum):
|
|||||||
TRUE = auto()
|
TRUE = auto()
|
||||||
FALSE = auto()
|
FALSE = auto()
|
||||||
NONE = auto()
|
NONE = auto()
|
||||||
|
STRING = auto()
|
||||||
|
|
||||||
# Keywords
|
# Keywords
|
||||||
TYPE = auto()
|
TYPE = auto()
|
||||||
|
|||||||
@@ -418,6 +418,9 @@ class MidasParser(Parser):
|
|||||||
if self.match(TokenType.NUMBER):
|
if self.match(TokenType.NUMBER):
|
||||||
return LiteralExpr(location=token.get_location(), value=token.value)
|
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():
|
if self.match_identifier():
|
||||||
return VariableExpr(location=token.get_location(), name=token)
|
return VariableExpr(location=token.get_location(), name=token)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user