Java Code Examples for org.mozilla.javascript.ast.Name#getIdentifier()
The following examples show how to use
org.mozilla.javascript.ast.Name#getIdentifier() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IRFactory.java From JsDroidCmd with Mozilla Public License 2.0 | 6 votes |
private Node transformXmlRef(Node pn, XmlRef node, int memberTypeFlags) { if ((memberTypeFlags & Node.ATTRIBUTE_FLAG) != 0) decompiler.addToken(Token.XMLATTR); Name namespace = node.getNamespace(); String ns = namespace != null ? namespace.getIdentifier() : null; if (ns != null) { decompiler.addName(ns); decompiler.addToken(Token.COLONCOLON); } if (node instanceof XmlPropRef) { String name = ((XmlPropRef)node).getPropName().getIdentifier(); decompiler.addName(name); return createPropertyGet(pn, ns, name, memberTypeFlags); } else { decompiler.addToken(Token.LB); Node expr = transform(((XmlElemRef)node).getExpression()); decompiler.addToken(Token.RB); return createElementGet(pn, ns, expr, memberTypeFlags); } }
Example 2
Source File: ConstraintVisitor.java From SJS with Apache License 2.0 | 6 votes |
/** * for an expression that consists of a simple reference to a variable, we create an ITerm * for that expression, find the unique representative for the referenced variable, and * generate a constraint that equates these. */ private ITypeTerm processVariableReference(Name name) { ITypeTerm expTerm = findOrCreateExpressionTerm(name); if (!ConstraintGenUtil.isGlobal(name)){ // no need for NameDeclarationTerm for global variables Name declaration = ConstraintGenUtil.findDecl(name); ITypeTerm nameTerm = findOrCreateNameDeclarationTerm(declaration); addTypeEqualityConstraint(expTerm, nameTerm, name.getLineno(), null); } else { String identifier = name.getIdentifier(); if (identifier.equals("undefined")){ addTypeEqualityConstraint(new TypeParamTerm(name), findOrCreateExpressionTerm(name), name.getLineno(), null); } else { ITypeTerm globalTerm = findOrCreateGlobalDeclarationTerm(name, jsEnv); addTypeEqualityConstraint(globalTerm, expTerm, name.getLineno(), null); createConstraintsForGlobalFunction(name); } } return expTerm; }
Example 3
Source File: ConstraintFactory.java From SJS with Apache License 2.0 | 5 votes |
/** * Find or create a term representing an expression consisting of a variable name */ public NameDeclarationTerm findOrCreateNameDeclarationTerm(final Name name){ Name name2 = ConstraintGenUtil.findDecl(name); if (name2 == null) { throw new Error("no declaration found for " + name.getIdentifier()); } if (!nameTerms.containsKey(name2)){ nameTerms.put(name2, new NameDeclarationTerm(name2)); } return nameTerms.get(name2); }
Example 4
Source File: ConstraintVisitor.java From SJS with Apache License 2.0 | 5 votes |
/** * Create constraints for parameterized external functions such as Array<T> */ private void createConstraintsForGlobalFunction(Name name) { ITypeTerm term = findOrCreateExpressionTerm(name); String functionName = name.getIdentifier(); Type type = jsEnv.get(functionName); if (type == null){ error("reference to unknown global: " + name.getIdentifier(), name); return; } ITypeTerm typeParamTerm = new TypeParamTerm(name); if (ConstraintGenUtil.isParameterized(type)){ generateConstraintsForType(name, typeParamTerm, term, type); } }
Example 5
Source File: Parser.java From JsDroidCmd with Mozilla Public License 2.0 | 4 votes |
private FunctionNode function(int type) throws IOException { int syntheticType = type; int baseLineno = ts.lineno; // line number where source starts int functionSourceStart = ts.tokenBeg; // start of "function" kwd Name name = null; AstNode memberExprNode = null; if (matchToken(Token.NAME)) { name = createNameNode(true, Token.NAME); if (inUseStrictDirective) { String id = name.getIdentifier(); if ("eval".equals(id)|| "arguments".equals(id)) { reportError("msg.bad.id.strict", id); } } if (!matchToken(Token.LP)) { if (compilerEnv.isAllowMemberExprAsFunctionName()) { AstNode memberExprHead = name; name = null; memberExprNode = memberExprTail(false, memberExprHead); } mustMatchToken(Token.LP, "msg.no.paren.parms"); } } else if (matchToken(Token.LP)) { // Anonymous function: leave name as null } else { if (compilerEnv.isAllowMemberExprAsFunctionName()) { // Note that memberExpr can not start with '(' like // in function (1+2).toString(), because 'function (' already // processed as anonymous function memberExprNode = memberExpr(false); } mustMatchToken(Token.LP, "msg.no.paren.parms"); } int lpPos = currentToken == Token.LP ? ts.tokenBeg : -1; if (memberExprNode != null) { syntheticType = FunctionNode.FUNCTION_EXPRESSION; } if (syntheticType != FunctionNode.FUNCTION_EXPRESSION && name != null && name.length() > 0) { // Function statements define a symbol in the enclosing scope defineSymbol(Token.FUNCTION, name.getIdentifier()); } FunctionNode fnNode = new FunctionNode(functionSourceStart, name); fnNode.setFunctionType(type); if (lpPos != -1) fnNode.setLp(lpPos - functionSourceStart); fnNode.setJsDocNode(getAndResetJsDoc()); PerFunctionVariables savedVars = new PerFunctionVariables(fnNode); try { parseFunctionParams(fnNode); fnNode.setBody(parseFunctionBody(type, fnNode)); fnNode.setEncodedSourceBounds(functionSourceStart, ts.tokenEnd); fnNode.setLength(ts.tokenEnd - functionSourceStart); if (compilerEnv.isStrictMode() && !fnNode.getBody().hasConsistentReturnUsage()) { String msg = (name != null && name.length() > 0) ? "msg.no.return.value" : "msg.anon.no.return.value"; addStrictWarning(msg, name == null ? "" : name.getIdentifier()); } } finally { savedVars.restore(); } if (memberExprNode != null) { // TODO(stevey): fix missing functionality Kit.codeBug(); fnNode.setMemberExprNode(memberExprNode); // rewrite later /* old code: if (memberExprNode != null) { pn = nf.createAssignment(Token.ASSIGN, memberExprNode, pn); if (functionType != FunctionNode.FUNCTION_EXPRESSION) { // XXX check JScript behavior: should it be createExprStatement? pn = nf.createExprStatementNoReturn(pn, baseLineno); } } */ } fnNode.setSourceName(sourceURI); fnNode.setBaseLineno(baseLineno); fnNode.setEndLineno(ts.lineno); // Set the parent scope. Needed for finding undeclared vars. // Have to wait until after parsing the function to set its parent // scope, since defineSymbol needs the defining-scope check to stop // at the function boundary when checking for redeclarations. if (compilerEnv.isIdeMode()) { fnNode.setParentScope(currentScope); } return fnNode; }
Example 6
Source File: Parser.java From JsDroidCmd with Mozilla Public License 2.0 | 4 votes |
private TryStatement tryStatement() throws IOException { if (currentToken != Token.TRY) codeBug(); consumeToken(); // Pull out JSDoc info and reset it before recursing. Comment jsdocNode = getAndResetJsDoc(); int tryPos = ts.tokenBeg, lineno = ts.lineno, finallyPos = -1; if (peekToken() != Token.LC) { reportError("msg.no.brace.try"); } AstNode tryBlock = statement(); int tryEnd = getNodeEnd(tryBlock); List<CatchClause> clauses = null; boolean sawDefaultCatch = false; int peek = peekToken(); if (peek == Token.CATCH) { while (matchToken(Token.CATCH)) { int catchLineNum = ts.lineno; if (sawDefaultCatch) { reportError("msg.catch.unreachable"); } int catchPos = ts.tokenBeg, lp = -1, rp = -1, guardPos = -1; if (mustMatchToken(Token.LP, "msg.no.paren.catch")) lp = ts.tokenBeg; mustMatchToken(Token.NAME, "msg.bad.catchcond"); Name varName = createNameNode(); String varNameString = varName.getIdentifier(); if (inUseStrictDirective) { if ("eval".equals(varNameString) || "arguments".equals(varNameString)) { reportError("msg.bad.id.strict", varNameString); } } AstNode catchCond = null; if (matchToken(Token.IF)) { guardPos = ts.tokenBeg; catchCond = expr(); } else { sawDefaultCatch = true; } if (mustMatchToken(Token.RP, "msg.bad.catchcond")) rp = ts.tokenBeg; mustMatchToken(Token.LC, "msg.no.brace.catchblock"); Block catchBlock = (Block)statements(); tryEnd = getNodeEnd(catchBlock); CatchClause catchNode = new CatchClause(catchPos); catchNode.setVarName(varName); catchNode.setCatchCondition(catchCond); catchNode.setBody(catchBlock); if (guardPos != -1) { catchNode.setIfPosition(guardPos - catchPos); } catchNode.setParens(lp, rp); catchNode.setLineno(catchLineNum); if (mustMatchToken(Token.RC, "msg.no.brace.after.body")) tryEnd = ts.tokenEnd; catchNode.setLength(tryEnd - catchPos); if (clauses == null) clauses = new ArrayList<CatchClause>(); clauses.add(catchNode); } } else if (peek != Token.FINALLY) { mustMatchToken(Token.FINALLY, "msg.try.no.catchfinally"); } AstNode finallyBlock = null; if (matchToken(Token.FINALLY)) { finallyPos = ts.tokenBeg; finallyBlock = statement(); tryEnd = getNodeEnd(finallyBlock); } TryStatement pn = new TryStatement(tryPos, tryEnd - tryPos); pn.setTryBlock(tryBlock); pn.setCatchClauses(clauses); pn.setFinallyBlock(finallyBlock); if (finallyPos != -1) { pn.setFinallyPosition(finallyPos - tryPos); } pn.setLineno(lineno); if (jsdocNode != null) { pn.setJsDocNode(jsdocNode); } return pn; }
Example 7
Source File: NameDeclarationTerm.java From SJS with Apache License 2.0 | 4 votes |
public NameDeclarationTerm(Name name) { super(name); this.identifier = name.getIdentifier(); this.type = new AnyType(); }
Example 8
Source File: ConstraintVisitor.java From SJS with Apache License 2.0 | 4 votes |
/** * assignment to the "prototype" property */ private void processAssignToPrototype(Assignment a, AstNode left, AstNode right, ITypeTerm expTerm) throws Error { PropertyGet pg = (PropertyGet)left; AstNode base = pg.getTarget(); ITypeTerm pgTerm = findOrCreateExpressionTerm(pg); if (base instanceof Name){ Name name = (Name)base; if (!validRHSForAssignToPrototype(right)) { error( "expression " + right.toSource() + " cannot be assigned to a constructor prototype (line " + right.getLineno() + ")", a); } // can only write to prototype immediately after declaration of // constructor of the same name AstNode parent = a.getParent(); if (!(parent instanceof ExpressionStatement)) { error( "assignment to prototype property not allowed here (line " + a.getLineno() + ")", a); return; } Node prev = getPredecessorNode(parent); if (!(prev instanceof FunctionNode)) { error( "assignment to prototype property only allowed after constructor declaration (line " + a.getLineno() + ")", a); return; } FunctionNode fn = (FunctionNode) prev; String functionName = fn.getName(); String identifier = name.getIdentifier(); if (!functionName.equals(identifier)) { error( "can only assign to prototype of function " + functionName + " here (line " + a.getLineno() + ")", a); return; } ITypeTerm baseTerm = findOrCreateExpressionTerm(base); // make term for expression ITypeTerm nameTerm = findOrCreateNameDeclarationTerm(name); // find unique representative for referenced Name addTypeEqualityConstraint(baseTerm, nameTerm, a.getLineno(), null); // equate them ITypeTerm protoTerm = findOrCreateProtoTerm(baseTerm, pg.getLineno()); ITypeTerm rightTerm = processExpression(right); addTypeEqualityConstraint(pgTerm, protoTerm, a.getLineno(), null); addTypeEqualityConstraint(rightTerm, protoTerm, a.getLineno(), null); addTypeEqualityConstraint(expTerm, protoTerm, a.getLineno(), null); } else { error("processAssignToPrototype: unsupported case for receiver expression: " + base.getClass().getName(), base); } }
Example 9
Source File: ConstraintVisitor.java From SJS with Apache License 2.0 | 4 votes |
private void checkForValidProtoPropAssign(Assignment a, PropertyGet pg, int assignLineNo, PropertyGet basePG) { AstNode baseTarget = basePG.getTarget(); if (!(baseTarget instanceof Name)) { error("assignment to property of prototype not valid (line " + assignLineNo + ")", a); return; } Name baseName = (Name) baseTarget; AstNode parent = a.getParent(); if (!(parent instanceof ExpressionStatement)) { error("assignment to property of prototype not valid (line " + assignLineNo + ")", a); return; } Node prev = getPredecessorNode(parent); if (prev instanceof FunctionNode) { FunctionNode fn = (FunctionNode) prev; String functionName = fn.getName(); String identifier = baseName.getIdentifier(); if (!functionName.equals(identifier)) { error("can only assign to prototype of function " + functionName + " here (line " + assignLineNo + ")", a); return; } } else if (prev instanceof ExpressionStatement) { // it needs to be an assignment either to C.prototype or C.prototype.foo // TODO clean up this gross code AstNode expression = ((ExpressionStatement)prev).getExpression(); if (!(expression instanceof Assignment)) { error("assignment to property of prototype not valid (line " + assignLineNo + ")", a); return; } Assignment prevAssign = (Assignment) expression; AstNode prevLeft = prevAssign.getLeft(); if (!(prevLeft instanceof PropertyGet)) { error("assignment to property of prototype not valid (line " + assignLineNo + ")", a); return; } PropertyGet prevPG = (PropertyGet) prevLeft; AstNode prevPGTarget = prevPG.getTarget(); if (prevPG.getProperty().getIdentifier().equals("prototype")) { checkForSameName(assignLineNo, baseName, prevPGTarget); } else if (prevPGTarget instanceof PropertyGet) { PropertyGet prevPGBasePG = (PropertyGet) prevPGTarget; if (!prevPGBasePG.getProperty().getIdentifier().equals("prototype")) { error("assignment to property of prototype not valid (line " + assignLineNo + ")", a); return; } checkForSameName(assignLineNo, baseName, prevPGBasePG.getTarget()); } else { error("assignment to property of prototype not valid (line " + assignLineNo + ")", a); return; } } else { error("assignment to property of prototype not valid (line " + assignLineNo + ")", a); return; } }