Esempio n. 1
0
  protected void processUses(FunctionStmtToken result, ListIterator<Token> iterator) {
    Token next = nextToken(iterator);
    if (next instanceof NamespaceUseStmtToken) {
      next = nextToken(iterator);
      if (!isOpenedBrace(next, BraceExprToken.Kind.SIMPLE)) unexpectedToken(next, "(");

      List<ArgumentStmtToken> arguments = new ArrayList<ArgumentStmtToken>();
      while (iterator.hasNext()) {
        ArgumentStmtToken argument = processArgument(iterator);
        if (argument == null) break;

        if (argument.getValue() != null) unexpectedToken(argument.getValue().getSingle());
        arguments.add(argument);

        FunctionStmtToken parent = analyzer.getFunction(true);
        if (parent != null) {
          parent.variable(argument.getName()).setUsed(true);

          if (argument.isReference()) {
            parent.variable(argument.getName()).setPassed(true).setUnstable(true);
          }

          parent = analyzer.peekClosure();
          if (parent != null) {
            parent.variable(argument.getName()).setUnstable(true);
          }
        }
      }

      result.setUses(arguments);
    } else {
      result.setUses(new ArrayList<ArgumentStmtToken>());
      iterator.previous();
    }
  }
Esempio n. 2
0
  @SuppressWarnings("unchecked")
  protected ArgumentStmtToken processArgument(ListIterator<Token> iterator) {
    boolean isReference = false;
    boolean isVariadic = false;

    VariableExprToken variable = null;
    ExprStmtToken value = null;

    Token next = nextToken(iterator);
    if (next instanceof CommaToken || isClosedBrace(next, BraceExprToken.Kind.SIMPLE)) return null;

    NameToken hintTypeClass = null;
    HintType hintType = null;

    if (next instanceof NameToken) {
      String word = ((NameToken) next).getName().toLowerCase();
      if (scalarTypeHints.contains(word)) hintType = HintType.of(word);
      else {
        hintType = jphp_scalarTypeHints.contains(word) ? null : HintType.of(word);

        if (hintType == null) hintTypeClass = analyzer.getRealName((NameToken) next);
      }

      next = nextToken(iterator);
    }

    if (next instanceof AmpersandRefToken) {
      isReference = true;
      next = nextToken(iterator);
    }

    if (next instanceof ArgumentUnpackExprToken) {
      isVariadic = true;
      next = nextToken(iterator);
    }

    if (next instanceof VariableExprToken) {
      variable = (VariableExprToken) next;
    } else unexpectedToken(next);

    next = nextToken(iterator);
    if (next instanceof AssignExprToken) {
      if (isVariadic) {
        unexpectedToken(next);
      }

      value =
          analyzer
              .generator(SimpleExprGenerator.class)
              .getToken(nextToken(iterator), iterator, true, BraceExprToken.Kind.SIMPLE);
    } else {
      if (next instanceof CommaToken || isClosedBrace(next, BraceExprToken.Kind.SIMPLE)) {
        if (next instanceof BraceExprToken) {
          iterator.previous();
        } else {
          if (isVariadic) {
            unexpectedToken(next);
          }
        }
      } else unexpectedToken(next);
    }

    ArgumentStmtToken argument = new ArgumentStmtToken(variable.getMeta());
    argument.setName(variable);
    argument.setHintType(hintType);
    argument.setHintTypeClass(hintTypeClass);
    argument.setReference(isReference);
    argument.setVariadic(isVariadic);
    argument.setValue(value);

    if (argument.isReference() && argument.getValue() != null)
      analyzer.getFunction().variable(argument.getName()).setUsed(true);

    return argument;
  }
Esempio n. 3
0
  @SuppressWarnings("unchecked")
  public FunctionStmtToken getToken(
      Token current, ListIterator<Token> iterator, boolean closureAllowed) {
    if (current instanceof FunctionStmtToken) {
      FunctionStmtToken result = (FunctionStmtToken) current;
      result.setStatic(analyzer.getFunction() == null);

      Class<? extends Token>[] excludes = new Class[] {EchoStmtToken.class, ImportExprToken.class};

      if (analyzer.getClazz() != null) {
        excludes = new Class[0];
      }

      Token next = nextTokenSensitive(iterator, excludes);

      if (next instanceof AmpersandRefToken) {
        result.setReturnReference(true);
        next = nextTokenSensitive(iterator, excludes);
      }

      if (next instanceof NameToken) {
        /*if (analyzer.getFunction() != null)
        unexpectedToken(current);*/

        analyzer.addScope(true);
        FunctionStmtToken oldFunction = analyzer.getFunction();
        analyzer.setFunction(result);

        BraceExprToken brace = nextAndExpected(iterator, BraceExprToken.class);
        if (!brace.isSimpleOpened()) unexpectedToken(brace, "(");

        result.setNamespace(analyzer.getNamespace());
        result.setName((NameToken) next);
        processArguments(result, iterator);
        processBody(result, iterator);

        result.setLabels(analyzer.getScope().getLabels());
        result.setLocal(analyzer.removeScope().getVariables());

        analyzer.setFunction(oldFunction);
        return result;
      } else if (next instanceof BraceExprToken) {
        // xClosure
        if (((BraceExprToken) next).isSimpleOpened()) {
          if (closureAllowed) {
            analyzer.pushClosure(result);

            analyzer.addScope(true);
            processArguments(result, iterator);
            processUses(result, iterator);
            processBody(result, iterator);
            // boolean thisExists = result.isThisExists();
            result.setLabels(analyzer.getScope().getLabels());
            result.setStaticExists(analyzer.getScope().isStaticExists());
            result.setLocal(analyzer.removeScope().getVariables());
            // result.setThisExists(thisExists);

            analyzer.popClosure();

            FunctionStmtToken prevClosure = analyzer.peekClosure();
            if (prevClosure != null) {
              if (result.isThisExists()) {
                analyzer.getScope().addVariable(FunctionStmtToken.thisVariable);
                // prevClosure.variable(FunctionStmtToken.thisVariable).setUsed(true);
                // prevClosure.setThisExists(true);
              }
            }

            List<VariableExprToken> uses = new ArrayList<VariableExprToken>();
            for (ArgumentStmtToken argument : result.getUses()) {
              if (argument.isReference()) {
                if (analyzer.getFunction() != null) {
                  analyzer.getFunction().variable(argument.getName()).setReference(true);
                }
              }
              uses.add(argument.getName());
            }

            analyzer.getScope().addVariables(uses);
            return result;
          }

          iterator.previous();
          return null;
        }
      }

      unexpectedToken(next);
    }

    return null;
  }