Ejemplo n.º 1
0
 protected void processBody(FunctionStmtToken result, ListIterator<Token> iterator) {
   Token next = nextToken(iterator);
   if (isOpenedBrace(next, BraceExprToken.Kind.BLOCK)) {
     BodyStmtToken body = analyzer.generator(BodyGenerator.class).getToken(next, iterator);
     result.setBody(body);
   } else if (next instanceof SemicolonToken) {
     result.setInterfacable(true);
     result.setBody(null);
   } else unexpectedToken(next);
 }
Ejemplo n.º 2
0
  protected void processArguments(FunctionStmtToken result, ListIterator<Token> iterator) {
    checkUnexpectedEnd(iterator);
    List<ArgumentStmtToken> arguments = new ArrayList<ArgumentStmtToken>();
    while (iterator.hasNext()) {
      ArgumentStmtToken argument = processArgument(iterator);
      if (argument == null) break;

      arguments.add(argument);
    }

    result.setArguments(arguments);
  }
Ejemplo n.º 3
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();
    }
  }
Ejemplo n.º 4
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;
  }