| 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(Location span) { | 27 String find(Location span) { |
| 28 if (span == null) return null; | 28 if (span == null) return null; |
| 29 | 29 |
| 30 _ensureFileParsed(span.source()); | 30 _ensureFileParsed(span.source); |
| 31 final comment = _comments[span.source().uri().toString()][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(Source source) { | 40 String findLibrary(Source source) { |
| 41 _ensureFileParsed(source); | 41 _ensureFileParsed(source); |
| 42 final comment = _libraryComments[source.uri().toString()]; | 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(Source source) { | 47 _ensureFileParsed(Source source) { |
| 48 _comments.putIfAbsent(source.uri().toString(), () => | 48 _comments.putIfAbsent(source.uri.toString(), () => |
| 49 _parseComments(source)); | 49 _parseComments(source)); |
| 50 } | 50 } |
| 51 | 51 |
| 52 _parseComments(Source source) { | 52 _parseComments(Source source) { |
| 53 final comments = new Map<int, String>(); | 53 final comments = new Map<int, String>(); |
| 54 | 54 |
| 55 final scanner = new dart2js.StringScanner(source.text(), true); | 55 final scanner = new dart2js.StringScanner(source.text, true); |
| 56 var lastComment = null; | 56 var lastComment = null; |
| 57 | 57 |
| 58 var token = scanner.tokenize(); | 58 var token = scanner.tokenize(); |
| 59 while (token.kind != dart2js.EOF_TOKEN) { | 59 while (token.kind != dart2js.EOF_TOKEN) { |
| 60 if (token.kind == dart2js.COMMENT_TOKEN) { | 60 if (token.kind == dart2js.COMMENT_TOKEN) { |
| 61 final text = token.slowToString(); | 61 final text = token.slowToString(); |
| 62 if (text.startsWith('/**')) { | 62 if (text.startsWith('/**')) { |
| 63 // Remember that we've encountered a doc comment. | 63 // Remember that we've encountered a doc comment. |
| 64 lastComment = stripComment(token.slowToString()); | 64 lastComment = stripComment(token.slowToString()); |
| 65 } else if (text.startsWith('///')) { | 65 } else if (text.startsWith('///')) { |
| 66 var line = text.substring(3); | 66 var line = text.substring(3); |
| 67 // Allow a leading space. | 67 // Allow a leading space. |
| 68 if (line.startsWith(' ')) line = line.substring(1); | 68 if (line.startsWith(' ')) line = line.substring(1); |
| 69 if (lastComment == null) { | 69 if (lastComment == null) { |
| 70 lastComment = line; | 70 lastComment = line; |
| 71 } else { | 71 } else { |
| 72 lastComment = '$lastComment$line'; | 72 lastComment = '$lastComment$line'; |
| 73 } | 73 } |
| 74 } | 74 } |
| 75 } else if (token.kind == dart2js.HASH_TOKEN) { | 75 } else if (token.kind == dart2js.HASH_TOKEN) { |
| 76 // Look for #library() to find the library comment. | 76 // Look for #library() to find the library comment. |
| 77 final next = token.next; | 77 final next = token.next; |
| 78 if ((lastComment != null) && (next.stringValue == 'library')) { | 78 if ((lastComment != null) && (next.stringValue == 'library')) { |
| 79 _libraryComments[source.uri().toString()] = lastComment; | 79 _libraryComments[source.uri.toString()] = lastComment; |
| 80 lastComment = null; | 80 lastComment = null; |
| 81 } | 81 } |
| 82 } else if (lastComment != null) { | 82 } else if (lastComment != null) { |
| 83 // We haven't attached the last doc comment to something yet, so stick | 83 // We haven't attached the last doc comment to something yet, so stick |
| 84 // it to this token. | 84 // it to this token. |
| 85 comments[token.charOffset] = lastComment; | 85 comments[token.charOffset] = lastComment; |
| 86 lastComment = null; | 86 lastComment = null; |
| 87 } | 87 } |
| 88 token = token.next; | 88 token = token.next; |
| 89 } | 89 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 109 line = line.substring(1); | 109 line = line.substring(1); |
| 110 } | 110 } |
| 111 | 111 |
| 112 buf.add(line); | 112 buf.add(line); |
| 113 buf.add('\n'); | 113 buf.add('\n'); |
| 114 } | 114 } |
| 115 | 115 |
| 116 return buf.toString(); | 116 return buf.toString(); |
| 117 } | 117 } |
| 118 } | 118 } |
| OLD | NEW |