diff --git a/midas/lexer/midas.py b/midas/lexer/midas.py index c3246fc..0510a6e 100644 --- a/midas/lexer/midas.py +++ b/midas/lexer/midas.py @@ -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 diff --git a/midas/lexer/token.py b/midas/lexer/token.py index f0c08a1..ce73aef 100644 --- a/midas/lexer/token.py +++ b/midas/lexer/token.py @@ -43,6 +43,7 @@ class TokenType(Enum): TRUE = auto() FALSE = auto() NONE = auto() + STRING = auto() # Keywords TYPE = auto() diff --git a/midas/parser/midas.py b/midas/parser/midas.py index 0d5e3ab..fdb58c2 100644 --- a/midas/parser/midas.py +++ b/midas/parser/midas.py @@ -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)