< prev index next >

src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/parser/Lexer.java

Print this page
rev 1391 : 8073613: Here documents: how to avoid string interpolation?


1434     /**
1435      * Scan over a here string.
1436      *
1437      * @return TRUE if is a here string.
1438      */
1439     private boolean scanHereString(final LineInfoReceiver lir) {
1440         assert ch0 == '<' && ch1 == '<';
1441         if (scripting) {
1442             // Record beginning of here string.
1443             final State saved = saveState();
1444 
1445             // << or <<<
1446             final boolean excludeLastEOL = ch2 != '<';
1447 
1448             if (excludeLastEOL) {
1449                 skip(2);
1450             } else {
1451                 skip(3);
1452             }
1453 
1454             // Scan identifier.





1455             final int identStart = position;
1456             final int identLength = scanIdentifier();








1457 
1458             // Check for identifier.
1459             if (identLength == 0) {
1460                 // Treat as shift.
1461                 restoreState(saved);
1462 
1463                 return false;
1464             }
1465 
1466             // Record rest of line.
1467             final State restState = saveState();
1468             // keep line number updated
1469             int lastLine = line;
1470 
1471             skipLine(false);
1472             lastLine++;
1473             int lastLinePosition = position;
1474             restState.setLimit(position);
1475 
1476             // Record beginning of string.


1506                 return false;
1507             }
1508 
1509             // Remove last end of line if specified.
1510             if (excludeLastEOL) {
1511                 // Handles \n.
1512                 if (content[stringEnd - 1] == '\n') {
1513                     stringEnd--;
1514                 }
1515 
1516                 // Handles \r and \r\n.
1517                 if (content[stringEnd - 1] == '\r') {
1518                     stringEnd--;
1519                 }
1520 
1521                 // Update end of string.
1522                 stringState.setLimit(stringEnd);
1523             }
1524 
1525             // Edit string if appropriate.
1526             if (scripting && !stringState.isEmpty()) {
1527                 editString(STRING, stringState);
1528             } else {
1529                 // Add here string.
1530                 add(STRING, stringState.position, stringState.limit);
1531             }
1532 
1533             // Scan rest of original line.
1534             final Lexer restLexer = new Lexer(this, restState);
1535 
1536             restLexer.lexify();
1537 
1538             return true;
1539         }
1540 
1541         return false;
1542     }
1543 
1544     /**
1545      * Breaks source content down into lex units, adding tokens to the token
1546      * stream. The routine scans until the stream buffer is full. Can be called




1434     /**
1435      * Scan over a here string.
1436      *
1437      * @return TRUE if is a here string.
1438      */
1439     private boolean scanHereString(final LineInfoReceiver lir) {
1440         assert ch0 == '<' && ch1 == '<';
1441         if (scripting) {
1442             // Record beginning of here string.
1443             final State saved = saveState();
1444 
1445             // << or <<<
1446             final boolean excludeLastEOL = ch2 != '<';
1447 
1448             if (excludeLastEOL) {
1449                 skip(2);
1450             } else {
1451                 skip(3);
1452             }
1453 
1454             // Scan identifier. It might be quoted, indicating that no string editing should take place.
1455             final char quoteChar = ch0;
1456             final boolean noStringEditing = isStringDelimiter(quoteChar);
1457             if (noStringEditing) {
1458                 skip(1);
1459             }
1460             final int identStart = position;
1461             final int identLength = scanIdentifier();
1462             if (noStringEditing) {
1463                 if (ch0 != quoteChar) {
1464                     error(Lexer.message("here.non.matching.delimiter"), last, position, position);
1465                     restoreState(saved);
1466                     return false;
1467                 }
1468                 skip(1);
1469             }
1470 
1471             // Check for identifier.
1472             if (identLength == 0) {
1473                 // Treat as shift.
1474                 restoreState(saved);
1475 
1476                 return false;
1477             }
1478 
1479             // Record rest of line.
1480             final State restState = saveState();
1481             // keep line number updated
1482             int lastLine = line;
1483 
1484             skipLine(false);
1485             lastLine++;
1486             int lastLinePosition = position;
1487             restState.setLimit(position);
1488 
1489             // Record beginning of string.


1519                 return false;
1520             }
1521 
1522             // Remove last end of line if specified.
1523             if (excludeLastEOL) {
1524                 // Handles \n.
1525                 if (content[stringEnd - 1] == '\n') {
1526                     stringEnd--;
1527                 }
1528 
1529                 // Handles \r and \r\n.
1530                 if (content[stringEnd - 1] == '\r') {
1531                     stringEnd--;
1532                 }
1533 
1534                 // Update end of string.
1535                 stringState.setLimit(stringEnd);
1536             }
1537 
1538             // Edit string if appropriate.
1539             if (!noStringEditing && !stringState.isEmpty()) {
1540                 editString(STRING, stringState);
1541             } else {
1542                 // Add here string.
1543                 add(STRING, stringState.position, stringState.limit);
1544             }
1545 
1546             // Scan rest of original line.
1547             final Lexer restLexer = new Lexer(this, restState);
1548 
1549             restLexer.lexify();
1550 
1551             return true;
1552         }
1553 
1554         return false;
1555     }
1556 
1557     /**
1558      * Breaks source content down into lex units, adding tokens to the token
1559      * stream. The routine scans until the stream buffer is full. Can be called


< prev index next >