feat: add error messages to lex leading zero error
This commit is contained in:
parent
e68f1b73e4
commit
d833c44eb8
64
build.zig
64
build.zig
@ -1,20 +1,18 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
// Although this function looks imperative, note that its job is to
|
|
||||||
// declaratively construct a build graph that will be executed by an external
|
|
||||||
// runner.
|
|
||||||
pub fn build(b: *std.Build) void {
|
pub fn build(b: *std.Build) void {
|
||||||
// Standard target options allows the person running `zig build` to choose
|
// Standard target options
|
||||||
// what target to build for. Here we do not override the defaults, which
|
|
||||||
// means any target is allowed, and the default is native. Other options
|
|
||||||
// for restricting supported target set are available.
|
|
||||||
const target = b.standardTargetOptions(.{});
|
const target = b.standardTargetOptions(.{});
|
||||||
|
|
||||||
// Standard optimization options allow the person running `zig build` to select
|
|
||||||
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
|
|
||||||
// set a preferred release mode, allowing the user to decide how to optimize.
|
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
|
|
||||||
|
// Create options module for tracing
|
||||||
|
const executionTracing = b.option(bool, "tracing", "enable execution tracing") orelse false;
|
||||||
|
const options = b.addOptions();
|
||||||
|
options.addOption(bool, "tracing", executionTracing);
|
||||||
|
|
||||||
|
// Create the options module that will be shared
|
||||||
|
const options_module = options.createModule();
|
||||||
|
|
||||||
const exe = b.addExecutable(.{
|
const exe = b.addExecutable(.{
|
||||||
.name = "thp",
|
.name = "thp",
|
||||||
.root_source_file = b.path("src/main.zig"),
|
.root_source_file = b.path("src/main.zig"),
|
||||||
@ -22,12 +20,8 @@ pub fn build(b: *std.Build) void {
|
|||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Declare an option to build the program with debug statements
|
// Add options to executable
|
||||||
const executionTracing = b.option(bool, "tracing", "enable execution tracing") orelse false;
|
exe.root_module.addImport("config", options_module);
|
||||||
const options = b.addOptions();
|
|
||||||
options.addOption(bool, "tracing", executionTracing);
|
|
||||||
|
|
||||||
exe.root_module.addOptions("config", options);
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Error handling module
|
// Error handling module
|
||||||
@ -37,6 +31,7 @@ pub fn build(b: *std.Build) void {
|
|||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
|
error_module.addImport("config", options_module);
|
||||||
exe.root_module.addImport("errors", error_module);
|
exe.root_module.addImport("errors", error_module);
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -47,8 +42,9 @@ pub fn build(b: *std.Build) void {
|
|||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
exe.root_module.addImport("lexic", lexic_module);
|
lexic_module.addImport("config", options_module);
|
||||||
lexic_module.addImport("errors", error_module);
|
lexic_module.addImport("errors", error_module);
|
||||||
|
exe.root_module.addImport("lexic", lexic_module);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Syntax module
|
// Syntax module
|
||||||
@ -58,54 +54,40 @@ pub fn build(b: *std.Build) void {
|
|||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
exe.root_module.addImport("syntax", syntax_module);
|
syntax_module.addImport("config", options_module);
|
||||||
syntax_module.addImport("lexic", lexic_module);
|
syntax_module.addImport("lexic", lexic_module);
|
||||||
syntax_module.addImport("errors", error_module);
|
syntax_module.addImport("errors", error_module);
|
||||||
|
exe.root_module.addImport("syntax", syntax_module);
|
||||||
|
|
||||||
// This declares intent for the executable to be installed into the
|
// Install step
|
||||||
// standard location when the user invokes the "install" step (the default
|
|
||||||
// step when running `zig build`).
|
|
||||||
b.installArtifact(exe);
|
b.installArtifact(exe);
|
||||||
|
|
||||||
// This *creates* a Run step in the build graph, to be executed when another
|
// Run command
|
||||||
// step is evaluated that depends on it. The next line below will establish
|
|
||||||
// such a dependency.
|
|
||||||
const run_cmd = b.addRunArtifact(exe);
|
const run_cmd = b.addRunArtifact(exe);
|
||||||
|
|
||||||
// By making the run step depend on the install step, it will be run from the
|
|
||||||
// installation directory rather than directly from within the cache directory.
|
|
||||||
// This is not necessary, however, if the application depends on other installed
|
|
||||||
// files, this ensures they will be present and in the expected location.
|
|
||||||
run_cmd.step.dependOn(b.getInstallStep());
|
run_cmd.step.dependOn(b.getInstallStep());
|
||||||
|
|
||||||
// This allows the user to pass arguments to the application in the build
|
// Pass arguments if any
|
||||||
// command itself, like this: `zig build run -- arg1 arg2 etc`
|
|
||||||
if (b.args) |args| {
|
if (b.args) |args| {
|
||||||
run_cmd.addArgs(args);
|
run_cmd.addArgs(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
// This creates a build step. It will be visible in the `zig build --help` menu,
|
// Run step
|
||||||
// and can be selected like this: `zig build run`
|
|
||||||
// This will evaluate the `run` step rather than the default, which is "install".
|
|
||||||
const run_step = b.step("run", "Run the app");
|
const run_step = b.step("run", "Run the app");
|
||||||
run_step.dependOn(&run_cmd.step);
|
run_step.dependOn(&run_cmd.step);
|
||||||
|
|
||||||
// Creates a step for unit testing. This only builds the test executable
|
// Unit tests
|
||||||
// but does not run it.
|
|
||||||
const exe_unit_tests = b.addTest(.{
|
const exe_unit_tests = b.addTest(.{
|
||||||
.root_source_file = b.path("src/main.zig"),
|
.root_source_file = b.path("src/main.zig"),
|
||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
|
exe_unit_tests.root_module.addImport("config", options_module);
|
||||||
exe_unit_tests.root_module.addImport("lexic", lexic_module);
|
exe_unit_tests.root_module.addImport("lexic", lexic_module);
|
||||||
exe_unit_tests.root_module.addImport("syntax", syntax_module);
|
exe_unit_tests.root_module.addImport("syntax", syntax_module);
|
||||||
exe_unit_tests.root_module.addImport("errors", error_module);
|
exe_unit_tests.root_module.addImport("errors", error_module);
|
||||||
|
|
||||||
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
|
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
|
||||||
|
|
||||||
// Similar to creating the run step earlier, this exposes a `test` step to
|
|
||||||
// the `zig build --help` menu, providing a way for the user to request
|
|
||||||
// running the unit tests.
|
|
||||||
const test_step = b.step("test", "Run ALL unit tests");
|
const test_step = b.step("test", "Run ALL unit tests");
|
||||||
test_step.dependOn(&run_exe_unit_tests.step);
|
test_step.dependOn(&run_exe_unit_tests.step);
|
||||||
|
|
||||||
@ -113,7 +95,6 @@ pub fn build(b: *std.Build) void {
|
|||||||
const files = [_][]const u8{
|
const files = [_][]const u8{
|
||||||
"src/01_lexic/root.zig",
|
"src/01_lexic/root.zig",
|
||||||
"src/02_syntax/root.zig",
|
"src/02_syntax/root.zig",
|
||||||
"src/02_syntax/root.zig",
|
|
||||||
"src/errors/root.zig",
|
"src/errors/root.zig",
|
||||||
};
|
};
|
||||||
for (files) |file| {
|
for (files) |file| {
|
||||||
@ -122,6 +103,7 @@ pub fn build(b: *std.Build) void {
|
|||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
|
file_unit_test.root_module.addImport("config", options_module);
|
||||||
file_unit_test.root_module.addImport("lexic", lexic_module);
|
file_unit_test.root_module.addImport("lexic", lexic_module);
|
||||||
file_unit_test.root_module.addImport("syntax", syntax_module);
|
file_unit_test.root_module.addImport("syntax", syntax_module);
|
||||||
file_unit_test.root_module.addImport("errors", error_module);
|
file_unit_test.root_module.addImport("errors", error_module);
|
||||||
|
@ -39,7 +39,7 @@ pub fn lex(
|
|||||||
|
|
||||||
// Attempt to lex an integer.
|
// Attempt to lex an integer.
|
||||||
// Floating point numbers are lexed through the int lexer
|
// Floating point numbers are lexed through the int lexer
|
||||||
return integer(input, cap, start);
|
return integer(input, cap, start, err, alloc);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// comptime function for lexing hex, octal and binary numbers.
|
/// comptime function for lexing hex, octal and binary numbers.
|
||||||
@ -100,7 +100,13 @@ fn prefixed(
|
|||||||
/// An integer cannot have a leading zero. That is an error to
|
/// An integer cannot have a leading zero. That is an error to
|
||||||
/// avoid confussion with PHP literal octals.
|
/// avoid confussion with PHP literal octals.
|
||||||
/// Floating point numbers can.
|
/// Floating point numbers can.
|
||||||
fn integer(input: []const u8, cap: usize, start: usize) LexError!?LexReturn {
|
fn integer(
|
||||||
|
input: []const u8,
|
||||||
|
cap: usize,
|
||||||
|
start: usize,
|
||||||
|
err: *errors.ErrorData,
|
||||||
|
alloc: std.mem.Allocator,
|
||||||
|
) LexError!?LexReturn {
|
||||||
assert(start < cap);
|
assert(start < cap);
|
||||||
const first_char = input[start];
|
const first_char = input[start];
|
||||||
if (!is_decimal_digit(first_char)) {
|
if (!is_decimal_digit(first_char)) {
|
||||||
@ -119,6 +125,10 @@ fn integer(input: []const u8, cap: usize, start: usize) LexError!?LexReturn {
|
|||||||
if (last_pos >= cap) {
|
if (last_pos >= cap) {
|
||||||
// leading zero on an integer, throw an error
|
// leading zero on an integer, throw an error
|
||||||
if (first_char == '0') {
|
if (first_char == '0') {
|
||||||
|
try err.init("Leading zero", start, start + 1, alloc);
|
||||||
|
try err.add_label("This decimal number has a leading zero.", start, last_pos);
|
||||||
|
err.set_help("If you want an octal number use '0o', otherwise remove the leading zero");
|
||||||
|
|
||||||
return LexError.LeadingZero;
|
return LexError.LeadingZero;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,6 +153,10 @@ fn integer(input: []const u8, cap: usize, start: usize) LexError!?LexReturn {
|
|||||||
else => {
|
else => {
|
||||||
// leading zero on an integer, throw an error
|
// leading zero on an integer, throw an error
|
||||||
if (first_char == '0') {
|
if (first_char == '0') {
|
||||||
|
try err.init("Leading zero", start, start + 1, alloc);
|
||||||
|
try err.add_label("This decimal number has a leading zero.", start, last_pos);
|
||||||
|
err.set_help("If you want an octal number use '0o', otherwise remove the leading zero");
|
||||||
|
|
||||||
return LexError.LeadingZero;
|
return LexError.LeadingZero;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,12 +45,13 @@ pub fn tokenize(
|
|||||||
var current_error: errors.ErrorData = undefined;
|
var current_error: errors.ErrorData = undefined;
|
||||||
const number_lex = number.lex(input, input_len, actual_next_pos, ¤t_error, alloc) catch |e| switch (e) {
|
const number_lex = number.lex(input, input_len, actual_next_pos, ¤t_error, alloc) catch |e| switch (e) {
|
||||||
// recoverable errors
|
// recoverable errors
|
||||||
LexError.Incomplete => {
|
LexError.Incomplete, LexError.LeadingZero => {
|
||||||
// add to list of errors
|
// add to list of errors
|
||||||
try err_arrl.append(current_error);
|
try err_arrl.append(current_error);
|
||||||
|
|
||||||
// ignore everything until whitespace and loop
|
// ignore everything until whitespace and loop
|
||||||
current_pos = ignore_until_whitespace(input, actual_next_pos);
|
current_pos = ignore_until_whitespace(input, actual_next_pos);
|
||||||
|
assert(current_pos > actual_next_pos);
|
||||||
continue;
|
continue;
|
||||||
},
|
},
|
||||||
// just throw unrecoverable errors
|
// just throw unrecoverable errors
|
||||||
|
@ -78,10 +78,10 @@ fn repl() !void {
|
|||||||
// Print errors
|
// Print errors
|
||||||
for (error_array.items) |e| {
|
for (error_array.items) |e| {
|
||||||
var err = e;
|
var err = e;
|
||||||
const err_str = try err.get_error_str(line, "repl", std.heap.page_allocator);
|
const err_str = try err.get_error_str(line, "repl", alloc);
|
||||||
try stdout.print("\n{s}\n", .{err_str});
|
try stdout.print("\n{s}\n", .{err_str});
|
||||||
try bw.flush();
|
try bw.flush();
|
||||||
std.heap.page_allocator.free(err_str);
|
alloc.free(err_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
// next repl line
|
// next repl line
|
||||||
|
Loading…
Reference in New Issue
Block a user