Search in sources :

Example 1 with TokenMeta

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

the class StringExprTokenTest method testSimple.

@Test
public void testSimple() {
    TokenMeta meta = new TokenMeta("foobar", 0, 0, 0, 0);
    StringExprToken token = new StringExprToken(meta, StringExprToken.Quote.SINGLE);
    Assert.assertEquals("foobar", token.getValue());
}
Also used : TokenMeta(org.develnext.jphp.core.tokenizer.TokenMeta) StringExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.StringExprToken) Test(org.junit.Test)

Example 2 with TokenMeta

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

the class Token method of.

public static Token of(TokenMeta meta) {
    TokenFinder finder = new TokenFinder();
    Class<? extends Token> clazz = finder.find(meta);
    try {
        return clazz.getConstructor(TokenMeta.class).newInstance(meta);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e.getTargetException());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : TokenFinder(org.develnext.jphp.core.tokenizer.TokenFinder) TokenMeta(org.develnext.jphp.core.tokenizer.TokenMeta) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with TokenMeta

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

the class SimpleExprGenerator method processString.

protected Token processString(StringExprToken string) {
    if (string.getSegments().isEmpty()) {
        if (string.getQuote() == StringExprToken.Quote.SHELL) {
            return new ShellExecExprToken(string.getMeta(), Arrays.<Token>asList(string));
        }
        return string;
    }
    List<Token> tokens = new ArrayList<Token>();
    int i = 0;
    String value = string.getValue();
    TokenMeta meta = string.getMeta();
    for (StringExprToken.Segment segment : string.getSegments()) {
        String prev = value.substring(i, segment.from);
        if (!prev.isEmpty()) {
            StringExprToken item = new StringExprToken(new TokenMeta(prev, meta.getStartLine() + i, meta.getEndLine(), meta.getStartLine(), meta.getEndLine()), StringExprToken.Quote.SINGLE);
            tokens.add(item);
        }
        String dynamic = value.substring(segment.from, segment.to);
        if (!segment.isVariable)
            dynamic = dynamic.substring(1, dynamic.length() - 1);
        Tokenizer tokenizer = new Tokenizer(dynamic + ";", analyzer.getContext());
        try {
            SyntaxAnalyzer syntaxAnalyzer = new SyntaxAnalyzer(analyzer.getEnvironment(), tokenizer, analyzer.getFunction());
            List<Token> tree = syntaxAnalyzer.getTree();
            analyzer.getScope().addVariables(syntaxAnalyzer.getScope().getVariables());
            assert tree.size() > 0;
            Token item = tree.get(0);
            if (!(item instanceof ExprStmtToken))
                unexpectedToken(item);
            ExprStmtToken expr = (ExprStmtToken) item;
            if (expr.isSingle()) {
                tokens.add(expr.getSingle());
            } else
                tokens.add(expr);
        } catch (ParseException e) {
            TraceInfo oldTrace = e.getTraceInfo();
            e.setTraceInfo(new TraceInfo(analyzer.getContext(), meta.getStartLine() + oldTrace.getStartLine(), meta.getEndLine() + oldTrace.getEndLine(), meta.getStartLine() + oldTrace.getStartLine(), meta.getEndLine() + oldTrace.getEndLine()));
            throw e;
        }
        i = segment.to;
    }
    String prev = value.substring(i);
    if (!prev.isEmpty()) {
        StringExprToken item = new StringExprToken(new TokenMeta(prev, meta.getStartLine() + i, meta.getEndLine(), meta.getStartLine(), meta.getEndLine()), StringExprToken.Quote.SINGLE);
        tokens.add(item);
    }
    if (string.getQuote() == StringExprToken.Quote.SHELL) {
        return new ShellExecExprToken(meta, tokens);
    }
    StringBuilderExprToken result = new StringBuilderExprToken(meta, tokens);
    result.setBinary(string.isBinary());
    return result;
}
Also used : TokenMeta(org.develnext.jphp.core.tokenizer.TokenMeta) MacroToken(org.develnext.jphp.core.tokenizer.token.expr.value.macro.MacroToken) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) Token(org.develnext.jphp.core.tokenizer.token.Token) CastExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.cast.CastExprToken) BreakToken(org.develnext.jphp.core.tokenizer.token.BreakToken) ColonToken(org.develnext.jphp.core.tokenizer.token.ColonToken) UnsetCastExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.cast.UnsetCastExprToken) TraceInfo(php.runtime.env.TraceInfo) SyntaxAnalyzer(org.develnext.jphp.core.syntax.SyntaxAnalyzer) ParseException(php.runtime.exceptions.ParseException) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer)

Example 4 with TokenMeta

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

the class MethodStmtCompiler method compile.

