Search in sources :

Example 6 with CommaToken

use of org.develnext.jphp.core.tokenizer.token.expr.CommaToken in project jphp by jphp-compiler.

the class FunctionGenerator method processArgument.

@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;
    boolean optional = false;
    if (next instanceof ValueIfElseToken) {
        optional = true;
        next = nextToken(iterator);
    }
    if (next instanceof SelfExprToken) {
        if (analyzer.getClazz() == null) {
            unexpectedToken(next);
        }
        if (!analyzer.getClazz().isTrait()) {
            next = analyzer.getClazz().getName();
            hintTypeClass = analyzer.getRealName((NameToken) next);
        } else {
            hintType = HintType.SELF;
        }
        next = nextToken(iterator);
    } else if (next instanceof NameToken) {
        String word = ((NameToken) next).getName().toLowerCase();
        if (scalarTypeHints.contains(word)) {
            hintType = HintType.of(word);
        } else if (disallowScalarTypeHints.contains(word)) {
            unexpectedToken(next, "valid type");
        } 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);
    argument.setOptional(optional);
    if (argument.isReference() && argument.getValue() != null)
        analyzer.getFunction().variable(argument.getName()).setUsed(true);
    return argument;
}
Also used : CommaToken(org.develnext.jphp.core.tokenizer.token.expr.CommaToken) AssignExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken) HintType(php.runtime.common.HintType) ValueIfElseToken(org.develnext.jphp.core.tokenizer.token.expr.operator.ValueIfElseToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) Token(org.develnext.jphp.core.tokenizer.token.Token) ValueIfElseToken(org.develnext.jphp.core.tokenizer.token.expr.operator.ValueIfElseToken) AssignExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken) ArgumentUnpackExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.ArgumentUnpackExprToken) CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) AmpersandRefToken(org.develnext.jphp.core.tokenizer.token.expr.operator.AmpersandRefToken) ColonToken(org.develnext.jphp.core.tokenizer.token.ColonToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) CommaToken(org.develnext.jphp.core.tokenizer.token.expr.CommaToken) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) ArgumentUnpackExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.ArgumentUnpackExprToken) AmpersandRefToken(org.develnext.jphp.core.tokenizer.token.expr.operator.AmpersandRefToken)

Example 7 with CommaToken

use of org.develnext.jphp.core.tokenizer.token.expr.CommaToken in project jphp by jphp-compiler.

the class TokenizerTest method testBraces.

@Test
public void testBraces() throws IOException {
    Tokenizer tokenizer = new Tokenizer(new Context(" :: ->foobar('a', 1, 3.0);"));
    assertTrue(tokenizer.nextToken() instanceof StaticAccessExprToken);
    assertTrue(tokenizer.nextToken() instanceof DynamicAccessExprToken);
    assertTrue(tokenizer.nextToken() instanceof NameToken);
    assertTrue(tokenizer.nextToken() instanceof BraceExprToken);
    assertTrue(tokenizer.nextToken() instanceof StringExprToken);
    assertTrue(tokenizer.nextToken() instanceof CommaToken);
    assertTrue(tokenizer.nextToken() instanceof IntegerExprToken);
    assertTrue(tokenizer.nextToken() instanceof CommaToken);
    assertTrue(tokenizer.nextToken() instanceof DoubleExprToken);
    assertTrue(tokenizer.nextToken() instanceof BraceExprToken);
    assertTrue(tokenizer.nextToken() instanceof SemicolonToken);
}
Also used : Context(php.runtime.env.Context) CommaToken(org.develnext.jphp.core.tokenizer.token.expr.CommaToken) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) DoubleExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.DoubleExprToken) StaticAccessExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.StaticAccessExprToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) IntegerExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.IntegerExprToken) DynamicAccessExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.DynamicAccessExprToken) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer) StringExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.StringExprToken) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) Test(org.junit.Test)

Example 8 with CommaToken

use of org.develnext.jphp.core.tokenizer.token.expr.CommaToken in project jphp by jphp-compiler.

the class ClassGenerator method processProperty.

