| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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.array_based_scanner; | 5 library fasta.scanner.array_based_scanner; |
| 6 | 6 |
| 7 import 'error_token.dart' show ErrorToken, UnmatchedToken; | 7 import 'error_token.dart' show ErrorToken, UnmatchedToken; |
| 8 | 8 |
| 9 import '../../scanner/token.dart' | 9 import '../../scanner/token.dart' |
| 10 show Keyword, KeywordTokenWithComment, Token, TokenType; | 10 show |
| 11 BeginToken, |
| 12 BeginTokenWithComment, |
| 13 Keyword, |
| 14 KeywordTokenWithComment, |
| 15 SyntheticToken, |
| 16 Token, |
| 17 TokenType, |
| 18 TokenWithComment; |
| 11 | 19 |
| 12 import 'token.dart' | 20 import 'token.dart' show StringToken; |
| 13 show BeginGroupToken, StringToken, SymbolToken, SyntheticSymbolToken; | |
| 14 | 21 |
| 15 import 'token_constants.dart' | 22 import 'token_constants.dart' |
| 16 show | 23 show |
| 17 LT_TOKEN, | 24 LT_TOKEN, |
| 18 OPEN_CURLY_BRACKET_TOKEN, | 25 OPEN_CURLY_BRACKET_TOKEN, |
| 19 OPEN_PAREN_TOKEN, | 26 OPEN_PAREN_TOKEN, |
| 20 STRING_INTERPOLATION_TOKEN; | 27 STRING_INTERPOLATION_TOKEN; |
| 21 | 28 |
| 22 import 'characters.dart' show $LF, $STX; | 29 import 'characters.dart' show $LF, $STX; |
| 23 | 30 |
| 24 import 'abstract_scanner.dart' show AbstractScanner, closeBraceInfoFor; | 31 import 'abstract_scanner.dart' show AbstractScanner, closeBraceInfoFor; |
| 25 | 32 |
| 26 import '../util/link.dart' show Link; | 33 import '../util/link.dart' show Link; |
| 27 | 34 |
| 28 abstract class ArrayBasedScanner extends AbstractScanner { | 35 abstract class ArrayBasedScanner extends AbstractScanner { |
| 29 bool hasErrors = false; | 36 bool hasErrors = false; |
| 30 | 37 |
| 31 ArrayBasedScanner(bool includeComments, bool scanGenericMethodComments, | 38 ArrayBasedScanner(bool includeComments, bool scanGenericMethodComments, |
| 32 {int numberOfBytesHint}) | 39 {int numberOfBytesHint}) |
| 33 : super(includeComments, scanGenericMethodComments, | 40 : super(includeComments, scanGenericMethodComments, |
| 34 numberOfBytesHint: numberOfBytesHint); | 41 numberOfBytesHint: numberOfBytesHint); |
| 35 | 42 |
| 36 /** | 43 /** |
| 37 * The stack of open groups, e.g [: { ... ( .. :] | 44 * The stack of open groups, e.g [: { ... ( .. :] |
| 38 * Each BeginGroupToken has a pointer to the token where the group | 45 * Each BeginToken has a pointer to the token where the group |
| 39 * ends. This field is set when scanning the end group token. | 46 * ends. This field is set when scanning the end group token. |
| 40 */ | 47 */ |
| 41 Link<BeginGroupToken> groupingStack = const Link<BeginGroupToken>(); | 48 Link<BeginToken> groupingStack = const Link<BeginToken>(); |
| 42 | 49 |
| 43 /** | 50 /** |
| 44 * Appends a fixed token whose kind and content is determined by [type]. | 51 * Appends a fixed token whose kind and content is determined by [type]. |
| 45 * Appends an *operator* token from [type]. | 52 * Appends an *operator* token from [type]. |
| 46 * | 53 * |
| 47 * An operator token represent operators like ':', '.', ';', '&&', '==', '--', | 54 * An operator token represent operators like ':', '.', ';', '&&', '==', '--', |
| 48 * '=>', etc. | 55 * '=>', etc. |
| 49 */ | 56 */ |
| 50 void appendPrecedenceToken(TokenType type) { | 57 void appendPrecedenceToken(TokenType type) { |
| 51 appendToken(new SymbolToken(type, tokenStart, comments)); | 58 appendToken(new TokenWithComment(type, tokenStart, comments)); |
| 52 } | 59 } |
| 53 | 60 |
| 54 /** | 61 /** |
| 55 * Appends a fixed token based on whether the current char is [choice] or not. | 62 * Appends a fixed token based on whether the current char is [choice] or not. |
| 56 * If the current char is [choice] a fixed token whose kind and content | 63 * If the current char is [choice] a fixed token whose kind and content |
| 57 * is determined by [yes] is appended, otherwise a fixed token whose kind | 64 * is determined by [yes] is appended, otherwise a fixed token whose kind |
| 58 * and content is determined by [no] is appended. | 65 * and content is determined by [no] is appended. |
| 59 */ | 66 */ |
| 60 int select(int choice, TokenType yes, TokenType no) { | 67 int select(int choice, TokenType yes, TokenType no) { |
| 61 int next = advance(); | 68 int next = advance(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 80 appendToken(new KeywordTokenWithComment(keyword, tokenStart, comments)); | 87 appendToken(new KeywordTokenWithComment(keyword, tokenStart, comments)); |
| 81 } | 88 } |
| 82 | 89 |
| 83 void appendEofToken() { | 90 void appendEofToken() { |
| 84 beginToken(); | 91 beginToken(); |
| 85 discardOpenLt(); | 92 discardOpenLt(); |
| 86 while (!groupingStack.isEmpty) { | 93 while (!groupingStack.isEmpty) { |
| 87 unmatchedBeginGroup(groupingStack.head); | 94 unmatchedBeginGroup(groupingStack.head); |
| 88 groupingStack = groupingStack.tail; | 95 groupingStack = groupingStack.tail; |
| 89 } | 96 } |
| 90 appendToken(new SymbolToken.eof(tokenStart, comments)); | 97 appendToken(new Token.eof(tokenStart, comments)); |
| 91 } | 98 } |
| 92 | 99 |
| 93 /** | 100 /** |
| 94 * Notifies scanning a whitespace character. Note that [appendWhiteSpace] is | 101 * Notifies scanning a whitespace character. Note that [appendWhiteSpace] is |
| 95 * not always invoked for [$SPACE] characters. | 102 * not always invoked for [$SPACE] characters. |
| 96 * | 103 * |
| 97 * This method is used by the scanners to track line breaks and create the | 104 * This method is used by the scanners to track line breaks and create the |
| 98 * [lineStarts] map. | 105 * [lineStarts] map. |
| 99 */ | 106 */ |
| 100 void appendWhiteSpace(int next) { | 107 void appendWhiteSpace(int next) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 111 */ | 118 */ |
| 112 void lineFeedInMultiline() { | 119 void lineFeedInMultiline() { |
| 113 lineStarts.add(stringOffset + 1); | 120 lineStarts.add(stringOffset + 1); |
| 114 } | 121 } |
| 115 | 122 |
| 116 /** | 123 /** |
| 117 * Appends a token that begins a new group, represented by [type]. | 124 * Appends a token that begins a new group, represented by [type]. |
| 118 * Group begin tokens are '{', '(', '[' and '${'. | 125 * Group begin tokens are '{', '(', '[' and '${'. |
| 119 */ | 126 */ |
| 120 void appendBeginGroup(TokenType type) { | 127 void appendBeginGroup(TokenType type) { |
| 121 Token token = new BeginGroupToken(type, tokenStart, comments); | 128 Token token = new BeginTokenWithComment(type, tokenStart, comments); |
| 122 appendToken(token); | 129 appendToken(token); |
| 123 | 130 |
| 124 // { [ ${ cannot appear inside a type parameters / arguments. | 131 // { [ ${ cannot appear inside a type parameters / arguments. |
| 125 if (!identical(type.kind, LT_TOKEN) && | 132 if (!identical(type.kind, LT_TOKEN) && |
| 126 !identical(type.kind, OPEN_PAREN_TOKEN)) { | 133 !identical(type.kind, OPEN_PAREN_TOKEN)) { |
| 127 discardOpenLt(); | 134 discardOpenLt(); |
| 128 } | 135 } |
| 129 groupingStack = groupingStack.prepend(token); | 136 groupingStack = groupingStack.prepend(token); |
| 130 } | 137 } |
| 131 | 138 |
| 132 /** | 139 /** |
| 133 * Appends a token that begins an end group, represented by [type]. | 140 * Appends a token that begins an end group, represented by [type]. |
| 134 * It handles the group end tokens '}', ')' and ']'. The tokens '>' and | 141 * It handles the group end tokens '}', ')' and ']'. The tokens '>' and |
| 135 * '>>' are handled separately bo [appendGt] and [appendGtGt]. | 142 * '>>' are handled separately bo [appendGt] and [appendGtGt]. |
| 136 */ | 143 */ |
| 137 int appendEndGroup(TokenType type, int openKind) { | 144 int appendEndGroup(TokenType type, int openKind) { |
| 138 assert(!identical(openKind, LT_TOKEN)); // openKind is < for > and >> | 145 assert(!identical(openKind, LT_TOKEN)); // openKind is < for > and >> |
| 139 discardBeginGroupUntil(openKind); | 146 discardBeginGroupUntil(openKind); |
| 140 appendPrecedenceToken(type); | 147 appendPrecedenceToken(type); |
| 141 Token close = tail; | 148 Token close = tail; |
| 142 if (groupingStack.isEmpty) { | 149 if (groupingStack.isEmpty) { |
| 143 return advance(); | 150 return advance(); |
| 144 } | 151 } |
| 145 BeginGroupToken begin = groupingStack.head; | 152 BeginToken begin = groupingStack.head; |
| 146 if (!identical(begin.kind, openKind)) { | 153 if (!identical(begin.kind, openKind)) { |
| 147 assert(begin.kind == STRING_INTERPOLATION_TOKEN && | 154 assert(begin.kind == STRING_INTERPOLATION_TOKEN && |
| 148 openKind == OPEN_CURLY_BRACKET_TOKEN); | 155 openKind == OPEN_CURLY_BRACKET_TOKEN); |
| 149 // We're ending an interpolated expression. | 156 // We're ending an interpolated expression. |
| 150 begin.endGroup = close; | 157 begin.endGroup = close; |
| 151 groupingStack = groupingStack.tail; | 158 groupingStack = groupingStack.tail; |
| 152 // Using "start-of-text" to signal that we're back in string | 159 // Using "start-of-text" to signal that we're back in string |
| 153 // scanning mode. | 160 // scanning mode. |
| 154 return $STX; | 161 return $STX; |
| 155 } | 162 } |
| 156 begin.endGroup = close; | 163 begin.endGroup = close; |
| 157 groupingStack = groupingStack.tail; | 164 groupingStack = groupingStack.tail; |
| 158 return advance(); | 165 return advance(); |
| 159 } | 166 } |
| 160 | 167 |
| 161 /** | 168 /** |
| 162 * Discards begin group tokens until a match with [openKind] is found. | 169 * Discards begin group tokens until a match with [openKind] is found. |
| 163 * This recovers nicely from from a situation like "{[}". | 170 * This recovers nicely from from a situation like "{[}". |
| 164 */ | 171 */ |
| 165 void discardBeginGroupUntil(int openKind) { | 172 void discardBeginGroupUntil(int openKind) { |
| 166 while (!groupingStack.isEmpty) { | 173 while (!groupingStack.isEmpty) { |
| 167 // Don't report unmatched errors for <; it is also the less-than operator. | 174 // Don't report unmatched errors for <; it is also the less-than operator. |
| 168 discardOpenLt(); | 175 discardOpenLt(); |
| 169 if (groupingStack.isEmpty) return; | 176 if (groupingStack.isEmpty) return; |
| 170 BeginGroupToken begin = groupingStack.head; | 177 BeginToken begin = groupingStack.head; |
| 171 if (openKind == begin.kind) return; | 178 if (openKind == begin.kind) return; |
| 172 if (openKind == OPEN_CURLY_BRACKET_TOKEN && | 179 if (openKind == OPEN_CURLY_BRACKET_TOKEN && |
| 173 begin.kind == STRING_INTERPOLATION_TOKEN) return; | 180 begin.kind == STRING_INTERPOLATION_TOKEN) return; |
| 174 unmatchedBeginGroup(begin); | 181 unmatchedBeginGroup(begin); |
| 175 groupingStack = groupingStack.tail; | 182 groupingStack = groupingStack.tail; |
| 176 } | 183 } |
| 177 } | 184 } |
| 178 | 185 |
| 179 /** | 186 /** |
| 180 * Appends a token for '>'. | 187 * Appends a token for '>'. |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 * something which cannot possibly be part of a type parameter/argument | 250 * something which cannot possibly be part of a type parameter/argument |
| 244 * list, like the '=' in the above example. | 251 * list, like the '=' in the above example. |
| 245 */ | 252 */ |
| 246 void discardOpenLt() { | 253 void discardOpenLt() { |
| 247 while (!groupingStack.isEmpty && | 254 while (!groupingStack.isEmpty && |
| 248 identical(groupingStack.head.kind, LT_TOKEN)) { | 255 identical(groupingStack.head.kind, LT_TOKEN)) { |
| 249 groupingStack = groupingStack.tail; | 256 groupingStack = groupingStack.tail; |
| 250 } | 257 } |
| 251 } | 258 } |
| 252 | 259 |
| 253 void unmatchedBeginGroup(BeginGroupToken begin) { | 260 void unmatchedBeginGroup(BeginToken begin) { |
| 254 // We want to ensure that unmatched BeginGroupTokens are reported as | 261 // We want to ensure that unmatched BeginTokens are reported as |
| 255 // errors. However, the diet parser assumes that groups are well-balanced | 262 // errors. However, the diet parser assumes that groups are well-balanced |
| 256 // and will never look at the endGroup token. This is a nice property that | 263 // and will never look at the endGroup token. This is a nice property that |
| 257 // allows us to skip quickly over correct code. By inserting an additional | 264 // allows us to skip quickly over correct code. By inserting an additional |
| 258 // synthetic token in the stream, we can keep ignoring endGroup tokens. | 265 // synthetic token in the stream, we can keep ignoring endGroup tokens. |
| 259 // | 266 // |
| 260 // [begin] --next--> [tail] | 267 // [begin] --next--> [tail] |
| 261 // [begin] --endG--> [synthetic] --next--> [next] --next--> [tail] | 268 // [begin] --endG--> [synthetic] --next--> [next] --next--> [tail] |
| 262 // | 269 // |
| 263 // This allows the diet parser to skip from [begin] via endGroup to | 270 // This allows the diet parser to skip from [begin] via endGroup to |
| 264 // [synthetic] and ignore the [synthetic] token (assuming it's correct), | 271 // [synthetic] and ignore the [synthetic] token (assuming it's correct), |
| (...skipping 24 matching lines...) Expand all Loading... |
| 289 // SymbolToken(})<----------------------------------+ | 296 // SymbolToken(})<----------------------------------+ |
| 290 // | | 297 // | |
| 291 // next | 298 // next |
| 292 // v | 299 // v |
| 293 // SymbolToken(;) | 300 // SymbolToken(;) |
| 294 // | | 301 // | |
| 295 // next | 302 // next |
| 296 // v | 303 // v |
| 297 // EOF | 304 // EOF |
| 298 TokenType type = closeBraceInfoFor(begin); | 305 TokenType type = closeBraceInfoFor(begin); |
| 299 appendToken(new SyntheticSymbolToken(type, tokenStart, comments)); | 306 appendToken(new SyntheticToken(type, tokenStart)); |
| 300 begin.endGroup = tail; | 307 begin.endGroup = tail; |
| 301 appendErrorToken(new UnmatchedToken(begin)); | 308 appendErrorToken(new UnmatchedToken(begin)); |
| 302 } | 309 } |
| 303 } | 310 } |
| OLD | NEW |