From d790670f4c11687fea50c2fd302ca8a815b32d68 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 14 Feb 2018 13:43:05 -0500 Subject: [PATCH] self hosted parser: support string literals --- std/zig/ast.zig | 11 +++++++++++ std/zig/parser.zig | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/std/zig/ast.zig b/std/zig/ast.zig index 60824b22b8..b6b091973d 100644 --- a/std/zig/ast.zig +++ b/std/zig/ast.zig @@ -18,6 +18,7 @@ pub const Node = struct { PrefixOp, IntegerLiteral, FloatLiteral, + StringLiteral, BuiltinCall, }; @@ -33,6 +34,7 @@ pub const Node = struct { Id.PrefixOp => @fieldParentPtr(NodePrefixOp, "base", base).iterate(index), Id.IntegerLiteral => @fieldParentPtr(NodeIntegerLiteral, "base", base).iterate(index), Id.FloatLiteral => @fieldParentPtr(NodeFloatLiteral, "base", base).iterate(index), + Id.StringLiteral => @fieldParentPtr(NodeStringLiteral, "base", base).iterate(index), Id.BuiltinCall => @fieldParentPtr(NodeBuiltinCall, "base", base).iterate(index), }; } @@ -271,3 +273,12 @@ pub const NodeBuiltinCall = struct { return null; } }; + +pub const NodeStringLiteral = struct { + base: Node, + token: Token, + + pub fn iterate(self: &NodeStringLiteral) ?&Node { + return null; + } +}; diff --git a/std/zig/parser.zig b/std/zig/parser.zig index 3f74a4affb..079a331c6d 100644 --- a/std/zig/parser.zig +++ b/std/zig/parser.zig @@ -314,6 +314,18 @@ pub const Parser = struct { try stack.append(State {.ExpectToken = Token.Id.LParen }); continue; }, + Token.Id.StringLiteral => { + const node = try arena.create(ast.NodeStringLiteral); + *node = ast.NodeStringLiteral { + .base = ast.Node {.id = ast.Node.Id.StringLiteral}, + .token = token, + }; + try stack.append(State { + .Operand = &node.base + }); + try stack.append(State.AfterOperand); + continue; + }, else => return self.parseError(token, "expected primary expression, found {}", @tagName(token.id)), } }, @@ -1026,11 +1038,28 @@ pub const Parser = struct { const float_literal = @fieldParentPtr(ast.NodeFloatLiteral, "base", base); try stream.print("{}", self.tokenizer.getTokenSlice(float_literal.token)); }, + ast.Node.Id.StringLiteral => { + const string_literal = @fieldParentPtr(ast.NodeStringLiteral, "base", base); + try stream.print("{}", self.tokenizer.getTokenSlice(string_literal.token)); + }, ast.Node.Id.BuiltinCall => { const builtin_call = @fieldParentPtr(ast.NodeBuiltinCall, "base", base); - try stream.print("{}()", self.tokenizer.getTokenSlice(builtin_call.builtin_token)); + try stream.print("{}(", self.tokenizer.getTokenSlice(builtin_call.builtin_token)); + try stack.append(RenderState { .Text = ")"}); + var i = builtin_call.params.len; + while (i != 0) { + i -= 1; + const param_node = builtin_call.params.at(i); + try stack.append(RenderState { .Expression = param_node}); + if (i != 0) { + try stack.append(RenderState { .Text = ", " }); + } + } }, - else => unreachable, + ast.Node.Id.Root, + ast.Node.Id.VarDecl, + ast.Node.Id.FnProto, + ast.Node.Id.ParamDecl => unreachable, }, RenderState.FnProtoRParen => |fn_proto| { try stream.print(")"); @@ -1141,6 +1170,7 @@ fn testCanonical(source: []const u8) !void { test "zig fmt" { try testCanonical( + \\const std = @import("std"); \\const std = @import(); \\ );