Search in sources :

Example 1 with CommentToken

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

the class SyntaxAnalyzer method process.

protected void process() {
    tokenizer.reset();
    tree.clear();
    Token token;
    while ((token = tokenizer.nextToken()) != null) {
        if (token instanceof CommentToken) {
            if (((CommentToken) token).getKind() != CommentToken.Kind.DOCTYPE)
                continue;
        }
        tokens.add(token);
    }
    /*if (tokenizer.hasDirective("mode")){
            Directive mode = tokenizer.getDirective("mode");
            try {
                this.langMode = LangMode.valueOf(mode.value.toUpperCase());
            } catch (IllegalArgumentException e){
                environment.warning(
                        mode.trace, "Invalid value '%s' for directive 'mode'", mode.value
                );
            }
        }*/
    ListIterator<Token> iterator = tokens.listIterator();
    tree = process(iterator);
}
Also used : CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) ValueExprToken(org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken) Token(org.develnext.jphp.core.tokenizer.token.Token) CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken) ClosureStmtToken(org.develnext.jphp.core.tokenizer.token.expr.value.ClosureStmtToken)

Example 2 with CommentToken

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

the class ClassGenerator method processBody.

@SuppressWarnings("unchecked")
protected void processBody(ClassStmtToken result, ListIterator<Token> iterator) {
    analyzer.setClazz(result);
    Token token = nextToken(iterator);
    if (token instanceof BraceExprToken) {
        BraceExprToken brace = (BraceExprToken) token;
        if (brace.isBlockOpened()) {
            List<ConstStmtToken> constants = new ArrayList<ConstStmtToken>();
            List<MethodStmtToken> methods = new ArrayList<MethodStmtToken>();
            List<ClassVarStmtToken> properties = new ArrayList<ClassVarStmtToken>();
            List<Token> modifiers = new ArrayList<Token>();
            CommentToken lastComment = null;
            boolean breakByClose = false;
            while (iterator.hasNext()) {
                Token current = iterator.next();
                if (current instanceof ExprStmtToken)
                    unexpectedToken(current, "expression");
                if (current instanceof ConstStmtToken) {
                    if (!modifiers.isEmpty())
                        unexpectedToken(modifiers.get(0));
                    if (result.isTrait())
                        unexpectedToken(current);
                    ConstStmtToken one = analyzer.generator(ConstGenerator.class).getToken(current, iterator);
                    one.setClazz(result);
                    one.setDocComment(lastComment);
                    lastComment = null;
                    constants.add(one);
                    modifiers.clear();
                } else if (isTokenClass(current, ClassGenerator.modifiers)) {
                    for (Token modifier : modifiers) {
                        if (modifier.getType() == current.getType())
                            unexpectedToken(current);
                    }
                    modifiers.add(current);
                } else if (current instanceof VariableExprToken) {
                    if (result.isInterface()) {
                        analyzer.getEnvironment().error(result.toTraceInfo(analyzer.getContext()), ErrorType.E_ERROR, Messages.ERR_INTERFACE_MAY_NOT_INCLUDE_VARS);
                    }
                    for (Token modifier : modifiers) {
                        if (isTokenClass(modifier, FinalStmtToken.class, AbstractStmtToken.class)) {
                            unexpectedToken(modifier);
                        }
                    }
                    List<ClassVarStmtToken> vars = processProperty(result, (VariableExprToken) current, modifiers, iterator);
                    if (lastComment != null) {
                        for (ClassVarStmtToken var : vars) {
                            var.setDocComment(lastComment);
                        }
                        lastComment = null;
                    }
                    properties.addAll(vars);
                    modifiers.clear();
                } else if (current instanceof FunctionStmtToken) {
                    FunctionStmtToken function = analyzer.generator(FunctionGenerator.class).getToken(current, iterator);
                    if (function == null) {
                        nextToken(iterator);
                        unexpectedToken(iterator);
                    }
                    MethodStmtToken method = new MethodStmtToken(function);
                    method.setClazz(result);
                    method.setDocComment(lastComment);
                    lastComment = null;
                    for (Token modifier : modifiers) {
                        if (modifier instanceof AbstractStmtToken)
                            method.setAbstract(true);
                        else if (modifier instanceof StaticExprToken)
                            method.setStatic(true);
                        else if (modifier instanceof FinalStmtToken) {
                            method.setFinal(true);
                        } else if (modifier instanceof PublicStmtToken) {
                            if (method.getModifier() != null)
                                unexpectedToken(modifier);
                            method.setModifier(Modifier.PUBLIC);
                        } else if (modifier instanceof PrivateStmtToken) {
                            if (method.getModifier() != null)
                                unexpectedToken(modifier);
                            method.setModifier(Modifier.PRIVATE);
                        } else if (modifier instanceof ProtectedStmtToken) {
                            if (method.getModifier() != null)
                                unexpectedToken(modifier);
                            method.setModifier(Modifier.PROTECTED);
                        }
                    }
                    if (method.getModifier() == null)
                        method.setModifier(Modifier.PUBLIC);
                    methods.add(method);
                    modifiers.clear();
                } else if (current instanceof NamespaceUseStmtToken) {
                    processUse(result, iterator);
                    lastComment = null;
                } else if (isClosedBrace(current, BraceExprToken.Kind.BLOCK)) {
                    breakByClose = true;
                    break;
                } else if (current instanceof CommentToken) {
                    lastComment = (CommentToken) current;
                } else
                    unexpectedToken(current);
            }
            if (!breakByClose) {
                // bug-fix from DN.
                token = nextTokenAndPrev(iterator);
                if (!isClosedBrace(token, BraceExprToken.Kind.BLOCK)) {
                    unexpectedToken(token);
                }
            }
            // ---
            result.setConstants(constants);
            result.setMethods(methods);
            result.setProperties(properties);
            analyzer.setClazz(null);
            return;
        }
    }
    unexpectedToken(token, "{");
}
Also used : CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) 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)

