| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 formatter_impl; | 5 library formatter_impl; |
| 6 | 6 |
| 7 import 'dart:math'; | 7 import 'dart:math'; |
| 8 | 8 |
| 9 import 'package:analyzer/analyzer.dart'; | 9 import 'package:analyzer/analyzer.dart'; |
| 10 import 'package:analyzer/src/generated/parser.dart'; | 10 import 'package:analyzer/src/generated/parser.dart'; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 /// Creates a new FormatterException with an optional error [message]. | 44 /// Creates a new FormatterException with an optional error [message]. |
| 45 const FormatterException([this.message = 'FormatterException']); | 45 const FormatterException([this.message = 'FormatterException']); |
| 46 | 46 |
| 47 FormatterException.forError(List<AnalysisError> errors, [LineInfo line]) : | 47 FormatterException.forError(List<AnalysisError> errors, [LineInfo line]) : |
| 48 message = _createMessage(errors); | 48 message = _createMessage(errors); |
| 49 | 49 |
| 50 static String _createMessage(errors) { | 50 static String _createMessage(errors) { |
| 51 //TODO(pquitslund): consider a verbosity flag to add/suppress details | 51 //TODO(pquitslund): consider a verbosity flag to add/suppress details |
| 52 var errorCode = errors[0].errorCode; | 52 var errorCode = errors[0].errorCode; |
| 53 var phase = errorCode is ParserErrorCode ? 'parsing' : 'scanning'; | 53 var phase = errorCode is ParserErrorCode ? 'parsing' : 'scanning'; |
| 54 return 'An error occured while ${phase} (${errorCode.name}).'; | 54 return 'An error occured while $phase (${errorCode.name}).'; |
| 55 } | 55 } |
| 56 | 56 |
| 57 String toString() => '$message'; | 57 String toString() => '$message'; |
| 58 } | 58 } |
| 59 | 59 |
| 60 /// Specifies the kind of code snippet to format. | 60 /// Specifies the kind of code snippet to format. |
| 61 class CodeKind { | 61 class CodeKind { |
| 62 | 62 |
| 63 final int _index; | 63 final int _index; |
| 64 | 64 |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 if (!checkTokens()) { | 199 if (!checkTokens()) { |
| 200 throwNotEqualException(token1, token2); | 200 throwNotEqualException(token1, token2); |
| 201 } | 201 } |
| 202 advance(); | 202 advance(); |
| 203 | 203 |
| 204 } | 204 } |
| 205 // TODO(pquitslund): consider a better way to notice trailing synthetics | 205 // TODO(pquitslund): consider a better way to notice trailing synthetics |
| 206 if (!isEOF(token2) && | 206 if (!isEOF(token2) && |
| 207 !(isCLOSE_CURLY_BRACKET(token2) && isEOF(token2.next))) { | 207 !(isCLOSE_CURLY_BRACKET(token2) && isEOF(token2.next))) { |
| 208 throw new FormatterException( | 208 throw new FormatterException( |
| 209 'Expected "EOF" but got "${token2}".'); | 209 'Expected "EOF" but got "$token2".'); |
| 210 } | 210 } |
| 211 } | 211 } |
| 212 | 212 |
| 213 checkPrecedingComments() { | 213 checkPrecedingComments() { |
| 214 var comment1 = token1.precedingComments; | 214 var comment1 = token1.precedingComments; |
| 215 var comment2 = token2.precedingComments; | 215 var comment2 = token2.precedingComments; |
| 216 while (comment1 != null) { | 216 while (comment1 != null) { |
| 217 if (comment2 == null) { | 217 if (comment2 == null) { |
| 218 throw new FormatterException( | 218 throw new FormatterException( |
| 219 'Expected comment, "${comment1}", at ${describeLocation(token1)}, ' | 219 'Expected comment, "$comment1", at ${describeLocation(token1)}, ' |
| 220 'but got none.'); | 220 'but got none.'); |
| 221 } | 221 } |
| 222 if (!equivalentComments(comment1, comment2)) { | 222 if (!equivalentComments(comment1, comment2)) { |
| 223 throwNotEqualException(comment1, comment2); | 223 throwNotEqualException(comment1, comment2); |
| 224 } | 224 } |
| 225 comment1 = comment1.next; | 225 comment1 = comment1.next; |
| 226 comment2 = comment2.next; | 226 comment2 = comment2.next; |
| 227 } | 227 } |
| 228 if (comment2 != null) { | 228 if (comment2 != null) { |
| 229 throw new FormatterException( | 229 throw new FormatterException( |
| 230 'Unexpected comment, "${comment2}", at ${describeLocation(token2)}.'); | 230 'Unexpected comment, "$comment2", at ${describeLocation(token2)}.'); |
| 231 } | 231 } |
| 232 } | 232 } |
| 233 | 233 |
| 234 bool equivalentComments(Token comment1, Token comment2) => | 234 bool equivalentComments(Token comment1, Token comment2) => |
| 235 comment1.lexeme.trim() == comment2.lexeme.trim(); | 235 comment1.lexeme.trim() == comment2.lexeme.trim(); |
| 236 | 236 |
| 237 throwNotEqualException(t1, t2) { | 237 throwNotEqualException(t1, t2) { |
| 238 throw new FormatterException( | 238 throw new FormatterException( |
| 239 'Expected "${t1}" but got "${t2}", at ${describeLocation(t1)}.'); | 239 'Expected "$t1" but got "$t2", at ${describeLocation(t1)}.'); |
| 240 } | 240 } |
| 241 | 241 |
| 242 String describeLocation(Token token) => lineInfo == null ? '<unknown>' : | 242 String describeLocation(Token token) => lineInfo == null ? '<unknown>' : |
| 243 'Line: ${lineInfo.getLocation(token.offset).lineNumber}, ' | 243 'Line: ${lineInfo.getLocation(token.offset).lineNumber}, ' |
| 244 'Column: ${lineInfo.getLocation(token.offset).columnNumber}'; | 244 'Column: ${lineInfo.getLocation(token.offset).columnNumber}'; |
| 245 | 245 |
| 246 advance() { | 246 advance() { |
| 247 token1 = token1.next; | 247 token1 = token1.next; |
| 248 token2 = token2.next; | 248 token2 = token2.next; |
| 249 } | 249 } |
| (...skipping 1595 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1845 @override | 1845 @override |
| 1846 visitEnumConstantDeclaration(EnumConstantDeclaration node) { | 1846 visitEnumConstantDeclaration(EnumConstantDeclaration node) { |
| 1847 // TODO: implement visitEnumConstantDeclaration | 1847 // TODO: implement visitEnumConstantDeclaration |
| 1848 } | 1848 } |
| 1849 | 1849 |
| 1850 @override | 1850 @override |
| 1851 visitEnumDeclaration(EnumDeclaration node) { | 1851 visitEnumDeclaration(EnumDeclaration node) { |
| 1852 // TODO: implement visitEnumDeclaration | 1852 // TODO: implement visitEnumDeclaration |
| 1853 } | 1853 } |
| 1854 } | 1854 } |
| OLD | NEW |