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

Side by Side Diff: pkg/front_end/lib/src/fasta/scanner/abstract_scanner.dart

Issue 2711453002: Implement shell-style comments and record shebangs as comments. (Closed)
Patch Set: Created 3 years, 10 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
« no previous file with comments | « no previous file | pkg/front_end/lib/src/fasta/scanner/array_based_scanner.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 library fasta.scanner.abstract_scanner; 5 library fasta.scanner.abstract_scanner;
6 6
7 import '../scanner.dart' show 7 import '../scanner.dart' show
8 ErrorToken, 8 ErrorToken,
9 Scanner, 9 Scanner,
10 buildUnexpectedCharacterToken; 10 buildUnexpectedCharacterToken;
(...skipping 12 matching lines...) Expand all
23 BeginGroupToken, 23 BeginGroupToken,
24 KeywordToken, 24 KeywordToken,
25 SymbolToken, 25 SymbolToken,
26 Token; 26 Token;
27 27
28 import 'token_constants.dart'; 28 import 'token_constants.dart';
29 29
30 import 'characters.dart'; 30 import 'characters.dart';
31 31
32 abstract class AbstractScanner implements Scanner { 32 abstract class AbstractScanner implements Scanner {
33 /// If true, include comments in the token stream.
33 final bool includeComments; 34 final bool includeComments;
34 35
36 /// If true, `#` starts a single line comment. This is the comment style
37 /// known from, for example, Unix shell and Python.
38 final bool enableShellStyleComments;
ahe 2017/02/21 16:57:10 I should probably remove this again. After thinkin
Paul Berry 2017/02/21 16:59:34 SGTM. I admit I was surprised you were heading do
ahe 2017/02/21 17:37:34 Yeah, it feels like a stupid idea now :-) Sometim
39
35 /** 40 /**
36 * The string offset for the next token that will be created. 41 * The string offset for the next token that will be created.
37 * 42 *
38 * Note that in the [Utf8BytesScanner], [stringOffset] and [scanOffset] values 43 * Note that in the [Utf8BytesScanner], [stringOffset] and [scanOffset] values
39 * are different. One string character can be encoded using multiple UTF-8 44 * are different. One string character can be encoded using multiple UTF-8
40 * bytes. 45 * bytes.
41 */ 46 */
42 int tokenStart = -1; 47 int tokenStart = -1;
43 48
44 /** 49 /**
45 * A pointer to the token stream created by this scanner. The first token 50 * A pointer to the token stream created by this scanner. The first token
46 * is a special token and not part of the source file. This is an 51 * is a special token and not part of the source file. This is an
47 * implementation detail to avoids special cases in the scanner. This token 52 * implementation detail to avoids special cases in the scanner. This token
48 * is not exposed to clients of the scanner, which are expected to invoke 53 * is not exposed to clients of the scanner, which are expected to invoke
49 * [firstToken] to access the token stream. 54 * [firstToken] to access the token stream.
50 */ 55 */
51 final Token tokens = new SymbolToken(EOF_INFO, -1); 56 final Token tokens = new SymbolToken(EOF_INFO, -1);
52 57
53 /** 58 /**
54 * A pointer to the last scanned token. 59 * A pointer to the last scanned token.
55 */ 60 */
56 Token tail; 61 Token tail;
57 62
58 final List<int> lineStarts = <int>[0]; 63 final List<int> lineStarts = <int>[0];
59 64
60 AbstractScanner(this.includeComments) { 65 AbstractScanner(this.includeComments, this.enableShellStyleComments) {
61 this.tail = this.tokens; 66 this.tail = this.tokens;
62 } 67 }
63 68
64 /** 69 /**
65 * Advances and returns the next character. 70 * Advances and returns the next character.
66 * 71 *
67 * If the next character is non-ASCII, then the returned value depends on the 72 * If the next character is non-ASCII, then the returned value depends on the
68 * scanner implementation. The [Utf8BytesScanner] returns a UTF-8 byte, while 73 * scanner implementation. The [Utf8BytesScanner] returns a UTF-8 byte, while
69 * the [StringScanner] returns a UTF-16 code unit. 74 * the [StringScanner] returns a UTF-16 code unit.
70 * 75 *
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 return unexpected(next); 399 return unexpected(next);
395 } 400 }
396 401
397 next = currentAsUnicode(next); 402 next = currentAsUnicode(next);
398 403
399 return unexpected(next); 404 return unexpected(next);
400 } 405 }
401 406
402 int tokenizeTag(int next) { 407 int tokenizeTag(int next) {
403 // # or #!.*[\n\r] 408 // # or #!.*[\n\r]
404 if (scanOffset == 0) { 409 if (scanOffset == 0 && identical(peek(), $BANG)) {
405 if (identical(peek(), $BANG)) { 410 tokenizeSingleLineComment(next, scanOffset + 1);
406 int start = scanOffset + 1; 411 } else if (enableShellStyleComments) {
407 bool asciiOnly = true; 412 tokenizeSingleLineComment(next, scanOffset);
408 do {
409 next = advance();
410 if (next > 127) asciiOnly = false;
411 } while (!identical(next, $LF) &&
412 !identical(next, $CR) &&
413 !identical(next, $EOF));
414 if (!asciiOnly) handleUnicode(start);
415 return next;
416 }
417 } 413 }
418 appendPrecedenceToken(HASH_INFO); 414 appendPrecedenceToken(HASH_INFO);
419 return advance(); 415 return advance();
420 } 416 }
421 417
422 int tokenizeTilde(int next) { 418 int tokenizeTilde(int next) {
423 // ~ ~/ ~/= 419 // ~ ~/ ~/=
424 next = advance(); 420 next = advance();
425 if (identical(next, $SLASH)) { 421 if (identical(next, $SLASH)) {
426 return select($EQ, TILDE_SLASH_EQ_INFO, TILDE_SLASH_INFO); 422 return select($EQ, TILDE_SLASH_EQ_INFO, TILDE_SLASH_INFO);
(...skipping 731 matching lines...) Expand 10 before | Expand all | Expand 10 after
1158 1154
1159 PrecedenceInfo closeBraceInfoFor(BeginGroupToken begin) { 1155 PrecedenceInfo closeBraceInfoFor(BeginGroupToken begin) {
1160 return const { 1156 return const {
1161 '(': CLOSE_PAREN_INFO, 1157 '(': CLOSE_PAREN_INFO,
1162 '[': CLOSE_SQUARE_BRACKET_INFO, 1158 '[': CLOSE_SQUARE_BRACKET_INFO,
1163 '{': CLOSE_CURLY_BRACKET_INFO, 1159 '{': CLOSE_CURLY_BRACKET_INFO,
1164 '<': GT_INFO, 1160 '<': GT_INFO,
1165 r'${': CLOSE_CURLY_BRACKET_INFO, 1161 r'${': CLOSE_CURLY_BRACKET_INFO,
1166 }[begin.value]; 1162 }[begin.value];
1167 } 1163 }
OLDNEW
« no previous file with comments | « no previous file | pkg/front_end/lib/src/fasta/scanner/array_based_scanner.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698