protected List<ClassVarStmtToken> processProperty(ClassStmtToken clazz, VariableExprToken current, List<Token> modifiers, ListIterator<Token> iterator) {
    Token next = current;
    Token prev = null;
    Set<VariableExprToken> variables = new LinkedHashSet<VariableExprToken>();
    List<ExprStmtToken> initValues = new ArrayList<>();
    ExprStmtToken initValue = null;
    List<ClassVarStmtToken> result = new ArrayList<ClassVarStmtToken>();
    Modifier modifier = Modifier.PUBLIC;
    boolean isStatic = false;
    for (Token token : modifiers) {
        if (token instanceof PrivateStmtToken)
            modifier = Modifier.PRIVATE;
        else if (token instanceof ProtectedStmtToken)
            modifier = Modifier.PROTECTED;
        else if (token instanceof StaticExprToken)
            isStatic = true;
    }
    do {
        if (next instanceof VariableExprToken) {
            if (!variables.add((VariableExprToken) next)) {
                throw new ParseException(Messages.ERR_IDENTIFIER_X_ALREADY_USED.fetch(next.getWord()), next.toTraceInfo(analyzer.getContext()));
            }
            initValues.add(null);
        } else if (next instanceof CommaToken) {
            if (!(prev instanceof VariableExprToken))
                unexpectedToken(next);
        } else if (next instanceof AssignExprToken) {
            if (!(prev instanceof VariableExprToken))
                unexpectedToken(next);
            initValue = analyzer.generator(SimpleExprGenerator.class).getToken(nextToken(iterator), iterator, Separator.COMMA_OR_SEMICOLON, null);
            initValues.set(initValues.size() - 1, initValue);
            if (iterator.hasPrevious() && isBreak(iterator.previous())) {
                iterator.next();
                break;
            }
            if (iterator.hasNext()) {
                iterator.next();
            }
        // break;
        } else if (next instanceof SemicolonToken) {
            if (!(prev instanceof VariableExprToken))
                unexpectedToken(next);
            break;
        }
        prev = next;
        next = nextToken(iterator);
    } while (true);
    int i = 0;
    for (VariableExprToken variable : variables) {
        ClassVarStmtToken classVar = new ClassVarStmtToken(variable.getMeta());
        classVar.setModifier(modifier);
        classVar.setStatic(isStatic);
        classVar.setValue(initValues.get(i));
        classVar.setVariable(variable);
        classVar.setClazz(clazz);
        result.add(classVar);
        i++;
    }
    return result;
}
Also used : CommaToken(org.develnext.jphp.core.tokenizer.token.expr.CommaToken) AssignExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken) Token(org.develnext.jphp.core.tokenizer.token.Token) AssignExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken) CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) CommaToken(org.develnext.jphp.core.tokenizer.token.expr.CommaToken) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) ParseException(php.runtime.exceptions.ParseException) Modifier(php.runtime.common.Modifier)

Example 9 with CommaToken

use of org.develnext.jphp.core.tokenizer.token.expr.CommaToken in project jphp by jphp-compiler.

the class ClassGenerator method processImplements.

protected void processImplements(ClassStmtToken result, ListIterator<Token> iterator) {
    checkUnexpectedEnd(iterator);
    Token token = iterator.next();
    if (token instanceof ImplementsStmtToken) {
        if (result.isTrait())
            unexpectedToken(token);
        if (result.isInterface())
            unexpectedToken(token, "extends");
        _processImplements(token, result, iterator);
    /*
            ImplementsStmtToken implement = new ImplementsStmtToken(token.getMeta());

            Token prev = null;
            List<FulledNameToken> names = new ArrayList<FulledNameToken>();
            do {
                token = nextToken(iterator);
                if (token instanceof NameToken){
                    names.add(analyzer.getRealName((NameToken)token));
                } else if (token instanceof CommaToken){
                    if (!(prev instanceof NameToken))
                        unexpectedToken(token);
                } else if (isOpenedBrace(token, BraceExprToken.Kind.BLOCK)){
                    iterator.previous();
                    break;
                } else
                    unexpectedToken(token);

                prev = token;
            } while (true);

            implement.setNames(names);
            result.setImplement(implement);*/
    } else
        iterator.previous();
}
Also used : ValueExprToken(org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken) Token(org.develnext.jphp.core.tokenizer.token.Token) ValueIfElseToken(org.develnext.jphp.core.tokenizer.token.expr.operator.ValueIfElseToken) AssignExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken) CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) CommaToken(org.develnext.jphp.core.tokenizer.token.expr.CommaToken) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken)

Example 10 with CommaToken

