From 0c8d894196d74a2afac9251a263b769946632896 Mon Sep 17 00:00:00 2001 From: Hackerpilot Date: Mon, 12 Aug 2013 18:32:09 +0000 Subject: [PATCH] Some changes necessary for DCD --- stdx/d/ast.d | 35 +++++++++++++++++++++++++++++++++++ stdx/d/parser.d | 49 ++++++++++++++++++++++++++++++++++--------------- 2 files changed, 69 insertions(+), 15 deletions(-) diff --git a/stdx/d/ast.d b/stdx/d/ast.d index 57fa255..bda1001 100644 --- a/stdx/d/ast.d +++ b/stdx/d/ast.d @@ -18,6 +18,9 @@ module stdx.d.ast; import stdx.d.lexer; import std.traits; +import std.algorithm; +import std.array; +import std.string; // TODO: Many of these classes can be simplified by using std.variant.Algebraic @@ -886,6 +889,7 @@ public: /** */ Constraint constraint; /** */ MemberFunctionAttribute[] memberFunctionAttributes; /** */ TemplateParameters templateParameters; + /** */ size_t location; } /// @@ -1083,6 +1087,16 @@ public: mixin (visitIfNotNull!(enumMembers)); } /** */ EnumMember[] enumMembers; + + /** + * Byte position of the opening brace + */ + size_t startLocation; + + /** + * Byte position of the closing brace + */ + size_t endLocation; } /// @@ -1878,6 +1892,20 @@ public: { mixin (visitIfNotNull!(type, name, default_)); } + + override string toString() + { + if (vararg) + return "..."; + string rVal; + if (type !is null) + rVal = type.toString() ~ " "; + if (name.type != TokenType.invalid) + rVal ~= name.value; + rVal ~= parameterAttributes.map!(x => " " ~ getTokenValue(x)).join(); + return rVal; + } + /** */ TokenType[] parameterAttributes; /** */ Type type; /** */ Token name; @@ -1893,6 +1921,13 @@ public: { mixin (visitIfNotNull!(parameters)); } + + override string toString() + { + if (hasVarargs) + return "(...)"; + return format("(%s)", parameters.map!"a.toString"().join(", ").array().idup); + } /** */ Parameter[] parameters; /** */ bool hasVarargs; } diff --git a/stdx/d/parser.d b/stdx/d/parser.d index 614fad0..aae916d 100644 --- a/stdx/d/parser.d +++ b/stdx/d/parser.d @@ -1434,8 +1434,10 @@ class ClassFour(A, B) if (someTest()) : Super {}}c; Constructor parseConstructor() { mixin(traceEnterAndExit!(__FUNCTION__)); - auto node = new Constructor; - if (expect(TokenType.this_) is null) return null; + Constructor node = new Constructor; + auto t = expect(TokenType.this_); + if (t is null) return null; + node.location = t.startIndex; auto p = peekPastParens(); bool isTemplate = false; if (p !is null && p.type == TokenType.lParen) @@ -1953,10 +1955,12 @@ class ClassFour(A, B) if (someTest()) : Super {}}c; EnumBody parseEnumBody() { mixin(traceEnterAndExit!(__FUNCTION__)); - auto node = new EnumBody; + EnumBody node = new EnumBody; if (!currentIs(TokenType.semicolon)) { - expect (TokenType.lBrace); + auto open = expect (TokenType.lBrace); + if (open is null) goto ret; + node.startLocation = open.startIndex; while (moreTokens()) { if (!currentIsOneOf(TokenType.comma, TokenType.rBrace)) @@ -1974,7 +1978,9 @@ class ClassFour(A, B) if (someTest()) : Super {}}c; goto ret; } } - expect (TokenType.rBrace); + auto close = expect (TokenType.rBrace); + if (close !is null) + node.endLocation = close.startIndex; } ret: return node; @@ -4532,10 +4538,16 @@ q{(int a, ...) { mixin(traceEnterAndExit!(__FUNCTION__)); auto node = new StructBody; - node.startLocation = expect(TokenType.lBrace).startIndex; + auto start = expect(TokenType.lBrace); + if (start !is null) node.startLocation = start.startIndex; while (!currentIs(TokenType.rBrace) && moreTokens()) - node.declarations ~= parseDeclaration(); - node.endLocation = expect(TokenType.rBrace).startIndex; + { + auto dec = parseDeclaration(); + if (dec !is null) + node.declarations ~= dec; + } + auto end = expect(TokenType.rBrace); + if (end !is null) node.endLocation = end.startIndex; return node; } @@ -5902,6 +5914,19 @@ q{doStuff(5)}c; */ void function(string, int, int, string) messageFunction; + bool isSliceExpression() + { + mixin(traceEnterAndExit!(__FUNCTION__)); + if (startsWith(TokenType.lBracket, TokenType.rBracket)) + return true; + return hasMagicDelimiter!(TokenType.slice)(); + } + + void setTokens(const(Token)[] tokens) + { + this.tokens = tokens; + } + private: bool isCastQualifier() const @@ -5926,13 +5951,7 @@ private: return hasMagicDelimiter!(TokenType.colon)(); } - bool isSliceExpression() - { - mixin(traceEnterAndExit!(__FUNCTION__)); - if (startsWith(TokenType.lBracket, TokenType.rBracket)) - return true; - return hasMagicDelimiter!(TokenType.slice)(); - } + bool hasMagicDelimiter(alias T)() {