1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
const std = @import("std");
const Allocator = std.mem.Allocator;
const eql = std.mem.eql;
const unicode = std.unicode;
const Lexed = @import("Lexed.zig");
iter: unicode.Utf8Iterator,
force_lit: bool = false,
const Self = @This();
pub const Error = error{
InvalidUtf8,
} || Allocator.Error;
pub fn init(content: []const u8) error{InvalidUtf8}!Self {
const view = try unicode.Utf8View.init(content);
return .{ .iter = view.iterator() };
}
pub fn nextKind(self: *Self) ?Lexed.Kind {
const next_rune = self.iter.peek(1);
if (next_rune.len == 0) return null;
return self.getCurrentKind(null, next_rune, &[0]u8{}).kind;
}
pub fn next(self: *Self, alloc: Allocator) Error!?Lexed {
var acc = try std.ArrayList(u8).initCapacity(alloc, 2);
errdefer acc.deinit(alloc);
var current_kind: ?Lexed.Kind = null;
while (self.iter.nextCodepointSlice()) |rune| {
if (eql(u8, rune, "\r")) continue;
var override_if: ?[]const u8 = null;
// escape chars
if (eql(u8, rune, "\\")) {
self.force_lit = true;
current_kind = .literal;
} else {
self.force_lit = false;
const res = self.getCurrentKind(current_kind, rune, acc.items);
current_kind = res.kind;
override_if = res.override_if;
try acc.appendSlice(alloc, rune);
}
// conds here to avoid creating complex condition in while
const next_rune = self.iter.peek(1);
const next_kind = self.getCurrentKind(current_kind, next_rune, acc.items).kind;
if (requiresSpace(current_kind.?) and next_kind != current_kind.?) {
if (eql(u8, next_rune, " ")) {
// consume next space
_ = self.iter.nextCodepoint();
break;
}
current_kind = switch (current_kind.?) {
.title => if (acc.items.len == 1) .tag else .literal,
else => .literal,
};
}
if (next_rune.len > 0 and
next_kind != current_kind.? and
(override_if == null or !eql(u8, override_if.?, next_rune)))
break;
}
const kind = current_kind orelse {
acc.deinit(alloc);
return null;
};
return .init(alloc, kind, acc);
}
const kindRes = struct {
kind: Lexed.Kind,
override_if: ?[]const u8 = null,
fn equals(self: @This(), v: @This()) bool {
if (self.kind != v.kind) return false;
if (self.override_if == null and v.override_if != null) return false;
if (self.override_if != null and v.override_if == null) return false;
if (self.override_if) |it| return eql(u8, it, v.override_if.?);
return true;
}
};
fn requiresDelimiter(before: ?Lexed.Kind, knd: Lexed.Kind) Lexed.Kind {
return if (before == null or before.?.isDelimiter() or before.? == knd) knd else .literal;
}
fn getCurrentKind(self: *Self, before: ?Lexed.Kind, rune: []const u8, acc: []const u8) kindRes {
if (self.force_lit) return .{ .kind = .literal };
if (eql(u8, rune, "\n")) return .{
.kind = if (before == .weak_delimiter) .strong_delimiter else .weak_delimiter,
.override_if = rune,
};
if (eql(u8, rune, "*")) return .{ .kind = .bold };
if (eql(u8, rune, "_")) return .{ .kind = .italic };
if (eql(u8, rune, ">")) return .{ .kind = requiresDelimiter(before, .quote) };
if (eql(u8, rune, ".")) return .{ .kind = requiresDelimiter(before, .list_ordored) };
if (eql(u8, rune, "-")) return .{ .kind = requiresDelimiter(before, .list_unordored) };
if (eql(u8, rune, "!")) return .{ .kind = requiresDelimiter(before, .image) };
if (eql(u8, rune, "<")) return .{ .kind = .ref };
if (is('#', 6, rune, acc)) return .{ .kind = requiresDelimiter(before, .title) };
if (isIn(links, rune, acc, before, .link)) return .{ .kind = .link };
if (isOneOrThree(":", rune, acc, .ref, .callout)) |it| return it;
if (isOneOrThree("$", rune, acc, .math, .math_block)) |it| return it;
if (isOneOrThree("`", rune, acc, .code, .code_block)) |it| return it;
return .{ .kind = .literal };
}
fn is(v: u8, maxLen: usize, rune: []const u8, acc: []const u8) bool {
if (!eql(u8, rune, &[_]u8{v})) return false;
for (acc) |it| if (it != v) return true;
return acc.len < maxLen;
}
const links = &[_][]const u8{ "[", "](", ")" };
fn isIn(ops: []const []const u8, rune: []const u8, p: []const u8, before: ?Lexed.Kind, now: Lexed.Kind) bool {
var acc = p;
if (before) |b| {
if (now != b) acc = &[_]u8{};
}
for (ops) |op| {
const ln = acc.len + rune.len;
if (op.len >= ln and eql(u8, acc, op[0..acc.len]) and eql(u8, rune, op[acc.len..ln]))
return true;
}
return false;
}
fn isOneOrThree(op: []const u8, rune: []const u8, p: []const u8, one: Lexed.Kind, three: Lexed.Kind) ?kindRes {
if (!eql(u8, rune, op)) return null;
var acc = p;
if (acc.len < op.len or !eql(u8, acc[0..op.len], op)) acc = &[_]u8{};
var iter = (unicode.Utf8View.init(acc) catch unreachable).iterator();
var ln: usize = 1; // number of runes
while (iter.nextCodepointSlice()) |it| : (ln += 1) {
if (!eql(u8, it, op)) return null;
}
return switch (ln) {
1 => .{
.kind = one,
.override_if = op,
},
2 => .{
.kind = .literal,
.override_if = op,
},
3 => .{ .kind = three },
else => unreachable,
};
}
fn requiresSpace(k: Lexed.Kind) bool {
return switch (k) {
.title, .list_ordored, .list_unordored => true,
else => false,
};
}
fn doTest(alloc: Allocator, l: *Self, k: Lexed.Kind, v: []const u8) !void {
var first = (try l.next(alloc)).?;
defer first.deinit();
std.testing.expect(first.equals(k, v)) catch |err| {
std.debug.print("{}({s})\n", .{ first.kind, first.content.items });
return err;
};
}
test "one or three" {
const expect = std.testing.expect;
// valid
try expect(isOneOrThree(":", ":", "", .ref, .callout).?.equals(.{ .kind = .ref, .override_if = ":" }));
try expect(isOneOrThree(":", ":", ":", .ref, .callout).?.equals(.{ .kind = .literal, .override_if = ":" }));
try expect(isOneOrThree(":", ":", "::", .ref, .callout).?.equals(.{ .kind = .callout }));
try expect(isOneOrThree(":", ":", "a", .ref, .callout).?.equals(.{ .kind = .ref, .override_if = ":" }));
// invalid
try expect(isOneOrThree(":", "a", "", .ref, .callout) == null);
try expect(isOneOrThree(":", "a", "b", .ref, .callout) == null);
try expect(isOneOrThree(":", "a", ":", .ref, .callout) == null);
}
test "is" {
const expect = std.testing.expect;
// valid
try expect(is('#', 6, "#", ""));
try expect(is('#', 6, "#", "#"));
try expect(is('#', 6, "#", "##"));
try expect(is('#', 6, "#", "###"));
try expect(is('#', 6, "#", "####"));
try expect(is('#', 6, "#", "#####"));
// invalid
try expect(!is('#', 6, "#", "######"));
try expect(!is('#', 6, "u", "##"));
}
test "lexer common" {
const expect = std.testing.expect;
var arena = std.heap.DebugAllocator(.{}).init;
defer if (arena.deinit() == .leak) std.debug.print("leaking!\n", .{});
const alloc = arena.allocator();
var l = try init("## hello world :)");
try doTest(alloc, &l, .title, "##");
try doTest(alloc, &l, .literal, "hello world ");
try doTest(alloc, &l, .ref, ":");
try doTest(alloc, &l, .link, ")");
try expect(try l.next(alloc) == null);
}
test "lexer multiline" {
const expect = std.testing.expect;
var arena = std.heap.DebugAllocator(.{}).init;
defer if (arena.deinit() == .leak) std.debug.print("leaking!\n", .{});
const alloc = arena.allocator();
var l = try init(
\\# Title
\\
\\paragraph
\\# a title
\\a # in sentence
\\
\\#tag
\\#tag2
);
try doTest(alloc, &l, .title, "#");
try doTest(alloc, &l, .literal, "Title");
try doTest(alloc, &l, .strong_delimiter, "\n\n");
try doTest(alloc, &l, .literal, "paragraph");
try doTest(alloc, &l, .weak_delimiter, "\n");
try doTest(alloc, &l, .title, "#");
try doTest(alloc, &l, .literal, "a title");
try doTest(alloc, &l, .weak_delimiter, "\n");
try doTest(alloc, &l, .literal, "a # in sentence");
try doTest(alloc, &l, .strong_delimiter, "\n\n");
try doTest(alloc, &l, .tag, "#");
try doTest(alloc, &l, .literal, "tag");
try doTest(alloc, &l, .weak_delimiter, "\n");
try doTest(alloc, &l, .tag, "#");
try doTest(alloc, &l, .literal, "tag2");
try expect(try l.next(alloc) == null);
}
|