@Override
public MethodEntity compile() {
    if (statement != null) {
        if (external)
            statement.setDynamicLocal(true);
        if (statement.getDocComment() != null)
            entity.setDocComment(new DocumentComment(statement.getDocComment().getComment()));
        entity.setAbstract(statement.isAbstract());
        entity.setAbstractable(statement.getBody() == null);
        entity.setFinal(statement.isFinal());
        entity.setStatic(statement.isStatic());
        entity.setModifier(statement.getModifier());
        entity.setReturnReference(statement.isReturnReference());
        entity.setTrace(statement.toTraceInfo(compiler.getContext()));
        entity.setImmutable(statement.getArguments().isEmpty());
        entity.setGeneratorEntity(generatorEntity);
        if (statement.getReturnHintTypeClass() != null) {
            entity.setReturnTypeChecker(TypeChecker.of(statement.getReturnHintTypeClass().getName()));
        } else if (statement.getReturnHintType() != null) {
            entity.setReturnTypeChecker(TypeChecker.of(statement.getReturnHintType()));
            if (statement.getReturnHintType() == HintType.SELF) {
                entity.setUsesStackTrace(true);
            }
        }
        entity.setReturnTypeNullable(statement.isReturnOptional());
        if (clazz.isSystem())
            entity.setInternalName(entity.getName());
        else
            entity.setInternalName(entity.getName() + "$" + clazz.entity.nextMethodIndex());
        ParameterEntity[] parameters = new ParameterEntity[statement.getArguments().size()];
        int i = 0;
        for (ArgumentStmtToken argument : statement.getArguments()) {
            parameters[i] = new ParameterEntity(compiler.getContext());
            ParameterEntity parameter = parameters[i];
            parameter.setReference(argument.isReference());
            parameter.setName(argument.getName().getName());
            parameter.setTrace(argument.toTraceInfo(compiler.getContext()));
            parameter.setNullable(argument.isOptional());
            parameter.setMutable(statement.isDynamicLocal() || statement.variable(argument.getName()).isMutable());
            parameter.setUsed(!statement.isUnusedVariable(argument.getName()));
            parameter.setVariadic(argument.isVariadic());
            parameter.setType(argument.getHintType());
            if (argument.getHintTypeClass() != null) {
                parameter.setTypeClass(argument.getHintTypeClass().getName());
            }
            ExpressionStmtCompiler expressionStmtCompiler = new ExpressionStmtCompiler(compiler);
            ExprStmtToken value = argument.getValue();
            if (value != null) {
                Memory defaultValue = expressionStmtCompiler.writeExpression(value, true, true, false);
                // try detect constant
                if (value.isSingle()) {
                    if (value.getSingle() instanceof NameToken) {
                        parameter.setDefaultValueConstName(((NameToken) value.getSingle()).getName());
                        if (defaultValue == null) {
                            defaultValue = (new ConstantMemory(((NameToken) value.getSingle()).getName()));
                            parameter.setMutable(true);
                        }
                    } else if (value.getSingle() instanceof StaticAccessExprToken) {
                        StaticAccessExprToken access = (StaticAccessExprToken) value.getSingle();
                        if (access.getClazz() instanceof NameToken && access.getField() instanceof NameToken) {
                            if (defaultValue == null)
                                defaultValue = (new ClassConstantMemory(((NameToken) access.getClazz()).getName(), ((NameToken) access.getField()).getName()));
                            parameter.setDefaultValueConstName(((NameToken) access.getClazz()).getName() + "::" + ((NameToken) access.getField()).getName());
                            parameter.setMutable(true);
                        }
                    }
                }
                if (defaultValue == null)
                    compiler.getEnvironment().error(argument.toTraceInfo(compiler.getContext()), ErrorType.E_COMPILE_ERROR, Messages.ERR_EXPECTED_CONST_VALUE, "$" + argument.getName().getName());
                parameter.setDefaultValue(defaultValue);
            }
            i++;
        }
        entity.setParameters(parameters);
    }
    if (statement != null && clazz.statement.isInterface()) {
        if (!statement.isInterfacable()) {
            compiler.getEnvironment().error(entity.getTrace(), Messages.ERR_INTERFACE_FUNCTION_CANNOT_CONTAIN_BODY.fetch(entity.getSignatureString(false)));
        }
        if (statement.isAbstract() || statement.isFinal()) {
            compiler.getEnvironment().error(entity.getTrace(), Messages.ERR_ACCESS_TYPE_FOR_INTERFACE_METHOD.fetch(entity.getSignatureString(false)));
        }
    } else {
        writeHeader();
        if (statement.isGenerator()) {
            entity.setEmpty(false);
            entity.setImmutable(false);
            entity.setResult(null);
            GeneratorStmtCompiler generatorStmtCompiler = new GeneratorStmtCompiler(compiler, statement);
            entity.setGeneratorEntity(generatorStmtCompiler.compile());
            ExpressionStmtCompiler expr = new ExpressionStmtCompiler(this, null);
            expr.makeUnknown(new TypeInsnNode(NEW, entity.getGeneratorEntity().getInternalName()));
            expr.stackPush(Memory.Type.REFERENCE);
            expr.writePushDup();
            // env
            expr.writePushEnv();
            // classEntity
            expr.writePushDup();
            expr.writePushConstString(compiler.getModule().getInternalName());
            expr.writePushConstInt((int) entity.getGeneratorEntity().getId());
            expr.writeSysDynamicCall(Environment.class, "__getGenerator", ClassEntity.class, String.class, Integer.TYPE);
            // self
            expr.writePushThis();
            // uses
            expr.writeVarLoad("~args");
            expr.writeSysCall(entity.getGeneratorEntity().getInternalName(), INVOKESPECIAL, Constants.INIT_METHOD, void.class, Environment.class, ClassEntity.class, Memory.class, Memory[].class);
            expr.writeSysStaticCall(ObjectMemory.class, "valueOf", Memory.class, IObject.class);
            expr.makeUnknown(new InsnNode(Opcodes.ARETURN));
            expr.stackPop();
        } else {
            ExpressionStmtCompiler expr = new ExpressionStmtCompiler(this, null);
            entity.setEmpty(true);
            if (entity.getResult() == null) {
                entity.setResult(Memory.UNDEFINED);
            }
            if (statement != null && statement.getBody() != null) {
                expr.writeDefineVariables(statement.getLocal());
                expr.write(statement.getBody());
                if (!statement.getBody().getInstructions().isEmpty()) {
                    entity.setEmpty(false);
                    if (entity.getResult() != null && entity.getResult().isUndefined()) {
                        entity.setResult(null);
                    }
                }
            }
            if (generatorEntity != null) {
                expr.writeVarLoad(LocalVariable.THIS);
                expr.writePushConstBoolean(false);
                expr.writeSysDynamicCall(null, "_setValid", void.class, Boolean.TYPE);
            }
            ReturnStmtToken token = new ReturnStmtToken(new TokenMeta("", 0, 0, 0, 0));
            token.setValue(null);
            token.setEmpty(true);
            expr.getCompiler(ReturnStmtToken.class).write(token);
        }
        writeFooter();
    }
    return entity;
}
Also used : TokenMeta(org.develnext.jphp.core.tokenizer.TokenMeta) StaticAccessExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.StaticAccessExprToken) ArrayMemory(php.runtime.memory.ArrayMemory) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory) Memory(php.runtime.Memory) ConstantMemory(php.runtime.memory.helper.ConstantMemory) ObjectMemory(php.runtime.memory.ObjectMemory) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) DocumentComment(php.runtime.reflection.DocumentComment) ParameterEntity(php.runtime.reflection.ParameterEntity) ClassConstantMemory(php.runtime.memory.helper.ClassConstantMemory) ConstantMemory(php.runtime.memory.helper.ConstantMemory)

