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");
|
||||
|
||||
// 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 {
|
||||
// Standard target options allows the person running `zig build` to choose
|
||||
// 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.
|
||||
// Standard target options
|
||||
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(.{});
|
||||
|
||||
// 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(.{
|
||||
.name = "thp",
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
@ -22,12 +20,8 @@ pub fn build(b: *std.Build) void {
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
// Declare an option to build the program with debug statements
|
||||
const executionTracing = b.option(bool, "tracing", "enable execution tracing") orelse false;
|
||||
const options = b.addOptions();
|
||||
options.addOption(bool, "tracing", executionTracing);
|
||||
|
||||
exe.root_module.addOptions("config", options);
|
||||
// Add options to executable
|
||||
exe.root_module.addImport("config", options_module);
|
||||
|
||||
//
|
||||
// Error handling module
|
||||
@ -37,6 +31,7 @@ pub fn build(b: *std.Build) void {
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
error_module.addImport("config", options_module);
|
||||
exe.root_module.addImport("errors", error_module);
|
||||
|
||||
//
|
||||
@ -47,8 +42,9 @@ pub fn build(b: *std.Build) void {
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
exe.root_module.addImport("lexic", lexic_module);
|
||||
lexic_module.addImport("config", options_module);
|
||||
lexic_module.addImport("errors", error_module);
|
||||
exe.root_module.addImport("lexic", lexic_module);
|
||||
|
||||
//
|
||||
// Syntax module
|
||||
@ -58,54 +54,40 @@ pub fn build(b: *std.Build) void {
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
exe.root_module.addImport("syntax", syntax_module);
|
||||
syntax_module.addImport("config", options_module);
|
||||
syntax_module.addImport("lexic", lexic_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
|
||||
// standard location when the user invokes the "install" step (the default
|
||||
// step when running `zig build`).
|
||||
// Install step
|
||||
b.installArtifact(exe);
|
||||
|
||||
// This *creates* a Run step in the build graph, to be executed when another
|
||||
// step is evaluated that depends on it. The next line below will establish
|
||||
// such a dependency.
|
||||
// Run command
|
||||
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());
|
||||
|
||||
// This allows the user to pass arguments to the application in the build
|
||||
// command itself, like this: `zig build run -- arg1 arg2 etc`
|
||||
// Pass arguments if any
|
||||
if (b.args) |args| {
|
||||
run_cmd.addArgs(args);
|
||||
}
|
||||
|
||||
// This creates a build step. It will be visible in the `zig build --help` menu,
|
||||
// and can be selected like this: `zig build run`
|
||||
// This will evaluate the `run` step rather than the default, which is "install".
|
||||
// Run step
|
||||
const run_step = b.step("run", "Run the app");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
|
||||
// Creates a step for unit testing. This only builds the test executable
|
||||
// but does not run it.
|
||||
// Unit tests
|
||||
const exe_unit_tests = b.addTest(.{
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
.target = target,
|
||||
.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("syntax", syntax_module);
|
||||
exe_unit_tests.root_module.addImport("errors", error_module);
|
||||
|
||||
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");
|
||||
test_step.dependOn(&run_exe_unit_tests.step);
|
||||
|
||||
@ -113,7 +95,6 @@ pub fn build(b: *std.Build) void {
|
||||
const files = [_][]const u8{
|
||||
"src/01_lexic/root.zig",
|
||||
"src/02_syntax/root.zig",
|
||||
"src/02_syntax/root.zig",
|
||||
"src/errors/root.zig",
|
||||
};
|
||||
for (files) |file| {
|
||||
@ -122,6 +103,7 @@ pub fn build(b: *std.Build) void {
|
||||
.target = target,
|
||||
.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("syntax", syntax_module);
|
||||
file_unit_test.root_module.addImport("errors", error_module);
|
||||
|
@ -39,7 +39,7 @@ pub fn lex(
|
||||
|
||||
// Attempt to lex an integer.
|
||||
// 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.
|
||||
@ -100,7 +100,13 @@ fn prefixed(
|
||||
/// An integer cannot have a leading zero. That is an error to
|
||||
/// avoid confussion with PHP literal octals.
|
||||
/// 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);
|
||||
const first_char = input[start];
|
||||
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) {
|
||||
// leading zero on an integer, throw an error
|
||||
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;
|
||||
}
|
||||
|
||||
@ -143,6 +153,10 @@ fn integer(input: []const u8, cap: usize, start: usize) LexError!?LexReturn {
|
||||
else => {
|
||||
// leading zero on an integer, throw an error
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -45,12 +45,13 @@ pub fn tokenize(
|
||||
var current_error: errors.ErrorData = undefined;
|
||||
const number_lex = number.lex(input, input_len, actual_next_pos, ¤t_error, alloc) catch |e| switch (e) {
|
||||
// recoverable errors
|
||||
LexError.Incomplete => {
|
||||
LexError.Incomplete, LexError.LeadingZero => {
|
||||
// add to list of errors
|
||||
try err_arrl.append(current_error);
|
||||
|
||||
// ignore everything until whitespace and loop
|
||||
current_pos = ignore_until_whitespace(input, actual_next_pos);
|
||||
assert(current_pos > actual_next_pos);
|
||||
continue;
|
||||
},
|
||||
// just throw unrecoverable errors
|
||||
|
@ -78,10 +78,10 @@ fn repl() !void {
|
||||
// Print errors
|
||||
for (error_array.items) |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 bw.flush();
|
||||
std.heap.page_allocator.free(err_str);
|
||||
alloc.free(err_str);
|
||||
}
|
||||
|
||||
// next repl line
|
||||
|
Loading…
Reference in New Issue
Block a user