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

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

Issue 10701091: Dartdoc and Apidoc updated to use dart2js through the mirror system. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: create-sdk.py updated Created 8 years, 5 months 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
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(), () => _parseComments(source)) ;
kasperl 2012/07/06 12:40:41 Line too long (exceeds 80 characters).
Johnni Winther 2012/07/09 14:57:18 Done.
49 } 49 }
50 50
51 _parseComments(SourceFile file) { 51 _parseComments(Source source) {
52 final comments = new Map<int, String>(); 52 final comments = new Map<int, String>();
53 53
54 final tokenizer = new Tokenizer(file, false); 54 final scanner = new dart2js.StringScanner(source.text(), true);
55 var lastComment = null; 55 var lastComment = null;
56 56
57 while (true) { 57 var token = scanner.tokenize();
58 final token = tokenizer.next(); 58 while (token.kind != dart2js.EOF_TOKEN) {
59 if (token.kind == TokenKind.END_OF_FILE) break; 59 if (token.kind == dart2js.COMMENT_TOKEN) {
60 60 final text = token.slowToString();
61 if (token.kind == TokenKind.COMMENT) {
62 final text = token.text;
63 if (text.startsWith('/**')) { 61 if (text.startsWith('/**')) {
64 // Remember that we've encountered a doc comment. 62 // Remember that we've encountered a doc comment.
65 lastComment = stripComment(token.text); 63 lastComment = stripComment(token.slowToString());
66 } else if (text.startsWith('///')) { 64 } else if (text.startsWith('///')) {
67 var line = text.substring(3); 65 var line = text.substring(3);
68 // Allow a leading space. 66 // Allow a leading space.
69 if (line.startsWith(' ')) line = line.substring(1); 67 if (line.startsWith(' ')) line = line.substring(1);
70 if (lastComment == null) { 68 if (lastComment == null) {
71 lastComment = line; 69 lastComment = line;
72 } else { 70 } else {
73 lastComment = '$lastComment$line'; 71 lastComment = '$lastComment$line';
74 } 72 }
75 } 73 }
76 } else if (token.kind == TokenKind.WHITESPACE) { 74 } 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. 75 // Look for #library() to find the library comment.
80 final next = tokenizer.next(); 76 final next = token.next;
81 if ((lastComment != null) && (next.kind == TokenKind.LIBRARY)) { 77 if ((lastComment != null) && (next.stringValue == 'library')) {
82 _libraryComments[file.filename] = lastComment; 78 _libraryComments[source.uri().toString()] = lastComment;
83 lastComment = null; 79 lastComment = null;
84 } 80 }
85 } else { 81 } else {
86 if (lastComment != null) { 82 if (lastComment != null) {
Lasse Reichstein Nielsen 2012/07/09 10:39:33 "else if" on one line.
Johnni Winther 2012/07/09 14:57:18 Done.
87 // 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
88 // it to this token. 84 // it to this token.
89 comments[token.start] = lastComment; 85 comments[token.charOffset] = lastComment;
90 lastComment = null; 86 lastComment = null;
91 } 87 }
92 } 88 }
89 token = token.next;
93 } 90 }
94 91
95 return comments; 92 return comments;
96 } 93 }
97 94
98 /** 95 /**
99 * Pulls the raw text out of a doc comment (i.e. removes the comment 96 * Pulls the raw text out of a doc comment (i.e. removes the comment
100 * characters). 97 * characters).
101 */ 98 */
102 stripComment(String comment) { 99 stripComment(String comment) {
(...skipping 10 matching lines...) Expand all
113 line = line.substring(1); 110 line = line.substring(1);
114 } 111 }
115 112
116 buf.add(line); 113 buf.add(line);
117 buf.add('\n'); 114 buf.add('\n');
118 } 115 }
119 116
120 return buf.toString(); 117 return buf.toString();
121 } 118 }
122 } 119 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698