Naming: class => node.

mess
Karchnu 2020-12-19 02:58:44 +01:00
parent bf8283b7cf
commit 8a0abd070e
1 changed files with 36 additions and 36 deletions

View File

@ -127,7 +127,7 @@ const Parser = struct {
/// /
// parseContainerMembers: actual parsing code starts here.
fn parseContainerMembers(p: *Parser, top_level: bool) ![]*Node {
std.debug.print("parseContainerMembers: is top? {}\n", .{top_level});
// std.debug.print("parseContainerMembers: is top? {}\n", .{top_level});
// list: all nodes in the ast.
var list = std.ArrayList(*Node).init(p.gpa);
defer list.deinit();
@ -159,10 +159,10 @@ const Parser = struct {
// ClassHdr => ClassHdrFull LBrace ClassCon RBrace |
// ClassHdrSimple LBrace ClassCon RBrace
// ClassHdrSimple: a class without identifier,
// ClassHdrSimple: a node without identifier,
// for a definition or when this won't be used later.
// ClassHdrSimple => Identifier
// ClassHdrFull: a class with an identifier.
// ClassHdrFull: a node with an identifier.
// ClassHdrFull => Identifier LParen Identifier RParen
// ClassCon => ClassHdr | statement ClassCon | nil
@ -196,9 +196,9 @@ const Parser = struct {
},
.Identifier => {
// Identifier => on top level this means a class.
// Identifier => on top level this means a node.
p.putBackToken(token);
p.parseClass() catch |err| switch(err) {
p.parseNode() catch |err| switch(err) {
// Propagate memory errors.
error.OutOfMemory => { return (error.OutOfMemory); },
error.InvalidCharacter => continue,
@ -230,7 +230,7 @@ const Parser = struct {
fn say(p: *Parser, comptime fmt: []const u8, args: anytype) void {
var i = p.indent;
while (i > 0) {
std.debug.print("\t", .{});
std.debug.print(" ", .{});
i-=1;
}
std.debug.print(fmt, args);
@ -244,42 +244,42 @@ const Parser = struct {
std.debug.print("TODO: file required: {}\n", .{file_to_read});
}
// TODO: class definition (inheritance).
// TODO: node definition (inheritance).
// fn parseDefine(p: *Parser) !?Definition {
fn parseDefine(p: *Parser) !void {
const define_token = try p.expectToken(.Keyword_define);
const new_class_name = try p.expectToken(.Identifier);
const parent_class_name = try p.expectToken(.Identifier);
const parent_node_name = try p.expectToken(.Identifier);
std.debug.print("TODO: class inheritance: {} < {}\n",
.{p.giveTokenContent(new_class_name), p.giveTokenContent(parent_class_name)});
std.debug.print("TODO: node inheritance: {} < {}\n",
.{p.giveTokenContent(new_class_name), p.giveTokenContent(parent_node_name)});
p.putBackToken(parent_class_name);
try p.parseClass();
// TODO: get the old class definition,
p.putBackToken(parent_node_name);
try p.parseNode();
// TODO: get the old node definition,
// create a new definition,
// then add old and new properties and children to it.
}
// TODO: class definition (inheritance).
fn parseClass(p: *Parser) !void {
// TODO: node definition (inheritance).
fn parseNode(p: *Parser) !void {
if (p.indent > 0) {
p.say("INNER parseClass\n", .{});
p.say("Child\n", .{});
}
else {
p.say("TOP parseClass\n", .{});
p.say("Node\n", .{});
}
p.indent += 1;
defer { p.indent -= 1; }
const class_name = p.eatToken(.Identifier);
if (class_name == null) {
const node_name = p.eatToken(.Identifier);
if (node_name == null) {
return;
}
// Either simple or full header.
const identifier: ?[] const u8 = try p.parseFullClassHeader();
const identifier: ?[] const u8 = try p.parseFullNodeHeader();
p.say("Reading class: {}", .{p.giveTokenContent(class_name.?)});
p.say("Node: {}", .{p.giveTokenContent(node_name.?)});
if (identifier) |id| {
std.debug.print(", id: {}\n", .{id});
@ -288,27 +288,27 @@ const Parser = struct {
std.debug.print("\n", .{});
}
// Starting the class.
// Starting the node.
// const lbrace = p.nextToken();
const ignored = try p.expectToken(.LBrace);
while (true) {
const token = p.nextToken();
// p.say("within a class, token: {}\n", .{p.giveTokenContent(token)});
// p.say("within a node, token: {}\n", .{p.giveTokenContent(token)});
switch (p.token_ids[token]) {
.Identifier => {
const following = p.nextToken();
switch (p.token_ids[following]) {
// Class header (with or without id).
// Node header (with or without id).
.LParen,
.LBrace => {
p.putBackToken(following);
p.putBackToken(token);
// p.say("reading a new class\n", .{});
// p.say("reading a new node\n", .{});
// WARNING: RECURSION: this may cause errors.
const res = p.parseClass();
const res = p.parseNode();
continue;
},
@ -317,7 +317,7 @@ const Parser = struct {
// p.putBackToken(token);
const value: PropertyValue = try p.parseValue();
p.say("attribute {} value: {}\n"
p.say("attribute {:>20} => {}\n"
, .{ p.giveTokenContent(token)
, value});
// const assignment = try p.parseAssignment();
@ -336,14 +336,14 @@ const Parser = struct {
const colon = p.expectToken(.Colon);
const value: PropertyValue = try p.parseValue();
p.say("attribute {} value: {}\n"
p.say("attribute {:>20} => {}\n"
, .{ p.source[attribute_loc.start..attribute_loc.end]
, value});
},
else => {
// Wasn't expected.
// Couln't understand what was in this class.
// Couln't understand what was in this node.
p.say("did not understand {} then {}\n"
, .{ p.giveTokenContent(token)
, p.giveTokenContent(following)});
@ -367,14 +367,14 @@ const Parser = struct {
else => {
p.putBackToken(token);
p.say("reading {} in a class, backing up\n"
p.say("reading {} in a node, backing up\n"
, .{p.giveTokenContent(token)});
break;
}
}
}
// Class definition or instance ends with a RBrace.
// Node definition or instance ends with a RBrace.
const end_of_class = try p.expectToken(.RBrace);
}
@ -402,7 +402,7 @@ const Parser = struct {
.FloatLiteral => {
// p.say("property: {} {} = {}\n"
// , .{p.giveTokenContent(class_name)
// , .{p.giveTokenContent(node_name)
// , p.giveTokenContent(attribute_name)
// , p.giveTokenContent(id_value)});
return PropertyValue{
@ -490,18 +490,18 @@ const Parser = struct {
fn parseProperty(p: *Parser) !void {
const property = try p.expectToken(.Keyword_property);
const class_name = try p.expectToken(.Identifier);
const node_name = try p.expectToken(.Identifier);
const attribute_name = try p.expectToken(.Identifier);
const colon = try p.expectToken(.Colon);
const value: PropertyValue = try p.parseValue();
p.say("property class {} name {} value: {}\n"
, .{ p.giveTokenContent(class_name)
p.say("property type {} name {} value: {}\n"
, .{ p.giveTokenContent(node_name)
, p.giveTokenContent(attribute_name)
, value});
}
fn parseFullClassHeader(p: *Parser) !?[]const u8 {
fn parseFullNodeHeader(p: *Parser) !?[]const u8 {
if (p.eatToken(.LParen) == null)
return null;