From 270cd6d9a1196ea802e79958605c9705c5ea22d4 Mon Sep 17 00:00:00 2001 From: Hackerpilot Date: Fri, 16 Aug 2013 23:48:51 +0000 Subject: [PATCH] Some parser fixes for DCD --- astprinter.d | 2 +- main.d | 2 +- stats.d | 11 +++-------- stdx/d/ast.d | 9 +++++++-- stdx/d/parser.d | 25 ++++++++++++++++++------- 5 files changed, 30 insertions(+), 19 deletions(-) diff --git a/astprinter.d b/astprinter.d index 024fb19..f10c44f 100644 --- a/astprinter.d +++ b/astprinter.d @@ -489,7 +489,7 @@ class XMLPrinter : ASTVisitor override void visit(ForeachStatement foreachStatement) { output.writeln(""); + foreachStatement.type), "\">"); output.writeln(""); } diff --git a/main.d b/main.d index 9a2efd0..7a90740 100644 --- a/main.d +++ b/main.d @@ -123,7 +123,7 @@ int main(string[] args) } else if (tokenCount) { - printTokenCount(stdout, tokens, to!size_t(f.size)); + printTokenCount(stdout, tokens); } else if (syntaxCheck) { diff --git a/stats.d b/stats.d index 85e2f4d..a50f5e4 100644 --- a/stats.d +++ b/stats.d @@ -6,6 +6,7 @@ module stats; import std.stdio; +import std.algorithm; import stdx.d.lexer; pure nothrow bool isLineOfCode(TokenType t) @@ -29,15 +30,9 @@ pure nothrow bool isLineOfCode(TokenType t) } } -void printTokenCount(Tokens)(File output, ref Tokens tokens, size_t fileSize) +void printTokenCount(Tokens)(File output, ref Tokens tokens) { - ulong count; - while(!tokens.empty) - { - tokens.popFront(); - ++count; - } - output.writefln("%d", count); + output.writefln("%d", tokens.count!(a => true)); } void printLineCount(Tokens)(File output, ref Tokens tokens) diff --git a/stdx/d/ast.d b/stdx/d/ast.d index 2fefd2d..a1c1246 100644 --- a/stdx/d/ast.d +++ b/stdx/d/ast.d @@ -1194,6 +1194,7 @@ public: /** */ ExpressionStatement test; /** */ Expression increment; /** */ StatementNoCaseNoDefault statementNoCaseNoDefault; + /** */ size_t startIndex; } /// @@ -1202,14 +1203,16 @@ class ForeachStatement : ASTNode public: override void accept(ASTVisitor visitor) { - mixin (visitIfNotNull!(foreachTypeList, low, high, + mixin (visitIfNotNull!(foreachType, foreachTypeList, low, high, statementNoCaseNoDefault)); } - /** */ TokenType foreachType; + /** */ TokenType type; /** */ ForeachTypeList foreachTypeList; + /** */ ForeachType foreachType; /** */ Expression low; /** */ Expression high; /** */ StatementNoCaseNoDefault statementNoCaseNoDefault; + /** */ size_t startIndex; } /// @@ -1429,6 +1432,7 @@ public: /** */ Expression expression; /** */ DeclarationOrStatement thenStatement; /** */ DeclarationOrStatement elseStatement; + /** */ size_t startIndex; } /// @@ -2852,6 +2856,7 @@ public: /** */ Expression expression; /** */ StatementNoCaseNoDefault statementNoCaseNoDefault; + /** */ size_t startIndex; } /// diff --git a/stdx/d/parser.d b/stdx/d/parser.d index 6487f15..31a7ef2 100644 --- a/stdx/d/parser.d +++ b/stdx/d/parser.d @@ -2142,6 +2142,7 @@ class ClassFour(A, B) if (someTest()) : Super {}}c; mixin(traceEnterAndExit!(__FUNCTION__)); auto node = new ForStatement; if (expect(TokenType.for_) is null) return null; + node.startIndex = current().startIndex; if (expect(TokenType.lParen) is null) return null; if (currentIs(TokenType.semicolon)) @@ -2174,17 +2175,19 @@ class ClassFour(A, B) if (someTest()) : Super {}}c; ForeachStatement parseForeachStatement() { mixin(traceEnterAndExit!(__FUNCTION__)); - auto node = new ForeachStatement; + ForeachStatement node = new ForeachStatement; if (currentIsOneOf(TokenType.foreach_, TokenType.foreach_reverse_)) - node.foreachType = advance().type; + node.type = advance().type; else { error(`"foreach" or "foreach_reverse" expected`); return null; } + node.startIndex = current().startIndex; if (expect(TokenType.lParen) is null) return null; - auto feType = parseForeachTypeList(); + ForeachTypeList feType = parseForeachTypeList(); bool canBeRange = feType.items.length == 1; + if (expect(TokenType.semicolon) is null) return null; node.low = parseExpression(); if (node.low is null) return null; @@ -2197,8 +2200,13 @@ class ClassFour(A, B) if (someTest()) : Super {}}c; } advance(); node.high = parseExpression(); + node.foreachType = feType.items[0]; if (node.high is null) return null; } + else + { + node.foreachTypeList = feType; + } if (expect(TokenType.rParen) is null) return null; node.statementNoCaseNoDefault = parseStatementNoCaseNoDefault(); if (node.statementNoCaseNoDefault is null) return null; @@ -2653,6 +2661,7 @@ body {} // six mixin(traceEnterAndExit!(__FUNCTION__)); auto node = new IfStatement; if (expect(TokenType.if_) is null) return null; + node.startIndex = current().startIndex; if (expect(TokenType.lParen) is null) return null; if (currentIs(TokenType.auto_)) @@ -2687,6 +2696,7 @@ body {} // six } if (expect(TokenType.rParen) is null) return null; + if (currentIs(TokenType.rBrace)) return node; // this line makes DCD better node.thenStatement = parseDeclarationOrStatement(); if (currentIs(TokenType.else_)) { @@ -5854,6 +5864,7 @@ q{doStuff(5)}c; mixin(traceEnterAndExit!(__FUNCTION__)); auto node = new WhileStatement; expect(TokenType.while_); + node.startIndex = current().startIndex; expect(TokenType.lParen); node.expression = parseExpression(); expect(TokenType.rParen); @@ -6393,7 +6404,7 @@ private: { if (index >= tokens.length) return false; - for (size_t i = 0; i != types.length; ++i) + for (size_t i = 0; (i < types.length) && ((index + i) < tokens.length); ++i) { if (tokens[index + i].type != types[i]) return false; @@ -6461,9 +6472,9 @@ private: void trace(lazy string message) {} } - static immutable string BASIC_TYPE_CASE_RANGE = q{case bool_: .. case wchar_:}; - static immutable string LITERAL_CASE_RANGE = q{case doubleLiteral: .. case wstringLiteral:}; - static immutable string SPECIAL_CASE_RANGE = q{case specialDate: .. case specialPrettyFunction:}; + enum string BASIC_TYPE_CASE_RANGE = q{case bool_: .. case wchar_:}; + enum string LITERAL_CASE_RANGE = q{case doubleLiteral: .. case wstringLiteral:}; + enum string SPECIAL_CASE_RANGE = q{case specialDate: .. case specialPrettyFunction:}; const(Token)[] tokens; int suppressMessages; size_t index;