Search in sources :

Example 1 with SyntaxAnalyzer

use of org.develnext.jphp.core.syntax.SyntaxAnalyzer in project jphp by jphp-compiler.

the class EvalFunctions method eval.

public static Memory eval(Environment env, TraceInfo trace, @Runtime.GetLocals ArrayMemory locals, String code) throws Throwable {
    Context context = new Context(code);
    try {
        Tokenizer tokenizer = new Tokenizer(context);
        SyntaxAnalyzer analyzer = syntaxAnalyzer.get();
        analyzer.reset(env, tokenizer);
        AbstractCompiler compiler = new JvmCompiler(env, context, analyzer);
        ModuleEntity module = compiler.compile();
        env.scope.loadModule(module);
        env.registerModule(module);
        return module.include(env, locals);
    } catch (ErrorException e) {
        if (e.getType() == ErrorType.E_PARSE) {
            if (env.isHandleErrors(ErrorType.E_PARSE))
                throw new ParseException(evalErrorMessage(e), trace);
        } else
            env.error(trace, e.getType(), evalErrorMessage(e));
    }
    return Memory.FALSE;
}
Also used : Context(php.runtime.env.Context) AbstractCompiler(php.runtime.common.AbstractCompiler) SyntaxAnalyzer(org.develnext.jphp.core.syntax.SyntaxAnalyzer) JvmCompiler(org.develnext.jphp.core.compiler.jvm.JvmCompiler) ModuleEntity(php.runtime.reflection.ModuleEntity) ParseException(php.runtime.exceptions.ParseException) ErrorException(php.runtime.exceptions.support.ErrorException) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer)

Example 2 with SyntaxAnalyzer

use of org.develnext.jphp.core.syntax.SyntaxAnalyzer in project jphp by jphp-compiler.

the class ASMExpressionTest method getASMExpression.

private ASMExpression getASMExpression(String expr) {
    environment.scope.setLangMode(LangMode.DEFAULT);
    Tokenizer tokenizer = null;
    try {
        tokenizer = new Tokenizer(context = new Context(expr + ";"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    SyntaxAnalyzer analyzer = new SyntaxAnalyzer(environment, tokenizer);
    Assert.assertTrue(analyzer.getTree().size() == 1);
    Assert.assertTrue(analyzer.getTree().get(0) instanceof ExprStmtToken);
    return new ASMExpression(null, context, (ExprStmtToken) analyzer.getTree().get(0));
}
Also used : Context(php.runtime.env.Context) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) SyntaxAnalyzer(org.develnext.jphp.core.syntax.SyntaxAnalyzer) IOException(java.io.IOException) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer)

Example 3 with SyntaxAnalyzer

use of org.develnext.jphp.core.syntax.SyntaxAnalyzer in project jphp by jphp-compiler.

the class JvmCompilerCase method getSyntax.

protected SyntaxAnalyzer getSyntax(Context context) {
    Tokenizer tokenizer = null;
    try {
        tokenizer = new Tokenizer(context);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    environment.scope.setLangMode(LangMode.DEFAULT);
    return new SyntaxAnalyzer(environment, tokenizer);
}
Also used : SyntaxAnalyzer(org.develnext.jphp.core.syntax.SyntaxAnalyzer) IOException(java.io.IOException) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer)

Example 4 with SyntaxAnalyzer

use of org.develnext.jphp.core.syntax.SyntaxAnalyzer in project jphp by jphp-compiler.

the class CLI method checkSyntax.

protected void checkSyntax(String filename) throws Throwable {
    Launcher launcher = new Launcher("jphp.conf", args);
    launcher.run(false, true);
    File file = new File(filename);
    Environment environment = new Environment(launcher.getCompileScope(), output);
    Context context = new Context(file);
    try {
        SyntaxAnalyzer analyzer = new SyntaxAnalyzer(environment, new Tokenizer(context));
        analyzer.getTree();
        output.println(String.format("No syntax errors detected in %s", filename));
    } catch (Exception e) {
        environment.catchUncaught(e);
    } catch (Throwable throwable) {
        throw new RuntimeException(throwable);
    } finally {
        try {
            environment.doFinal();
        } catch (Throwable throwable) {
            throw new RuntimeException(throwable);
        }
    }
}
Also used : Context(php.runtime.env.Context) SyntaxAnalyzer(org.develnext.jphp.core.syntax.SyntaxAnalyzer) Launcher(php.runtime.launcher.Launcher) Environment(php.runtime.env.Environment) File(java.io.File) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer)

Example 5 with SyntaxAnalyzer

use of org.develnext.jphp.core.syntax.SyntaxAnalyzer 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)

Aggregations

SyntaxAnalyzer (org.develnext.jphp.core.syntax.SyntaxAnalyzer)6 Tokenizer (org.develnext.jphp.core.tokenizer.Tokenizer)6 IOException (java.io.IOException)3 Context (php.runtime.env.Context)3 ParseException (php.runtime.exceptions.ParseException)2 File (java.io.File)1 JvmCompiler (org.develnext.jphp.core.compiler.jvm.JvmCompiler)1 TokenMeta (org.develnext.jphp.core.tokenizer.TokenMeta)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 MacroToken (org.develnext.jphp.core.tokenizer.token.expr.value.macro.MacroToken)1 ExprStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken)1 AbstractCompiler (php.runtime.common.AbstractCompiler)1 Environment (php.runtime.env.Environment)1 TraceInfo (php.runtime.env.TraceInfo)1 ErrorException (php.runtime.exceptions.support.ErrorException)1