Example 5 with TokenMeta

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

the class TokenMetaTest method testSimple.

@Test
public void testSimple() throws Exception {
    TokenMeta meta = new TokenMeta("foobar", 1, 2, 3, 4);
    Assert.assertEquals("foobar", meta.getWord());
    Assert.assertEquals(1, meta.getStartLine());
    Assert.assertEquals(2, meta.getEndLine());
    Assert.assertEquals(3, meta.getStartPosition());
    Assert.assertEquals(4, meta.getEndPosition());
}
Also used : TokenMeta(org.develnext.jphp.core.tokenizer.TokenMeta) Test(org.junit.Test)

Aggregations

TokenMeta (org.develnext.jphp.core.tokenizer.TokenMeta)5 Test (org.junit.Test)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SyntaxAnalyzer (org.develnext.jphp.core.syntax.SyntaxAnalyzer)1 TokenFinder (org.develnext.jphp.core.tokenizer.TokenFinder)1 Tokenizer (org.develnext.jphp.core.tokenizer.Tokenizer)1 BreakToken (org.develnext.jphp.core.tokenizer.token.BreakToken)1 ColonToken (org.develnext.jphp.core.tokenizer.token.ColonToken)1 SemicolonToken (org.develnext.jphp.core.tokenizer.token.SemicolonToken)1 Token (org.develnext.jphp.core.tokenizer.token.Token)1 CastExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.cast.CastExprToken)1 UnsetCastExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.cast.UnsetCastExprToken)1 NameToken (org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)1 StaticAccessExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.StaticAccessExprToken)1 StringExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.StringExprToken)1 MacroToken (org.develnext.jphp.core.tokenizer.token.expr.value.macro.MacroToken)1 Memory (php.runtime.Memory)1 TraceInfo (php.runtime.env.TraceInfo)1 ParseException (php.runtime.exceptions.ParseException)1 ArrayMemory (php.runtime.memory.ArrayMemory)1