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

Side by Side Diff: pkg/analyzer/lib/src/generated/scanner.dart

Issue 1434863003: initial generic method comment parsing (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 1 month 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
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 library engine.scanner; 5 library engine.scanner;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 8
9 import 'error.dart'; 9 import 'error.dart';
10 import 'java_engine.dart'; 10 import 'java_engine.dart';
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 * Initialize a newly created token to represent a token of the given [type] 239 * Initialize a newly created token to represent a token of the given [type]
240 * with the given [value] at the given [offset]. 240 * with the given [value] at the given [offset].
241 */ 241 */
242 CommentToken(TokenType type, String value, int offset) 242 CommentToken(TokenType type, String value, int offset)
243 : super(type, value, offset); 243 : super(type, value, offset);
244 244
245 @override 245 @override
246 CommentToken copy() => new CommentToken(type, _value, offset); 246 CommentToken copy() => new CommentToken(type, _value, offset);
247 } 247 }
248 248
249 class GenericMethodCommentToken extends CommentToken {
Brian Wilkerson 2015/11/11 21:53:42 It isn't clear to me that we need a separate class
Jennifer Messerly 2015/11/12 00:20:00 Good catch. Yeah I think my order was I made the c
250 /**
251 * Initialize a newly created token to represent a token of the given [type]
252 * with the given [value] at the given [offset].
253 */
254 GenericMethodCommentToken(TokenType type, String value, int offset)
255 : super(type, value, offset);
256
257 @override
258 GenericMethodCommentToken copy() =>
259 new GenericMethodCommentToken(type, _value, offset);
260 }
261
249 /** 262 /**
250 * A documentation comment token. 263 * A documentation comment token.
251 */ 264 */
252 class DocumentationCommentToken extends CommentToken { 265 class DocumentationCommentToken extends CommentToken {
253 /** 266 /**
254 * The references embedded within the documentation comment. 267 * The references embedded within the documentation comment.
255 * This list will be empty unless this is a documentation comment that has 268 * This list will be empty unless this is a documentation comment that has
256 * references embedded within it. 269 * references embedded within it.
257 */ 270 */
258 final List<Token> references = <Token>[]; 271 final List<Token> references = <Token>[];
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after
716 * empty. 729 * empty.
717 */ 730 */
718 int _stackEnd = -1; 731 int _stackEnd = -1;
719 732
720 /** 733 /**
721 * A flag indicating whether any unmatched groups were found during the parse. 734 * A flag indicating whether any unmatched groups were found during the parse.
722 */ 735 */
723 bool _hasUnmatchedGroups = false; 736 bool _hasUnmatchedGroups = false;
724 737
725 /** 738 /**
739 * A flag indicating whether to parse generic method comments, of the form
Brian Wilkerson 2015/11/11 21:53:42 Maybe "parse" --> "scan" in both the comment and t
Jennifer Messerly 2015/11/12 00:20:00 Sounds good to me. Done.
740 * `/*=T*/` and `/*<T>*/`.
741 */
742 bool parseGenericMethodComments = false;
743
744 /**
726 * Initialize a newly created scanner to scan characters from the given 745 * Initialize a newly created scanner to scan characters from the given
727 * [source]. The given character [_reader] will be used to read the characters 746 * [source]. The given character [_reader] will be used to read the characters
728 * in the source. The given [_errorListener] will be informed of any errors 747 * in the source. The given [_errorListener] will be informed of any errors
729 * that are found. 748 * that are found.
730 */ 749 */
731 Scanner(this.source, this._reader, this._errorListener) { 750 Scanner(this.source, this._reader, this._errorListener) {
732 _tokens = new Token(TokenType.EOF, -1); 751 _tokens = new Token(TokenType.EOF, -1);
733 _tokens.setNext(_tokens); 752 _tokens.setNext(_tokens);
734 _tail = _tokens; 753 _tail = _tokens;
735 _tokenStart = -1; 754 _tokenStart = -1;
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
1005 token = new BeginTokenWithComment(type, _tokenStart, _firstComment); 1024 token = new BeginTokenWithComment(type, _tokenStart, _firstComment);
1006 _firstComment = null; 1025 _firstComment = null;
1007 _lastComment = null; 1026 _lastComment = null;
1008 } 1027 }
1009 _tail = _tail.setNext(token); 1028 _tail = _tail.setNext(token);
1010 _groupingStack.add(token); 1029 _groupingStack.add(token);
1011 _stackEnd++; 1030 _stackEnd++;
1012 } 1031 }
1013 1032
1014 void _appendCommentToken(TokenType type, String value) { 1033 void _appendCommentToken(TokenType type, String value) {
1015 // Ignore comment tokens if client specified that it doesn't need them. 1034 CommentToken token = null;
1016 if (!_preserveComments) { 1035 TokenType genericComment = _matchGenericMethodCommentType(value);
1036 if (genericComment != null) {
1037 token = new GenericMethodCommentToken(genericComment, value, _tokenStart);
1038 } else if (!_preserveComments) {
1039 // Ignore comment tokens if client specified that it doesn't need them.
1017 return; 1040 return;
1018 }
1019 // OK, remember comment tokens.
1020 CommentToken token;
1021 if (_isDocumentationComment(value)) {
1022 token = new DocumentationCommentToken(type, value, _tokenStart);
1023 } else { 1041 } else {
1024 token = new CommentToken(type, value, _tokenStart); 1042 // OK, remember comment tokens.
1043 if (_isDocumentationComment(value)) {
1044 token = new DocumentationCommentToken(type, value, _tokenStart);
1045 } else {
1046 token = new CommentToken(type, value, _tokenStart);
1047 }
1025 } 1048 }
1026 if (_firstComment == null) { 1049 if (_firstComment == null) {
1027 _firstComment = token; 1050 _firstComment = token;
1028 _lastComment = _firstComment; 1051 _lastComment = _firstComment;
1029 } else { 1052 } else {
1030 _lastComment = _lastComment.setNext(token); 1053 _lastComment = _lastComment.setNext(token);
1031 } 1054 }
1032 } 1055 }
1033 1056
1034 void _appendEndToken(TokenType type, TokenType beginType) { 1057 void _appendEndToken(TokenType type, TokenType beginType) {
(...skipping 778 matching lines...) Expand 10 before | Expand all | Expand 10 after
1813 } 1836 }
1814 } 1837 }
1815 1838
1816 /** 1839 /**
1817 * Checks if [value] is a single-line or multi-line comment. 1840 * Checks if [value] is a single-line or multi-line comment.
1818 */ 1841 */
1819 static bool _isDocumentationComment(String value) { 1842 static bool _isDocumentationComment(String value) {
1820 return StringUtilities.startsWith3(value, 0, 0x2F, 0x2F, 0x2F) || 1843 return StringUtilities.startsWith3(value, 0, 0x2F, 0x2F, 0x2F) ||
1821 StringUtilities.startsWith3(value, 0, 0x2F, 0x2A, 0x2A); 1844 StringUtilities.startsWith3(value, 0, 0x2F, 0x2A, 0x2A);
1822 } 1845 }
1846
1847 /**
1848 * Checks if [value] is the start of a generic method type annotation comment.
1849 *
1850 * This can either be of the form `/*<T>*/` or `/*=T*/`. The token type is
1851 * returned, or null if it was not a generic method comment.
1852 */
1853 TokenType _matchGenericMethodCommentType(String value) {
1854 if (parseGenericMethodComments) {
1855 // Match /*< and >*/
1856 if (StringUtilities.startsWith3(value, 0, 0x2F, 0x2A, 0x3C) &&
1857 StringUtilities.endsWith3(value, 0x3E, 0x2A, 0x2F)) {
1858 return TokenType.GENERIC_METHOD_TYPE_LIST;
1859 }
1860 // Match /*=
1861 if (StringUtilities.startsWith3(value, 0, 0x2F, 0x2A, 0x3D)) {
1862 return TokenType.GENERIC_METHOD_TYPE_ASSIGN;
1863 }
1864 }
1865 return null;
1866 }
1823 } 1867 }
1824 1868
1825 /** 1869 /**
1826 * The error codes used for errors detected by the scanner. 1870 * The error codes used for errors detected by the scanner.
1827 */ 1871 */
1828 class ScannerErrorCode extends ErrorCode { 1872 class ScannerErrorCode extends ErrorCode {
1829 static const ScannerErrorCode ILLEGAL_CHARACTER = 1873 static const ScannerErrorCode ILLEGAL_CHARACTER =
1830 const ScannerErrorCode('ILLEGAL_CHARACTER', "Illegal character {0}"); 1874 const ScannerErrorCode('ILLEGAL_CHARACTER', "Illegal character {0}");
1831 1875
1832 static const ScannerErrorCode MISSING_DIGIT = 1876 static const ScannerErrorCode MISSING_DIGIT =
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
2050 * Return the next token in the token stream. 2094 * Return the next token in the token stream.
2051 */ 2095 */
2052 Token get next => _next; 2096 Token get next => _next;
2053 2097
2054 /** 2098 /**
2055 * Return the first comment in the list of comments that precede this token, 2099 * Return the first comment in the list of comments that precede this token,
2056 * or `null` if there are no comments preceding this token. Additional 2100 * or `null` if there are no comments preceding this token. Additional
2057 * comments can be reached by following the token stream using [next] until 2101 * comments can be reached by following the token stream using [next] until
2058 * `null` is returned. 2102 * `null` is returned.
2059 * 2103 *
2060 * For example, if the original contents were "/* one */ /* two */ id", then 2104 * For example, if the original contents were `/* one */ /* two */ id`, then
2061 * the first preceding comment token will have a lexeme of "/* one */" and 2105 * the first preceding comment token will have a lexeme of `/* one */` and
2062 * the next comment token will have a lexeme of "/* two */". 2106 * the next comment token will have a lexeme of `/* two */`.
2063 */ 2107 */
2064 CommentToken get precedingComments => null; 2108 CommentToken get precedingComments => null;
2065 2109
2066 /** 2110 /**
2067 * Apply (add) the given [delta] to this token's offset. 2111 * Apply (add) the given [delta] to this token's offset.
2068 */ 2112 */
2069 void applyDelta(int delta) { 2113 void applyDelta(int delta) {
2070 offset += delta; 2114 offset += delta;
2071 } 2115 }
2072 2116
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after
2494 2538
2495 static const TokenType BACKPING = 2539 static const TokenType BACKPING =
2496 const TokenType('BACKPING', TokenClass.NO_CLASS, "`"); 2540 const TokenType('BACKPING', TokenClass.NO_CLASS, "`");
2497 2541
2498 static const TokenType BACKSLASH = 2542 static const TokenType BACKSLASH =
2499 const TokenType('BACKSLASH', TokenClass.NO_CLASS, "\\"); 2543 const TokenType('BACKSLASH', TokenClass.NO_CLASS, "\\");
2500 2544
2501 static const TokenType PERIOD_PERIOD_PERIOD = 2545 static const TokenType PERIOD_PERIOD_PERIOD =
2502 const TokenType('PERIOD_PERIOD_PERIOD', TokenClass.NO_CLASS, "..."); 2546 const TokenType('PERIOD_PERIOD_PERIOD', TokenClass.NO_CLASS, "...");
2503 2547
2548 static const TokenType GENERIC_METHOD_TYPE_LIST =
2549 const TokenType('GENERIC_METHOD_TYPE_LIST');
2550
2551 static const TokenType GENERIC_METHOD_TYPE_ASSIGN =
2552 const TokenType('GENERIC_METHOD_TYPE_ASSIGN');
2553
2504 /** 2554 /**
2505 * The class of the token. 2555 * The class of the token.
2506 */ 2556 */
2507 final TokenClass _tokenClass; 2557 final TokenClass _tokenClass;
2508 2558
2509 /** 2559 /**
2510 * The name of the token type. 2560 * The name of the token type.
2511 */ 2561 */
2512 final String name; 2562 final String name;
2513 2563
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
2662 CommentToken get precedingComments => _precedingComment; 2712 CommentToken get precedingComments => _precedingComment;
2663 2713
2664 void set precedingComments(CommentToken comment) { 2714 void set precedingComments(CommentToken comment) {
2665 _precedingComment = comment; 2715 _precedingComment = comment;
2666 _setCommentParent(_precedingComment); 2716 _setCommentParent(_precedingComment);
2667 } 2717 }
2668 2718
2669 @override 2719 @override
2670 Token copy() => new TokenWithComment(type, offset, precedingComments); 2720 Token copy() => new TokenWithComment(type, offset, precedingComments);
2671 } 2721 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698