aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2026-05-04 16:40:38 +0200
committerAnhgelus Morhtuuzh <william@herges.fr>2026-05-04 16:40:38 +0200
commit8dc9a978fc2b0d01c20270b1b690e815e74fdcc7 (patch)
tree477ac32bb4e2bf8bb7f0905333411ac96eb6077c
parent308c8893d9706318e8e756069f40c1435a70df91 (diff)
feat(lexer): error contains line
-rw-r--r--examples/main.c2
-rw-r--r--include/typdown.h2
-rw-r--r--src/lexer/Lexer.zig25
-rw-r--r--src/parser.zig37
-rw-r--r--src/root.zig6
5 files changed, 51 insertions, 21 deletions
diff --git a/examples/main.c b/examples/main.c
index d28af79..2b3c3ca 100644
--- a/examples/main.c
+++ b/examples/main.c
@@ -9,7 +9,7 @@ void foo(char *v) {
for (int i = 0; i < doc.errors_len; i++) {
struct typdown_Error error = doc.errors[i];
printf("cannot parse '%s', error: %s (%d)\n", v, typdown_getErrorString(error.code), error.code);
- printf("%d to %d ", error.location.beg, error.location.end);
+ printf("line %d: ", error.location.line);
for (int j = error.location.beg; j < error.location.end; j++)
printf("%c", v[j]);
printf("\n");
diff --git a/include/typdown.h b/include/typdown.h
index abf395d..e9d7f19 100644
--- a/include/typdown.h
+++ b/include/typdown.h
@@ -4,7 +4,7 @@
struct typdown_Error {
uint8_t code;
- struct {size_t beg; size_t end;} location;
+ struct {size_t beg; size_t end; size_t line;} location;
};
struct typdown_Document {
diff --git a/src/lexer/Lexer.zig b/src/lexer/Lexer.zig
index c14ce01..b72fe3b 100644
--- a/src/lexer/Lexer.zig
+++ b/src/lexer/Lexer.zig
@@ -9,6 +9,7 @@ iter: unicode.Utf8Iterator,
force_lit: bool = false,
before: ?Token = null,
new_line: bool = true,
+current_line: usize = 1,
const Self = @This();
@@ -38,6 +39,7 @@ pub fn next(self: *Self) ?Token {
var current_kind: ?Token.Kind = null;
while (self.iter.nextCodepointSlice()) |rune| {
if (eql(u8, rune, "\r")) continue;
+ if (eql(u8, rune, "\n")) self.current_line += 1;
var override_if: ?[]const u8 = null;
// escape chars
if (eql(u8, rune, "\\")) {
@@ -243,6 +245,8 @@ test "lexer image" {
}
test "lexer multiline" {
+ const expect = std.testing.expect;
+
var l = try init(
\\# Title
\\
@@ -255,20 +259,35 @@ test "lexer multiline" {
);
try doTest(&l, .title, "#");
+ try expect(l.current_line == 1);
try doTest(&l, .literal, "Title");
+ try expect(l.current_line == 1);
try doTest(&l, .strong_delimiter, "\n\n");
+ try expect(l.current_line == 3);
try doTest(&l, .literal, "paragraph");
+ try expect(l.current_line == 3);
try doTest(&l, .weak_delimiter, "\n");
+ try expect(l.current_line == 4);
try doTest(&l, .title, "#");
+ try expect(l.current_line == 4);
try doTest(&l, .literal, "a title");
+ try expect(l.current_line == 4);
try doTest(&l, .weak_delimiter, "\n");
+ try expect(l.current_line == 5);
try doTest(&l, .literal, "a # in sentence");
+ try expect(l.current_line == 5);
try doTest(&l, .strong_delimiter, "\n\n");
+ try expect(l.current_line == 7);
try doTest(&l, .tag, "#");
+ try expect(l.current_line == 7);
try doTest(&l, .literal, "tag");
+ try expect(l.current_line == 7);
try doTest(&l, .weak_delimiter, "\n");
+ try expect(l.current_line == 8);
try doTest(&l, .tag, "#");
+ try expect(l.current_line == 8);
try doTest(&l, .literal, "tag2");
+ try expect(l.current_line == 8);
try std.testing.expect(l.next() == null);
}
@@ -299,8 +318,14 @@ test "trim space" {
);
try expect(l.next().?.equals(.quote, ">"));
+ try expect(l.current_line == 1);
try expect(l.next().?.equals(.literal, "hello"));
+ try expect(l.current_line == 1);
try expect(l.next().?.equals(.weak_delimiter, "\n"));
+ try expect(l.current_line == 2);
try expect(l.next().?.equals(.quote, ">"));
+ try expect(l.current_line == 2);
try expect(l.next().?.equals(.literal, "world"));
+ try expect(l.current_line == 2);
+ try expect(l.next() == null);
}
diff --git a/src/parser.zig b/src/parser.zig
index e3eb5d5..f49bdc4 100644
--- a/src/parser.zig
+++ b/src/parser.zig
@@ -25,12 +25,27 @@ pub const Error = error{FeatureNotSupported} ||
math.Error ||
Allocator.Error;
+const Self = @This();
+
/// Document represents a parsed typdown document.
pub const Document = struct {
/// Root of the document: used to render the document is other languages.
root: *Element.Root,
/// Errors got while parsing the document.
- errors: ?[]DocError = null,
+ errors: ?[]Document.Error = null,
+
+ /// DocError contains information about the error.
+ pub const Error = struct {
+ /// Error returned.
+ err: Self.Error,
+ /// Location of the error in the source.
+ location: struct { beg: usize, end: usize, line: usize },
+
+ /// Extract the invalid content from the source.
+ pub fn extract(self: @This(), source: []const u8) []const u8 {
+ return source[self.location.beg..self.location.end];
+ }
+ };
pub fn deinit(self: @This(), alloc: Allocator) void {
self.root.deinit();
@@ -38,19 +53,6 @@ pub const Document = struct {
}
};
-/// DocError contains information about the error.
-pub const DocError = struct {
- /// Error returned.
- err: Error,
- /// Location of the error in the source.
- location: struct { beg: usize, end: usize },
-
- /// Extract the invalid content from the source.
- pub fn extract(self: @This(), source: []const u8) []const u8 {
- return source[self.location.beg..self.location.end];
- }
-};
-
pub fn parseReader(parent: Allocator, r: *std.io.Reader) !Document {
var l = try Lexer.initReader(parent, r);
defer parent.free(l.iter.bytes);
@@ -66,7 +68,7 @@ fn gen(parent: Allocator, l: *Lexer) Allocator.Error!Document {
var root = try Element.Root.init(parent);
errdefer root.deinit();
const alloc = root.allocator();
- var doc_errors = std.ArrayList(DocError).empty;
+ var doc_errors = std.ArrayList(Document.Error).empty;
errdefer doc_errors.deinit(parent);
base: while (l.peek()) |it| {
const beg = l.iter.i;
@@ -93,7 +95,10 @@ fn gen(parent: Allocator, l: *Lexer) Allocator.Error!Document {
if (err == Error.OutOfMemory) return Error.OutOfMemory;
var end = l.iter.i;
if (beg == end) end += 1;
- try doc_errors.append(parent, .{ .err = err, .location = .{ .beg = beg, .end = end } });
+ try doc_errors.append(parent, .{
+ .err = err,
+ .location = .{ .beg = beg, .end = end, .line = l.current_line },
+ });
_ = l.next();
// consume until next delimiter
while (l.next()) |next| if (next.kind.isDelimiter()) continue :base;
diff --git a/src/root.zig b/src/root.zig
index 30386e9..cef8519 100644
--- a/src/root.zig
+++ b/src/root.zig
@@ -53,7 +53,7 @@ const typdown_Document = extern struct {
const typdown_Error = extern struct {
code: u8,
- location: extern struct { beg: usize, end: usize },
+ location: extern struct { beg: usize, end: usize, line: usize },
};
};
@@ -65,7 +65,7 @@ fn from(alloc: Allocator, doc: Document) typdown_Document {
for (errors) |err| {
res.errors.?[0] = .{
.code = getErrorCode(err.err),
- .location = .{ .beg = err.location.beg, .end = err.location.end },
+ .location = .{ .beg = err.location.beg, .end = err.location.end, .line = err.location.line },
};
}
res.errors_len = errors.len;
@@ -75,7 +75,7 @@ fn from(alloc: Allocator, doc: Document) typdown_Document {
fn fromError(alloc: Allocator, err: Error) typdown_Document {
var v = alloc.alloc(typdown_Document.typdown_Error, 1) catch |e| @panic(@errorName(e));
- v[0] = .{ .code = getErrorCode(err), .location = .{ .beg = 0, .end = 0 } };
+ v[0] = .{ .code = getErrorCode(err), .location = .{ .beg = 0, .end = 0, .line = 0 } };
return .{ .errors = v.ptr, .errors_len = 1 };
}