Example #1
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;
  }