Compare commits
No commits in common. "26f5fed3219ed90e14778dc31be74e6a72cc6538" and "1c8a50f7976e226003068973107a270bfdb56452" have entirely different histories.
26f5fed321
...
1c8a50f797
@ -24,7 +24,6 @@ Now in Zig!
|
|||||||
|
|
||||||
## v0.0.1
|
## v0.0.1
|
||||||
|
|
||||||
- [x] Lex integers & floating point numbers
|
|
||||||
- [ ]
|
- [ ]
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,12 +10,12 @@
|
|||||||
|
|
||||||
// This is a [Semantic Version](https://semver.org/).
|
// This is a [Semantic Version](https://semver.org/).
|
||||||
// In a future version of Zig it will be used for package deduplication.
|
// In a future version of Zig it will be used for package deduplication.
|
||||||
.version = "0.0.1",
|
.version = "0.0.0",
|
||||||
|
|
||||||
// This field is optional.
|
// This field is optional.
|
||||||
// This is currently advisory only; Zig does not yet do anything
|
// This is currently advisory only; Zig does not yet do anything
|
||||||
// with this value.
|
// with this value.
|
||||||
.minimum_zig_version = "0.13.0",
|
//.minimum_zig_version = "0.11.0",
|
||||||
|
|
||||||
// This field is optional.
|
// This field is optional.
|
||||||
// Each dependency must either provide a `url` and `hash`, or a `path`.
|
// Each dependency must either provide a `url` and `hash`, or a `path`.
|
||||||
|
@ -110,7 +110,7 @@ fn integer(input: []const u8, cap: usize, start: usize) LexError!?LexReturn {
|
|||||||
},
|
},
|
||||||
// if an `e` (exponential notiation) is found, lex that
|
// if an `e` (exponential notiation) is found, lex that
|
||||||
'e' => {
|
'e' => {
|
||||||
return scientific(input, cap, start, last_pos);
|
return null;
|
||||||
},
|
},
|
||||||
// otherwise return the current integer
|
// otherwise return the current integer
|
||||||
else => {
|
else => {
|
||||||
@ -131,8 +131,8 @@ fn integer(input: []const u8, cap: usize, start: usize) LexError!?LexReturn {
|
|||||||
///
|
///
|
||||||
/// token_start: the position the current token started at
|
/// token_start: the position the current token started at
|
||||||
/// decimal_point: the position of the decimal point `.`
|
/// decimal_point: the position of the decimal point `.`
|
||||||
fn floating_point(input: []const u8, cap: usize, token_start: usize, period_pos: usize) LexError!?LexReturn {
|
fn floating_point(input: []const u8, cap: usize, token_start: usize, decimal_point: usize) LexError!?LexReturn {
|
||||||
var current_pos = period_pos + 1;
|
var current_pos = decimal_point + 1;
|
||||||
|
|
||||||
// there should be at least 1 digit after the period
|
// there should be at least 1 digit after the period
|
||||||
if (current_pos >= cap or !utils.is_decimal_digit(input[current_pos])) {
|
if (current_pos >= cap or !utils.is_decimal_digit(input[current_pos])) {
|
||||||
@ -146,12 +146,6 @@ fn floating_point(input: []const u8, cap: usize, token_start: usize, period_pos:
|
|||||||
current_pos += 1;
|
current_pos += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if the current character is a `e`,
|
|
||||||
// if so lex a scientific number
|
|
||||||
if (current_pos < cap and input[current_pos] == 'e') {
|
|
||||||
return scientific(input, cap, token_start, current_pos);
|
|
||||||
}
|
|
||||||
|
|
||||||
// return the matched fp number
|
// return the matched fp number
|
||||||
return .{
|
return .{
|
||||||
Token.init(input[token_start..current_pos], TokenType.Float, token_start),
|
Token.init(input[token_start..current_pos], TokenType.Float, token_start),
|
||||||
@ -159,38 +153,6 @@ fn floating_point(input: []const u8, cap: usize, token_start: usize, period_pos:
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// exp_pos is the position at the `e` character
|
|
||||||
fn scientific(input: []const u8, cap: usize, token_start: usize, exp_pos: usize) LexError!?LexReturn {
|
|
||||||
var current_pos = exp_pos + 1;
|
|
||||||
|
|
||||||
// expect `+` or `-`
|
|
||||||
if (current_pos >= cap) {
|
|
||||||
return LexError.IncompleteScientificNumber;
|
|
||||||
}
|
|
||||||
const sign_char = input[current_pos];
|
|
||||||
if (sign_char != '+' and sign_char != '-') {
|
|
||||||
return LexError.IncompleteScientificNumber;
|
|
||||||
}
|
|
||||||
current_pos += 1;
|
|
||||||
const digits_start = current_pos;
|
|
||||||
|
|
||||||
// lex at least 1 digit
|
|
||||||
while (current_pos < cap and utils.is_decimal_digit(input[current_pos])) {
|
|
||||||
current_pos += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if there is no difference, no extra digits were lexed.
|
|
||||||
if (digits_start == current_pos) {
|
|
||||||
return LexError.IncompleteScientificNumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
// return the scientific number
|
|
||||||
return .{
|
|
||||||
Token.init(input[token_start..current_pos], TokenType.Float, token_start),
|
|
||||||
current_pos,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
test "int lexer 1" {
|
test "int lexer 1" {
|
||||||
const input = "322 ";
|
const input = "322 ";
|
||||||
const result = try integer(input, input.len, 0);
|
const result = try integer(input, input.len, 0);
|
||||||
@ -431,61 +393,3 @@ test "should fail on incomplete fp number" {
|
|||||||
|
|
||||||
try std.testing.expect(false);
|
try std.testing.expect(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
test "should lex scientific number" {
|
|
||||||
const input = "42e+3";
|
|
||||||
const result = try lex(input, input.len, 0);
|
|
||||||
|
|
||||||
if (result) |tuple| {
|
|
||||||
const r = tuple[0];
|
|
||||||
try std.testing.expectEqualDeep("42e+3", r.value);
|
|
||||||
} else {
|
|
||||||
try std.testing.expect(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
test "should fail on incomplete scientific number" {
|
|
||||||
const input = "123e";
|
|
||||||
const result = lex(input, input.len, 0) catch |err| {
|
|
||||||
try std.testing.expect(err == token.LexError.IncompleteScientificNumber);
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (result) |tuple| {
|
|
||||||
const r = tuple[0];
|
|
||||||
std.debug.print("{s}\n", .{r.value});
|
|
||||||
} else {
|
|
||||||
std.debug.print("nil returned", .{});
|
|
||||||
}
|
|
||||||
|
|
||||||
try std.testing.expect(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
test "should fail on incomplete scientific number 2" {
|
|
||||||
const input = "123e+";
|
|
||||||
const result = lex(input, input.len, 0) catch |err| {
|
|
||||||
try std.testing.expect(err == token.LexError.IncompleteScientificNumber);
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (result) |tuple| {
|
|
||||||
const r = tuple[0];
|
|
||||||
std.debug.print("{s}\n", .{r.value});
|
|
||||||
} else {
|
|
||||||
std.debug.print("nil returned", .{});
|
|
||||||
}
|
|
||||||
|
|
||||||
try std.testing.expect(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
test "should lex floating scientific number" {
|
|
||||||
const input = "0.58e+3";
|
|
||||||
const result = try lex(input, input.len, 0);
|
|
||||||
|
|
||||||
if (result) |tuple| {
|
|
||||||
const r = tuple[0];
|
|
||||||
try std.testing.expectEqualDeep("0.58e+3", r.value);
|
|
||||||
} else {
|
|
||||||
try std.testing.expect(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -21,5 +21,4 @@ pub const LexError = error{
|
|||||||
LeadingZero,
|
LeadingZero,
|
||||||
Incomplete,
|
Incomplete,
|
||||||
IncompleteFloatingNumber,
|
IncompleteFloatingNumber,
|
||||||
IncompleteScientificNumber,
|
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user