Example #1
0
  public JsFunction mapFunction(Node fnNode) throws JsParserException {
    int nodeType = fnNode.getType();
    assert nodeType == TokenStream.FUNCTION
        : "Expected function node, got: " + TokenStream.tokenToName(nodeType);
    Node fromFnNameNode = fnNode.getFirstChild();
    Node fromParamNode = fnNode.getFirstChild().getNext().getFirstChild();
    Node fromBodyNode = fnNode.getFirstChild().getNext().getNext();
    JsFunction toFn = scopeContext.enterFunction();

    // Decide the function's name, if any.
    //
    String fnNameIdent = fromFnNameNode.getString();
    if (fnNameIdent != null && fnNameIdent.length() > 0) {
      scopeContext.globalNameFor(fnNameIdent);
    }

    while (fromParamNode != null) {
      String fromParamName = fromParamNode.getString();
      JsName name = scopeContext.localNameFor(fromParamName);
      toFn.getParameters().add(new JsParameter(name));
      fromParamNode = fromParamNode.getNext();
    }

    // Map the function's body.
    //
    JsBlock toBody = mapBlock(fromBodyNode);
    toFn.setBody(toBody);

    scopeContext.exitFunction();
    return toFn;
  }