use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.
the class Parser method objliteralProperty.
private AstNode objliteralProperty() throws IOException {
AstNode pname;
int tt = peekToken();
switch(tt) {
case Token.NAME:
pname = createNameNode();
break;
case Token.STRING:
pname = createStringLiteral();
break;
case Token.NUMBER:
pname = new NumberLiteral(ts.tokenBeg, ts.getString(), ts.getNumber());
break;
default:
if (compilerEnv.isReservedKeywordAsIdentifier() && TokenStream.isKeyword(ts.getString(), compilerEnv.getLanguageVersion(), inUseStrictDirective)) {
// convert keyword to property name, e.g. ({if: 1})
pname = createNameNode();
break;
}
return null;
}
return pname;
}
use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.
the class Parser method parseFunctionBody.
private AstNode parseFunctionBody(int type, FunctionNode fnNode) throws IOException {
boolean isExpressionClosure = false;
if (!matchToken(Token.LC)) {
if (compilerEnv.getLanguageVersion() < Context.VERSION_1_8 && type != FunctionNode.ARROW_FUNCTION) {
reportError("msg.no.brace.body");
} else {
isExpressionClosure = true;
}
}
boolean isArrow = type == FunctionNode.ARROW_FUNCTION;
++nestingOfFunction;
int pos = ts.tokenBeg;
// starts at LC position
Block pn = new Block(pos);
boolean inDirectivePrologue = true;
boolean savedStrictMode = inUseStrictDirective;
// Don't set 'inUseStrictDirective' to false: inherit strict mode.
pn.setLineno(ts.lineno);
try {
if (isExpressionClosure) {
AstNode returnValue = assignExpr();
ReturnStatement n = new ReturnStatement(returnValue.getPosition(), returnValue.getLength(), returnValue);
// expression closure flag is required on both nodes
n.putProp(Node.EXPRESSION_CLOSURE_PROP, Boolean.TRUE);
pn.putProp(Node.EXPRESSION_CLOSURE_PROP, Boolean.TRUE);
if (isArrow) {
n.putProp(Node.ARROW_FUNCTION_PROP, Boolean.TRUE);
}
pn.addStatement(n);
} else {
bodyLoop: for (; ; ) {
AstNode n;
int tt = peekToken();
switch(tt) {
case Token.ERROR:
case Token.EOF:
case Token.RC:
break bodyLoop;
case Token.FUNCTION:
consumeToken();
n = function(FunctionNode.FUNCTION_STATEMENT);
break;
default:
n = statement();
if (inDirectivePrologue) {
String directive = getDirective(n);
if (directive == null) {
inDirectivePrologue = false;
} else if (directive.equals("严格模式")) {
inUseStrictDirective = true;
fnNode.setInStrictMode(true);
if (!savedStrictMode) {
setRequiresActivation();
}
}
}
break;
}
pn.addStatement(n);
}
}
} catch (ParserException e) {
// Ignore it
} finally {
--nestingOfFunction;
inUseStrictDirective = savedStrictMode;
}
int end = ts.tokenEnd;
getAndResetJsDoc();
if (!isExpressionClosure && mustMatchToken(Token.RC, "msg.no.brace.after.body"))
end = ts.tokenEnd;
pn.setLength(end - pos);
return pn;
}
use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.
the class Parser method argumentList.
private List<AstNode> argumentList() throws IOException {
if (matchToken(Token.RP))
return null;
List<AstNode> result = new ArrayList<AstNode>();
boolean wasInForInit = inForInit;
inForInit = false;
try {
do {
if (peekToken() == Token.YIELD) {
reportError("msg.yield.parenthesized");
}
AstNode en = assignExpr();
if (peekToken() == Token.FOR) {
try {
result.add(generatorExpression(en, 0, true));
} catch (IOException ex) {
// #TODO
}
} else {
result.add(en);
}
} while (matchToken(Token.COMMA));
} finally {
inForInit = wasInForInit;
}
mustMatchToken(Token.RP, "msg.no.paren.arg");
return result;
}
use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.
the class Parser method arrowFunction.
private AstNode arrowFunction(AstNode params) throws IOException {
// line number where source starts
int baseLineno = ts.lineno;
// start of "function" kwd
int functionSourceStart = params != null ? params.getPosition() : -1;
FunctionNode fnNode = new FunctionNode(functionSourceStart);
fnNode.setFunctionType(FunctionNode.ARROW_FUNCTION);
fnNode.setJsDocNode(getAndResetJsDoc());
// Would prefer not to call createDestructuringAssignment until codegen,
// but the symbol definitions have to happen now, before body is parsed.
Map<String, Node> destructuring = new HashMap<String, Node>();
Set<String> paramNames = new HashSet<String>();
PerFunctionVariables savedVars = new PerFunctionVariables(fnNode);
try {
if (params instanceof ParenthesizedExpression) {
fnNode.setParens(0, params.getLength());
AstNode p = ((ParenthesizedExpression) params).getExpression();
if (!(p instanceof EmptyExpression)) {
arrowFunctionParams(fnNode, p, destructuring, paramNames);
}
} else {
arrowFunctionParams(fnNode, params, destructuring, paramNames);
}
if (!destructuring.isEmpty()) {
Node destructuringNode = new Node(Token.COMMA);
// Add assignment helper for each destructuring parameter
for (Map.Entry<String, Node> param : destructuring.entrySet()) {
Node assign = createDestructuringAssignment(Token.VAR, param.getValue(), createName(param.getKey()));
destructuringNode.addChildToBack(assign);
}
fnNode.putProp(Node.DESTRUCTURING_PARAMS, destructuringNode);
}
fnNode.setBody(parseFunctionBody(FunctionNode.ARROW_FUNCTION, fnNode));
fnNode.setEncodedSourceBounds(functionSourceStart, ts.tokenEnd);
fnNode.setLength(ts.tokenEnd - functionSourceStart);
} finally {
savedVars.restore();
}
if (fnNode.isGenerator()) {
reportError("msg.arrowfunction.generator");
return makeErrorNode();
}
fnNode.setSourceName(sourceURI);
fnNode.setBaseLineno(baseLineno);
fnNode.setEndLineno(ts.lineno);
return fnNode;
}
use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.
the class Parser method whileLoop.
private WhileLoop whileLoop() throws IOException {
if (currentToken != Token.WHILE)
codeBug();
consumeToken();
int pos = ts.tokenBeg;
WhileLoop pn = new WhileLoop(pos);
pn.setLineno(ts.lineno);
enterLoop(pn);
try {
ConditionData data = condition();
pn.setCondition(data.condition);
pn.setParens(data.lp - pos, data.rp - pos);
AstNode body = statement();
pn.setLength(getNodeEnd(body) - pos);
pn.setBody(body);
} finally {
exitLoop();
}
return pn;
}
Aggregations