authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-09 00:19:06-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-09 00:19:24-05:00
log052800e9529a3c2b403b7f43ffba4c981935d0a6
tree399a929419b2d853c8d274191685f9a80c5ae31a
parentd6f2af378abe7467657f6fbf8133b995e5f55142
signature Commit is signed but in an unrecognized format.

zig fmt: support threadlocal


5 files changed, 73 insertions(+), 0 deletions(-)

std/zig/ast.zig+6
......@@ -110,6 +110,7 @@ pub const Tree = struct {
110110pub const Error = union(enum) {
111111 InvalidToken: InvalidToken,
112112 ExpectedVarDeclOrFn: ExpectedVarDeclOrFn,
113 ExpectedVarDecl: ExpectedVarDecl,
113114 ExpectedAggregateKw: ExpectedAggregateKw,
114115 UnattachedDocComment: UnattachedDocComment,
115116 ExpectedEqOrSemi: ExpectedEqOrSemi,
......@@ -133,6 +134,7 @@ pub const Error = union(enum) {
133134 // TODO https://github.com/ziglang/zig/issues/683
134135 @TagType(Error).InvalidToken => |*x| return x.render(tokens, stream),
135136 @TagType(Error).ExpectedVarDeclOrFn => |*x| return x.render(tokens, stream),
137 @TagType(Error).ExpectedVarDecl => |*x| return x.render(tokens, stream),
136138 @TagType(Error).ExpectedAggregateKw => |*x| return x.render(tokens, stream),
137139 @TagType(Error).UnattachedDocComment => |*x| return x.render(tokens, stream),
138140 @TagType(Error).ExpectedEqOrSemi => |*x| return x.render(tokens, stream),
......@@ -158,6 +160,7 @@ pub const Error = union(enum) {
158160 // TODO https://github.com/ziglang/zig/issues/683
159161 @TagType(Error).InvalidToken => |x| return x.token,
160162 @TagType(Error).ExpectedVarDeclOrFn => |x| return x.token,
163 @TagType(Error).ExpectedVarDecl => |x| return x.token,
161164 @TagType(Error).ExpectedAggregateKw => |x| return x.token,
162165 @TagType(Error).UnattachedDocComment => |x| return x.token,
163166 @TagType(Error).ExpectedEqOrSemi => |x| return x.token,
......@@ -180,6 +183,7 @@ pub const Error = union(enum) {
180183
181184 pub const InvalidToken = SingleTokenError("Invalid token {}");
182185 pub const ExpectedVarDeclOrFn = SingleTokenError("Expected variable declaration or function, found {}");
186 pub const ExpectedVarDecl = SingleTokenError("Expected variable declaration, found {}");
183187 pub const ExpectedAggregateKw = SingleTokenError("Expected " ++ @tagName(Token.Id.Keyword_struct) ++ ", " ++ @tagName(Token.Id.Keyword_union) ++ ", or " ++ @tagName(Token.Id.Keyword_enum) ++ ", found {}");
184188 pub const ExpectedEqOrSemi = SingleTokenError("Expected '=' or ';', found {}");
185189 pub const ExpectedSemiOrLBrace = SingleTokenError("Expected ';' or '{{', found {}");
......@@ -496,6 +500,7 @@ pub const Node = struct {
496500 base: Node,
497501 doc_comments: ?*DocComment,
498502 visib_token: ?TokenIndex,
503 thread_local_token: ?TokenIndex,
499504 name_token: TokenIndex,
500505 eq_token: TokenIndex,
501506 mut_token: TokenIndex,
......@@ -536,6 +541,7 @@ pub const Node = struct {
536541
537542 pub fn firstToken(self: *const VarDecl) TokenIndex {
538543 if (self.visib_token) |visib_token| return visib_token;
544 if (self.thread_local_token) |thread_local_token| return thread_local_token;
539545 if (self.comptime_token) |comptime_token| return comptime_token;
540546 if (self.extern_export_token) |extern_export_token| return extern_export_token;
541547 assert(self.lib_name == null);
std/zig/parse.zig+54
......@@ -229,6 +229,32 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
229229 }) catch unreachable;
230230 continue;
231231 },
232 State.ThreadLocal => |ctx| {
233 const token = nextToken(&tok_it, &tree);
234 const token_index = token.index;
235 const token_ptr = token.ptr;
236 switch (token_ptr.id) {
237 Token.Id.Keyword_var, Token.Id.Keyword_const => {
238 try stack.append(State{
239 .VarDecl = VarDeclCtx{
240 .comments = ctx.comments,
241 .visib_token = ctx.visib_token,
242 .thread_local_token = ctx.thread_local_token,
243 .lib_name = ctx.lib_name,
244 .comptime_token = ctx.comptime_token,
245 .extern_export_token = ctx.extern_export_token,
246 .mut_token = token_index,
247 .list = ctx.list,
248 },
249 });
250 continue;
251 },
252 else => {
253 ((try tree.errors.addOne())).* = Error{ .ExpectedVarDecl = Error.ExpectedVarDecl{ .token = token_index } };
254 return tree;
255 },
256 }
257 },
232258 State.TopLevelDecl => |ctx| {
233259 const token = nextToken(&tok_it, &tree);
234260 const token_index = token.index;
......@@ -260,6 +286,28 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
260286 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.expr } });
261287 continue;
262288 },
289 Token.Id.Keyword_threadlocal => {
290 if (ctx.extern_export_inline_token) |annotated_token| {
291 if (annotated_token.ptr.id == Token.Id.Keyword_inline) {
292 ((try tree.errors.addOne())).* = Error{ .InvalidToken = Error.InvalidToken{ .token = annotated_token.index } };
293 return tree;
294 }
295 }
296
297 try stack.append(State{
298 .ThreadLocal = VarDeclCtx{
299 .comments = ctx.comments,
300 .visib_token = ctx.visib_token,
301 .thread_local_token = token_index,
302 .lib_name = ctx.lib_name,
303 .comptime_token = null,
304 .extern_export_token = if (ctx.extern_export_inline_token) |at| at.index else null,
305 .mut_token = undefined,
306 .list = ctx.decls,
307 },
308 });
309 continue;
310 },
263311 Token.Id.Keyword_var, Token.Id.Keyword_const => {
264312 if (ctx.extern_export_inline_token) |annotated_token| {
265313 if (annotated_token.ptr.id == Token.Id.Keyword_inline) {
......@@ -272,6 +320,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
272320 .VarDecl = VarDeclCtx{
273321 .comments = ctx.comments,
274322 .visib_token = ctx.visib_token,
323 .thread_local_token = null,
275324 .lib_name = ctx.lib_name,
276325 .comptime_token = null,
277326 .extern_export_token = if (ctx.extern_export_inline_token) |at| at.index else null,
......@@ -611,6 +660,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
611660 .base = ast.Node{ .id = ast.Node.Id.VarDecl },
612661 .doc_comments = ctx.comments,
613662 .visib_token = ctx.visib_token,
663 .thread_local_token = ctx.thread_local_token,
614664 .mut_token = ctx.mut_token,
615665 .comptime_token = ctx.comptime_token,
616666 .extern_export_token = ctx.extern_export_token,
......@@ -1094,6 +1144,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
10941144 .VarDecl = VarDeclCtx{
10951145 .comments = null,
10961146 .visib_token = null,
1147 .thread_local_token = null,
10971148 .comptime_token = null,
10981149 .extern_export_token = null,
10991150 .lib_name = null,
......@@ -1150,6 +1201,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
11501201 .VarDecl = VarDeclCtx{
11511202 .comments = null,
11521203 .visib_token = null,
1204 .thread_local_token = null,
11531205 .comptime_token = ctx.comptime_token,
11541206 .extern_export_token = null,
11551207 .lib_name = null,
......@@ -2937,6 +2989,7 @@ const TopLevelDeclCtx = struct {
29372989const VarDeclCtx = struct {
29382990 mut_token: TokenIndex,
29392991 visib_token: ?TokenIndex,
2992 thread_local_token: ?TokenIndex,
29402993 comptime_token: ?TokenIndex,
29412994 extern_export_token: ?TokenIndex,
29422995 lib_name: ?*ast.Node,
......@@ -3081,6 +3134,7 @@ const State = union(enum) {
30813134 ContainerInitArg: *ast.Node.ContainerDecl,
30823135 ContainerDecl: *ast.Node.ContainerDecl,
30833136
3137 ThreadLocal: VarDeclCtx,
30843138 VarDecl: VarDeclCtx,
30853139 VarDeclAlign: *ast.Node.VarDecl,
30863140 VarDeclSection: *ast.Node.VarDecl,
std/zig/parser_test.zig+8
......@@ -1,3 +1,10 @@
1test "zig fmt: threadlocal" {
2 try testCanonical(
3 \\threadlocal var x: i32 = 1234;
4 \\
5 );
6}
7
18test "zig fmt: linksection" {
29 try testCanonical(
310 \\export var aoeu: u64 linksection(".text.derp") = 1234;
......@@ -5,6 +12,7 @@ test "zig fmt: linksection" {
512 \\
613 );
714}
15
816test "zig fmt: shebang line" {
917 try testCanonical(
1018 \\#!/usr/bin/env zig
std/zig/render.zig+3
......@@ -1706,6 +1706,9 @@ fn renderVarDecl(
17061706 try renderToken(tree, stream, comptime_token, indent, start_col, Space.Space); // comptime
17071707 }
17081708
1709 if (var_decl.thread_local_token) |thread_local_token| {
1710 try renderToken(tree, stream, thread_local_token, indent, start_col, Space.Space); // threadlocal
1711 }
17091712 try renderToken(tree, stream, var_decl.mut_token, indent, start_col, Space.Space); // var
17101713
17111714 const name_space = if (var_decl.type_node == null and (var_decl.align_node != null or
std/zig/tokenizer.zig+2
......@@ -53,6 +53,7 @@ pub const Token = struct {
5353 Keyword{ .bytes = "switch", .id = Id.Keyword_switch },
5454 Keyword{ .bytes = "test", .id = Id.Keyword_test },
5555 Keyword{ .bytes = "this", .id = Id.Keyword_this },
56 Keyword{ .bytes = "threadlocal", .id = Id.Keyword_threadlocal },
5657 Keyword{ .bytes = "true", .id = Id.Keyword_true },
5758 Keyword{ .bytes = "try", .id = Id.Keyword_try },
5859 Keyword{ .bytes = "undefined", .id = Id.Keyword_undefined },
......@@ -182,6 +183,7 @@ pub const Token = struct {
182183 Keyword_switch,
183184 Keyword_test,
184185 Keyword_this,
186 Keyword_threadlocal,
185187 Keyword_true,
186188 Keyword_try,
187189 Keyword_undefined,