Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: utils/dartdoc/comment_map.dart

Issue 8947005: Refactor dartdoc into a class. Use method overriding to extend. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 /**
6 * The cached lookup-table to associate doc comments with spans. The outer map
7 * is from filenames to doc comments in that file. The inner map maps from the
8 * token positions to doc comments. Each position is the starting offset of the
9 * next non-comment token *following* the doc comment. For example, the position
10 * for this comment would be the position of the "class" token below.
11 */
12 class CommentMap {
13 Map<String, Map<int, String>> _comments;
nweiz 2011/12/15 19:39:04 Mention that this is a map from filename -> positi
Bob Nystrom 2011/12/15 20:20:11 Done.
14
15 /** Doc comments before #library() directives. */
16 Map<String, String> _libraryComments;
17
18 CommentMap()
19 : _comments = <Map<int, String>>{},
20 _libraryComments = <String>{};
21
22 /** Finds the doc comment preceding the given source span, if there is one. */
23 String find(SourceSpan span) {
24 if (span == null) return null;
25
26 _ensureFileParsed(span.file);
27 final comment = _comments[span.file.filename][span.start];
28 if (comment == null) return '';
29 return comment;
30 }
31
32 /**
33 * Finds the doc comment associated with the `#library` directive for the
34 * given file.
35 */
36 String findLibrary(SourceFile file) {
37 _ensureFileParsed(file);
38 final comment = _libraryComments[file.filename];
39 if (comment == null) return '';
40 return comment;
41 }
42
43 _ensureFileParsed(SourceFile file) {
44 _comments.putIfAbsent(file.filename, () => _parseComments(file));
45 }
46
47 _parseComments(SourceFile file) {
48 final comments = new Map<int, String>();
49
50 final tokenizer = new Tokenizer(file, false);
51 var lastComment = null;
52
53 while (true) {
54 final token = tokenizer.next();
55 if (token.kind == TokenKind.END_OF_FILE) break;
56
57 if (token.kind == TokenKind.COMMENT) {
58 final text = token.text;
59 if (text.startsWith('/**')) {
60 // Remember that we've encountered a doc comment.
61 lastComment = stripComment(token.text);
62 } else if (text.startsWith('///')) {
63 var line = text.substring(3, text.length);
nweiz 2011/12/15 19:39:04 text.substring(3) is equivalent to text.substring(
Bob Nystrom 2011/12/15 20:20:11 Done, though I'm not sure how that worked before.
nweiz 2011/12/15 20:54:07 I'm pretty sure optional parameters can be used po
64 // Allow a leading space.
65 if (line.startsWith(' ')) line = line.substring(1, text.length);
nweiz 2011/12/15 19:39:04 This probably wants to be line.length, not text.le
Bob Nystrom 2011/12/15 20:20:11 Done.
66 if (lastComment == null) {
67 lastComment = line;
68 } else {
69 lastComment = '$lastComment$line';
70 }
71 }
72 } else if (token.kind == TokenKind.WHITESPACE) {
73 // Ignore whitespace tokens.
74 } else if (token.kind == TokenKind.HASH) {
75 // Look for #library() to find the library comment.
76 final next = tokenizer.next();
77 if ((lastComment != null) && (next.kind == TokenKind.LIBRARY)) {
78 _libraryComments[file.filename] = lastComment;
79 lastComment = null;
80 }
81 } else {
82 if (lastComment != null) {
83 // We haven't attached the last doc comment to something yet, so stick
84 // it to this token.
85 comments[token.start] = lastComment;
86 lastComment = null;
87 }
88 }
89 }
90
91 return comments;
92 }
93
94 /**
95 * Pulls the raw text out of a doc comment (i.e. removes the comment
96 * characters).
97 */
98 stripComment(String comment) {
99 StringBuffer buf = new StringBuffer();
100
101 for (final line in comment.split('\n')) {
102 line = line.trim();
103 if (line.startsWith('/**')) line = line.substring(3, line.length);
104 if (line.endsWith('*/')) line = line.substring(0, line.length - 2);
105 line = line.trim();
106 if (line.startsWith('* ')) {
107 line = line.substring(2, line.length);
108 } else if (line.startsWith('*')) {
109 line = line.substring(1, line.length);
110 }
111
112 buf.add(line);
113 buf.add('\n');
114 }
115
116 return buf.toString();
117 }
118 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698