mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-24 04:54:40 +02:00
8021130: Comments need to be tokens
Reviewed-by: lagergren, attila
This commit is contained in:
parent
e2975f94c6
commit
6983b4e9e3
3 changed files with 29 additions and 11 deletions
|
@ -25,6 +25,7 @@
|
||||||
|
|
||||||
package jdk.nashorn.internal.parser;
|
package jdk.nashorn.internal.parser;
|
||||||
|
|
||||||
|
import static jdk.nashorn.internal.parser.TokenType.COMMENT;
|
||||||
import static jdk.nashorn.internal.parser.TokenType.EOF;
|
import static jdk.nashorn.internal.parser.TokenType.EOF;
|
||||||
import static jdk.nashorn.internal.parser.TokenType.EOL;
|
import static jdk.nashorn.internal.parser.TokenType.EOL;
|
||||||
import static jdk.nashorn.internal.parser.TokenType.IDENT;
|
import static jdk.nashorn.internal.parser.TokenType.IDENT;
|
||||||
|
@ -135,14 +136,27 @@ public abstract class AbstractParser {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Seek next token that is not an EOL.
|
* Seek next token that is not an EOL or comment.
|
||||||
*
|
*
|
||||||
* @return tokenType of next token.
|
* @return tokenType of next token.
|
||||||
*/
|
*/
|
||||||
protected final TokenType next() {
|
protected final TokenType next() {
|
||||||
do {
|
do {
|
||||||
nextOrEOL();
|
nextOrEOL();
|
||||||
} while (type == EOL);
|
} while (type == EOL || type == COMMENT);
|
||||||
|
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Seek next token or EOL (skipping comments.)
|
||||||
|
*
|
||||||
|
* @return tokenType of next token.
|
||||||
|
*/
|
||||||
|
protected final TokenType nextOrEOL() {
|
||||||
|
do {
|
||||||
|
nextToken();
|
||||||
|
} while (type == COMMENT);
|
||||||
|
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
@ -152,7 +166,7 @@ public abstract class AbstractParser {
|
||||||
*
|
*
|
||||||
* @return tokenType of next token.
|
* @return tokenType of next token.
|
||||||
*/
|
*/
|
||||||
protected final TokenType nextOrEOL() {
|
private final TokenType nextToken() {
|
||||||
// Capture last token tokenType.
|
// Capture last token tokenType.
|
||||||
last = type;
|
last = type;
|
||||||
if (type != EOF) {
|
if (type != EOF) {
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
package jdk.nashorn.internal.parser;
|
package jdk.nashorn.internal.parser;
|
||||||
|
|
||||||
import static jdk.nashorn.internal.parser.TokenType.ADD;
|
import static jdk.nashorn.internal.parser.TokenType.ADD;
|
||||||
|
import static jdk.nashorn.internal.parser.TokenType.COMMENT;
|
||||||
import static jdk.nashorn.internal.parser.TokenType.DECIMAL;
|
import static jdk.nashorn.internal.parser.TokenType.DECIMAL;
|
||||||
import static jdk.nashorn.internal.parser.TokenType.EOF;
|
import static jdk.nashorn.internal.parser.TokenType.EOF;
|
||||||
import static jdk.nashorn.internal.parser.TokenType.EOL;
|
import static jdk.nashorn.internal.parser.TokenType.EOL;
|
||||||
|
@ -426,6 +427,9 @@ public class Lexer extends Scanner {
|
||||||
* @return True if a comment.
|
* @return True if a comment.
|
||||||
*/
|
*/
|
||||||
protected boolean skipComments() {
|
protected boolean skipComments() {
|
||||||
|
// Save the current position.
|
||||||
|
final int start = position;
|
||||||
|
|
||||||
if (ch0 == '/') {
|
if (ch0 == '/') {
|
||||||
// Is it a // comment.
|
// Is it a // comment.
|
||||||
if (ch1 == '/') {
|
if (ch1 == '/') {
|
||||||
|
@ -436,10 +440,9 @@ public class Lexer extends Scanner {
|
||||||
skip(1);
|
skip(1);
|
||||||
}
|
}
|
||||||
// Did detect a comment.
|
// Did detect a comment.
|
||||||
|
add(COMMENT, start);
|
||||||
return true;
|
return true;
|
||||||
} else if (ch1 == '*') {
|
} else if (ch1 == '*') {
|
||||||
// Record beginning of comment.
|
|
||||||
final int start = position;
|
|
||||||
// Skip over /*.
|
// Skip over /*.
|
||||||
skip(2);
|
skip(2);
|
||||||
// Scan for */.
|
// Scan for */.
|
||||||
|
@ -461,11 +464,11 @@ public class Lexer extends Scanner {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Did detect a comment.
|
// Did detect a comment.
|
||||||
|
add(COMMENT, start);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
} else if (ch0 == '#') {
|
||||||
|
assert scripting;
|
||||||
if (scripting && ch0 == '#') {
|
|
||||||
// shell style comment
|
// shell style comment
|
||||||
// Skip over #.
|
// Skip over #.
|
||||||
skip(1);
|
skip(1);
|
||||||
|
@ -474,6 +477,7 @@ public class Lexer extends Scanner {
|
||||||
skip(1);
|
skip(1);
|
||||||
}
|
}
|
||||||
// Did detect a comment.
|
// Did detect a comment.
|
||||||
|
add(COMMENT, start);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -562,7 +566,7 @@ public class Lexer extends Scanner {
|
||||||
*
|
*
|
||||||
* @param token the token.
|
* @param token the token.
|
||||||
* @param startTokenType the token type.
|
* @param startTokenType the token type.
|
||||||
* @parasm lir LineInfoReceiver that receives line info for multi-line string literals.
|
* @param lir LineInfoReceiver that receives line info for multi-line string literals.
|
||||||
* @return True if a literal beginning with startToken was found and scanned.
|
* @return True if a literal beginning with startToken was found and scanned.
|
||||||
*/
|
*/
|
||||||
protected boolean scanLiteral(final long token, final TokenType startTokenType, final LineInfoReceiver lir) {
|
protected boolean scanLiteral(final long token, final TokenType startTokenType, final LineInfoReceiver lir) {
|
||||||
|
@ -1460,11 +1464,10 @@ public class Lexer extends Scanner {
|
||||||
final State restState = saveState();
|
final State restState = saveState();
|
||||||
// keep line number updated
|
// keep line number updated
|
||||||
int lastLine = line;
|
int lastLine = line;
|
||||||
int lastLinePosition = linePosition;
|
|
||||||
|
|
||||||
skipLine(false);
|
skipLine(false);
|
||||||
lastLine++;
|
lastLine++;
|
||||||
lastLinePosition = position;
|
int lastLinePosition = position;
|
||||||
restState.setLimit(position);
|
restState.setLimit(position);
|
||||||
|
|
||||||
// Record beginning of string.
|
// Record beginning of string.
|
||||||
|
|
|
@ -44,6 +44,7 @@ public enum TokenType {
|
||||||
ERROR (SPECIAL, null),
|
ERROR (SPECIAL, null),
|
||||||
EOF (SPECIAL, null),
|
EOF (SPECIAL, null),
|
||||||
EOL (SPECIAL, null),
|
EOL (SPECIAL, null),
|
||||||
|
COMMENT (SPECIAL, null),
|
||||||
|
|
||||||
NOT (UNARY, "!", 14, false),
|
NOT (UNARY, "!", 14, false),
|
||||||
NE (BINARY, "!=", 9, true),
|
NE (BINARY, "!=", 9, true),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue