Compare commits

..

No commits in common. "498ea72da43a139ad9882d57af7b0aadbc32370c" and "8a9522d954f1d14cfa63793afdb09dc1f5c80324" have entirely different histories.

View file

@ -116,16 +116,10 @@ pub const Scanner = struct {
}; };
} }
fn makeTokenLexeme( fn makeTokenAdvance(self: *Scanner, ttype: TokenType) Token {
self: *Scanner, var tok = self.makeToken(ttype);
ttype: TokenType, self.current += 1;
lexeme: []const u8, return tok;
) 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.
@ -204,48 +198,6 @@ 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],
);
}
fn makeTripleMatchToken(
self: *Scanner,
char1: u8,
ttype1: TokenType,
char2,
ttype2: TokenType,
fallback: TokenType,
) Token {
if (self.match(char1)) {
return self.makeToken(ttype1);
} else if (self.match(char2)) {
return self.makeToken(ttype2);
} else {
return self.makeToken(fallback);
}
}
pub fn nextToken(self: *Scanner) !?Token { pub fn nextToken(self: *Scanner) !?Token {
self.skipWhitespace(); self.skipWhitespace();
self.start = self.current; self.start = self.current;
@ -272,14 +224,26 @@ pub const Scanner = struct {
'$' => self.makeToken(.DollarSign), '$' => self.makeToken(.DollarSign),
'-' => self.makeToken(.Minus), '-' => self.makeToken(.Minus),
'+' => self.makeToken(.Plus),
'*' => self.makeToken(.Star), '*' => self.makeToken(.Star),
'%' => self.makeToken(.Modulo),
'!' => self.makeMatchToken('=', .BangEqual, .Bang), '!' => self.makeMatchToken('=', .BangEqual, .Bang),
'=' => self.makeMatchToken('=', .EqualEqual, .Equal), '=' => self.makeMatchToken('=', .EqualEqual, .Equal),
// there can be three tokens from a <
// - <, which is LessThan
// - <=, which is LessEqual
// - <<, which is LeftDoubleChevron
'<' => blk: {
if (self.match('=')) {
break :blk self.makeToken(.LessEqual);
} else if (self.match('<')) {
break :blk self.makeToken(.LeftDoubleChevron);
} else {
break :blk self.makeToken(.Less);
}
},
'>' => self.makeMatchToken('=', .GreaterEqual, .Greater), '>' => self.makeMatchToken('=', .GreaterEqual, .Greater),
'+' => self.makeTripleMatchToken('+', .PlusPlus, '=', .PlusEqual, .Plus),
'<' => self.makeTripleMatchToken('=', .LessEqual, '<', .LeftDoubleChevron, .Less),
'/' => blk: { '/' => blk: {
var next = self.peekNext(); var next = self.peekNext();
@ -306,9 +270,6 @@ pub const Scanner = struct {
} }
}, },
'\'' => try self.doString('\''),
'"' => try self.doString('"'),
else => return ScannerError.Unexpected, else => return ScannerError.Unexpected,
}; };