add string support
This commit is contained in:
parent
8a9522d954
commit
285f0b8410
1 changed files with 38 additions and 4 deletions
|
@ -116,10 +116,16 @@ pub const Scanner = struct {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn makeTokenAdvance(self: *Scanner, ttype: TokenType) Token {
|
fn makeTokenLexeme(
|
||||||
var tok = self.makeToken(ttype);
|
self: *Scanner,
|
||||||
self.current += 1;
|
ttype: TokenType,
|
||||||
return tok;
|
lexeme: []const u8,
|
||||||
|
) Token {
|
||||||
|
return Token{
|
||||||
|
.ttype = ttype,
|
||||||
|
.lexeme = lexeme,
|
||||||
|
.line = self.line,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if the next character matches what is expected.
|
/// Check if the next character matches what is expected.
|
||||||
|
@ -198,6 +204,31 @@ pub const Scanner = struct {
|
||||||
return self.makeToken(ttype);
|
return self.makeToken(ttype);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Consume a string. stop_char is used to determine
|
||||||
|
/// if the string is a single quote or double quote string
|
||||||
|
fn doString(self: *Scanner, stop_char: u8) !Token {
|
||||||
|
// consume entire string
|
||||||
|
while (self.peekNext() != stop_char and !self.isAtEnd()) {
|
||||||
|
if (self.peek() == '\n') self.line += 1;
|
||||||
|
_ = self.advance();
|
||||||
|
}
|
||||||
|
|
||||||
|
// unterminated string.
|
||||||
|
if (self.isAtEnd()) {
|
||||||
|
return ScannerError.Unterminated;
|
||||||
|
}
|
||||||
|
|
||||||
|
// the closing character of the string
|
||||||
|
_ = self.advance();
|
||||||
|
|
||||||
|
// remove the starting and ending chars of the string
|
||||||
|
const lexeme = self.currentLexeme();
|
||||||
|
return self.makeTokenLexeme(
|
||||||
|
.String,
|
||||||
|
lexeme[1 .. lexeme.len - 1],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn nextToken(self: *Scanner) !?Token {
|
pub fn nextToken(self: *Scanner) !?Token {
|
||||||
self.skipWhitespace();
|
self.skipWhitespace();
|
||||||
self.start = self.current;
|
self.start = self.current;
|
||||||
|
@ -270,6 +301,9 @@ pub const Scanner = struct {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
'\'' => try self.doString('\''),
|
||||||
|
'"' => try self.doString('"'),
|
||||||
|
|
||||||
else => return ScannerError.Unexpected,
|
else => return ScannerError.Unexpected,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue