use of org.mozilla.javascript.ast.VariableDeclaration in project HL4A by HL4A.
the class Parser method let.
// have to pass in 'let' kwd position to compute kid offsets properly
private AstNode let(boolean isStatement, int pos) throws IOException {
LetNode pn = new LetNode(pos);
pn.setLineno(ts.lineno);
if (mustMatchToken(Token.LP, "msg.no.paren.after.let"))
pn.setLp(ts.tokenBeg - pos);
pushScope(pn);
try {
VariableDeclaration vars = variables(Token.LET, ts.tokenBeg, isStatement);
pn.setVariables(vars);
if (mustMatchToken(Token.RP, "msg.no.paren.let")) {
pn.setRp(ts.tokenBeg - pos);
}
if (isStatement && peekToken() == Token.LC) {
// let statement
consumeToken();
// position stmt at LC
int beg = ts.tokenBeg;
AstNode stmt = statements();
mustMatchToken(Token.RC, "msg.no.curly.let");
stmt.setLength(ts.tokenEnd - beg);
pn.setLength(ts.tokenEnd - pos);
pn.setBody(stmt);
pn.setType(Token.LET);
} else {
// let expression
AstNode expr = expr();
pn.setLength(getNodeEnd(expr) - pos);
pn.setBody(expr);
if (isStatement) {
// let expression in statement context
ExpressionStatement es = new ExpressionStatement(pn, !insideFunction());
es.setLineno(pn.getLineno());
return es;
}
}
} finally {
popScope();
}
return pn;
}
use of org.mozilla.javascript.ast.VariableDeclaration in project st-js by st-js.
the class RhinoJavaScriptBuilder method variableDeclaration.
/**
* {@inheritDoc}
*/
@Override
public AstNode variableDeclaration(boolean statement, CharSequence name, AstNode init) {
VariableDeclaration vars = new VariableDeclaration();
vars.setIsStatement(statement);
VariableInitializer var = new VariableInitializer();
var.setTarget(name(name));
var.setInitializer(init);
vars.addVariable(var);
return vars;
}
use of org.mozilla.javascript.ast.VariableDeclaration in project st-js by st-js.
the class RhinoJavaScriptBuilder method variableDeclaration.
/**
* {@inheritDoc}
*/
@Override
public AstNode variableDeclaration(boolean statement, Iterable<NameValue<AstNode>> vars) {
VariableDeclaration varDecl = new VariableDeclaration();
varDecl.setIsStatement(statement);
for (NameValue<AstNode> v : vars) {
VariableInitializer var = new VariableInitializer();
var.setTarget(name(v.getName()));
var.setInitializer(v.getValue());
varDecl.addVariable(var);
}
return varDecl;
}
use of org.mozilla.javascript.ast.VariableDeclaration in project HL4A by HL4A.
the class IRFactory method transformForInLoop.
private Node transformForInLoop(ForInLoop loop) {
decompiler.addToken(Token.FOR);
if (loop.isForEach())
decompiler.addName("each ");
decompiler.addToken(Token.LP);
loop.setType(Token.LOOP);
pushScope(loop);
try {
int declType = -1;
AstNode iter = loop.getIterator();
if (iter instanceof VariableDeclaration) {
declType = ((VariableDeclaration) iter).getType();
}
Node lhs = transform(iter);
if (loop.isForOf()) {
decompiler.addName("of ");
} else {
decompiler.addToken(Token.IN);
}
Node obj = transform(loop.getIteratedObject());
decompiler.addToken(Token.RP);
decompiler.addEOL(Token.LC);
Node body = transform(loop.getBody());
decompiler.addEOL(Token.RC);
return createForIn(declType, loop, lhs, obj, body, loop.isForEach(), loop.isForOf());
} finally {
popScope();
}
}
use of org.mozilla.javascript.ast.VariableDeclaration in project HL4A by HL4A.
the class Parser method variables.
/**
* Parse a 'var' or 'const' statement, or a 'var' init list in a for
* statement.
* @param declType A token value: either VAR, CONST, or LET depending on
* context.
* @param pos the position where the node should start. It's sometimes
* the var/const/let keyword, and other times the beginning of the first
* token in the first variable declaration.
* @return the parsed variable list
*/
private VariableDeclaration variables(int declType, int pos, boolean isStatement) throws IOException {
int end;
VariableDeclaration pn = new VariableDeclaration(pos);
pn.setType(declType);
pn.setLineno(ts.lineno);
Comment varjsdocNode = getAndResetJsDoc();
if (varjsdocNode != null) {
pn.setJsDocNode(varjsdocNode);
}
// var {b: s2, a: s1} = foo, x = 6, y, [s3, s4] = bar;
for (; ; ) {
AstNode destructuring = null;
Name name = null;
int tt = peekToken(), kidPos = ts.tokenBeg;
end = ts.tokenEnd;
if (tt == Token.LB || tt == Token.LC) {
// Destructuring assignment, e.g., var [a,b] = ...
destructuring = destructuringPrimaryExpr();
end = getNodeEnd(destructuring);
if (!(destructuring instanceof DestructuringForm))
reportError("msg.bad.assign.left", kidPos, end - kidPos);
markDestructuring(destructuring);
} else {
// Simple variable name
mustMatchToken(Token.NAME, "msg.bad.var");
name = createNameNode();
name.setLineno(ts.getLineno());
if (inUseStrictDirective) {
String id = ts.getString();
if ("eval".equals(id) || "arguments".equals(ts.getString())) {
reportError("msg.bad.id.strict", id);
}
}
defineSymbol(declType, ts.getString(), inForInit);
}
int lineno = ts.lineno;
Comment jsdocNode = getAndResetJsDoc();
AstNode init = null;
if (matchToken(Token.ASSIGN)) {
init = assignExpr();
end = getNodeEnd(init);
}
VariableInitializer vi = new VariableInitializer(kidPos, end - kidPos);
if (destructuring != null) {
if (init == null && !inForInit) {
reportError("msg.destruct.assign.no.init");
}
vi.setTarget(destructuring);
} else {
vi.setTarget(name);
}
vi.setInitializer(init);
vi.setType(declType);
vi.setJsDocNode(jsdocNode);
vi.setLineno(lineno);
pn.addVariable(vi);
if (!matchToken(Token.COMMA))
break;
}
pn.setLength(end - pos);
pn.setIsStatement(isStatement);
return pn;
}
Aggregations