Chromium Code Reviews| OLD | NEW |
|---|---|
| 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.incremental_resolver; | 5 library engine.incremental_resolver; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 import 'dart:math' as math; | 8 import 'dart:math' as math; |
| 9 | 9 |
| 10 import 'ast.dart'; | 10 import 'ast.dart'; |
| (...skipping 1243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1254 } | 1254 } |
| 1255 | 1255 |
| 1256 /** | 1256 /** |
| 1257 * Attempts to update [oldUnit] to the state corresponding to [newCode]. | 1257 * Attempts to update [oldUnit] to the state corresponding to [newCode]. |
| 1258 * Returns `true` if success, or `false` otherwise. | 1258 * Returns `true` if success, or `false` otherwise. |
| 1259 * The [oldUnit] might be damaged. | 1259 * The [oldUnit] might be damaged. |
| 1260 */ | 1260 */ |
| 1261 bool resolve(String newCode) { | 1261 bool resolve(String newCode) { |
| 1262 logger.enter('diff/resolve $_unitSource'); | 1262 logger.enter('diff/resolve $_unitSource'); |
| 1263 try { | 1263 try { |
| 1264 // prepare old unit | |
| 1264 CompilationUnit oldUnit = _units[_unitSource]; | 1265 CompilationUnit oldUnit = _units[_unitSource]; |
| 1266 if (!_areCurlyBracketsBalanced(oldUnit.beginToken)) { | |
| 1267 logger.log('Unbalanced number of curly brackets in the old unit.'); | |
| 1268 return false; | |
| 1269 } | |
| 1265 _unitElement = oldUnit.element; | 1270 _unitElement = oldUnit.element; |
| 1271 // prepare ne unit | |
|
Brian Wilkerson
2015/01/20 19:08:27
'ne' --> 'new'
| |
| 1266 CompilationUnit newUnit = _parseUnit(newCode); | 1272 CompilationUnit newUnit = _parseUnit(newCode); |
| 1273 if (!_areCurlyBracketsBalanced(newUnit.beginToken)) { | |
| 1274 logger.log('Unbalanced number of curly brackets in the new unit.'); | |
| 1275 return false; | |
| 1276 } | |
| 1277 // find difference | |
| 1267 _TokenPair firstPair = | 1278 _TokenPair firstPair = |
| 1268 _findFirstDifferentToken(oldUnit.beginToken, newUnit.beginToken); | 1279 _findFirstDifferentToken(oldUnit.beginToken, newUnit.beginToken); |
| 1269 _TokenPair lastPair = | 1280 _TokenPair lastPair = |
| 1270 _findLastDifferentToken(oldUnit.endToken, newUnit.endToken); | 1281 _findLastDifferentToken(oldUnit.endToken, newUnit.endToken); |
| 1271 if (firstPair != null && lastPair != null) { | 1282 if (firstPair != null && lastPair != null) { |
| 1272 int firstOffsetOld = firstPair.oldToken.offset; | 1283 int firstOffsetOld = firstPair.oldToken.offset; |
| 1273 int firstOffsetNew = firstPair.newToken.offset; | 1284 int firstOffsetNew = firstPair.newToken.offset; |
| 1274 int lastOffsetOld = lastPair.oldToken.end; | 1285 int lastOffsetOld = lastPair.oldToken.end; |
| 1275 int lastOffsetNew = lastPair.newToken.end; | 1286 int lastOffsetNew = lastPair.newToken.end; |
| 1276 int beginOffsetOld = math.min(firstOffsetOld, lastOffsetOld); | 1287 int beginOffsetOld = math.min(firstOffsetOld, lastOffsetOld); |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1478 } | 1489 } |
| 1479 token = token.next; | 1490 token = token.next; |
| 1480 } | 1491 } |
| 1481 } | 1492 } |
| 1482 | 1493 |
| 1483 void _updateEntry() { | 1494 void _updateEntry() { |
| 1484 _entry.setValue(DartEntry.SCAN_ERRORS, _newScanErrors); | 1495 _entry.setValue(DartEntry.SCAN_ERRORS, _newScanErrors); |
| 1485 _entry.setValue(DartEntry.PARSE_ERRORS, _newParseErrors); | 1496 _entry.setValue(DartEntry.PARSE_ERRORS, _newParseErrors); |
| 1486 } | 1497 } |
| 1487 | 1498 |
| 1499 /** | |
| 1500 * Checks if [token] has a balanced number of open and closed curly brackets. | |
| 1501 */ | |
| 1502 static bool _areCurlyBracketsBalanced(Token token) { | |
|
Brian Wilkerson
2015/01/20 19:08:27
Not for this CL, but... The scanner already comput
| |
| 1503 int numOpen = _getTokenCount(token, TokenType.OPEN_CURLY_BRACKET); | |
| 1504 int numOpen2 = | |
| 1505 _getTokenCount(token, TokenType.STRING_INTERPOLATION_EXPRESSION); | |
| 1506 int numClosed = _getTokenCount(token, TokenType.CLOSE_CURLY_BRACKET); | |
| 1507 return numOpen + numOpen2 == numClosed; | |
| 1508 } | |
| 1509 | |
| 1488 static _TokenDifferenceKind _compareToken(Token oldToken, Token newToken, | 1510 static _TokenDifferenceKind _compareToken(Token oldToken, Token newToken, |
| 1489 int delta) { | 1511 int delta) { |
| 1490 if (oldToken == null && newToken == null) { | 1512 if (oldToken == null && newToken == null) { |
| 1491 return null; | 1513 return null; |
| 1492 } | 1514 } |
| 1493 if (oldToken == null || newToken == null) { | 1515 if (oldToken == null || newToken == null) { |
| 1494 return _TokenDifferenceKind.CONTENT; | 1516 return _TokenDifferenceKind.CONTENT; |
| 1495 } | 1517 } |
| 1496 if (oldToken.type != newToken.type) { | 1518 if (oldToken.type != newToken.type) { |
| 1497 return _TokenDifferenceKind.CONTENT; | 1519 return _TokenDifferenceKind.CONTENT; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1573 } | 1595 } |
| 1574 | 1596 |
| 1575 static Token _getBeginTokenNotComment(AstNode node) { | 1597 static Token _getBeginTokenNotComment(AstNode node) { |
| 1576 Token oldBeginToken = node.beginToken; | 1598 Token oldBeginToken = node.beginToken; |
| 1577 if (oldBeginToken is CommentToken) { | 1599 if (oldBeginToken is CommentToken) { |
| 1578 oldBeginToken = (oldBeginToken as CommentToken).parent; | 1600 oldBeginToken = (oldBeginToken as CommentToken).parent; |
| 1579 } | 1601 } |
| 1580 return oldBeginToken; | 1602 return oldBeginToken; |
| 1581 } | 1603 } |
| 1582 | 1604 |
| 1583 | |
| 1584 static List<AstNode> _getParents(AstNode node) { | 1605 static List<AstNode> _getParents(AstNode node) { |
| 1585 List<AstNode> parents = <AstNode>[]; | 1606 List<AstNode> parents = <AstNode>[]; |
| 1586 while (node != null) { | 1607 while (node != null) { |
| 1587 parents.insert(0, node); | 1608 parents.insert(0, node); |
| 1588 node = node.parent; | 1609 node = node.parent; |
| 1589 } | 1610 } |
| 1590 return parents; | 1611 return parents; |
| 1591 } | 1612 } |
| 1592 | 1613 |
| 1614 | |
| 1615 /** | |
| 1616 * Returns number of tokens with the given [type]. | |
| 1617 */ | |
| 1618 static int _getTokenCount(Token token, TokenType type) { | |
| 1619 int count = 0; | |
| 1620 while (token.type != TokenType.EOF) { | |
| 1621 if (token.type == type) { | |
| 1622 count++; | |
| 1623 } | |
| 1624 token = token.next; | |
| 1625 } | |
| 1626 return count; | |
| 1627 } | |
| 1628 | |
| 1593 /** | 1629 /** |
| 1594 * Set the given [comment] as a "precedingComments" for [parent]. | 1630 * Set the given [comment] as a "precedingComments" for [parent]. |
| 1595 */ | 1631 */ |
| 1596 static void _setPrecedingComments(Token parent, CommentToken comment) { | 1632 static void _setPrecedingComments(Token parent, CommentToken comment) { |
| 1597 if (parent is BeginTokenWithComment) { | 1633 if (parent is BeginTokenWithComment) { |
| 1598 parent.precedingComments = comment; | 1634 parent.precedingComments = comment; |
| 1599 } else if (parent is KeywordTokenWithComment) { | 1635 } else if (parent is KeywordTokenWithComment) { |
| 1600 parent.precedingComments = comment; | 1636 parent.precedingComments = comment; |
| 1601 } else if (parent is StringTokenWithComment) { | 1637 } else if (parent is StringTokenWithComment) { |
| 1602 parent.precedingComments = comment; | 1638 parent.precedingComments = comment; |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1881 String toString() => name; | 1917 String toString() => name; |
| 1882 } | 1918 } |
| 1883 | 1919 |
| 1884 | 1920 |
| 1885 class _TokenPair { | 1921 class _TokenPair { |
| 1886 final _TokenDifferenceKind kind; | 1922 final _TokenDifferenceKind kind; |
| 1887 final Token oldToken; | 1923 final Token oldToken; |
| 1888 final Token newToken; | 1924 final Token newToken; |
| 1889 _TokenPair(this.kind, this.oldToken, this.newToken); | 1925 _TokenPair(this.kind, this.oldToken, this.newToken); |
| 1890 } | 1926 } |
| OLD | NEW |