OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * The cached lookup-table to associate doc comments with spans. The outer map | 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 | 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 | 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 | 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. | 10 * for this comment would be the position of the "class" token below. |
11 */ | 11 */ |
12 class CommentMap { | 12 class CommentMap { |
13 /** | 13 /** |
14 * Maps from (filename, pos) to doc comment preceding the token at that | 14 * Maps from (filename, pos) to doc comment preceding the token at that |
15 * position. | 15 * position. |
16 */ | 16 */ |
17 Map<String, Map<int, String>> _comments; | 17 Map<String, Map<int, String>> _comments; |
18 | 18 |
19 /** Doc comments before #library() directives. */ | 19 /** Doc comments before #library() directives. */ |
20 Map<String, String> _libraryComments; | 20 Map<String, String> _libraryComments; |
21 | 21 |
22 CommentMap() | 22 CommentMap() |
23 : _comments = <Map<int, String>>{}, | 23 : _comments = <Map<int, String>>{}, |
24 _libraryComments = <String>{}; | 24 _libraryComments = <String>{}; |
25 | 25 |
26 /** Finds the doc comment preceding the given source span, if there is one. */ | 26 /** Finds the doc comment preceding the given source span, if there is one. */ |
27 String find(SourceSpan span) { | 27 String find(Location span) { |
28 if (span == null) return null; | 28 if (span == null) return null; |
29 | 29 |
30 _ensureFileParsed(span.file); | 30 _ensureFileParsed(span.source()); |
31 final comment = _comments[span.file.filename][span.start]; | 31 final comment = _comments[span.source().uri().toString()][span.start()]; |
32 if (comment == null) return ''; | 32 if (comment == null) return ''; |
33 return comment; | 33 return comment; |
34 } | 34 } |
35 | 35 |
36 /** | 36 /** |
37 * Finds the doc comment associated with the `#library` directive for the | 37 * Finds the doc comment associated with the `#library` directive for the |
38 * given file. | 38 * given file. |
39 */ | 39 */ |
40 String findLibrary(SourceFile file) { | 40 String findLibrary(Source source) { |
41 _ensureFileParsed(file); | 41 _ensureFileParsed(source); |
42 final comment = _libraryComments[file.filename]; | 42 final comment = _libraryComments[source.uri().toString()]; |
43 if (comment == null) return ''; | 43 if (comment == null) return ''; |
44 return comment; | 44 return comment; |
45 } | 45 } |
46 | 46 |
47 _ensureFileParsed(SourceFile file) { | 47 _ensureFileParsed(Source source) { |
48 _comments.putIfAbsent(file.filename, () => _parseComments(file)); | 48 _comments.putIfAbsent(source.uri().toString(), () => |
| 49 _parseComments(source)); |
49 } | 50 } |
50 | 51 |
51 _parseComments(SourceFile file) { | 52 _parseComments(Source source) { |
52 final comments = new Map<int, String>(); | 53 final comments = new Map<int, String>(); |
53 | 54 |
54 final tokenizer = new Tokenizer(file, false); | 55 final scanner = new dart2js.StringScanner(source.text(), true); |
55 var lastComment = null; | 56 var lastComment = null; |
56 | 57 |
57 while (true) { | 58 var token = scanner.tokenize(); |
58 final token = tokenizer.next(); | 59 while (token.kind != dart2js.EOF_TOKEN) { |
59 if (token.kind == TokenKind.END_OF_FILE) break; | 60 if (token.kind == dart2js.COMMENT_TOKEN) { |
60 | 61 final text = token.slowToString(); |
61 if (token.kind == TokenKind.COMMENT) { | |
62 final text = token.text; | |
63 if (text.startsWith('/**')) { | 62 if (text.startsWith('/**')) { |
64 // Remember that we've encountered a doc comment. | 63 // Remember that we've encountered a doc comment. |
65 lastComment = stripComment(token.text); | 64 lastComment = stripComment(token.slowToString()); |
66 } else if (text.startsWith('///')) { | 65 } else if (text.startsWith('///')) { |
67 var line = text.substring(3); | 66 var line = text.substring(3); |
68 // Allow a leading space. | 67 // Allow a leading space. |
69 if (line.startsWith(' ')) line = line.substring(1); | 68 if (line.startsWith(' ')) line = line.substring(1); |
70 if (lastComment == null) { | 69 if (lastComment == null) { |
71 lastComment = line; | 70 lastComment = line; |
72 } else { | 71 } else { |
73 lastComment = '$lastComment$line'; | 72 lastComment = '$lastComment$line'; |
74 } | 73 } |
75 } | 74 } |
76 } else if (token.kind == TokenKind.WHITESPACE) { | 75 } else if (token.kind == dart2js.HASH_TOKEN) { |
77 // Ignore whitespace tokens. | |
78 } else if (token.kind == TokenKind.HASH) { | |
79 // Look for #library() to find the library comment. | 76 // Look for #library() to find the library comment. |
80 final next = tokenizer.next(); | 77 final next = token.next; |
81 if ((lastComment != null) && (next.kind == TokenKind.LIBRARY)) { | 78 if ((lastComment != null) && (next.stringValue == 'library')) { |
82 _libraryComments[file.filename] = lastComment; | 79 _libraryComments[source.uri().toString()] = lastComment; |
83 lastComment = null; | 80 lastComment = null; |
84 } | 81 } |
85 } else { | 82 } else if (lastComment != null) { |
86 if (lastComment != null) { | 83 // We haven't attached the last doc comment to something yet, so stick |
87 // We haven't attached the last doc comment to something yet, so stick | 84 // it to this token. |
88 // it to this token. | 85 comments[token.charOffset] = lastComment; |
89 comments[token.start] = lastComment; | 86 lastComment = null; |
90 lastComment = null; | |
91 } | |
92 } | 87 } |
| 88 token = token.next; |
93 } | 89 } |
94 | 90 |
95 return comments; | 91 return comments; |
96 } | 92 } |
97 | 93 |
98 /** | 94 /** |
99 * Pulls the raw text out of a doc comment (i.e. removes the comment | 95 * Pulls the raw text out of a doc comment (i.e. removes the comment |
100 * characters). | 96 * characters). |
101 */ | 97 */ |
102 stripComment(String comment) { | 98 stripComment(String comment) { |
(...skipping 10 matching lines...) Expand all Loading... |
113 line = line.substring(1); | 109 line = line.substring(1); |
114 } | 110 } |
115 | 111 |
116 buf.add(line); | 112 buf.add(line); |
117 buf.add('\n'); | 113 buf.add('\n'); |
118 } | 114 } |
119 | 115 |
120 return buf.toString(); | 116 return buf.toString(); |
121 } | 117 } |
122 } | 118 } |
OLD | NEW |