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 {
8989 ptr: &Token,
9090 };
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
92101 fn ListState(comptime T: type) type {
93102 return struct {
94103 list: &ArrayList(T),
......@@ -131,7 +140,7 @@ pub const Parser = struct {
131140 /// optional state is found, the parser will revert to the state it was in
132141 /// when the optional was added. This will polute the arena allocator with
133142 /// "leaked" nodes. TODO: Figure out if it's nessesary to handle leaked nodes.
134 Optional: Parser,
143 Optional: RevertState,
135144
136145 /// Optional can be reverted by adding the Required state to the stack.
137146 Required,
......@@ -598,7 +607,13 @@ pub const Parser = struct {
598607 const node = try self.createControlFlowExpr(arena, token, ast.NodeControlFlowExpression.Kind.Return);
599608 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;
602617 try stack.append(State { .Expression = DestPtr { .NullableField = &node.rhs } });
603618 continue;
604619 },
......@@ -673,7 +688,7 @@ pub const Parser = struct {
673688 continue;
674689 }
675690
676 node.op.Catch = try self.createIdentifier(arena, undefined);
691 node.op.Catch = try self.createIdentifier(arena, Token(undefined));
677692 try stack.append(State { .ExpectToken = Token.Id.Pipe });
678693 try stack.append(State {
679694 .ExpectTokenSave = ExpectTokenSave {
......@@ -1910,7 +1925,7 @@ pub const Parser = struct {
19101925 .base = self.initNode(ast.Node.Id.ControlFlowExpression),
19111926 .ltoken = *ltoken,
19121927 .kind = *kind,
1913 .rhs = undefined,
1928 .rhs = null,
19141929 };
19151930 return node;
19161931 }
......@@ -2067,7 +2082,9 @@ pub const Parser = struct {
20672082 while (stack.popOrNull()) |state| {
20682083 switch (state) {
20692084 State.Optional => |revert| {
2070 *self = state.Optional;
2085 *self = revert.parser;
2086 *self.tokenizer = revert.tokenizer;
2087 *revert.ptr = null;
20712088 return;
20722089 },
20732090 State.Required => {
......@@ -2359,7 +2376,7 @@ pub const Parser = struct {
23592376
23602377 if (prefix_op_node.op == ast.NodeInfixOp.InfixOp.Catch) {
23612378 if (prefix_op_node.op.Catch) |payload| {
2362 try stack.append(RenderState { .Text = "|" });
2379 try stack.append(RenderState { .Text = "| " });
23632380 try stack.append(RenderState { .Expression = &payload.base });
23642381 try stack.append(RenderState { .Text = "|" });
23652382 }
......@@ -2956,6 +2973,10 @@ test "zig fmt: return" {
29562973 \\ return 0;
29572974 \\}
29582975 \\
2976 \\fn bar() void {
2977 \\ return;
2978 \\}
2979 \\
29592980 );
29602981}
29612982