Skip to content

Commit

Permalink
self hosted: add tokenizer test fix eof handling
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Feb 13, 2018
1 parent dfbb825 commit 2dcff95
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions std/zig/tokenizer.zig
Expand Up @@ -72,6 +72,8 @@ pub const Token = struct {
Eof,
Builtin,
Bang,
Pipe,
PipeEqual,
Equal,
EqualEqual,
BangEqual,
Expand Down Expand Up @@ -193,6 +195,7 @@ pub const Tokenizer = struct {
StringLiteralBackslash,
Equal,
Bang,
Pipe,
Minus,
Slash,
LineComment,
Expand Down Expand Up @@ -248,6 +251,9 @@ pub const Tokenizer = struct {
'!' => {
state = State.Bang;
},
'|' => {
state = State.Pipe;
},
'(' => {
result.id = Token.Id.LParen;
self.index += 1;
Expand Down Expand Up @@ -394,6 +400,18 @@ pub const Tokenizer = struct {
},
},

State.Pipe => switch (c) {
'=' => {
result.id = Token.Id.PipeEqual;
self.index += 1;
break;
},
else => {
result.id = Token.Id.Pipe;
break;
},
},

State.Equal => switch (c) {
'=' => {
result.id = Token.Id.EqualEqual;
Expand Down Expand Up @@ -525,9 +543,7 @@ pub const Tokenizer = struct {
else => break,
},
}
}
result.end = self.index;
if (self.index == self.buffer.len) {
} else if (self.index == self.buffer.len) {
switch (state) {
State.Start,
State.C,
Expand Down Expand Up @@ -578,6 +594,9 @@ pub const Tokenizer = struct {
State.Period2 => {
result.id = Token.Id.Ellipsis2;
},
State.Pipe => {
result.id = Token.Id.Pipe;
},
}
}
if (result.id == Token.Id.Eof) {
Expand All @@ -587,6 +606,7 @@ pub const Tokenizer = struct {
}
}

result.end = self.index;
return result;
}

Expand Down Expand Up @@ -716,6 +736,13 @@ test "tokenizer - string identifier and builtin fns" {
);
}

test "tokenizer - pipe and then invalid" {
testTokenize("||=", []Token.Id{
Token.Id.Pipe,
Token.Id.PipeEqual,
});
}

fn testTokenize(source: []const u8, expected_tokens: []const Token.Id) void {
var tokenizer = Tokenizer.init(source);
for (expected_tokens) |expected_token_id| {
Expand Down

0 comments on commit 2dcff95

Please sign in to comment.