Example 3 with CommentToken

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

the class FunctionGenerator method getToken.

@SuppressWarnings("unchecked")
public FunctionStmtToken getToken(Token current, ListIterator<Token> iterator, boolean closureAllowed) {
    if (current instanceof FunctionStmtToken) {
        CommentToken commentToken = null;
        iterator.previous();
        if (iterator.hasPrevious()) {
            int cnt = 0;
            while (iterator.hasPrevious()) {
                cnt++;
                Token previous = iterator.previous();
                if (previous.isNamedToken())
                    continue;
                if (previous instanceof CommentToken && ((CommentToken) previous).getKind() == CommentToken.Kind.DOCTYPE) {
                    commentToken = ((CommentToken) previous);
                }
                break;
            }
            for (int i = 0; i < cnt; i++) {
                iterator.next();
            }
        }
        iterator.next();
        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);
            result.setDocComment(commentToken);
            processArguments(result, iterator);
            processBody(result, iterator);
            result.setTypeInfo(analyzer.getScope().getTypeInfo());
            result.setLabels(analyzer.getScope().getLabels());
            result.setLocal(analyzer.removeScope().getVariables());
            Token previous = iterator.previous();
            result.getMeta().setEndLine(previous.getMeta().getStartLine());
            result.getMeta().setEndPosition(previous.getMeta().getStartPosition());
            iterator.next();
            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.setTypeInfo(analyzer.getScope().getTypeInfo());
                    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);
                            }
                        }
                        if (analyzer.getFunction() != null) {
                            analyzer.getFunction().variable(argument.getName()).setUsed(true);
                        }
                        uses.add(argument.getName());
                    }
                    analyzer.getScope().addVariables(uses);
                    Token previous = iterator.previous();
                    result.getMeta().setEndLine(previous.getMeta().getStartLine());
                    result.getMeta().setEndPosition(previous.getMeta().getStartPosition());
                    iterator.next();
                    return result;
                }
                iterator.previous();
                return null;
            }
        }
        unexpectedToken(next);
    }
    return null;
}
Also used : CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) Token(org.develnext.jphp.core.tokenizer.token.Token) 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) 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) AmpersandRefToken(org.develnext.jphp.core.tokenizer.token.expr.operator.AmpersandRefToken)

Example 4 with CommentToken

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

the class Generator method nextToken.

protected Token nextToken(ListIterator<Token> iterator) {
    checkUnexpectedEnd(iterator);
    Token tk = iterator.next();
    if (tk instanceof CommentToken)
        return nextToken(iterator);
    return tk;
}
Also used : CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) BreakToken(org.develnext.jphp.core.tokenizer.token.BreakToken) Token(org.develnext.jphp.core.tokenizer.token.Token) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken)

Example 5 with CommentToken

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

the class Generator method nextTokenAndPrev.

protected Token nextTokenAndPrev(ListIterator<Token> iterator) {
    checkUnexpectedEnd(iterator);
    Token result = iterator.next();
    if (result instanceof CommentToken) {
        return nextTokenAndPrev(iterator);
    }
    iterator.previous();
    return result;
}
Also used : CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) BreakToken(org.develnext.jphp.core.tokenizer.token.BreakToken) Token(org.develnext.jphp.core.tokenizer.token.Token) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) BraceExprToken(org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken)

Aggregations

CommentToken (org.develnext.jphp.core.tokenizer.token.CommentToken)8 Token (org.develnext.jphp.core.tokenizer.token.Token)8 SemicolonToken (org.develnext.jphp.core.tokenizer.token.SemicolonToken)6 BraceExprToken (org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken)5 CommaToken (org.develnext.jphp.core.tokenizer.token.expr.CommaToken)3 AssignExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.AssignExprToken)3 NameToken (org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)3 Tokenizer (org.develnext.jphp.core.tokenizer.Tokenizer)2 BreakToken (org.develnext.jphp.core.tokenizer.token.BreakToken)2 Test (org.junit.Test)2 Context (php.runtime.env.Context)2 ValueExprToken (org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)1 AmpersandRefToken (org.develnext.jphp.core.tokenizer.token.expr.operator.AmpersandRefToken)1 ArgumentUnpackExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.ArgumentUnpackExprToken)1 ClosureStmtToken (org.develnext.jphp.core.tokenizer.token.expr.value.ClosureStmtToken)1 FulledNameToken (org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)1