OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, 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 /** | |
14 * Maps from (filename, pos) to doc comment preceding the token at that | |
15 * position. | |
16 */ | |
17 Map<String, Map<int, String>> _comments; | |
18 | |
19 /** Doc comments before #library() directives. */ | |
20 Map<String, String> _libraryComments; | |
21 | |
22 CommentMap() | |
23 : _comments = <String, Map<int, String>>{}, | |
24 _libraryComments = <String, String>{}; | |
25 | |
26 /** Finds the doc comment preceding the given source span, if there is one. */ | |
27 String find(Location span) { | |
28 if (span == null) return null; | |
29 | |
30 _ensureFileParsed(span.source); | |
31 final comment = _comments[span.source.uri.toString()][span.start]; | |
32 if (comment == null) return ''; | |
33 return comment; | |
34 } | |
35 | |
36 /** | |
37 * Finds the doc comment associated with the `#library` directive for the | |
38 * given file. | |
39 */ | |
40 String findLibrary(Source source) { | |
41 _ensureFileParsed(source); | |
42 final comment = _libraryComments[source.uri.toString()]; | |
43 if (comment == null) return ''; | |
44 return comment; | |
45 } | |
46 | |
47 _ensureFileParsed(Source source) { | |
48 _comments.putIfAbsent(source.uri.toString(), () => | |
49 _parseComments(source)); | |
50 } | |
51 | |
52 _parseComments(Source source) { | |
53 final comments = new Map<int, String>(); | |
54 | |
55 final scanner = new dart2js.StringScanner(source.text, true); | |
56 var lastComment = null; | |
57 | |
58 var token = scanner.tokenize(); | |
59 while (token.kind != dart2js.EOF_TOKEN) { | |
60 if (token.kind == dart2js.COMMENT_TOKEN) { | |
61 final text = token.slowToString(); | |
62 if (text.startsWith('/**')) { | |
63 // Remember that we've encountered a doc comment. | |
64 lastComment = stripComment(token.slowToString()); | |
65 } else if (text.startsWith('///')) { | |
66 var line = text.substring(3); | |
67 // Allow a leading space. | |
68 if (line.startsWith(' ')) line = line.substring(1); | |
69 if (lastComment == null) { | |
70 lastComment = line; | |
71 } else { | |
72 lastComment = '$lastComment$line'; | |
73 } | |
74 } | |
75 } else if (token.kind == dart2js.HASH_TOKEN) { | |
76 // Look for #library() to find the library comment. | |
77 final next = token.next; | |
78 if ((lastComment != null) && (next.stringValue == 'library')) { | |
79 _libraryComments[source.uri.toString()] = lastComment; | |
80 lastComment = null; | |
81 } | |
82 } else 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.charOffset] = lastComment; | |
86 lastComment = null; | |
87 } | |
88 token = token.next; | |
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 (var line in comment.split('\n')) { | |
102 line = line.trim(); | |
103 if (line.startsWith('/**')) line = line.substring(3); | |
104 if (line.endsWith('*/')) line = line.substring(0, line.length - 2); | |
105 line = line.trim(); | |
106 if (line.startsWith('* ')) { | |
107 line = line.substring(2); | |
108 } else if (line.startsWith('*')) { | |
109 line = line.substring(1); | |
110 } | |
111 | |
112 buf.add(line); | |
113 buf.add('\n'); | |
114 } | |
115 | |
116 return buf.toString(); | |
117 } | |
118 } | |
OLD | NEW |