Search in sources :

Example 6 with Tokenizer

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

the class TokenizerTest method testComments.

@Test
public void testComments() throws IOException {
    Tokenizer tokenizer = new Tokenizer(new Context("/** FOO BAR \n\r100500 */"));
    Token token = tokenizer.nextToken();
    assertTrue(token instanceof CommentToken);
    assertEquals(CommentToken.Kind.DOCTYPE, ((CommentToken) token).getKind());
    assertEquals("FOO BAR\n\n100500", ((CommentToken) token).getComment());
    // simple
    tokenizer = new Tokenizer(new Context("// foobar \n $x"));
    token = tokenizer.nextToken();
    assertTrue(token instanceof CommentToken);
    assertEquals(CommentToken.Kind.SIMPLE, ((CommentToken) token).getKind());
    assertEquals(" foobar ", ((CommentToken) token).getComment());
    assertTrue(tokenizer.nextToken() instanceof VariableExprToken);
    assertNull(tokenizer.nextToken());
    tokenizer = new Tokenizer(new Context("# // foobar \n $x"));
    token = tokenizer.nextToken();
    assertTrue(token instanceof CommentToken);
    assertEquals(CommentToken.Kind.SIMPLE, ((CommentToken) token).getKind());
    assertEquals(" // foobar ", ((CommentToken) token).getComment());
    assertTrue(tokenizer.nextToken() instanceof VariableExprToken);
    assertNull(tokenizer.nextToken());
    // block
    tokenizer = new Tokenizer(new Context("/* foobar \n */"));
    token = tokenizer.nextToken();
    assertTrue(token instanceof CommentToken);
    assertEquals(CommentToken.Kind.BLOCK, ((CommentToken) token).getKind());
}
Also used : Context(php.runtime.env.Context) CommentToken(org.develnext.jphp.core.tokenizer.token.CommentToken) 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)

Example 7 with Tokenizer

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

the class TokenizerTest method testComplexOperators.

@Test
public void testComplexOperators() throws IOException {
    Tokenizer tokenizer = new Tokenizer(new Context("== >= <= === !== != && ||"));
    assertTrue(tokenizer.nextToken() instanceof EqualExprToken);
    assertTrue(tokenizer.nextToken() instanceof GreaterOrEqualExprToken);
    assertTrue(tokenizer.nextToken() instanceof SmallerOrEqualToken);
    assertTrue(tokenizer.nextToken() instanceof IdenticalExprToken);
    assertTrue(tokenizer.nextToken() instanceof NotIdenticalExprToken);
    assertTrue(tokenizer.nextToken() instanceof BooleanNotEqualExprToken);
    assertTrue(tokenizer.nextToken() instanceof BooleanAndExprToken);
    assertTrue(tokenizer.nextToken() instanceof BooleanOrExprToken);
}
Also used : Context(php.runtime.env.Context) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer) Test(org.junit.Test)

Example 8 with Tokenizer

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

the class TokenizerTest method testParseError.

@Test
public void testParseError() throws IOException {
    Throwable ex = null;
    Tokenizer tokenizer = new Tokenizer(new Context("  'foobar \n "));
    try {
        tokenizer.nextToken();
    } catch (Throwable e) {
        ex = e;
    }
    assertTrue(ex instanceof ParseException);
    TraceInfo traceInfo = ((ParseException) ex).getTraceInfo();
    assertNotNull(traceInfo);
    assertNull(traceInfo.getContext().getFileName());
    assertEquals(1, traceInfo.getStartLine());
    assertEquals(1, traceInfo.getEndLine());
    assertEquals(1, traceInfo.getStartPosition());
    assertEquals(1, traceInfo.getEndPosition());
}
Also used : Context(php.runtime.env.Context) ParseException(php.runtime.exceptions.ParseException) TraceInfo(php.runtime.env.TraceInfo) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer) Test(org.junit.Test)

Example 9 with Tokenizer

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

the class TokenizerTest method testSplitNot.

@Test
public void testSplitNot() throws IOException {
    Tokenizer tokenizer = new Tokenizer(new Context("!true"));
    assertTrue(tokenizer.nextToken() instanceof BooleanNotExprToken);
    assertTrue(tokenizer.nextToken() instanceof BooleanExprToken);
}
Also used : Context(php.runtime.env.Context) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer) Test(org.junit.Test)

Example 10 with Tokenizer

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

the class TokenizerTest method testComplex.

@Test
public void testComplex() throws IOException {
    Tokenizer tokenizer = new Tokenizer(new Context("0==10==='30';"));
    assertTrue(tokenizer.nextToken() instanceof IntegerExprToken);
    assertTrue(tokenizer.nextToken() instanceof EqualExprToken);
    assertTrue(tokenizer.nextToken() instanceof IntegerExprToken);
    assertTrue(tokenizer.nextToken() instanceof IdenticalExprToken);
    assertTrue(tokenizer.nextToken() instanceof StringExprToken);
    assertTrue(tokenizer.nextToken() instanceof SemicolonToken);
    tokenizer = new Tokenizer(new Context("F =; 20;"));
    Token token;
    assertTrue((token = tokenizer.nextToken()) instanceof NameToken);
    assertEquals("F", ((NameToken) token).getName());
    assertTrue(tokenizer.nextToken() instanceof AssignExprToken);
    assertTrue(tokenizer.nextToken() instanceof SemicolonToken);
    assertTrue(tokenizer.nextToken() instanceof IntegerExprToken);
    assertTrue(tokenizer.nextToken() instanceof SemicolonToken);
    tokenizer = new Tokenizer(new Context("123foobar MAX_64Bit"));
    token = tokenizer.nextToken();
    assertTrue(token instanceof IntegerExprToken);
    token = tokenizer.nextToken();
    assertTrue(token instanceof NameToken);
    assertEquals("foobar", token.getWord());
    assertTrue(tokenizer.nextToken() instanceof NameToken);
    assertNull(tokenizer.nextToken());
    assertNull(tokenizer.nextToken());
}
Also used : Context(php.runtime.env.Context) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) 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

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