feat: add limited support of comments inside js call.

This commit is contained in:
Artem Kobzar
2022-04-07 15:32:03 +02:00
committed by teamcity
parent 6e30ce2763
commit 45e3c94a8b
20 changed files with 890 additions and 20 deletions
@@ -216,6 +216,12 @@ public class JsAstMapper {
case TokenStream.LABEL:
return mapLabel(node);
case TokenStream.SINGLE_LINE_COMMENT:
return mapSingleLineComment(node);
case TokenStream.MULTI_LINE_COMMENT:
return mapMultiLineComment(node);
default:
int tokenType = node.getType();
throw createParserException("Unexpected top-level token type: "
@@ -1096,6 +1102,14 @@ public class JsAstMapper {
return toVars;
}
private JsSingleLineComment mapSingleLineComment(Node node) {
return new JsSingleLineComment(node.getString());
}
private JsMultiLineComment mapMultiLineComment(Node node) {
return new JsMultiLineComment(node.getString());
}
private JsNode mapWithStatement(Node withNode) throws JsParserException {
// The "with" statement is unsupported because it introduces ambiguity
// related to whether or not a name is obfuscatable that we cannot resolve
@@ -331,6 +331,14 @@ public class IRFactory {
return new Node(nodeType, left, right, nodeOp, location);
}
public Node createSingleLineComment(String string, CodePosition position) {
return Node.newComment(TokenStream.SINGLE_LINE_COMMENT, string, position);
}
public Node createMultiLineComment(String string, CodePosition position) {
return Node.newComment(TokenStream.MULTI_LINE_COMMENT, string, position);
}
public Node createAssignment(int nodeOp, Node left, Node right, CodePosition location) {
int nodeType = left.getType();
switch (nodeType) {
@@ -90,6 +90,37 @@ public class Node implements Cloneable {
private String str;
}
private static class CommentNode extends Node {
CommentNode(int type, String str, CodePosition position) {
super(type, position);
if (null == str) {
throw new IllegalArgumentException("CommentNode: str is null");
}
this.str = str;
}
/** returns the string content.
* @return non null.
*/
@Override
public String getString() {
return this.str;
}
/** sets the string content.
* @param str the new value. Non null.
*/
@Override
public void setString(String str) {
if (null == str) {
throw new IllegalArgumentException("CommentNode: str is null");
}
this.str = str;
}
private String str;
}
public Node(int nodeType) {
type = nodeType;
}
@@ -191,6 +222,10 @@ public class Node implements Cloneable {
return new StringNode(type, str, position);
}
public static Node newComment(int type, String str, CodePosition position) {
return new CommentNode(type, str, position);
}
public int getType() {
return type;
}
@@ -110,7 +110,7 @@ public class Parser {
while (true) {
ts.flags |= TokenStream.TSF_REGEXP;
tt = ts.getToken();
tt = ts.getTokenWithComment();
ts.flags &= ~TokenStream.TSF_REGEXP;
if (tt <= TokenStream.EOF) {
@@ -360,7 +360,7 @@ public class Parser {
int lastExprType; // For wellTerminated
tt = ts.getToken();
tt = ts.getTokenWithComment();
CodePosition position = ts.tokenPosition;
switch (tt) {
@@ -593,6 +593,15 @@ public class Parser {
}
break;
}
case TokenStream.SINGLE_LINE_COMMENT:
pn = nf.createSingleLineComment(ts.getString(), ts.tokenPosition);
break;
case TokenStream.MULTI_LINE_COMMENT:
pn = nf.createMultiLineComment(ts.getString(), ts.tokenPosition);
break;
case TokenStream.RETURN: {
Node retExpr = null;
int lineno;
@@ -1000,7 +1009,7 @@ public class Parser {
) throws IOException, JavaScriptException {
lastExprEndLine = ts.getLineno();
int tt;
while ((tt = ts.getToken()) > TokenStream.EOF) {
while ((tt = ts.getTokenWithComment()) > TokenStream.EOF) {
CodePosition position = ts.tokenPosition;
if (tt == TokenStream.DOT) {
ts.treatKeywordAsIdentifier = true;
@@ -40,6 +40,7 @@ package com.google.gwt.dev.js.rhino;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;
/**
* This class implements the JavaScript scanner.
@@ -253,7 +254,9 @@ public class TokenStream {
LAST_TOKEN = 147,
NUMBER_INT = 148,
SINGLE_LINE_COMMENT = 149,
MULTI_LINE_COMMENT = 150,
// This value is only used as a return value for getTokenHelper,
// which is only called from getToken and exists to avoid an excessive
// recursion problem if a number of lines in a row are comments.
@@ -413,6 +416,8 @@ public class TokenStream {
case NEWLOCAL: return "newlocal";
case USELOCAL: return "uselocal";
case SCRIPT: return "script";
case SINGLE_LINE_COMMENT: return "singlelinecomment";
case MULTI_LINE_COMMENT: return "multilinecomment";
}
return "<unknown="+token+">";
}
@@ -586,7 +591,7 @@ public class TokenStream {
in.unread();
}
public int getToken() throws IOException {
public int getTokenWithComment() throws IOException {
lastTokenPosition = tokenPosition;
int c;
do {
@@ -597,6 +602,17 @@ public class TokenStream {
return c;
}
public int getToken() throws IOException {
lastTokenPosition = tokenPosition;
int c;
do {
c = getTokenHelper();
} while (c == RETRY_TOKEN || c == SINGLE_LINE_COMMENT || c == MULTI_LINE_COMMENT);
updatePosition();
return c;
}
private int getTokenHelper() throws IOException {
int c;
tokenno++;
@@ -1059,19 +1075,25 @@ public class TokenStream {
case '/':
// is it a // comment?
if (in.match('/')) {
skipLine();
return RETRY_TOKEN;
stringBufferTop = 0;
while ((c = in.read()) != -1 && c != '\n') {
addToString(c);
}
this.string = getStringFromBuffer();
return SINGLE_LINE_COMMENT;
}
if (in.match('*')) {
stringBufferTop = 0;
while ((c = in.read()) != -1 &&
!(c == '*' && in.match('/'))) {
; // empty loop body
addToString(c);
}
if (c == EOF_CHAR) {
reportTokenError("msg.unterminated.comment", null);
return ERROR;
}
return RETRY_TOKEN; // `goto retry'
this.string = getStringFromBuffer();
return MULTI_LINE_COMMENT; // `goto retry'
}
// is it a regexp?