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
|
const std = @import("std");
const testing = std.testing;
const expect = testing.expect;
const alloc = std.heap.smp_allocator;
const io = std.Io.Threaded.global_single_threaded.io();
pub const zisp = @import("../zisp.zig");
pub const value = zisp.value;
pub const Value = zisp.Value;
fn parse(str: []const u8) Value {
var fbs = std.Io.Reader.fixed(str);
return zisp.io.parse.fromReaderNoError(alloc, io, &fbs);
}
test "parse empty" {
try expect(parse("").eq(value.eof));
try expect(parse(";").eq(value.eof));
try expect(parse(";~").eq(value.eof));
try expect(parse(" ;").eq(value.eof));
try expect(parse(" ;~").eq(value.eof));
try expect(parse("; ").eq(value.eof));
try expect(parse(";~ ").eq(value.eof));
try expect(parse(";\n").eq(value.eof));
try expect(parse(";\n ").eq(value.eof));
try expect(parse(";~foo").eq(value.eof));
try expect(parse(";~foo ").eq(value.eof));
try expect(parse(";~foo;").eq(value.eof));
try expect(parse(";~foo ;").eq(value.eof));
try expect(parse("\t\r\n ;foo\n;~(\nbar\n)").eq(value.eof));
}
test "parse short bare string" {
const str = value.sstr.pack;
try expect(parse(".").eq(str(".")));
try expect(parse("..").eq(str("..")));
try expect(parse("...").eq(str("...")));
try expect(parse(".a.b").eq(str(".a.b")));
try expect(parse("+0.1").eq(str("+0.1")));
try expect(parse("-0.1").eq(str("-0.1")));
try expect(parse("10.1").eq(str("10.1")));
try expect(parse("|x()|").eq(str("x()")));
try expect(parse("|{\\|}|").eq(str("{|}")));
try expect(parse("foobar").eq(str("foobar")));
try expect(parse("!$%*+-").eq(str("!$%*+-")));
try expect(parse("/<=>?@").eq(str("/<=>?@")));
try expect(parse("^_~000").eq(str("^_~000")));
}
test "parse long bare string" {
const str = value.istr.intern;
try expect(parse("foobarbaz").eq(str("foobarbaz")));
try expect(parse(".foo.bar.baz").eq(str(".foo.bar.baz")));
try expect(parse("+foo.bar.baz").eq(str("+foo.bar.baz")));
try expect(parse("-foo.bar.baz").eq(str("-foo.bar.baz")));
try expect(parse("0foo.bar.baz").eq(str("0foo.bar.baz")));
try expect(parse("!$%*+-/<=>?@^_~").eq(str("!$%*+-/<=>?@^_~")));
try expect(parse("|foo\\x20;bar\\x0a;baz|").eq(str("foo bar\nbaz")));
}
test "parse" {
const val = parse("foo");
try testing.expect(value.sstr.check(val));
const s = value.sstr.unpack(val);
try testing.expectEqualStrings("foo", s.slice());
}
test "parse2" {
const val = parse(
\\ ;; Testing some crazy datum comments
\\ ;~"bar"([x #"y"]{##`,'z}) #"foo"
\\ ;; end
);
const r = value.rune.unpack(value.pair.car(val));
try testing.expectEqualStrings("HASH", r.slice());
const s = value.pair.cdr(value.pair.cdr(val));
try testing.expect(value.sstr.check(s));
const f = value.sstr.unpack(s);
try testing.expectEqualStrings("foo", f.slice());
}
test "parse3" {
const val = parse(
\\(foo ;~x ;~(x y) ;~x #bar [#x #"baz"] 'bat)
);
const car = value.pair.car;
const cdr = value.pair.cdr;
const e1 = car(val);
const e2 = car(cdr(val));
const e3 = car(cdr(cdr(val)));
const e4 = car(cdr(cdr(cdr(val))));
try testing.expect(value.sstr.check(e1));
try testing.expect(value.rune.check(e2));
try testing.expect(value.pair.check(e3) != null);
try testing.expect(value.pair.check(e4) != null);
}
test "parse4" {
const val = parse("(foo & ;~x bar ;~y)");
const s = value.sstr.unpack(value.pair.car(val));
try testing.expectEqualStrings("foo", s.slice());
const f = value.sstr.unpack(value.pair.cdr(val));
try testing.expectEqualStrings("bar", f.slice());
}
test "print" {
var buf: [32]u8 = undefined;
var w = std.Io.Writer.fixed(&buf);
const v = parse("#foo");
try zisp.io.print.toWriter(&w, v);
try testing.expectEqualStrings("#foo", buf[0..w.end]);
}
test "print2" {
var buf: [128]u8 = undefined;
var w = std.Io.Writer.fixed(&buf);
const v = parse("#{foo bar['x]}");
try zisp.io.print.toWriter(&w, v);
try testing.expectEqualStrings(
"(#HASH #BRACE foo (#JOIN bar #SQUARE (#QUOTE & x)))",
buf[0..w.end],
);
}
fn parseAndPrint(str: []const u8) !void {
var buf: [64]u8 = undefined;
var fw = std.Io.File.stderr().writer(io, &buf);
const w = &fw.interface;
const v = parse(str);
try zisp.io.print.toWriter(w, v);
try w.writeByte('\n');
try w.flush();
}
test "print3" {
try parseAndPrint("#{foo bar['x](y)(z)}");
}
test "print4" {
try parseAndPrint("(foo ;~bar)");
}
test "print5" {
try parseAndPrint("(;~foo foo ;~bar & ;~bar bar ;~bar)");
}
test "print6" {
try parseAndPrint("(foo bar ... baz bat.(qux))");
}
test "print7" {
try parseAndPrint("#`(#,(->keyword (syntax->datum #'sym)) & in)");
}
fn parseBench(path: []const u8, iters: usize) !void {
var timer = try std.time.Timer.start();
for (0..iters) |i| {
_ = i;
const file = try std.Io.Dir.cwd().openFile(io, path, .{});
defer file.close(io);
var buf: [4096]u8 = undefined;
var file_reader = file.reader(io, &buf);
const reader = &file_reader.interface;
while (true) {
const v = zisp.io.parse.fromReaderNoError(alloc, io, reader);
if (value.eof.eq(v)) break;
}
}
const ns: f64 = @floatFromInt(timer.lap());
const secs = ns / 1_000_000_000;
std.debug.print(
"parse {s} x {}: {d:.3}s\n",
.{ path, iters, secs },
);
}
test "parse bench" {
try parseBench("src/test/data/parser-test-1.scm", 800);
try parseBench("src/test/data/parser-test-2.scm", 6400);
//try parseBench("src/test/data/parser-torture.scm", 1);
}
|