From ac5d29819c238eefc45d8b43cd40eda6ad5ae4de Mon Sep 17 00:00:00 2001 From: Luna Date: Mon, 26 Aug 2019 21:43:58 -0300 Subject: [PATCH] split access modifier parsing into own func - revamp parsing of access modifiers so it is correct --- examples/hello.v | 13 ++++++++++ src/ast_printer.zig | 4 +++ src/parser.zig | 59 +++++++++++++++++++++++++++++++++++---------- 3 files changed, 63 insertions(+), 13 deletions(-) diff --git a/examples/hello.v b/examples/hello.v index 377d337..3d16d61 100644 --- a/examples/hello.v +++ b/examples/hello.v @@ -54,3 +54,16 @@ fn main(a int) int { } fn (v Typ) voidfunc() {} + +struct Foo { + a int +mut: + b int + c int +pub: + d int +pub mut: + e int +pub mut mut: + f int +} diff --git a/src/ast_printer.zig b/src/ast_printer.zig index 3a0ed4f..25f90b4 100644 --- a/src/ast_printer.zig +++ b/src/ast_printer.zig @@ -107,6 +107,10 @@ pub fn printNode(node: *Node, ident: usize) void { std.debug.warn("pub "); } + if (field.mutable_outside) { + std.debug.warn("MUT_OUT "); + } + std.debug.warn("{} {})\n", field.name.lexeme, field.typ.lexeme); } print(ident, "))\n"); diff --git a/src/parser.zig b/src/parser.zig index a9b63c3..9c0d436 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -502,19 +502,7 @@ pub const Parser = struct { var field_state = FieldState{}; while (!self.check(.RightBrace)) { - if (self.check(.Mut)) { - _ = try self.consumeSingle(.Mut); - _ = try self.consumeSingle(.Colon); - if (field_state.mutable) { - field_state.mutable_outside = true; - } else { - field_state.mutable = true; - } - } else if (self.check(.Pub)) { - _ = try self.consumeSingle(.Pub); - _ = try self.consumeSingle(.Colon); - field_state.public = true; - } + try self.parseFieldModifiers(&field_state); const field_name = try self.consumeSingle(.Identifier); const field_type = try self.consumeSingle(.Identifier); @@ -538,6 +526,51 @@ pub const Parser = struct { return Node.mkStructDecl(self.allocator, name, fields); } + fn parseFieldModifiers(self: *@This(), field_state: *FieldState) !void { + + // there are five access modifiers: + // - none (private immutable) + // - mut (private mutable) + // - pub (public immutable) + // - pub mut (public mutable only in module) + // - pub mut mut (public mutable everywhere) + + // this function takes care of that by changing the current FieldState + // to what the modifiers dictate. + switch (self.peek().ttype) { + .Mut => { + // There are no oher modifiers that start with mut, so we + // can just go the way of marking it as mutable + _ = try self.consumeSingle(.Mut); + _ = try self.consumeSingle(.Colon); + + field_state.mutable = true; + }, + + // 'pub', 'pub mut', and 'pub mut mut' are all handled here + .Pub => { + _ = try self.consumeSingle(.Pub); + field_state.public = true; + + if (self.check(.Mut)) { + _ = try self.consumeSingle(.Mut); + + field_state.mutable = true; + if (self.check(.Mut)) { + _ = try self.consumeSingle(.Mut); + field_state.mutable_outside = true; + } + } + + _ = try self.consumeSingle(.Colon); + }, + + // if it isn't mut or pub we're likely in an identifier, just + // ignore it. + else => return, + } + } + fn parseTopDecl(self: *@This()) !*Node { return switch (self.peek().ttype) { .Fn => try self.parseFnDecl(),