authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-04-06 09:36:11+02:00
committergravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-04-06 09:36:11+02:00
logf667744d44b0fc3599c85c01d3fcf3b63c4e68a6
tree9787b6be35bee64adfafa07faaf7ca8eefb90323
parente45de607d6437428d82f69711a8cc8f338d019c8

std.zig.parser Fixed:

* Parsing of the optional expression in contrl flow expr * Rendering of catch expressions

1 files changed, 27 insertions(+), 6 deletions(-)

std/zig/parser.zig+27-6
...@@ -89,6 +89,15 @@ pub const Parser = struct {...@@ -89,6 +89,15 @@ pub const Parser = struct {
89 ptr: &Token,89 ptr: &Token,
90 };90 };
9191
92 const RevertState = struct {
93 parser: Parser,
94 tokenizer: Tokenizer,
95
96 // We expect, that if something is optional, then there is a field,
97 // that needs to be set to null, when we revert.
98 ptr: &?&ast.Node,
99 };
100
92 fn ListState(comptime T: type) type {101 fn ListState(comptime T: type) type {
93 return struct {102 return struct {
94 list: &ArrayList(T),103 list: &ArrayList(T),
...@@ -131,7 +140,7 @@ pub const Parser = struct {...@@ -131,7 +140,7 @@ pub const Parser = struct {
131 /// optional state is found, the parser will revert to the state it was in140 /// optional state is found, the parser will revert to the state it was in
132 /// when the optional was added. This will polute the arena allocator with141 /// when the optional was added. This will polute the arena allocator with
133 /// "leaked" nodes. TODO: Figure out if it's nessesary to handle leaked nodes.142 /// "leaked" nodes. TODO: Figure out if it's nessesary to handle leaked nodes.
134 Optional: Parser,143 Optional: RevertState,
135144
136 /// Optional can be reverted by adding the Required state to the stack.145 /// Optional can be reverted by adding the Required state to the stack.
137 Required,146 Required,
...@@ -598,7 +607,13 @@ pub const Parser = struct {...@@ -598,7 +607,13 @@ pub const Parser = struct {
598 const node = try self.createControlFlowExpr(arena, token, ast.NodeControlFlowExpression.Kind.Return);607 const node = try self.createControlFlowExpr(arena, token, ast.NodeControlFlowExpression.Kind.Return);
599 dest_ptr.store(&node.base);608 dest_ptr.store(&node.base);
600609
601 stack.append(State { .Optional = *self }) catch unreachable;610 stack.append(State {
611 .Optional = RevertState {
612 .parser = *self,
613 .tokenizer = *self.tokenizer,
614 .ptr = &node.rhs,
615 }
616 }) catch unreachable;
602 try stack.append(State { .Expression = DestPtr { .NullableField = &node.rhs } });617 try stack.append(State { .Expression = DestPtr { .NullableField = &node.rhs } });
603 continue;618 continue;
604 },619 },
...@@ -673,7 +688,7 @@ pub const Parser = struct {...@@ -673,7 +688,7 @@ pub const Parser = struct {
673 continue;688 continue;
674 }689 }
675690
676 node.op.Catch = try self.createIdentifier(arena, undefined);691 node.op.Catch = try self.createIdentifier(arena, Token(undefined));
677 try stack.append(State { .ExpectToken = Token.Id.Pipe });692 try stack.append(State { .ExpectToken = Token.Id.Pipe });
678 try stack.append(State {693 try stack.append(State {
679 .ExpectTokenSave = ExpectTokenSave {694 .ExpectTokenSave = ExpectTokenSave {
...@@ -1910,7 +1925,7 @@ pub const Parser = struct {...@@ -1910,7 +1925,7 @@ pub const Parser = struct {
1910 .base = self.initNode(ast.Node.Id.ControlFlowExpression),1925 .base = self.initNode(ast.Node.Id.ControlFlowExpression),
1911 .ltoken = *ltoken,1926 .ltoken = *ltoken,
1912 .kind = *kind,1927 .kind = *kind,
1913 .rhs = undefined,1928 .rhs = null,
1914 };1929 };
1915 return node;1930 return node;
1916 }1931 }
...@@ -2067,7 +2082,9 @@ pub const Parser = struct {...@@ -2067,7 +2082,9 @@ pub const Parser = struct {
2067 while (stack.popOrNull()) |state| {2082 while (stack.popOrNull()) |state| {
2068 switch (state) {2083 switch (state) {
2069 State.Optional => |revert| {2084 State.Optional => |revert| {
2070 *self = state.Optional;2085 *self = revert.parser;
2086 *self.tokenizer = revert.tokenizer;
2087 *revert.ptr = null;
2071 return;2088 return;
2072 },2089 },
2073 State.Required => {2090 State.Required => {
...@@ -2359,7 +2376,7 @@ pub const Parser = struct {...@@ -2359,7 +2376,7 @@ pub const Parser = struct {
23592376
2360 if (prefix_op_node.op == ast.NodeInfixOp.InfixOp.Catch) {2377 if (prefix_op_node.op == ast.NodeInfixOp.InfixOp.Catch) {
2361 if (prefix_op_node.op.Catch) |payload| {2378 if (prefix_op_node.op.Catch) |payload| {
2362 try stack.append(RenderState { .Text = "|" });2379 try stack.append(RenderState { .Text = "| " });
2363 try stack.append(RenderState { .Expression = &payload.base });2380 try stack.append(RenderState { .Expression = &payload.base });
2364 try stack.append(RenderState { .Text = "|" });2381 try stack.append(RenderState { .Text = "|" });
2365 }2382 }
...@@ -2956,6 +2973,10 @@ test "zig fmt: return" {...@@ -2956,6 +2973,10 @@ test "zig fmt: return" {
2956 \\ return 0;2973 \\ return 0;
2957 \\}2974 \\}
2958 \\2975 \\
2976 \\fn bar() void {
2977 \\ return;
2978 \\}
2979 \\
2959 );2980 );
2960}2981}
29612982