feat: lex datatype
This commit is contained in:
parent
92794cc07a
commit
15a66ebc3d
@ -94,6 +94,7 @@ pub fn build(b: *std.Build) void {
|
|||||||
"src/01_lexic/root.zig",
|
"src/01_lexic/root.zig",
|
||||||
"src/01_lexic/number.zig",
|
"src/01_lexic/number.zig",
|
||||||
"src/01_lexic/identifier.zig",
|
"src/01_lexic/identifier.zig",
|
||||||
|
"src/01_lexic/datatype.zig",
|
||||||
"src/01_lexic/token.zig",
|
"src/01_lexic/token.zig",
|
||||||
"src/01_lexic/utils.zig",
|
"src/01_lexic/utils.zig",
|
||||||
};
|
};
|
||||||
|
53
src/01_lexic/datatype.zig
Normal file
53
src/01_lexic/datatype.zig
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
const token = @import("./token.zig");
|
||||||
|
const utils = @import("./utils.zig");
|
||||||
|
|
||||||
|
const Token = token.Token;
|
||||||
|
const TokenType = token.TokenType;
|
||||||
|
const LexError = token.LexError;
|
||||||
|
const LexReturn = token.LexReturn;
|
||||||
|
|
||||||
|
/// Lexes a Datatype
|
||||||
|
pub fn lex(input: []const u8, start: usize) LexError!?LexReturn {
|
||||||
|
const cap = input.len;
|
||||||
|
var final_pos = start;
|
||||||
|
|
||||||
|
if (start >= cap) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// lex uppercase
|
||||||
|
if (!utils.is_uppercase(input[start])) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
final_pos += 1;
|
||||||
|
|
||||||
|
// lex many lowercase/uppercase/underscore/number
|
||||||
|
if (utils.lex_many(utils.is_identifier_char, input, final_pos)) |new_pos| {
|
||||||
|
final_pos = new_pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
return .{
|
||||||
|
Token.init(input[start..final_pos], TokenType.Identifier, start),
|
||||||
|
final_pos,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
test "should lex datatype" {
|
||||||
|
const input = "MyType";
|
||||||
|
const output = try lex(input, 0);
|
||||||
|
|
||||||
|
if (output) |tuple| {
|
||||||
|
const t = tuple[0];
|
||||||
|
try std.testing.expectEqualDeep("MyType", t.value);
|
||||||
|
} else {
|
||||||
|
try std.testing.expect(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test "shouldnt lex identifier" {
|
||||||
|
const input = "myDatatype";
|
||||||
|
const output = try lex(input, 0);
|
||||||
|
|
||||||
|
try std.testing.expect(output == null);
|
||||||
|
}
|
@ -7,6 +7,7 @@ const TokenType = token.TokenType;
|
|||||||
const LexError = token.LexError;
|
const LexError = token.LexError;
|
||||||
const LexReturn = token.LexReturn;
|
const LexReturn = token.LexReturn;
|
||||||
|
|
||||||
|
/// Lexes a datatype
|
||||||
pub fn lex(input: []const u8, start: usize) LexError!?LexReturn {
|
pub fn lex(input: []const u8, start: usize) LexError!?LexReturn {
|
||||||
const cap = input.len;
|
const cap = input.len;
|
||||||
var final_pos = start;
|
var final_pos = start;
|
||||||
|
@ -27,8 +27,6 @@ pub fn tokenize(input: []const u8, alloc: std.mem.Allocator) !void {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std.debug.print("array list len: {d}", .{tokens.items.len});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ignores all whitespace on `input` since `start`
|
/// Ignores all whitespace on `input` since `start`
|
||||||
|
@ -22,6 +22,10 @@ pub fn is_lowercase(c: u8) bool {
|
|||||||
return 'a' <= c and c <= 'z';
|
return 'a' <= c and c <= 'z';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_uppercase(c: u8) bool {
|
||||||
|
return 'A' <= c and c <= 'Z';
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_lowercase_underscore(c: u8) bool {
|
pub fn is_lowercase_underscore(c: u8) bool {
|
||||||
return c == '_' or ('a' <= c and c <= 'z');
|
return c == '_' or ('a' <= c and c <= 'z');
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user