From 1bea958fedb9d58f17b64e1800e8ab8f31c63157 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Fri, 18 Dec 2020 04:12:10 +0100 Subject: [PATCH] Reading property values. --- src/parse.zig | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/src/parse.zig b/src/parse.zig index 99c61f4..492a417 100644 --- a/src/parse.zig +++ b/src/parse.zig @@ -189,6 +189,7 @@ const Parser = struct { // Propagate memory errors. error.OutOfMemory => { return (error.OutOfMemory); }, error.InvalidCharacter => continue, + error.NoSpaceLeft => continue, error.Overflow => continue, error.ParseError => continue, }; // |normal_value| { stuff; to; do; } @@ -201,6 +202,7 @@ const Parser = struct { // Propagate memory errors. error.OutOfMemory => { return (error.OutOfMemory); }, error.InvalidCharacter => continue, + error.NoSpaceLeft => continue, error.Overflow => continue, error.ParseError => { p.say("we catched a ParseError on token: {}\n" @@ -376,11 +378,13 @@ const Parser = struct { .StringLiteral => { return PropertyValue{.string = p.giveTokenContent(token)}; }, + .IntegerLiteral => { return PropertyValue{ .integer = try std.fmt.parseInt(u64, p.giveTokenContent(token), 10) }; }, + .FloatLiteral => { // p.say("property: {} {} = {}\n" // , .{p.giveTokenContent(class_name) @@ -390,13 +394,17 @@ const Parser = struct { .float = try std.fmt.parseFloat(f64, p.giveTokenContent(token)) }; }, + .Identifier => { // Loop over identifier and points. - return error.ParseError; - // p.putBackToken(token); - // // TODO: do the loop - // const string: []const u8 = p.giveStringFromReference(); + p.putBackToken(token); + const string = try p.parseReference(); + p.say("parsed reference: {}\n", .{string}); + return PropertyValue{ + .reference = string + }; }, + else => { return error.ParseError; } @@ -404,6 +412,37 @@ const Parser = struct { } } + // Get the representation of the reference in a simple string. + fn parseReference(p: *Parser) ![]u8 { + + var representation: [100]u8 = undefined; + var fbs = std.io.fixedBufferStream(representation[0..]); + + // First part of the reference has to be an Identifier. + const id = try p.expectToken(.Identifier); + try std.fmt.format(fbs.writer(), "{}", .{p.giveTokenContent(id)}); + + while (true) { + const token = p.nextToken(); + switch (p.token_ids[token]) { + + .Period => { + p.say("reading a point\n", .{}); + // Following token is expected to be an Identifier. + const following = try p.expectToken(.Identifier); + try std.fmt.format(fbs.writer(), ".{}", .{p.giveTokenContent(following)}); + }, + + else => { + p.putBackToken(token); + break; + }, + } + } + + return representation[0..fbs.pos]; + } + fn parseProperty(p: *Parser) !void { const property = try p.expectToken(.Keyword_property); const class_name = try p.expectToken(.Identifier);