From 22a62bfe841c90936e5e240678b2dea5450101c2 Mon Sep 17 00:00:00 2001 From: Hackerpilot Date: Tue, 13 Nov 2012 12:49:02 -0800 Subject: [PATCH] Added alias declarations to modules, structs, classes, etc. Fixed issue #15 --- parser.d | 39 +++++++++++++++++++++++++++++++++++++-- types.d | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/parser.d b/parser.d index 1b62fc7..42fb723 100644 --- a/parser.d +++ b/parser.d @@ -272,7 +272,10 @@ Module parseModule(const Token[] tokens, string protection = "public", string[] tokens.skipBlockStatement(index); break; case TokenType.Alias: - tokens.skipBlockStatement(index); + Alias a = parseAlias(tokens, index, + localProtection.empty() ? protection : localProtection, + attributes); + mod.aliases ~= a; break; case TokenType.Import: mod.imports ~= parseImports(tokens, index); @@ -511,9 +514,9 @@ in } body { - ++index; Enum e = new Enum; e.line = tokens[index].lineNumber; + ++index; string enumType; if (tokens[index] == TokenType.LBrace) @@ -773,6 +776,7 @@ void parseStructBody(const Token[] tokens, ref size_t index, Struct st) st.bodyEnd = tokens[index - 1].startIndex; st.functions.insertInPlace(0, m.functions); st.variables.insertInPlace(0, m.variables); + st.aliases.insertInPlace(0, m.aliases); } @@ -877,3 +881,34 @@ body { return parseInherits(tokens, ++index, protection, attributes); } + + +/** + * Parse an alias declaration. + * Note that the language spec mentions a "AliasInitializerList" in the grammar, + * but there seems to be no example of this being used, nor has the compiler + * accepted any of my attempts to create one. Therefore, it's not supported here + */ +Alias parseAlias(const Token[] tokens, ref size_t index, string protection, + string[] attributes) +in +{ + assert(tokens[index] == TokenType.Alias); +} +body +{ + index++; + Alias a = new Alias; + a.aliasedType = parseTypeDeclaration(tokens, index); + a.attributes = attributes; + a.protection = protection; + if (tokens[index] == TokenType.Identifier) + { + a.name = tokens[index].value; + a.line = tokens[index].lineNumber; + skipBlockStatement(tokens, index); + } + else + return null; + return a; +} diff --git a/types.d b/types.d index f70dc50..b52dddb 100644 --- a/types.d +++ b/types.d @@ -33,7 +33,10 @@ unittest { assert(escapeJSON("abc\"def") == "abc\\\"def"); } */ void writeJSONString(File f, const string name, const string value, uint indent = 0) { - f.write(std.array.replicate(" ", indent), "\"", name, "\" : \"", escapeJSON(value), "\""); + if (value is null) + f.write(std.array.replicate(" ", indent), "\"", name, "\" : null"); + else + f.write(std.array.replicate(" ", indent), "\"", name, "\" : \"", escapeJSON(value), "\""); } /** @@ -98,6 +101,26 @@ protected: } } + +/** + * Alias Declaration + */ +class Alias : Base +{ +public: + + string aliasedType; + +protected: + + override void printMembers(File f, uint indent = 0) const + { + super.printMembers(f, indent); + f.writeln(","); + writeJSONString(f, "aliasedType", aliasedType, indent); + } +} + /** * Varible declaration */ @@ -156,6 +179,9 @@ public: /// List of member variables; may be empty Variable[] variables; + /// List of aliases defined + Alias[] aliases; + /// Source code character position of the beginning of the struct body size_t bodyStart; @@ -228,6 +254,15 @@ protected: else f.writeln(); } + f.writeln(std.array.replicate(" ", indent), "],\n", std.array.replicate(" ", indent), "\"aliases\" : ["); + foreach(i, al; aliases) + { + al.writeJSONTo(f, indent); + if (i + 1 < aliases.length) + f.writeln(","); + else + f.writeln(); + } f.write(std.array.replicate(" ", indent), "]"); } } @@ -405,6 +440,9 @@ public: /// List of enums declared in this module Enum[] enums; + + /// List of aliases declared in this module + Alias[] aliases; } /** @@ -506,6 +544,15 @@ public: else f.writeln(); } + f.writeln(" ],\n \"aliases\" : ["); + foreach(i, a; aliases) + { + a.writeJSONTo(f, indent + 1); + if (i + 1 < aliases.length) + f.writeln(","); + else + f.writeln(); + } f.writeln(" ]\n}"); } @@ -554,6 +601,7 @@ public: } } + immutable(string[][string]) typeProperties; immutable(string[]) floatProperties; immutable(string[]) integralProperties;