| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 licenset hat can be found in the LICENSE file. | 3 // BSD-style licenset hat can be found in the LICENSE file. |
| 4 | 4 |
| 5 library fasta.scanner.recover; | 5 library fasta.scanner.recover; |
| 6 | 6 |
| 7 import '../../scanner/token.dart' show TokenType; | 7 import '../../scanner/token.dart' show TokenType; |
| 8 | 8 |
| 9 import '../fasta_codes.dart' | 9 import '../fasta_codes.dart' |
| 10 show | 10 show |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 return skipToEof(errorTail); | 152 return skipToEof(errorTail); |
| 153 } | 153 } |
| 154 | 154 |
| 155 recoverUnmatched() { | 155 recoverUnmatched() { |
| 156 // TODO(ahe): Try to use top-level keywords (such as `class`, `typedef`, | 156 // TODO(ahe): Try to use top-level keywords (such as `class`, `typedef`, |
| 157 // and `enum`) and identation to recover. | 157 // and `enum`) and identation to recover. |
| 158 return errorTail.next; | 158 return errorTail.next; |
| 159 } | 159 } |
| 160 | 160 |
| 161 for (Token current = tokens; !current.isEof; current = current.next) { | 161 for (Token current = tokens; !current.isEof; current = current.next) { |
| 162 if (current is ErrorToken) { | 162 while (current is ErrorToken) { |
| 163 ErrorToken first = current; | 163 ErrorToken first = current; |
| 164 Token next = current; | 164 Token next = current; |
| 165 bool treatAsWhitespace = false; | |
| 166 do { | 165 do { |
| 167 current = next; | 166 current = next; |
| 168 if (errorTail == null) { | 167 if (errorTail == null) { |
| 169 error = next; | 168 error = next; |
| 170 } else { | 169 } else { |
| 171 errorTail.next = next; | 170 errorTail.next = next; |
| 172 next.previous = errorTail; | 171 next.previous = errorTail; |
| 173 } | 172 } |
| 174 errorTail = next; | 173 errorTail = next; |
| 175 next = next.next; | 174 next = next.next; |
| 176 } while (next is ErrorToken && first.errorCode == next.errorCode); | 175 } while (next is ErrorToken && first.errorCode == next.errorCode); |
| 177 | 176 |
| 178 FastaCode code = first.errorCode; | 177 FastaCode code = first.errorCode; |
| 179 if (code == codeEncoding || | 178 if (code == codeEncoding || |
| 180 code == codeNonAsciiWhitespace || | 179 code == codeNonAsciiWhitespace || |
| 181 code == codeAsciiControlCharacter) { | 180 code == codeAsciiControlCharacter) { |
| 182 treatAsWhitespace = true; | 181 current = errorTail.next; |
| 183 } else if (code == codeNonAsciiIdentifier) { | 182 } else if (code == codeNonAsciiIdentifier) { |
| 184 current = recoverIdentifier(first); | 183 current = recoverIdentifier(first); |
| 185 assert(current.next != null); | 184 assert(current.next != null); |
| 186 } else if (code == codeMissingExponent) { | 185 } else if (code == codeMissingExponent) { |
| 187 current = recoverExponent(); | 186 current = recoverExponent(); |
| 188 assert(current.next != null); | 187 assert(current.next != null); |
| 189 } else if (code == codeUnterminatedString) { | 188 } else if (code == codeUnterminatedString) { |
| 190 current = recoverString(); | 189 current = recoverString(); |
| 191 assert(current.next != null); | 190 assert(current.next != null); |
| 192 } else if (code == codeExpectedHexDigit) { | 191 } else if (code == codeExpectedHexDigit) { |
| 193 current = recoverHexDigit(); | 192 current = recoverHexDigit(); |
| 194 assert(current.next != null); | 193 assert(current.next != null); |
| 195 } else if (code == codeUnexpectedDollarInString) { | 194 } else if (code == codeUnexpectedDollarInString) { |
| 196 current = recoverStringInterpolation(); | 195 current = recoverStringInterpolation(); |
| 197 assert(current.next != null); | 196 assert(current.next != null); |
| 198 } else if (code == codeUnterminatedComment) { | 197 } else if (code == codeUnterminatedComment) { |
| 199 current = recoverComment(); | 198 current = recoverComment(); |
| 200 assert(current.next != null); | 199 assert(current.next != null); |
| 201 } else if (code == codeUnmatchedToken) { | 200 } else if (code == codeUnmatchedToken) { |
| 202 current = recoverUnmatched(); | 201 current = recoverUnmatched(); |
| 203 assert(current.next != null); | 202 assert(current.next != null); |
| 204 } else { | 203 } else { |
| 205 treatAsWhitespace = true; | 204 current = errorTail.next; |
| 206 } | 205 } |
| 207 if (treatAsWhitespace) continue; | |
| 208 } | 206 } |
| 209 if (goodTail == null) { | 207 if (goodTail == null) { |
| 210 good = current; | 208 good = current; |
| 211 } else { | 209 } else { |
| 212 goodTail.next = current; | 210 goodTail.next = current; |
| 213 current.previous = goodTail; | 211 current.previous = goodTail; |
| 214 } | 212 } |
| 215 beforeGoodTail = goodTail; | 213 beforeGoodTail = goodTail; |
| 216 goodTail = current; | 214 goodTail = current; |
| 217 } | 215 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 242 | 240 |
| 243 String closeBraceFor(String openBrace) { | 241 String closeBraceFor(String openBrace) { |
| 244 return const { | 242 return const { |
| 245 '(': ')', | 243 '(': ')', |
| 246 '[': ']', | 244 '[': ']', |
| 247 '{': '}', | 245 '{': '}', |
| 248 '<': '>', | 246 '<': '>', |
| 249 r'${': '}', | 247 r'${': '}', |
| 250 }[openBrace]; | 248 }[openBrace]; |
| 251 } | 249 } |
| OLD | NEW |