fix: throw leading zero error only on integer
This commit is contained in:
parent
76c823cc54
commit
98bef47824
@ -13,8 +13,6 @@ const LexReturn = struct { Token, usize };
|
|||||||
/// Attempts to lex a number, as per the language grammar.
|
/// Attempts to lex a number, as per the language grammar.
|
||||||
///
|
///
|
||||||
/// A number is either an Int or a Float.
|
/// A number is either an Int or a Float.
|
||||||
/// No number can have a leading zero. That is an error to
|
|
||||||
/// avoid confussion with PHP literal octals.
|
|
||||||
pub fn lex(input: []const u8, cap: usize, start: usize) LexError!?LexReturn {
|
pub fn lex(input: []const u8, cap: usize, start: usize) LexError!?LexReturn {
|
||||||
const first_char = input[start];
|
const first_char = input[start];
|
||||||
|
|
||||||
@ -26,8 +24,7 @@ pub fn lex(input: []const u8, cap: usize, start: usize) LexError!?LexReturn {
|
|||||||
'o', 'O' => return prefixed('o', input, cap, start),
|
'o', 'O' => return prefixed('o', input, cap, start),
|
||||||
'b', 'B' => return prefixed('b', input, cap, start),
|
'b', 'B' => return prefixed('b', input, cap, start),
|
||||||
else => {
|
else => {
|
||||||
// Leading zero found. Throw an error.
|
// Continue
|
||||||
return LexError.LeadingZero;
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -51,7 +48,7 @@ fn prefixed(comptime prefix: u8, input: []const u8, cap: usize, start: usize) !?
|
|||||||
|
|
||||||
var end_position = start + 2;
|
var end_position = start + 2;
|
||||||
|
|
||||||
// There should be at least 1 hex digit
|
// There should be at least 1 valid digit
|
||||||
if (end_position >= cap or !validator(input[end_position])) {
|
if (end_position >= cap or !validator(input[end_position])) {
|
||||||
return LexError.Incomplete;
|
return LexError.Incomplete;
|
||||||
}
|
}
|
||||||
@ -68,19 +65,16 @@ fn prefixed(comptime prefix: u8, input: []const u8, cap: usize, start: usize) !?
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn binary() !?LexReturn {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Attempts to lex an integer number.
|
/// Attempts to lex an integer number.
|
||||||
///
|
///
|
||||||
/// This function fails if the first digit it encounters is a `0`,
|
/// This function also attempts to lex a floating point number.
|
||||||
/// this is because it could cause confusion with PHP literal integers,
|
/// If it succeedes, it returns a floating point token.
|
||||||
/// where a number that starts with a `0` is octal, not decimal.
|
/// Otherwise, it only returns an integer token.
|
||||||
///
|
///
|
||||||
/// For this reason, this function should be called after the lexers
|
/// An integer cannot have a leading zero. That is an error to
|
||||||
/// for hex, octal and binary have been called.
|
/// avoid confussion with PHP literal octals.
|
||||||
fn integer(input: []const u8, cap: usize, start: usize) !?LexReturn {
|
/// Floating point numbers can.
|
||||||
|
fn integer(input: []const u8, cap: usize, start: usize) LexError!?LexReturn {
|
||||||
const first_char = input[start];
|
const first_char = input[start];
|
||||||
if (!is_decimal_digit(first_char)) {
|
if (!is_decimal_digit(first_char)) {
|
||||||
return null;
|
return null;
|
||||||
@ -91,9 +85,45 @@ fn integer(input: []const u8, cap: usize, start: usize) !?LexReturn {
|
|||||||
last_pos += 1;
|
last_pos += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return .{
|
// up to here an integer was lexed.
|
||||||
Token.init(input[start..last_pos], TokenType.Int, start),
|
// now check if a floating point number can be lexed
|
||||||
last_pos,
|
|
||||||
|
// if we hit eof, return the current integer
|
||||||
|
if (last_pos >= cap) {
|
||||||
|
// leading zero on an integer, throw an error
|
||||||
|
if (first_char == '0') {
|
||||||
|
return LexError.LeadingZero;
|
||||||
|
}
|
||||||
|
|
||||||
|
return .{
|
||||||
|
Token.init(input[start..last_pos], TokenType.Int, start),
|
||||||
|
last_pos,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const next_char = input[last_pos];
|
||||||
|
|
||||||
|
return switch (next_char) {
|
||||||
|
// if a dot is found, lex a fp number
|
||||||
|
'.' => {
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
// if an `e` (exponential notiation) is found, lex that
|
||||||
|
'e' => {
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
// otherwise return the current integer
|
||||||
|
else => {
|
||||||
|
// leading zero on an integer, throw an error
|
||||||
|
if (first_char == '0') {
|
||||||
|
return LexError.LeadingZero;
|
||||||
|
}
|
||||||
|
|
||||||
|
return .{
|
||||||
|
Token.init(input[start..last_pos], TokenType.Int, start),
|
||||||
|
last_pos,
|
||||||
|
};
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,6 +182,23 @@ test "should lex hex number" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "should fail on integer with leading zero" {
|
||||||
|
const input = "0322";
|
||||||
|
const result = lex(input, input.len, 0) catch |err| {
|
||||||
|
try std.testing.expect(err == token.LexError.LeadingZero);
|
||||||
|
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 hex number 2" {
|
test "should lex hex number 2" {
|
||||||
const input = " 0Xff00AA ";
|
const input = " 0Xff00AA ";
|
||||||
const result = try lex(input, input.len, 2);
|
const result = try lex(input, input.len, 2);
|
||||||
@ -267,3 +314,6 @@ test "shouldnt parse incomplete binary number" {
|
|||||||
|
|
||||||
try std.testing.expect(false);
|
try std.testing.expect(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: test 0.1, the current impl should fail because of the
|
||||||
|
// leading zero
|
||||||
|
Loading…
Reference in New Issue
Block a user