diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-11-17 01:38:35 -0500 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-11-17 01:38:35 -0500 |
| commit | 704374e51294e14285b0b54030c1cb6154868098 (patch) | |
| tree | a0e33d754ab4b5fe84961a48711cdeff23dc858b /std | |
| parent | 38629628814aae6ba09bc2c25fdbac407a5d9e17 (diff) | |
| download | zig-704374e51294e14285b0b54030c1cb6154868098.tar.gz zig-704374e51294e14285b0b54030c1cb6154868098.zip | |
rename `section` keyword to `linksection`
add zig fmt support for this syntax
closes #1152
Diffstat (limited to 'std')
| -rw-r--r-- | std/zig/ast.zig | 12 | ||||
| -rw-r--r-- | std/zig/parse.zig | 37 | ||||
| -rw-r--r-- | std/zig/parser_test.zig | 7 | ||||
| -rw-r--r-- | std/zig/render.zig | 28 | ||||
| -rw-r--r-- | std/zig/tokenizer.zig | 4 |
5 files changed, 82 insertions, 6 deletions
diff --git a/std/zig/ast.zig b/std/zig/ast.zig index bf2c3593a4..bb2099fb75 100644 --- a/std/zig/ast.zig +++ b/std/zig/ast.zig @@ -503,6 +503,7 @@ pub const Node = struct { lib_name: ?*Node, type_node: ?*Node, align_node: ?*Node, + section_node: ?*Node, init_node: ?*Node, semicolon_token: TokenIndex, @@ -519,6 +520,11 @@ pub const Node = struct { i -= 1; } + if (self.section_node) |section_node| { + if (i < 1) return section_node; + i -= 1; + } + if (self.init_node) |init_node| { if (i < 1) return init_node; i -= 1; @@ -821,6 +827,7 @@ pub const Node = struct { body_node: ?*Node, lib_name: ?*Node, // populated if this is an extern declaration align_expr: ?*Node, // populated if align(A) is present + section_expr: ?*Node, // populated if linksection(A) is present pub const ParamList = SegmentedList(*Node, 2); @@ -845,6 +852,11 @@ pub const Node = struct { i -= 1; } + if (self.section_expr) |section_expr| { + if (i < 1) return section_expr; + i -= 1; + } + switch (self.return_type) { // TODO allow this and next prong to share bodies since the types are the same ReturnType.Explicit => |node| { diff --git a/std/zig/parse.zig b/std/zig/parse.zig index 2fb811bc41..a216484d7d 100644 --- a/std/zig/parse.zig +++ b/std/zig/parse.zig @@ -291,6 +291,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { .body_node = null, .lib_name = ctx.lib_name, .align_expr = null, + .section_expr = null, }); try ctx.decls.push(&fn_proto.base); stack.append(State{ .FnDef = fn_proto }) catch unreachable; @@ -601,6 +602,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { .extern_export_token = ctx.extern_export_token, .type_node = null, .align_node = null, + .section_node = null, .init_node = null, .lib_name = ctx.lib_name, // initialized later @@ -622,7 +624,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; }, State.VarDeclAlign => |var_decl| { - try stack.append(State{ .VarDeclEq = var_decl }); + try stack.append(State{ .VarDeclSection = var_decl }); const next_token = nextToken(&tok_it, &tree); const next_token_index = next_token.index; @@ -637,6 +639,22 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { prevToken(&tok_it, &tree); continue; }, + State.VarDeclSection => |var_decl| { + try stack.append(State{ .VarDeclEq = var_decl }); + + const next_token = nextToken(&tok_it, &tree); + const next_token_index = next_token.index; + const next_token_ptr = next_token.ptr; + if (next_token_ptr.id == Token.Id.Keyword_linksection) { + try stack.append(State{ .ExpectToken = Token.Id.RParen }); + try stack.append(State{ .Expression = OptionalCtx{ .RequiredNull = &var_decl.section_node } }); + try stack.append(State{ .ExpectToken = Token.Id.LParen }); + continue; + } + + prevToken(&tok_it, &tree); + continue; + }, State.VarDeclEq => |var_decl| { const token = nextToken(&tok_it, &tree); const token_index = token.index; @@ -719,7 +737,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; }, State.FnProtoAlign => |fn_proto| { - stack.append(State{ .FnProtoReturnType = fn_proto }) catch unreachable; + stack.append(State{ .FnProtoSection = fn_proto }) catch unreachable; if (eatToken(&tok_it, &tree, Token.Id.Keyword_align)) |align_token| { try stack.append(State{ .ExpectToken = Token.Id.RParen }); @@ -728,6 +746,16 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { } continue; }, + State.FnProtoSection => |fn_proto| { + stack.append(State{ .FnProtoReturnType = fn_proto }) catch unreachable; + + if (eatToken(&tok_it, &tree, Token.Id.Keyword_linksection)) |align_token| { + try stack.append(State{ .ExpectToken = Token.Id.RParen }); + try stack.append(State{ .Expression = OptionalCtx{ .RequiredNull = &fn_proto.section_expr } }); + try stack.append(State{ .ExpectToken = Token.Id.LParen }); + } + continue; + }, State.FnProtoReturnType => |fn_proto| { const token = nextToken(&tok_it, &tree); const token_index = token.index; @@ -1524,6 +1552,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { .body_node = null, .lib_name = null, .align_expr = null, + .section_expr = null, }); ctx.opt_ctx.store(&fn_proto.base); stack.append(State{ .FnProto = fn_proto }) catch unreachable; @@ -2579,6 +2608,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { .body_node = null, .lib_name = null, .align_expr = null, + .section_expr = null, }); opt_ctx.store(&fn_proto.base); stack.append(State{ .FnProto = fn_proto }) catch unreachable; @@ -2600,6 +2630,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { .body_node = null, .lib_name = null, .align_expr = null, + .section_expr = null, }); opt_ctx.store(&fn_proto.base); stack.append(State{ .FnProto = fn_proto }) catch unreachable; @@ -2985,12 +3016,14 @@ const State = union(enum) { VarDecl: VarDeclCtx, VarDeclAlign: *ast.Node.VarDecl, + VarDeclSection: *ast.Node.VarDecl, VarDeclEq: *ast.Node.VarDecl, VarDeclSemiColon: *ast.Node.VarDecl, FnDef: *ast.Node.FnProto, FnProto: *ast.Node.FnProto, FnProtoAlign: *ast.Node.FnProto, + FnProtoSection: *ast.Node.FnProto, FnProtoReturnType: *ast.Node.FnProto, ParamDecl: *ast.Node.FnProto, diff --git a/std/zig/parser_test.zig b/std/zig/parser_test.zig index 1744c9a067..2b60fb9b00 100644 --- a/std/zig/parser_test.zig +++ b/std/zig/parser_test.zig @@ -1,3 +1,10 @@ +test "zig fmt: linksection" { + try testCanonical( + \\export var aoeu: u64 linksection(".text.derp") = 1234; + \\export nakedcc fn _start() linksection(".text.boot") noreturn {} + \\ + ); +} test "zig fmt: shebang line" { try testCanonical( \\#!/usr/bin/env zig diff --git a/std/zig/render.zig b/std/zig/render.zig index 832cb7fe86..e55a0beb93 100644 --- a/std/zig/render.zig +++ b/std/zig/render.zig @@ -1148,6 +1148,17 @@ fn renderExpression( try renderToken(tree, stream, align_rparen, indent, start_col, Space.Space); // ) } + if (fn_proto.section_expr) |section_expr| { + const section_rparen = tree.nextToken(section_expr.lastToken()); + const section_lparen = tree.prevToken(section_expr.firstToken()); + const section_kw = tree.prevToken(section_lparen); + + try renderToken(tree, stream, section_kw, indent, start_col, Space.None); // section + try renderToken(tree, stream, section_lparen, indent, start_col, Space.None); // ( + try renderExpression(allocator, stream, tree, indent, start_col, section_expr, Space.None); + try renderToken(tree, stream, section_rparen, indent, start_col, Space.Space); // ) + } + switch (fn_proto.return_type) { ast.Node.FnProto.ReturnType.Explicit => |node| { return renderExpression(allocator, stream, tree, indent, start_col, node, space); @@ -1698,12 +1709,14 @@ fn renderVarDecl( try renderToken(tree, stream, var_decl.mut_token, indent, start_col, Space.Space); // var const name_space = if (var_decl.type_node == null and (var_decl.align_node != null or - var_decl.init_node != null)) Space.Space else Space.None; + var_decl.section_node != null or var_decl.init_node != null)) Space.Space else Space.None; try renderToken(tree, stream, var_decl.name_token, indent, start_col, name_space); if (var_decl.type_node) |type_node| { try renderToken(tree, stream, tree.nextToken(var_decl.name_token), indent, start_col, Space.Space); - const s = if (var_decl.align_node != null or var_decl.init_node != null) Space.Space else Space.None; + const s = if (var_decl.align_node != null or + var_decl.section_node != null or + var_decl.init_node != null) Space.Space else Space.None; try renderExpression(allocator, stream, tree, indent, start_col, type_node, s); } @@ -1714,6 +1727,17 @@ fn renderVarDecl( try renderToken(tree, stream, align_kw, indent, start_col, Space.None); // align try renderToken(tree, stream, lparen, indent, start_col, Space.None); // ( try renderExpression(allocator, stream, tree, indent, start_col, align_node, Space.None); + const s = if (var_decl.section_node != null or var_decl.init_node != null) Space.Space else Space.None; + try renderToken(tree, stream, rparen, indent, start_col, s); // ) + } + + if (var_decl.section_node) |section_node| { + const lparen = tree.prevToken(section_node.firstToken()); + const section_kw = tree.prevToken(lparen); + const rparen = tree.nextToken(section_node.lastToken()); + try renderToken(tree, stream, section_kw, indent, start_col, Space.None); // linksection + try renderToken(tree, stream, lparen, indent, start_col, Space.None); // ( + try renderExpression(allocator, stream, tree, indent, start_col, section_node, Space.None); const s = if (var_decl.init_node != null) Space.Space else Space.None; try renderToken(tree, stream, rparen, indent, start_col, s); // ) } diff --git a/std/zig/tokenizer.zig b/std/zig/tokenizer.zig index a3ba67052a..4941fe2cc2 100644 --- a/std/zig/tokenizer.zig +++ b/std/zig/tokenizer.zig @@ -46,7 +46,7 @@ pub const Token = struct { Keyword{ .bytes = "pub", .id = Id.Keyword_pub }, Keyword{ .bytes = "resume", .id = Id.Keyword_resume }, Keyword{ .bytes = "return", .id = Id.Keyword_return }, - Keyword{ .bytes = "section", .id = Id.Keyword_section }, + Keyword{ .bytes = "linksection", .id = Id.Keyword_linksection }, Keyword{ .bytes = "stdcallcc", .id = Id.Keyword_stdcallcc }, Keyword{ .bytes = "struct", .id = Id.Keyword_struct }, Keyword{ .bytes = "suspend", .id = Id.Keyword_suspend }, @@ -175,7 +175,7 @@ pub const Token = struct { Keyword_pub, Keyword_resume, Keyword_return, - Keyword_section, + Keyword_linksection, Keyword_stdcallcc, Keyword_struct, Keyword_suspend, |
