Skip to content

Commit

Permalink
Simplify JsDoc parsing for Rhino users, attach the Comment node rather
Browse files Browse the repository at this point in the history
than just the JsDoc string.

Committed by John Lenz
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=26010038
  • Loading branch information
Nick Santos authored and hns committed May 7, 2012
1 parent f454411 commit 4814182
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 39 deletions.
20 changes: 17 additions & 3 deletions src/org/mozilla/javascript/Node.java
Expand Up @@ -41,6 +41,7 @@

package org.mozilla.javascript;

import org.mozilla.javascript.ast.Comment;
import org.mozilla.javascript.ast.FunctionNode;
import org.mozilla.javascript.ast.Jump;
import org.mozilla.javascript.ast.Name;
Expand Down Expand Up @@ -210,14 +211,27 @@ public Node setType(int type) {
* this node
*/
public String getJsDoc() {
return (String) getProp(JSDOC_PROP);
Comment comment = getJsDocNode();
if (comment != null) {
return comment.getValue();
}
return null;
}

/**
* Gets the JsDoc Comment object attached to this node.
* @return the Comment or {@code null} if no JsDoc is attached to
* this node
*/
public Comment getJsDocNode() {
return (Comment) getProp(JSDOC_PROP);
}

/**
* Sets the JsDoc comment string attached to this node.
*/
public void setJsDoc(String jsdoc) {
putProp(JSDOC_PROP, jsdoc);
public void setJsDocNode(Comment jsdocNode) {
putProp(JSDOC_PROP, jsdocNode);
}

public boolean hasChildren() {
Expand Down
79 changes: 43 additions & 36 deletions src/org/mozilla/javascript/Parser.java
Expand Up @@ -111,7 +111,7 @@ public class Parser
private int syntaxErrorCount;

private List<Comment> scannedComments;
private String currentJsDocComment;
private Comment currentJsDocComment;

protected int nestingOfFunction;
private LabeledStatement currentLabel;
Expand Down Expand Up @@ -282,20 +282,20 @@ private void recordComment(int lineno) {
scannedComments = new ArrayList<Comment>();
}
String comment = ts.getAndResetCurrentComment();
if (ts.commentType == Token.CommentType.JSDOC &&
compilerEnv.isRecordingLocalJsDocComments()) {
currentJsDocComment = comment;
}
Comment commentNode = new Comment(ts.tokenBeg,
ts.getTokenLength(),
ts.commentType,
comment);
if (ts.commentType == Token.CommentType.JSDOC &&
compilerEnv.isRecordingLocalJsDocComments()) {
currentJsDocComment = commentNode;
}
commentNode.setLineno(lineno);
scannedComments.add(commentNode);
}

private String getAndResetJsDoc() {
String saved = currentJsDocComment;
private Comment getAndResetJsDoc() {
Comment saved = currentJsDocComment;
currentJsDocComment = null;
return saved;
}
Expand Down Expand Up @@ -815,7 +815,7 @@ private FunctionNode function(int type)
if (lpPos != -1)
fnNode.setLp(lpPos - functionSourceStart);

fnNode.setJsDoc(getAndResetJsDoc());
fnNode.setJsDocNode(getAndResetJsDoc());

PerFunctionVariables savedVars = new PerFunctionVariables(fnNode);
try {
Expand Down Expand Up @@ -1383,7 +1383,7 @@ private TryStatement tryStatement()
consumeToken();

// Pull out JSDoc info and reset it before recursing.
String jsdoc = getAndResetJsDoc();
Comment jsdocNode = getAndResetJsDoc();

int tryPos = ts.tokenBeg, lineno = ts.lineno, finallyPos = -1;
if (peekToken() != Token.LC) {
Expand Down Expand Up @@ -1468,8 +1468,8 @@ private TryStatement tryStatement()
}
pn.setLineno(lineno);

if (jsdoc != null) {
pn.setJsDoc(jsdoc);
if (jsdocNode != null) {
pn.setJsDocNode(jsdocNode);
}

return pn;
Expand Down Expand Up @@ -1605,7 +1605,7 @@ private WithStatement withStatement()
AstNode body = statement();

WithStatement pn = new WithStatement(pos, getNodeEnd(body) - pos);
pn.setJsDoc(getAndResetJsDoc());
pn.setJsDocNode(getAndResetJsDoc());
pn.setExpression(obj);
pn.setStatement(body);
pn.setParens(lp, rp);
Expand Down Expand Up @@ -1851,9 +1851,9 @@ private VariableDeclaration variables(int declType, int pos, boolean isStatement
VariableDeclaration pn = new VariableDeclaration(pos);
pn.setType(declType);
pn.setLineno(ts.lineno);
String varjsdoc = getAndResetJsDoc();
if (varjsdoc != null) {
pn.setJsDoc(varjsdoc);
Comment varjsdocNode = getAndResetJsDoc();
if (varjsdocNode != null) {
pn.setJsDocNode(varjsdocNode);
}
// Example:
// var foo = {a: 1, b: 2}, bar = [3, 4];
Expand Down Expand Up @@ -1888,7 +1888,7 @@ private VariableDeclaration variables(int declType, int pos, boolean isStatement

int lineno = ts.lineno;

String jsdoc = getAndResetJsDoc();
Comment jsdocNode = getAndResetJsDoc();

AstNode init = null;
if (matchToken(Token.ASSIGN)) {
Expand All @@ -1907,7 +1907,7 @@ private VariableDeclaration variables(int declType, int pos, boolean isStatement
}
vi.setInitializer(init);
vi.setType(declType);
vi.setJsDoc(jsdoc);
vi.setJsDocNode(jsdocNode);
vi.setLineno(lineno);
pn.addVariable(vi);

Expand Down Expand Up @@ -2063,7 +2063,7 @@ private AstNode assignExpr()
consumeToken();

// Pull out JSDoc info and reset it before recursing.
String jsdoc = getAndResetJsDoc();
Comment jsdocNode = getAndResetJsDoc();

markDestructuring(pn);
int opPos = ts.tokenBeg;
Expand All @@ -2072,14 +2072,14 @@ private AstNode assignExpr()
pn = new Assignment(tt, pn, assignExpr(), opPos);

pn.setLineno(opLineno);
if (jsdoc != null) {
pn.setJsDoc(jsdoc);
if (jsdocNode != null) {
pn.setJsDocNode(jsdocNode);
}
} else if (tt == Token.SEMI && pn.getType() == Token.GETPROP) {
// This may be dead code added intentionally, for JSDoc purposes.
// For example: /** @type Number */ C.prototype.x;
if (currentJsDocComment != null) {
pn.setJsDoc(getAndResetJsDoc());
pn.setJsDocNode(getAndResetJsDoc());
}
}
return pn;
Expand Down Expand Up @@ -2876,19 +2876,19 @@ private AstNode parenExpr() throws IOException {
boolean wasInForInit = inForInit;
inForInit = false;
try {
String jsdoc = getAndResetJsDoc();
Comment jsdocNode = getAndResetJsDoc();
int lineno = ts.lineno;
int begin = ts.tokenBeg;
AstNode e = expr();
if (peekToken() == Token.FOR) {
return generatorExpression(e, begin);
}
ParenthesizedExpression pn = new ParenthesizedExpression(e);
if (jsdoc == null) {
jsdoc = getAndResetJsDoc();
if (jsdocNode == null) {
jsdocNode = getAndResetJsDoc();
}
if (jsdoc != null) {
pn.setJsDoc(jsdoc);
if (jsdocNode != null) {
pn.setJsDocNode(jsdocNode);
}
mustMatchToken(Token.RP, "msg.no.paren");
pn.setLength(ts.tokenEnd - pn.getPosition());
Expand Down Expand Up @@ -3180,7 +3180,7 @@ private ObjectLiteral objectLiteral()
for (;;) {
String propertyName = null;
int tt = peekToken();
String jsdoc = getAndResetJsDoc();
Comment jsdocNode = getAndResetJsDoc();
switch(tt) {
case Token.NAME:
case Token.STRING:
Expand All @@ -3200,14 +3200,14 @@ private ObjectLiteral objectLiteral()
{
consumeToken();
name = createNameNode();
name.setJsDoc(jsdoc);
name.setJsDocNode(jsdocNode);
ObjectProperty objectProp = getterSetterProperty(ppos, name,
"get".equals(propertyName));
elems.add(objectProp);
propertyName = objectProp.getLeft().getString();
} else {
AstNode pname = stringProp != null ? stringProp : name;
pname.setJsDoc(jsdoc);
pname.setJsDocNode(jsdocNode);
elems.add(plainProperty(pname, tt));
}
break;
Expand All @@ -3217,7 +3217,7 @@ private ObjectLiteral objectLiteral()
AstNode nl = new NumberLiteral(ts.tokenBeg,
ts.getString(),
ts.getNumber());
nl.setJsDoc(jsdoc);
nl.setJsDocNode(jsdocNode);
propertyName = ts.getString();
elems.add(plainProperty(nl, tt));
break;
Expand All @@ -3228,12 +3228,18 @@ private ObjectLiteral objectLiteral()
break commaLoop;

default:
if (convertToName(tt)) {
consumeToken();
AstNode pname = createNameNode();
pname.setJsDoc(jsdoc);
elems.add(plainProperty(pname, tt));
break;
if (compilerEnv.isReservedKeywordAsIdentifier()) {
// convert keyword to property name, e.g. ({if: 1})
propertyName = Token.keywordToName(tt);
if (propertyName != null) {
afterComma = -1;
saveNameTokenData(ts.tokenBeg, propertyName, ts.lineno);
consumeToken();
AstNode pname = createNameNode();
pname.setJsDocNode(jsdocNode);
elems.add(plainProperty(pname, tt));
break;
}
}
reportError("msg.bad.prop");
break;
Expand All @@ -3248,6 +3254,7 @@ private ObjectLiteral objectLiteral()

// Eat any dangling jsdoc in the property.
getAndResetJsDoc();
jsdocNode = null;

if (matchToken(Token.COMMA)) {
afterComma = ts.tokenEnd;
Expand Down

0 comments on commit 4814182

Please sign in to comment.