Search in sources :

Example 16 with Context

use of php.runtime.env.Context in project jphp by jphp-compiler.

the class SimpleExprTest method testSimpleCall.

@Test
public void testSimpleCall() throws IOException {
    Tokenizer tokenizer = new Tokenizer(new Context("myCall(1 * 2, func(3, 2), 4);"));
    SyntaxAnalyzer analyzer = new SyntaxAnalyzer(environment, tokenizer);
    List<Token> tokens = analyzer.getTree();
    Assert.assertTrue(tokens.size() == 1);
    Assert.assertTrue(tokens.get(0) instanceof ExprStmtToken);
    ExprStmtToken expr = (ExprStmtToken) tokens.get(0);
    tokens = expr.getTokens();
    Assert.assertTrue(tokens.size() == 1);
    Assert.assertTrue(tokens.get(0) instanceof CallExprToken);
    CallExprToken call = (CallExprToken) expr.getTokens().get(0);
    Assert.assertTrue(call.getName() instanceof NameToken);
    Assert.assertEquals("myCall", ((NameToken) call.getName()).getName());
    Assert.assertTrue(call.getParameters().size() == 3);
    Assert.assertTrue(call.getParameters().get(0).getTokens().size() == 3);
    Assert.assertTrue(call.getParameters().get(2).getTokens().size() == 1);
    ExprStmtToken param = call.getParameters().get(1);
    Assert.assertTrue(param.getTokens().size() == 1);
    Assert.assertTrue(param.getTokens().get(0) instanceof CallExprToken);
    CallExprToken subCall = (CallExprToken) param.getTokens().get(0);
    Assert.assertTrue(subCall.getParameters().size() == 2);
}
Also used : Context(php.runtime.env.Context) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) Token(org.develnext.jphp.core.tokenizer.token.Token) ValueIfElseToken(org.develnext.jphp.core.tokenizer.token.expr.operator.ValueIfElseToken) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) MinusExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.MinusExprToken) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer) Test(org.junit.Test)

Example 17 with Context

use of php.runtime.env.Context in project jphp by jphp-compiler.

the class SyntaxAnalyzerTest method testSimple.

@Test
public void testSimple() throws IOException {
    Tokenizer tokenizer = new Tokenizer(new Context("foobar;"));
    SyntaxAnalyzer analyzer = new SyntaxAnalyzer(environment, tokenizer);
    Assert.assertTrue(analyzer.getTree().size() == 1);
    Assert.assertTrue(analyzer.getTree().listIterator().next() instanceof ExprStmtToken);
}
Also used : Context(php.runtime.env.Context) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer) Test(org.junit.Test)

Example 18 with Context

use of php.runtime.env.Context 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 19 with Context

use of php.runtime.env.Context in project jphp by jphp-compiler.

the class CLI method executeFile.

protected void executeFile(String filename) throws Throwable {
    Launcher launcher = new Launcher("jphp.conf", args);
    launcher.run(false);
    File file = new File(filename);
    Environment environment = new Environment(launcher.getCompileScope(), output);
    environment.getDefaultBuffer().setImplicitFlush(true);
    try {
        Context context = new Context(file);
        ModuleEntity module = environment.importModule(context);
        module.include(environment);
    } 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) Launcher(php.runtime.launcher.Launcher) Environment(php.runtime.env.Environment) ModuleEntity(php.runtime.reflection.ModuleEntity) File(java.io.File)

Example 20 with Context

use of php.runtime.env.Context in project jphp by jphp-compiler.

the class TokenizerTest method testStringSlashes.

@Test
public void testStringSlashes() throws IOException {
    Token token;
    Tokenizer tokenizer = new Tokenizer(new Context(" 'foo\\'bar' \"foo\\\"bar\""));
    token = tokenizer.nextToken();
    assertTrue(token instanceof StringExprToken);
    assertEquals("foo'bar", ((StringExprToken) token).getValue());
    token = tokenizer.nextToken();
    assertTrue(token instanceof StringExprToken);
    assertEquals("foo\"bar", ((StringExprToken) token).getValue());
}
Also used : Context(php.runtime.env.Context) Token(org.develnext.jphp.core.tokenizer.token.Token) CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer) Test(org.junit.Test)

Aggregations

Context (php.runtime.env.Context)30 Tokenizer (org.develnext.jphp.core.tokenizer.Tokenizer)27 Test (org.junit.Test)23 Token (org.develnext.jphp.core.tokenizer.token.Token)10 CommentToken (org.develnext.jphp.core.tokenizer.token.CommentToken)8 SemicolonToken (org.develnext.jphp.core.tokenizer.token.SemicolonToken)8 ExprStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken)4 File (java.io.File)3 IOException (java.io.IOException)3 SyntaxAnalyzer (org.develnext.jphp.core.syntax.SyntaxAnalyzer)3 FunctionStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.FunctionStmtToken)3 ModuleEntity (php.runtime.reflection.ModuleEntity)3 Environment (php.runtime.env.Environment)2 ParseException (php.runtime.exceptions.ParseException)2 Launcher (php.runtime.launcher.Launcher)2 DataInputStream (java.io.DataInputStream)1 InputStream (java.io.InputStream)1 BigInteger (java.math.BigInteger)1 JvmCompiler (org.develnext.jphp.core.compiler.jvm.JvmCompiler)1 MinusExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.MinusExprToken)1