authorgravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2019-06-20 22:06:20-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-19 17:11:58-04:00
log4708fb23c0d96de171d173e252689d26cd01b102
tree55bb810c4b4c795f87b100b91d420f11e3c344d3
parentd4ff27180b31d7a4a5c284453b37d5301aece06d
signaturelock-open Commit is signed but in an unrecognized format.

Generate parse error from &&


2 files changed, 17 insertions(+), 1 deletions(-)

std/zig/ast.zig+4
......@@ -113,6 +113,7 @@ pub const Tree = struct {
113113
114114pub const Error = union(enum) {
115115 InvalidToken: InvalidToken,
116 InvalidAmpersandAmpersand: InvalidAmpersandAmpersand,
116117 ExpectedContainerMembers: ExpectedContainerMembers,
117118 ExpectedStringLiteral: ExpectedStringLiteral,
118119 ExpectedIntegerLiteral: ExpectedIntegerLiteral,
......@@ -161,6 +162,7 @@ pub const Error = union(enum) {
161162 switch (self.*) {
162163 // TODO https://github.com/ziglang/zig/issues/683
163164 @TagType(Error).InvalidToken => |*x| return x.render(tokens, stream),
165 @TagType(Error).InvalidAmpersandAmpersand => |*x| return x.render(tokens, stream),
164166 @TagType(Error).ExpectedContainerMembers => |*x| return x.render(tokens, stream),
165167 @TagType(Error).ExpectedStringLiteral => |*x| return x.render(tokens, stream),
166168 @TagType(Error).ExpectedIntegerLiteral => |*x| return x.render(tokens, stream),
......@@ -211,6 +213,7 @@ pub const Error = union(enum) {
211213 switch (self.*) {
212214 // TODO https://github.com/ziglang/zig/issues/683
213215 @TagType(Error).InvalidToken => |x| return x.token,
216 @TagType(Error).InvalidAmpersandAmpersand => |x| return x.token,
214217 @TagType(Error).ExpectedContainerMembers => |x| return x.token,
215218 @TagType(Error).ExpectedStringLiteral => |x| return x.token,
216219 @TagType(Error).ExpectedIntegerLiteral => |x| return x.token,
......@@ -291,6 +294,7 @@ pub const Error = union(enum) {
291294 pub const ExpectedDerefOrUnwrap = SingleTokenError("Expected pointer dereference or optional unwrap, found {}");
292295 pub const ExpectedSuffixOp = SingleTokenError("Expected pointer dereference, optional unwrap, or field access, found {}");
293296
297 pub const InvalidAmpersandAmpersand = SimpleError("Invalid token '&&', 'and' performs boolean AND");
294298 pub const ExpectedParamType = SimpleError("Expected parameter type");
295299 pub const ExpectedPubItem = SimpleError("Pub must be followed by fn decl, var decl, or container member");
296300 pub const UnattachedDocComment = SimpleError("Unattached documentation comment");
std/zig/parse.zig+13-1
......@@ -774,7 +774,7 @@ fn parseBoolAndExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node
774774 arena,
775775 it,
776776 tree,
777 SimpleBinOpParseFn(.Keyword_and, Node.InfixOp.Op.BoolAnd),
777 parseAmpersandAmpersandOp,
778778 parseCompareExpr,
779779 .Infinitely,
780780 );
......@@ -2687,6 +2687,18 @@ fn SimpleBinOpParseFn(comptime token: Token.Id, comptime op: Node.InfixOp.Op) No
26872687
26882688// Helper parsers not included in the grammar
26892689
2690fn parseAmpersandAmpersandOp(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
2691 const op_parse_and = SimpleBinOpParseFn(.Keyword_and, Node.InfixOp.Op.BoolAnd);
2692 const op_token = eatToken(it, .AmpersandAmpersand);
2693 if (op_token != null) {
2694 try tree.errors.push(AstError{
2695 .InvalidAmpersandAmpersand = AstError.InvalidAmpersandAmpersand{ .token = it.index },
2696 });
2697 return error.ParseError;
2698 }
2699 return op_parse_and(arena, it, tree);
2700}
2701
26902702fn parseBuiltinCall(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
26912703 const token = eatToken(it, .Builtin) orelse return null;
26922704 const params = (try parseFnCallArguments(arena, it, tree)) orelse {