Skip to content

Commit

Permalink
add syntax errors when parsing zon files
Browse files Browse the repository at this point in the history
  • Loading branch information
tiehuis committed Apr 2, 2023
1 parent 1de64db commit 4a3a383
Show file tree
Hide file tree
Showing 5 changed files with 505 additions and 145 deletions.
21 changes: 21 additions & 0 deletions lib/std/zig/Ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ tokens: TokenList.Slice,
/// references to the root node, this means 0 is available to indicate null.
nodes: NodeList.Slice,
extra_data: []Node.Index,
mode: Mode,

errors: []const Error,

Expand Down Expand Up @@ -100,6 +101,7 @@ pub fn parse(gpa: Allocator, source: [:0]const u8, mode: Mode) Allocator.Error!A
.nodes = parser.nodes.toOwnedSlice(),
.extra_data = try parser.extra_data.toOwnedSlice(gpa),
.errors = try parser.errors.toOwnedSlice(gpa),
.mode = mode,
};
}

Expand Down Expand Up @@ -445,6 +447,20 @@ pub fn renderError(tree: Ast, parse_error: Error, stream: anytype) !void {
}),
}
},

.expected_zon_literal => {
return stream.writeAll("zon only supports 'true', 'false', 'undefined' or 'null' simple literals");
},
.expected_zon_expr => {
return stream.writeAll("zon top-level must be an anonymous struct literal, anonymous tuple, anonymous enum or simple expression");
},
.unexpected_zon_minus => {
return stream.writeAll("zon only supports single minus on integer or float literals");
},
.unexpected_zon_syntax => {
const found_tag = token_tags[parse_error.token + @boolToInt(parse_error.token_is_prev)];
return stream.print("unexpected zon token: '{s}'\n", .{found_tag.symbol()});
},
}
}

Expand Down Expand Up @@ -2902,6 +2918,11 @@ pub const Error = struct {

/// `expected_tag` is populated.
expected_token,

expected_zon_literal,
expected_zon_expr,
unexpected_zon_minus,
unexpected_zon_syntax,
};
};

Expand Down
Loading

0 comments on commit 4a3a383

Please sign in to comment.