Пример #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;
  }
Пример #2
0
  @Nullable
  private JsNameRef getTargetLabel(@NotNull Node statementWithLabel) {
    int type = statementWithLabel.getType();
    if (type != TokenStream.BREAK && type != TokenStream.CONTINUE) {
      String tokenTypeName = TokenStream.tokenToName(statementWithLabel.getType());
      throw new AssertionError("Unexpected node type with label: " + tokenTypeName);
    }

    Node label = statementWithLabel.getFirstChild();
    if (label == null) return null;

    String identifier = label.getString();
    assert identifier != null : "If label exists identifier should not be null";

    JsName labelName = scopeContext.labelFor(identifier);
    assert labelName != null : "Unknown label name: " + identifier;

    return labelName.makeRef();
  }