use of org.develnext.jphp.core.tokenizer.token.expr.CommaToken in project jphp by jphp-compiler.

the class ConstGenerator method getToken.

/*@SuppressWarnings("unchecked")
    protected void processBody(ConstStmtToken result, ListIterator<Token> iterator){
        Token current = nextToken(iterator);
        if (!(current instanceof AssignExprToken))
            unexpectedToken(current, "=");

        ExprStmtToken value = analyzer.generator(SimpleExprGenerator.class).getToken(nextToken(iterator), iterator);
        if (value == null)
            unexpectedToken(nextToken(iterator));

        result.setValue(value);
    }*/
@Override
@SuppressWarnings("unchecked")
public ConstStmtToken getToken(Token current, ListIterator<Token> iterator) {
    if (current instanceof ConstStmtToken) {
        ConstStmtToken result = (ConstStmtToken) current;
        Token prev = null;
        if (analyzer.getClazz() == null)
            result.setNamespace(analyzer.getNamespace());
        while (true) {
            Token next = analyzer.getClazz() == null ? nextToken(iterator) : nextTokenSensitive(iterator, ClassStmtToken.class);
            if (next instanceof NameToken) {
                if (next instanceof FulledNameToken && !((FulledNameToken) next).isProcessed(NamespaceUseStmtToken.UseType.CONSTANT))
                    unexpectedToken(next, TokenType.T_STRING);
                Token token = nextToken(iterator);
                if (!(token instanceof AssignExprToken))
                    unexpectedToken(token, "=");
                ExprStmtToken value = analyzer.generator(SimpleExprGenerator.class).getToken(nextToken(iterator), iterator, Separator.COMMA_OR_SEMICOLON, null);
                if (!isBreak(iterator.previous())) {
                    iterator.next();
                }
                if (value == null)
                    unexpectedToken(iterator.previous());
                result.add((NameToken) next, value);
            } else if (next instanceof CommaToken) {
                if (prev instanceof CommaToken)
                    unexpectedToken(next);
                prev = next;
            } else if (isBreak(next)) {
                break;
            } else
                unexpectedToken(next, TokenType.T_STRING);
        }
        return result;
    }
    return null;
}
Also used : CommaToken(org.develnext.jphp.core.tokenizer.token.expr.CommaToken) AssignExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken) SimpleExprGenerator(org.develnext.jphp.core.syntax.generators.manually.SimpleExprGenerator) ValueExprToken(org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken) Token(org.develnext.jphp.core.tokenizer.token.Token) AssignExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken) ClassExprToken(org.develnext.jphp.core.tokenizer.token.expr.ClassExprToken) ImportExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.ImportExprToken) OperatorExprToken(org.develnext.jphp.core.tokenizer.token.expr.OperatorExprToken) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken) CommaToken(org.develnext.jphp.core.tokenizer.token.expr.CommaToken) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)

Aggregations

CommaToken (org.develnext.jphp.core.tokenizer.token.expr.CommaToken)10 SemicolonToken (org.develnext.jphp.core.tokenizer.token.SemicolonToken)9 Token (org.develnext.jphp.core.tokenizer.token.Token)9 BraceExprToken (org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken)9 AssignExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken)8 CommentToken (org.develnext.jphp.core.tokenizer.token.CommentToken)7 ValueExprToken (org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)6 ValueIfElseToken (org.develnext.jphp.core.tokenizer.token.expr.operator.ValueIfElseToken)6 NameToken (org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)3 FulledNameToken (org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)2 ParseException (php.runtime.exceptions.ParseException)2 ArrayList (java.util.ArrayList)1 SimpleExprGenerator (org.develnext.jphp.core.syntax.generators.manually.SimpleExprGenerator)1 Tokenizer (org.develnext.jphp.core.tokenizer.Tokenizer)1 ColonToken (org.develnext.jphp.core.tokenizer.token.ColonToken)1 BackslashExprToken (org.develnext.jphp.core.tokenizer.token.expr.BackslashExprToken)1 ClassExprToken (org.develnext.jphp.core.tokenizer.token.expr.ClassExprToken)1 OperatorExprToken (org.develnext.jphp.core.tokenizer.token.expr.OperatorExprToken)1 AmpersandRefToken (org.develnext.jphp.core.tokenizer.token.expr.operator.AmpersandRefToken)1 ArgumentUnpackExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.ArgumentUnpackExprToken)1