comparison trunk/src/dil/doc/Doc.d @ 714:140469ecb90e

Added code and applied fixes. Added getDDocText() and isLineComment(). Renamed getDocComments() to getDocTokens(). Fixed it as well. printSymbolTable() is called recursively now. Fixed genAnonymousID().
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Thu, 31 Jan 2008 01:10:30 +0100
parents f8875ef9a66d
children 08e6174a2e1c
comparison
equal deleted inserted replaced
713:1bfae3480fdc 714:140469ecb90e
21 /++ 21 /++
22 Returns the surrounding documentation comment tokens. 22 Returns the surrounding documentation comment tokens.
23 Note: this function works correctly only if 23 Note: this function works correctly only if
24 the source text is syntactically correct. 24 the source text is syntactically correct.
25 +/ 25 +/
26 Token*[] getDocComments(Node node, bool function(Token*) isDocComment = &isDDocComment) 26 Token*[] getDocTokens(Node node, bool function(Token*) isDocComment = &isDDocComment)
27 { 27 {
28 Token*[] comments; 28 Token*[] comments;
29 auto isEnumMember = node.kind == NodeKind.EnumMemberDeclaration;
29 // Get preceding comments. 30 // Get preceding comments.
30 auto token = node.begin; 31 auto token = node.begin;
31 // Scan backwards until we hit another declaration. 32 // Scan backwards until we hit another declaration.
33 Loop:
32 while (1) 34 while (1)
33 { 35 {
34 token = token.prev; 36 token = token.prev;
35 if (token.kind == TOK.LBrace || 37 if (token.kind == TOK.LBrace ||
36 token.kind == TOK.RBrace || 38 token.kind == TOK.RBrace ||
37 token.kind == TOK.Semicolon || 39 token.kind == TOK.Semicolon ||
38 token.kind == TOK.HEAD || 40 token.kind == TOK.HEAD ||
39 (node.kind == NodeKind.EnumMemberDeclaration && token.kind == TOK.Comma)) 41 (isEnumMember && token.kind == TOK.Comma))
40 break; 42 break;
41 43
42 if (token.kind == TOK.Comment) 44 if (token.kind == TOK.Comment)
43 { 45 { // Check that this comment doesn't belong to the previous declaration.
44 // Check that this comment doesn't belong to the previous declaration.
45 if (node.kind == NodeKind.EnumMemberDeclaration && token.kind == TOK.Comma)
46 break;
47 switch (token.prev.kind) 46 switch (token.prev.kind)
48 { 47 {
49 case TOK.Semicolon, TOK.RBrace: 48 case TOK.Semicolon, TOK.RBrace, TOK.Comma:
50 break; 49 break Loop;
51 default: 50 default:
52 if (isDocComment(token)) 51 if (isDocComment(token))
53 comments ~= token; 52 comments = [token] ~ comments;
54 } 53 }
55 } 54 }
56 } 55 }
57 // Get single comment to the right. 56 // Get single comment to the right.
58 token = node.end.next; 57 token = node.end.next;
59 if (token.kind == TOK.Comment && isDocComment(token)) 58 if (token.kind == TOK.Comment && isDocComment(token))
60 comments ~= token; 59 comments ~= token;
61 else if (node.kind == NodeKind.EnumMemberDeclaration) 60 else if (isEnumMember)
62 { 61 {
63 token = node.end.nextNWS; 62 token = node.end.nextNWS;
64 if (token.kind == TOK.Comma) 63 if (token.kind == TOK.Comma)
65 { 64 {
66 token = token.next; 65 token = token.next;
69 } 68 }
70 } 69 }
71 return comments; 70 return comments;
72 } 71 }
73 72
73 bool isLineComment(Token* t)
74 {
75 assert(t.kind == TOK.Comment);
76 return t.start[1] == '/';
77 }
78
79 /// Extracts the text body of the comment tokens.
80 string getDDocText(Token*[] tokens)
81 {
82 string result;
83 foreach (token; tokens)
84 {
85 auto n = isLineComment(token) ? 0 : 2; // 0 for "//", 2 for "+/" and "*/".
86 result ~= sanitize(token.srcText[3 .. $-n], token.start[1]);
87 }
88 return result;
89 }
90
74 bool isspace(char c) 91 bool isspace(char c)
75 { 92 {
76 return c == ' ' || c == '\t' || c == '\v' || c == '\f'; 93 return c == ' ' || c == '\t' || c == '\v' || c == '\f';
77 } 94 }
78 95
79 /// Sanitizes a DDoc comment string. 96 /// Sanitizes a DDoc comment string.
97 /// Leading "commentChar"s are removed from the lines.
80 /// The various newline types are converted to '\n'. 98 /// The various newline types are converted to '\n'.
81 /// Params: 99 /// Params:
82 /// comment = the string to be sanitized. 100 /// comment = the string to be sanitized.
83 /// commentChar = '/', '+', or '*' 101 /// commentChar = '/', '+', or '*'
84 string sanitize(string comment, char commentChar) 102 string sanitize(string comment, char commentChar)