OLD | NEW |
(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 /** |
| 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 = <Map<int, String>>{}, |
| 24 _libraryComments = <String>{}; |
| 25 |
| 26 /** Finds the doc comment preceding the given source span, if there is one. */ |
| 27 String find(SourceSpan span) { |
| 28 if (span == null) return null; |
| 29 |
| 30 _ensureFileParsed(span.file); |
| 31 final comment = _comments[span.file.filename][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(SourceFile file) { |
| 41 _ensureFileParsed(file); |
| 42 final comment = _libraryComments[file.filename]; |
| 43 if (comment == null) return ''; |
| 44 return comment; |
| 45 } |
| 46 |
| 47 _ensureFileParsed(SourceFile file) { |
| 48 _comments.putIfAbsent(file.filename, () => _parseComments(file)); |
| 49 } |
| 50 |
| 51 _parseComments(SourceFile file) { |
| 52 final comments = new Map<int, String>(); |
| 53 |
| 54 final tokenizer = new Tokenizer(file, false); |
| 55 var lastComment = null; |
| 56 |
| 57 while (true) { |
| 58 final token = tokenizer.next(); |
| 59 if (token.kind == TokenKind.END_OF_FILE) break; |
| 60 |
| 61 if (token.kind == TokenKind.COMMENT) { |
| 62 final text = token.text; |
| 63 if (text.startsWith('/**')) { |
| 64 // Remember that we've encountered a doc comment. |
| 65 lastComment = stripComment(token.text); |
| 66 } else if (text.startsWith('///')) { |
| 67 var line = text.substring(3); |
| 68 // Allow a leading space. |
| 69 if (line.startsWith(' ')) line = line.substring(1); |
| 70 if (lastComment == null) { |
| 71 lastComment = line; |
| 72 } else { |
| 73 lastComment = '$lastComment$line'; |
| 74 } |
| 75 } |
| 76 } else if (token.kind == TokenKind.WHITESPACE) { |
| 77 // Ignore whitespace tokens. |
| 78 } else if (token.kind == TokenKind.HASH) { |
| 79 // Look for #library() to find the library comment. |
| 80 final next = tokenizer.next(); |
| 81 if ((lastComment != null) && (next.kind == TokenKind.LIBRARY)) { |
| 82 _libraryComments[file.filename] = lastComment; |
| 83 lastComment = null; |
| 84 } |
| 85 } else { |
| 86 if (lastComment != null) { |
| 87 // We haven't attached the last doc comment to something yet, so stick |
| 88 // it to this token. |
| 89 comments[token.start] = lastComment; |
| 90 lastComment = null; |
| 91 } |
| 92 } |
| 93 } |
| 94 |
| 95 return comments; |
| 96 } |
| 97 |
| 98 /** |
| 99 * Pulls the raw text out of a doc comment (i.e. removes the comment |
| 100 * characters). |
| 101 */ |
| 102 stripComment(String comment) { |
| 103 StringBuffer buf = new StringBuffer(); |
| 104 |
| 105 for (final line in comment.split('\n')) { |
| 106 line = line.trim(); |
| 107 if (line.startsWith('/**')) line = line.substring(3); |
| 108 if (line.endsWith('*/')) line = line.substring(0, line.length - 2); |
| 109 line = line.trim(); |
| 110 if (line.startsWith('* ')) { |
| 111 line = line.substring(2); |
| 112 } else if (line.startsWith('*')) { |
| 113 line = line.substring(1); |
| 114 } |
| 115 |
| 116 buf.add(line); |
| 117 buf.add('\n'); |
| 118 } |
| 119 |
| 120 return buf.toString(); |
| 121 } |
| 122 } |
OLD | NEW |