Beispiel #1
0
  public Arity(
      int required,
      int optional,
      boolean allowsMore,
      boolean hasKeywords,
      boolean hasKeyRest,
      int definedKeywords,
      ArgsNode argsNode) {
    this.required = required;
    this.optional = optional;
    this.allowsMore = allowsMore;
    this.definedKeywords = definedKeywords;
    this.hasKeywords = hasKeywords;
    this.hasKeyRest = hasKeyRest;

    if (argsNode.hasKwargs()) {
      keywordArguments = new ArrayList<>();
      if (argsNode.getKeywords() != null) {
        for (Node node : argsNode.getKeywords().children()) {
          final KeywordArgNode kwarg = (KeywordArgNode) node;
          final AssignableNode assignableNode = kwarg.getAssignable();

          if (assignableNode instanceof LocalAsgnNode) {
            keywordArguments.add(((LocalAsgnNode) assignableNode).getName());
          } else if (assignableNode instanceof DAsgnNode) {
            keywordArguments.add(((DAsgnNode) assignableNode).getName());
          } else {
            throw new UnsupportedOperationException("unsupported keyword arg " + node);
          }
        }
      }
    } else {
      keywordArguments = null;
    }
  }
Beispiel #2
0
  public static Arity getArity(org.jruby.ast.ArgsNode argsNode) {
    final String[] keywordArguments;

    if (argsNode.hasKwargs() && argsNode.getKeywords() != null) {
      final org.jruby.ast.Node[] keywordNodes = argsNode.getKeywords().children();
      final int keywordsCount = keywordNodes.length;

      keywordArguments = new String[keywordsCount];
      for (int i = 0; i < keywordsCount; i++) {
        final KeywordArgNode kwarg = (KeywordArgNode) keywordNodes[i];
        final AssignableNode assignableNode = kwarg.getAssignable();

        if (assignableNode instanceof LocalAsgnNode) {
          keywordArguments[i] = ((LocalAsgnNode) assignableNode).getName();
        } else if (assignableNode instanceof DAsgnNode) {
          keywordArguments[i] = ((DAsgnNode) assignableNode).getName();
        } else {
          throw new UnsupportedOperationException("unsupported keyword arg " + kwarg);
        }
      }
    } else {
      keywordArguments = Arity.NO_KEYWORDS;
    }

    return new Arity(
        argsNode.getPreCount(),
        argsNode.getOptionalArgsCount(),
        argsNode.hasRestArg(),
        argsNode.getPostCount(),
        keywordArguments,
        argsNode.hasKeyRest());
  }