-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokeniser.zig
More file actions
26 lines (23 loc) · 738 Bytes
/
tokeniser.zig
File metadata and controls
26 lines (23 loc) · 738 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const std = @import("std");
pub fn tokenise(input: []const u8, allocator: std.mem.Allocator) ![]const []const u8 {
var tokens = std.ArrayList([]const u8).init(allocator);
var i: usize = 0;
while (i < input.len) {
const c = input[i];
if (std.ascii.isWhitespace(c)) {
i += 1;
continue;
}
if (c == '(' or c == ')') {
try tokens.append(input[i .. i + 1]);
i += 1;
continue;
}
const start = i;
while (i < input.len and !std.ascii.isWhitespace(input[i]) and input[i] != '(' and input[i] != ')') {
i += 1;
}
try tokens.append(input[start..i]);
}
return tokens.toOwnedSlice();
}