# HG changeset patch # User Aziz K?ksal # Date 1201738230 -3600 # Node ID 140469ecb90e04e614326336880b513da6de01ce # Parent 1bfae3480fdcff0371c82b74ee1bc5567d535775 Added code and applied fixes. Added getDDocText() and isLineComment(). Renamed getDocComments() to getDocTokens(). Fixed it as well. printSymbolTable() is called recursively now. Fixed genAnonymousID(). diff -r 1bfae3480fdc -r 140469ecb90e trunk/src/dil/doc/Doc.d --- a/trunk/src/dil/doc/Doc.d Wed Jan 30 23:23:58 2008 +0100 +++ b/trunk/src/dil/doc/Doc.d Thu Jan 31 01:10:30 2008 +0100 @@ -23,12 +23,14 @@ Note: this function works correctly only if the source text is syntactically correct. +/ -Token*[] getDocComments(Node node, bool function(Token*) isDocComment = &isDDocComment) +Token*[] getDocTokens(Node node, bool function(Token*) isDocComment = &isDDocComment) { Token*[] comments; + auto isEnumMember = node.kind == NodeKind.EnumMemberDeclaration; // Get preceding comments. auto token = node.begin; // Scan backwards until we hit another declaration. +Loop: while (1) { token = token.prev; @@ -36,21 +38,18 @@ token.kind == TOK.RBrace || token.kind == TOK.Semicolon || token.kind == TOK.HEAD || - (node.kind == NodeKind.EnumMemberDeclaration && token.kind == TOK.Comma)) + (isEnumMember && token.kind == TOK.Comma)) break; if (token.kind == TOK.Comment) - { - // Check that this comment doesn't belong to the previous declaration. - if (node.kind == NodeKind.EnumMemberDeclaration && token.kind == TOK.Comma) - break; + { // Check that this comment doesn't belong to the previous declaration. switch (token.prev.kind) { - case TOK.Semicolon, TOK.RBrace: - break; + case TOK.Semicolon, TOK.RBrace, TOK.Comma: + break Loop; default: if (isDocComment(token)) - comments ~= token; + comments = [token] ~ comments; } } } @@ -58,7 +57,7 @@ token = node.end.next; if (token.kind == TOK.Comment && isDocComment(token)) comments ~= token; - else if (node.kind == NodeKind.EnumMemberDeclaration) + else if (isEnumMember) { token = node.end.nextNWS; if (token.kind == TOK.Comma) @@ -71,12 +70,31 @@ return comments; } +bool isLineComment(Token* t) +{ + assert(t.kind == TOK.Comment); + return t.start[1] == '/'; +} + +/// Extracts the text body of the comment tokens. +string getDDocText(Token*[] tokens) +{ + string result; + foreach (token; tokens) + { + auto n = isLineComment(token) ? 0 : 2; // 0 for "//", 2 for "+/" and "*/". + result ~= sanitize(token.srcText[3 .. $-n], token.start[1]); + } + return result; +} + bool isspace(char c) { return c == ' ' || c == '\t' || c == '\v' || c == '\f'; } /// Sanitizes a DDoc comment string. +/// Leading "commentChar"s are removed from the lines. /// The various newline types are converted to '\n'. /// Params: /// comment = the string to be sanitized. diff -r 1bfae3480fdc -r 140469ecb90e trunk/src/dil/lexer/IdTable.d --- a/trunk/src/dil/lexer/IdTable.d Wed Jan 30 23:23:58 2008 +0100 +++ b/trunk/src/dil/lexer/IdTable.d Thu Jan 31 01:10:30 2008 +0100 @@ -129,15 +129,16 @@ +/ static uint anonCount; - Identifier* genAnonymousID(char[] str) + Identifier* genAnonymousID(char[] prefix) { ++anonCount; auto x = anonCount; // Convert count to a string and append it to str. + char[] num; do - str = cast(char)('0' + (x % 10)) ~ str; + num = cast(char)('0' + (x % 10)) ~ num; while (x /= 10) - return Identifier(str, TOK.Identifier); + return Identifier(prefix ~ num, TOK.Identifier); } Identifier* genAnonEnumID() diff -r 1bfae3480fdc -r 140469ecb90e trunk/src/dil/semantic/Pass1.d --- a/trunk/src/dil/semantic/Pass1.d Wed Jan 30 23:23:58 2008 +0100 +++ b/trunk/src/dil/semantic/Pass1.d Thu Jan 31 01:10:30 2008 +0100 @@ -201,7 +201,7 @@ auto isAnonymous = d.name is null; if (isAnonymous) d.symbol.name = IdTable.genAnonEnumID(); - insert(d.symbol, d.name); + insert(d.symbol, d.symbol.name); auto parentScopeSymbol = scop.symbol; enterScope(d.symbol); // Declare members. diff -r 1bfae3480fdc -r 140469ecb90e trunk/src/main.d --- a/trunk/src/main.d Wed Jan 30 23:23:58 2008 +0100 +++ b/trunk/src/main.d Thu Jan 31 01:10:30 2008 +0100 @@ -63,19 +63,21 @@ auto pass1 = new SemanticPass1(mod); pass1.start(); - void printSymbolTable(ScopeSymbol scopeSym) + void printSymbolTable(ScopeSymbol scopeSym, char[] indent) { foreach (member; scopeSym.members) { - auto tokens = getDocComments(member.node); + auto tokens = getDocTokens(member.node); char[] docText; foreach (token; tokens) docText ~= token.srcText; - Stdout.formatln("Id:{}, Symbol:{}, DocText:{}", member.name.str, member.classinfo.name, docText); + Stdout(indent).formatln("Id:{}, Symbol:{}, DocText:{}", member.name.str, member.classinfo.name, docText); + if (auto s = cast(ScopeSymbol)member) + printSymbolTable(s, indent ~ "→ "); } } - printSymbolTable(mod); + printSymbolTable(mod, ""); } printErrors(infoMan);