Changeset 219285 in webkit for trunk/Source/JavaScriptCore/parser/Lexer.cpp
- Timestamp:
- Jul 9, 2017, 7:34:55 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/parser/Lexer.cpp
r219263 r219285 1376 1376 } 1377 1377 1378 // While the lexer accepts <LF><CR> (not <CR><LF>) sequence1379 // as one line terminator and increments one line number,1380 // TemplateLiteral considers it as two line terminators <LF> and <CR>.1381 //1382 // TemplateLiteral normalizes line terminators as follows.1383 //1384 // <LF> => <LF>1385 // <CR> => <LF>1386 // <CR><LF> => <LF>1387 // <\u2028> => <\u2028>1388 // <\u2029> => <\u2029>1389 //1390 // So, <LF><CR> should be normalized to <LF><LF>.1391 // However, the lexer should increment the line number only once for <LF><CR>.1392 //1393 // To achieve this, LineNumberAdder holds the current status of line terminator sequence.1394 // When TemplateLiteral lexer encounters a line terminator, it notifies to LineNumberAdder.1395 // LineNumberAdder maintains the status and increments the line number when it's necessary.1396 // For example, LineNumberAdder increments the line number only once for <LF><CR> and <CR><LF>.1397 template<typename CharacterType>1398 class LineNumberAdder {1399 public:1400 LineNumberAdder(int& lineNumber)1401 : m_lineNumber(lineNumber)1402 {1403 }1404 1405 void clear()1406 {1407 m_previous = 0;1408 }1409 1410 void add(CharacterType character)1411 {1412 ASSERT(Lexer<CharacterType>::isLineTerminator(character));1413 if (m_previous == '\r' && character == '\n')1414 m_previous = 0;1415 else {1416 ++m_lineNumber;1417 m_previous = character;1418 }1419 }1420 1421 private:1422 int& m_lineNumber;1423 CharacterType m_previous { 0 };1424 };1425 1426 1378 template <typename T> 1427 1379 typename Lexer<T>::StringParseResult Lexer<T>::parseTemplateLiteral(JSTokenData* tokenData, RawStringsBuildMode rawStringsBuildMode) … … 1431 1383 const T* rawStringStart = currentSourcePtr(); 1432 1384 1433 LineNumberAdder<T> lineNumberAdder(m_lineNumber);1434 1435 1385 while (m_current != '`') { 1436 1386 if (UNLIKELY(m_current == '\\')) { 1437 lineNumberAdder.clear();1438 1387 if (stringStart != currentSourcePtr()) 1439 1388 append16(stringStart, currentSourcePtr() - stringStart); … … 1456 1405 } 1457 1406 1458 lineNumberAdder.add(m_current); 1459 shift(); 1460 if (m_current == '\n') { 1461 lineNumberAdder.add(m_current); 1462 shift(); 1463 } 1464 1407 shiftLineTerminator(); 1465 1408 rawStringStart = currentSourcePtr(); 1466 } else { 1467 lineNumberAdder.add(m_current); 1468 shift(); 1469 } 1409 } else 1410 shiftLineTerminator(); 1470 1411 } else { 1471 1412 bool strictMode = true; … … 1494 1435 if (atEnd()) { 1495 1436 m_lexErrorMessage = ASCIILiteral("Unexpected EOF"); 1496 return atEnd() ? StringUnterminated : StringCannotBeParsed;1437 return StringUnterminated; 1497 1438 } 1498 1439 … … 1508 1449 if (rawStringsBuildMode == RawStringsBuildMode::BuildRawStrings) 1509 1450 m_bufferForRawTemplateString16.append('\n'); 1510 lineNumberAdder.add(m_current); 1511 shift(); 1512 if (m_current == '\n') { 1513 lineNumberAdder.add(m_current); 1514 shift(); 1515 } 1451 shiftLineTerminator(); 1516 1452 stringStart = currentSourcePtr(); 1517 1453 rawStringStart = currentSourcePtr(); 1518 } else { 1519 lineNumberAdder.add(m_current); 1520 shift(); 1521 } 1454 } else 1455 shiftLineTerminator(); 1522 1456 continue; 1523 1457 } … … 1525 1459 } 1526 1460 1527 lineNumberAdder.clear();1528 1461 shift(); 1529 1462 }
Note:
See TracChangeset
for help on using the changeset viewer.