Search in sources :

Example 16 with Tokenizer

use of org.develnext.jphp.core.tokenizer.Tokenizer 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 17 with Tokenizer

use of org.develnext.jphp.core.tokenizer.Tokenizer 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 18 with Tokenizer

use of org.develnext.jphp.core.tokenizer.Tokenizer 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 19 with Tokenizer

use of org.develnext.jphp.core.tokenizer.Tokenizer 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 20 with Tokenizer

use of org.develnext.jphp.core.tokenizer.Tokenizer 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

Tokenizer (org.develnext.jphp.core.tokenizer.Tokenizer)30 Context (php.runtime.env.Context)27 Test (org.junit.Test)23 Token (org.develnext.jphp.core.tokenizer.token.Token)11 SemicolonToken (org.develnext.jphp.core.tokenizer.token.SemicolonToken)9 CommentToken (org.develnext.jphp.core.tokenizer.token.CommentToken)8 SyntaxAnalyzer (org.develnext.jphp.core.syntax.SyntaxAnalyzer)6 IOException (java.io.IOException)4 ExprStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken)4 FunctionStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.FunctionStmtToken)3 ParseException (php.runtime.exceptions.ParseException)3 TraceInfo (php.runtime.env.TraceInfo)2 File (java.io.File)1 BigInteger (java.math.BigInteger)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 MinusExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.MinusExprToken)1 ValueIfElseToken (org.develnext.jphp.core.tokenizer.token.expr.operator.ValueIfElseToken)1