authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-04-09 11:17:57+02:00
committergravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-04-09 11:17:57+02:00
loge24409ebe0f50be9e01810a5f61bb4c09db57d28
tree8fcbd128892b51f843f6ae499d537e54ac01a6e2
parente260c8ca632fb2569f99d182dd6d1daea2b6df63

std.zig.parser unified code for rendering and parsing semicolon in statements


1 files changed, 40 insertions(+), 69 deletions(-)

std/zig/parser.zig+40-69
......@@ -2053,44 +2053,49 @@ pub const Parser = struct {
20532053
20542054 State.Semicolon => |node_ptr| {
20552055 const node = *node_ptr;
2056 switch (node.id) {
2057 ast.Node.Id.Root,
2058 ast.Node.Id.StructField,
2059 ast.Node.Id.UnionTag,
2060 ast.Node.Id.EnumTag,
2061 ast.Node.Id.ParamDecl,
2062 ast.Node.Id.Block,
2063 ast.Node.Id.Payload,
2064 ast.Node.Id.Switch,
2065 ast.Node.Id.SwitchCase,
2066 ast.Node.Id.SwitchElse,
2067 ast.Node.Id.FieldInitializer,
2068 ast.Node.Id.LineComment,
2069 ast.Node.Id.TestDecl => continue,
2070 ast.Node.Id.While => {
2071 const while_node = @fieldParentPtr(ast.NodeWhile, "base", node);
2072 if (while_node.@"else") |@"else"| {
2073 stack.append(State { .Semicolon = &@"else".base }) catch unreachable;
2074 continue;
2075 }
2076
2077 stack.append(State { .Semicolon = &while_node.body }) catch unreachable;
2078 continue;
2079 },
2080 ast.Node.Id.Else => {
2081 const else_node = @fieldParentPtr(ast.NodeElse, "base", node);
2082 stack.append(State { .Semicolon = &else_node.body }) catch unreachable;
2083 continue;
2084 },
2085 else => {
2086 _ = (try self.eatToken(&stack, Token.Id.Semicolon)) ?? continue;
2087 }
2056 if (requireSemiColon(node)) {
2057 _ = (try self.eatToken(&stack, Token.Id.Semicolon)) ?? continue;
20882058 }
20892059 }
20902060 }
20912061 }
20922062 }
20932063
2064 fn requireSemiColon(node: &const ast.Node) bool {
2065 var n = node;
2066 while (true) {
2067 switch (n.id) {
2068 ast.Node.Id.Root,
2069 ast.Node.Id.StructField,
2070 ast.Node.Id.UnionTag,
2071 ast.Node.Id.EnumTag,
2072 ast.Node.Id.ParamDecl,
2073 ast.Node.Id.Block,
2074 ast.Node.Id.Payload,
2075 ast.Node.Id.Switch,
2076 ast.Node.Id.SwitchCase,
2077 ast.Node.Id.SwitchElse,
2078 ast.Node.Id.FieldInitializer,
2079 ast.Node.Id.LineComment,
2080 ast.Node.Id.TestDecl => return false,
2081 ast.Node.Id.While => {
2082 const while_node = @fieldParentPtr(ast.NodeWhile, "base", n);
2083 if (while_node.@"else") |@"else"| {
2084 n = @"else".base;
2085 continue;
2086 }
2087
2088 n = while_node.body;
2089 },
2090 ast.Node.Id.Else => {
2091 const else_node = @fieldParentPtr(ast.NodeElse, "base", n);
2092 n = else_node.body;
2093 },
2094 else => return true,
2095 }
2096 }
2097 }
2098
20942099 fn commaOrEnd(self: &Parser, stack: &ArrayList(State), end: &const Token.Id, maybe_ptr: ?&Token, state_after_comma: &const State) !void {
20952100 var token = self.getNextToken();
20962101 switch (token.id) {
......@@ -2559,7 +2564,6 @@ pub const Parser = struct {
25592564 Expression: &ast.Node,
25602565 VarDecl: &ast.NodeVarDecl,
25612566 Statement: &ast.Node,
2562 Semicolon: &ast.Node,
25632567 FieldInitializer: &ast.NodeFieldInitializer,
25642568 PrintIndent,
25652569 Indent: usize,
......@@ -3344,44 +3348,11 @@ pub const Parser = struct {
33443348 try stack.append(RenderState { .VarDecl = var_decl});
33453349 },
33463350 else => {
3347 try stack.append(RenderState { .Semicolon = base });
3348 try stack.append(RenderState { .Expression = base });
3349 },
3350 }
3351 },
3352 RenderState.Semicolon => |base| {
3353 switch (base.id) {
3354 ast.Node.Id.Root,
3355 ast.Node.Id.StructField,
3356 ast.Node.Id.UnionTag,
3357 ast.Node.Id.EnumTag,
3358 ast.Node.Id.ParamDecl,
3359 ast.Node.Id.Block,
3360 ast.Node.Id.Payload,
3361 ast.Node.Id.Switch,
3362 ast.Node.Id.SwitchCase,
3363 ast.Node.Id.SwitchElse,
3364 ast.Node.Id.FieldInitializer,
3365 ast.Node.Id.LineComment,
3366 ast.Node.Id.TestDecl => {},
3367 ast.Node.Id.While => {
3368 const while_node = @fieldParentPtr(ast.NodeWhile, "base", base);
3369 if (while_node.@"else") |@"else"| {
3370 stack.append(RenderState { .Semicolon = &@"else".base }) catch unreachable;
3371 continue;
3351 if (requireSemiColon(base)) {
3352 try stack.append(RenderState { .Text = ";" });
33723353 }
3373
3374 stack.append(RenderState { .Semicolon = while_node.body }) catch unreachable;
3375 continue;
3376 },
3377 ast.Node.Id.Else => {
3378 const else_node = @fieldParentPtr(ast.NodeElse, "base", base);
3379 stack.append(RenderState { .Semicolon = else_node.body }) catch unreachable;
3380 continue;
3354 try stack.append(RenderState { .Expression = base });
33813355 },
3382 else => {
3383 try stack.append(RenderState { .Text = ";" });
3384 }
33853356 }
33863357 },
33873358 RenderState.Indent => |new_indent| indent = new_indent,