From 2088089e368e5621673bf83472e441b28c43c81a Mon Sep 17 00:00:00 2001 From: Hackerpilot Date: Sun, 1 Sep 2013 13:53:36 +0000 Subject: [PATCH] Moved string formatting out of the AST classes --- astprinter.d | 7 +++- build.sh | 2 +- stdx/d/ast.d | 112 --------------------------------------------------- 3 files changed, 7 insertions(+), 114 deletions(-) diff --git a/astprinter.d b/astprinter.d index f10c44f..b969aa0 100644 --- a/astprinter.d +++ b/astprinter.d @@ -7,6 +7,8 @@ import stdx.d.lexer; import stdx.d.ast; import std.stdio; import std.string; +import std.array; +import formatter; template tagAndAccept(string tagName) { @@ -1285,7 +1287,10 @@ class XMLPrinter : ASTVisitor override void visit(Type type) { - output.writeln(""); + auto app = appender!string(); + auto formatter = new Formatter!(typeof(app))(app); + formatter.format(type); + output.writeln(""); type.accept(this); output.writeln(""); } diff --git a/build.sh b/build.sh index af69433..312fd1c 100755 --- a/build.sh +++ b/build.sh @@ -1,5 +1,5 @@ #dmd *.d stdx/d/*.d -release -inline -noboundscheck -O -w -wi -m64 -property -ofdscanner-dmd -dmd main.d stats.d imports.d highlighter.d ctags.d astprinter.d stdx/d/*.d -g -m64 -wi -ofdscanner +dmd main.d stats.d imports.d highlighter.d ctags.d astprinter.d formatter.d stdx/d/*.d -g -m64 -wi -ofdscanner #ldc2 -O3 *.d stdx/d/*.d -of=dscanner-ldc -release -m64 #ldc2 *.d stdx/d/*.d -of=dscanner -unittest -m64 -g #/opt/gdc/bin/gdc -O3 -odscanner-gdc -fno-bounds-check -frelease -m64 *.d stdx/d/*.d diff --git a/stdx/d/ast.d b/stdx/d/ast.d index a1c1246..e8446e4 100644 --- a/stdx/d/ast.d +++ b/stdx/d/ast.d @@ -1370,19 +1370,6 @@ public: mixin (visitIfNotNull!(identifiersOrTemplateInstances)); } - override string toString() - { - string rVal; - bool first = true; - foreach (iot; identifiersOrTemplateInstances) - { - if (!first) - rVal ~= "."; - first = true; - rVal ~= iot.toString(); - } - return rVal; - } /** */ IdentifierOrTemplateInstance[] identifiersOrTemplateInstances; } @@ -1395,13 +1382,6 @@ public: mixin (visitIfNotNull!(identifier, templateInstance)); } - override string toString() - { - if (identifier.type == TokenType.identifier) - return identifier.value; - else - return templateInstance.toString(); - } /** */ Token identifier; /** */ TemplateInstance templateInstance; } @@ -1897,19 +1877,6 @@ public: mixin (visitIfNotNull!(type, name, default_)); } - override string toString() - { - string rVal; - rVal ~= parameterAttributes.map!(x => " " ~ getTokenValue(x)).join(); - if (type !is null) - rVal = type.toString(); - if (vararg) - rVal ~= "..."; - if (name.type != TokenType.invalid) - rVal ~= " " ~ name.value; - return rVal; - } - /** */ TokenType[] parameterAttributes; /** */ Type type; /** */ Token name; @@ -1926,12 +1893,6 @@ public: mixin (visitIfNotNull!(parameters)); } - override string toString() - { - if (hasVarargs) - return "(...)"; - return format("(%s)", parameters.map!"a.toString"().join(", ").array().idup); - } /** */ Parameter[] parameters; /** */ bool hasVarargs; } @@ -2313,13 +2274,6 @@ public: mixin (visitIfNotNull!(dot, identifierOrTemplateChain)); } - override string toString() - { - if (dot != TokenType.invalid) - return "." ~ identifierOrTemplateChain.toString(); - else - return identifierOrTemplateChain.toString(); - } /** */ IdentifierOrTemplateChain identifierOrTemplateChain; /** */ Token dot; } @@ -2598,27 +2552,6 @@ public: mixin (visitIfNotNull!(type2, typeSuffixes)); } - override string toString() - { - string result; - bool first = true; - foreach (constructor; typeConstructors) - { - if (!first) - result ~= " "; - first = false; - result ~= getTokenValue(constructor); - } - if (typeConstructors.length > 0) - result ~= " "; - result ~= type2.toString(); - foreach (suffix; typeSuffixes) - { - result ~= suffix.toString(); - } - return result; - } - /** */ TokenType[] typeConstructors; /** */ TypeSuffix[] typeSuffixes; /** */ Type2 type2; @@ -2634,23 +2567,6 @@ public: identifierOrTemplateChain, type)); } - override string toString() - { - if (symbol !is null) - { - return symbol.toString(); - } - else if (typeofExpression !is null) - { - return ""; - } - else if (typeConstructor != TokenType.invalid) - { - return getTokenValue(typeConstructor) ~ "(" ~ type.toString() ~ ")"; - } - else - return getTokenValue(builtinType); - } /** */ TokenType builtinType; /** */ Symbol symbol; /** */ TypeofExpression typeofExpression; @@ -2681,34 +2597,6 @@ public: memberFunctionAttributes)); } - override string toString() - { - if (star) - return "*"; - else if (array) - { - if (type is null) - { - if (low is null) - return "[]"; - else - { - if (high is null) - return "[" ~ low.toString() ~ "]"; - else - return "[" ~ low.toString() ~ ".." ~ high.toString() ~ "]"; - } - } - else - return "[" ~ type.toString() ~ "]"; - } - else - { - // TODO - return " " ~ delegateOrFunction.value ~ "()"; - } - } - /** */ Token delegateOrFunction; /** */ bool star; /** */ bool array;