Skip to content

Commit bd46305

Browse files
RobinThriftalecthomas
authored andcommitted
Add Zig lexer
Add a lexer for the Zig language (https://ziglang.org) based on the pygments Zig lexer.
1 parent 6896804 commit bd46305

4 files changed

Lines changed: 574 additions & 0 deletions

File tree

lexers/lexers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
_ "github.com/alecthomas/chroma/lexers/w"
3333
_ "github.com/alecthomas/chroma/lexers/x"
3434
_ "github.com/alecthomas/chroma/lexers/y"
35+
_ "github.com/alecthomas/chroma/lexers/z"
3536
)
3637

3738
// Registry of Lexers.

lexers/testdata/zig.actual

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const std = @import("std.zig");
2+
const builtin = std.builtin;
3+
const testing = std.testing;
4+
5+
pub fn once(comptime f: fn () void) Once(f) {
6+
return Once(f){};
7+
}
8+
9+
/// An object that executes the function `f` just once.
10+
pub fn Once(comptime f: fn () void) type {
11+
return struct {
12+
done: bool = false,
13+
mutex: std.Mutex = std.Mutex.init(),
14+
15+
/// Call the function `f`.
16+
/// If `call` is invoked multiple times `f` will be executed only the
17+
/// first time.
18+
/// The invocations are thread-safe.
19+
pub fn call(self: *@This()) void {
20+
if (@atomicLoad(bool, &self.done, .Acquire))
21+
return;
22+
23+
return self.callSlow();
24+
}
25+
26+
fn callSlow(self: *@This()) void {
27+
@setCold(true);
28+
29+
const T = self.mutex.acquire();
30+
defer T.release();
31+
32+
// The first thread to acquire the mutex gets to run the initializer
33+
if (!self.done) {
34+
f();
35+
@atomicStore(bool, &self.done, true, .Release);
36+
}
37+
}
38+
};
39+
}
40+
41+
var global_number: i32 = 0;
42+
var global_once = once(incr);
43+
44+
fn incr() void {
45+
global_number += 1;
46+
}
47+
48+
test "Once executes its function just once" {
49+
if (builtin.single_threaded) {
50+
global_once.call();
51+
global_once.call();
52+
} else {
53+
var threads: [10]*std.Thread = undefined;
54+
defer for (threads) |handle| handle.wait();
55+
56+
for (threads) |*handle| {
57+
handle.* = try std.Thread.spawn(@as(u8, 0), struct {
58+
fn thread_fn(x: u8) void {
59+
global_once.call();
60+
}
61+
}.thread_fn);
62+
}
63+
}
64+
65+
testing.expectEqual(@as(i32, 1), global_number);
66+
}

0 commit comments

Comments
 (0)