use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.
the class Parser method propertyAccess.
/**
* Handles any construct following a "." or ".." operator.
* @param pn the left-hand side (target) of the operator. Never null.
* @return a PropertyGet, XmlMemberGet, or ErrorNode
*/
private AstNode propertyAccess(int tt, AstNode pn) throws IOException {
if (pn == null)
codeBug();
int memberTypeFlags = 0, lineno = ts.lineno, dotPos = ts.tokenBeg;
consumeToken();
if (tt == Token.DOTDOT) {
mustHaveXML();
memberTypeFlags = Node.DESCENDANTS_FLAG;
}
if (!compilerEnv.isXmlAvailable()) {
int maybeName = nextToken();
if (maybeName != Token.NAME && !(compilerEnv.isReservedKeywordAsIdentifier() && TokenStream.isKeyword(ts.getString(), compilerEnv.getLanguageVersion(), inUseStrictDirective))) {
reportError("msg.no.name.after.dot");
}
Name name = createNameNode(true, Token.GETPROP);
PropertyGet pg = new PropertyGet(pn, name, dotPos);
pg.setLineno(lineno);
return pg;
}
// right side of . or .. operator
AstNode ref = null;
int token = nextToken();
switch(token) {
case Token.THROW:
// needed for generator.throw();
saveNameTokenData(ts.tokenBeg, "throw", ts.lineno);
ref = propertyName(-1, "throw", memberTypeFlags);
break;
case Token.NAME:
// handles: name, ns::name, ns::*, ns::[expr]
ref = propertyName(-1, ts.getString(), memberTypeFlags);
break;
case Token.MUL:
// handles: *, *::name, *::*, *::[expr]
saveNameTokenData(ts.tokenBeg, "*", ts.lineno);
ref = propertyName(-1, "*", memberTypeFlags);
break;
case Token.XMLATTR:
// handles: '@attr', '@ns::attr', '@ns::*', '@ns::*',
// '@::attr', '@::*', '@*', '@*::attr', '@*::*'
ref = attributeAccess();
break;
case Token.RESERVED:
{
String name = ts.getString();
saveNameTokenData(ts.tokenBeg, name, ts.lineno);
ref = propertyName(-1, name, memberTypeFlags);
break;
}
default:
if (compilerEnv.isReservedKeywordAsIdentifier()) {
// allow keywords as property names, e.g. ({if: 1})
String name = Token.keywordToName(token);
if (name != null) {
saveNameTokenData(ts.tokenBeg, name, ts.lineno);
ref = propertyName(-1, name, memberTypeFlags);
break;
}
}
reportError("msg.no.name.after.dot");
return makeErrorNode();
}
boolean xml = ref instanceof XmlRef;
InfixExpression result = xml ? new XmlMemberGet() : new PropertyGet();
if (xml && tt == Token.DOT)
result.setType(Token.DOT);
int pos = pn.getPosition();
result.setPosition(pos);
result.setLength(getNodeEnd(ref) - pos);
result.setOperatorPosition(dotPos - pos);
result.setLineno(pn.getLineno());
// do this after setting position
result.setLeft(pn);
result.setRight(ref);
return result;
}
use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.
the class Parser method forLoop.
private Loop forLoop() throws IOException {
if (currentToken != Token.FOR)
codeBug();
consumeToken();
int forPos = ts.tokenBeg, lineno = ts.lineno;
boolean isForEach = false, isForIn = false, isForOf = false;
int eachPos = -1, inPos = -1, lp = -1, rp = -1;
// init is also foo in 'foo in object'
AstNode init = null;
// cond is also object in 'foo in object'
AstNode cond = null;
AstNode incr = null;
Loop pn = null;
Scope tempScope = new Scope();
// decide below what AST class to use
pushScope(tempScope);
try {
// See if this is a for each () instead of just a for ()
if (matchToken(Token.NAME)) {
if ("each".equals(ts.getString())) {
isForEach = true;
eachPos = ts.tokenBeg - forPos;
} else {
reportError("msg.no.paren.for");
}
}
if (mustMatchToken(Token.LP, "msg.no.paren.for"))
lp = ts.tokenBeg - forPos;
int tt = peekToken();
init = forLoopInit(tt);
if (matchToken(Token.IN)) {
isForIn = true;
inPos = ts.tokenBeg - forPos;
// object over which we're iterating
cond = expr();
} else if (compilerEnv.getLanguageVersion() >= Context.VERSION_ES6 && matchToken(Token.NAME) && "of".equals(ts.getString())) {
isForOf = true;
inPos = ts.tokenBeg - forPos;
// object over which we're iterating
cond = expr();
} else {
// ordinary for-loop
mustMatchToken(Token.SEMI, "msg.no.semi.for");
if (peekToken() == Token.SEMI) {
// no loop condition
cond = new EmptyExpression(ts.tokenBeg, 1);
cond.setLineno(ts.lineno);
} else {
cond = expr();
}
mustMatchToken(Token.SEMI, "msg.no.semi.for.cond");
int tmpPos = ts.tokenEnd;
if (peekToken() == Token.RP) {
incr = new EmptyExpression(tmpPos, 1);
incr.setLineno(ts.lineno);
} else {
incr = expr();
}
}
if (mustMatchToken(Token.RP, "msg.no.paren.for.ctrl"))
rp = ts.tokenBeg - forPos;
if (isForIn || isForOf) {
ForInLoop fis = new ForInLoop(forPos);
if (init instanceof VariableDeclaration) {
// check that there was only one variable given
if (((VariableDeclaration) init).getVariables().size() > 1) {
reportError("msg.mult.index");
}
}
if (isForOf && isForEach) {
reportError("msg.invalid.for.each");
}
fis.setIterator(init);
fis.setIteratedObject(cond);
fis.setInPosition(inPos);
fis.setIsForEach(isForEach);
fis.setEachPosition(eachPos);
fis.setIsForOf(isForOf);
pn = fis;
} else {
ForLoop fl = new ForLoop(forPos);
fl.setInitializer(init);
fl.setCondition(cond);
fl.setIncrement(incr);
pn = fl;
}
// replace temp scope with the new loop object
currentScope.replaceWith(pn);
popScope();
// We have to parse the body -after- creating the loop node,
// so that the loop node appears in the loopSet, allowing
// break/continue statements to find the enclosing loop.
enterLoop(pn);
try {
AstNode body = statement();
pn.setLength(getNodeEnd(body) - forPos);
pn.setBody(body);
} finally {
exitLoop();
}
} finally {
if (currentScope == tempScope) {
popScope();
}
}
pn.setParens(lp, rp);
pn.setLineno(lineno);
return pn;
}
use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.
the class Parser method eqExpr.
private AstNode eqExpr() throws IOException {
AstNode pn = relExpr();
for (; ; ) {
int tt = peekToken(), opPos = ts.tokenBeg;
switch(tt) {
case Token.EQ:
case Token.NE:
case Token.SHEQ:
case Token.SHNE:
consumeToken();
int parseToken = tt;
if (compilerEnv.getLanguageVersion() == Context.VERSION_1_2) {
// JavaScript 1.2 uses shallow equality for == and != .
if (tt == Token.EQ)
parseToken = Token.SHEQ;
else if (tt == Token.NE)
parseToken = Token.SHNE;
}
pn = new InfixExpression(parseToken, pn, relExpr(), opPos);
continue;
}
break;
}
return pn;
}
use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.
the class Parser method mulExpr.
private AstNode mulExpr() throws IOException {
AstNode pn = unaryExpr();
for (; ; ) {
int tt = peekToken(), opPos = ts.tokenBeg;
switch(tt) {
case Token.MUL:
case Token.DIV:
case Token.MOD:
consumeToken();
pn = new InfixExpression(tt, pn, unaryExpr(), opPos);
continue;
}
break;
}
return pn;
}
use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.
the class Parser method orExpr.
private AstNode orExpr() throws IOException {
AstNode pn = andExpr();
if (matchToken(Token.OR)) {
int opPos = ts.tokenBeg;
pn = new InfixExpression(Token.OR, pn, orExpr(), opPos);
}
return pn;
}
Aggregations