2013-01-25 18:36:16 +04:00
|
|
|
<?php
|
2013-06-28 11:53:53 +04:00
|
|
|
namespace Fenom;
|
2014-02-27 16:30:44 +04:00
|
|
|
|
2013-09-15 16:05:18 +04:00
|
|
|
use Fenom\Error\UnexpectedTokenException;
|
2013-06-28 11:53:53 +04:00
|
|
|
use Fenom\Tokenizer;
|
2013-01-25 18:36:16 +04:00
|
|
|
|
2017-04-22 15:59:20 +03:00
|
|
|
class TokenizerTest extends TestCase
|
2013-07-29 14:58:14 +04:00
|
|
|
{
|
2013-01-25 18:36:16 +04:00
|
|
|
|
2014-02-27 16:30:44 +04:00
|
|
|
public function testGetName()
|
|
|
|
{
|
|
|
|
$this->assertSame('T_DOUBLE_COLON', Tokenizer::getName(T_DOUBLE_COLON));
|
|
|
|
$this->assertSame('++', Tokenizer::getName('++'));
|
2014-02-28 18:32:59 +04:00
|
|
|
$this->assertSame('T_STRING', Tokenizer::getName(array(T_STRING, 'all', "", 1, "T_STRING")));
|
2014-02-27 16:30:44 +04:00
|
|
|
$this->assertNull(Tokenizer::getName(false));
|
|
|
|
}
|
|
|
|
|
2013-07-29 14:58:14 +04:00
|
|
|
public function testTokens()
|
|
|
|
{
|
2014-05-06 14:22:58 +04:00
|
|
|
$code = 'hello, please resolve this example: sin($x)+tan($x*$t) = {U|[0,1]}';
|
2013-07-29 14:58:14 +04:00
|
|
|
$tokens = new Tokenizer($code);
|
2013-09-19 23:10:19 +04:00
|
|
|
$this->assertSame(27, $tokens->count());
|
2013-09-13 21:19:51 +04:00
|
|
|
$this->assertSame($tokens, $tokens->back());
|
2013-07-29 14:58:14 +04:00
|
|
|
$this->assertSame(T_STRING, $tokens->key());
|
|
|
|
$this->assertSame("hello", $tokens->current());
|
2013-09-13 21:19:51 +04:00
|
|
|
$this->assertSame(1, $tokens->getLine());
|
|
|
|
|
2013-01-25 18:36:16 +04:00
|
|
|
|
2013-07-29 14:58:14 +04:00
|
|
|
$this->assertTrue($tokens->isNext(","));
|
2013-09-13 21:19:51 +04:00
|
|
|
|
2013-07-29 14:58:14 +04:00
|
|
|
$this->assertFalse($tokens->isNext("="));
|
|
|
|
$this->assertFalse($tokens->isNext(T_STRING));
|
|
|
|
$this->assertFalse($tokens->isNext($tokens::MACRO_UNARY));
|
2013-01-25 18:36:16 +04:00
|
|
|
|
2013-07-29 14:58:14 +04:00
|
|
|
$this->assertFalse($tokens->isNext("=", T_STRING, $tokens::MACRO_UNARY));
|
|
|
|
$this->assertTrue($tokens->isNext("=", T_STRING, $tokens::MACRO_UNARY, ","));
|
2013-01-25 18:36:16 +04:00
|
|
|
|
2013-07-29 14:58:14 +04:00
|
|
|
$this->assertSame(",", $tokens->getNext());
|
|
|
|
$this->assertSame(",", $tokens->key());
|
|
|
|
$this->assertSame("please", $tokens->getNext(T_STRING));
|
2014-05-06 14:22:58 +04:00
|
|
|
$this->assertSame(
|
|
|
|
array(
|
|
|
|
T_STRING,
|
|
|
|
'please',
|
|
|
|
' ',
|
2015-02-12 12:13:35 +03:00
|
|
|
1
|
2014-05-06 14:22:58 +04:00
|
|
|
),
|
2023-02-05 23:59:04 +03:00
|
|
|
$tokens->currToken()
|
2014-05-06 14:22:58 +04:00
|
|
|
);
|
2013-07-29 14:58:14 +04:00
|
|
|
$this->assertSame("resolve", $tokens->getNext($tokens::MACRO_UNARY, T_STRING));
|
2013-01-25 18:36:16 +04:00
|
|
|
|
2013-07-29 14:58:14 +04:00
|
|
|
$tokens->next();
|
|
|
|
$tokens->next();
|
|
|
|
$tokens->next();
|
2013-01-25 18:36:16 +04:00
|
|
|
|
2013-07-29 14:58:14 +04:00
|
|
|
$this->assertSame(":", $tokens->current());
|
|
|
|
$this->assertSame(":", $tokens->key());
|
2013-01-25 18:36:16 +04:00
|
|
|
|
|
|
|
|
2013-07-29 14:58:14 +04:00
|
|
|
$this->assertSame("sin", $tokens->getNext($tokens::MACRO_STRING));
|
|
|
|
$this->assertSame("sin", $tokens->current());
|
2013-09-15 16:05:18 +04:00
|
|
|
$this->assertTrue($tokens->isPrev(":"));
|
2013-07-29 14:58:14 +04:00
|
|
|
$this->assertSame(T_STRING, $tokens->key());
|
|
|
|
$this->assertTrue($tokens->is(T_STRING));
|
|
|
|
$this->assertTrue($tokens->is($tokens::MACRO_STRING));
|
|
|
|
$this->assertFalse($tokens->is($tokens::MACRO_EQUALS));
|
|
|
|
$this->assertFalse($tokens->is(T_DNUMBER));
|
|
|
|
$this->assertFalse($tokens->is(":"));
|
|
|
|
$this->assertSame("(", $tokens->getNext("(", ")"));
|
2013-09-19 23:10:19 +04:00
|
|
|
$this->assertTrue($tokens->hasBackList(T_STRING, ':'));
|
|
|
|
$this->assertFalse($tokens->hasBackList(T_LNUMBER, ':'));
|
2013-01-25 18:36:16 +04:00
|
|
|
|
2013-07-29 14:58:14 +04:00
|
|
|
$tokens->next();
|
|
|
|
$tokens->next();
|
|
|
|
$this->assertSame("+", $tokens->getNext($tokens::MACRO_BINARY));
|
2013-09-15 16:05:18 +04:00
|
|
|
|
|
|
|
$this->assertSame($code, $tokens->getSnippetAsString(-100, 100));
|
|
|
|
$this->assertSame('+', $tokens->getSnippetAsString(100, -100));
|
2013-09-15 16:39:29 +04:00
|
|
|
$this->assertSame('sin($x)+tan($x*$t)', $tokens->getSnippetAsString(-4, 6));
|
2014-02-27 16:30:44 +04:00
|
|
|
$this->assertSame('}', $tokens->end()->current());
|
2013-07-29 14:58:14 +04:00
|
|
|
}
|
2013-01-25 18:36:16 +04:00
|
|
|
|
2023-02-20 00:14:08 +03:00
|
|
|
public function testComplexTokens()
|
|
|
|
{
|
|
|
|
$text = "one\\two";
|
|
|
|
$tokens = new Tokenizer($text);
|
|
|
|
$this->assertSame("one", $tokens->current());
|
|
|
|
$this->assertSame("\\", $tokens->next()->current());
|
|
|
|
$this->assertSame("two", $tokens->next()->current());
|
|
|
|
$this->assertFalse($tokens->next()->valid());
|
|
|
|
|
|
|
|
$text = "\\one\\two";
|
|
|
|
|
|
|
|
$tokens = new Tokenizer($text);
|
|
|
|
$this->assertSame("\\", $tokens->current());
|
|
|
|
$this->assertSame("one", $tokens->next()->current());
|
|
|
|
$this->assertSame("\\", $tokens->next()->current());
|
|
|
|
$this->assertSame("two", $tokens->next()->current());
|
|
|
|
$this->assertFalse($tokens->next()->valid());
|
|
|
|
}
|
|
|
|
|
2013-07-29 14:58:14 +04:00
|
|
|
public function testSkip()
|
|
|
|
{
|
2014-05-06 14:22:58 +04:00
|
|
|
$text = "1 foo: bar ( 3 + double ) ";
|
2013-01-25 18:36:16 +04:00
|
|
|
$tokens = new Tokenizer($text);
|
|
|
|
|
|
|
|
$tokens->skip()->skip(T_STRING)->skip(':');
|
|
|
|
try {
|
|
|
|
$tokens->skip(T_STRING)->skip('(')->skip(':');
|
2013-07-29 14:58:14 +04:00
|
|
|
} catch (\Exception $e) {
|
2013-07-29 16:15:52 +04:00
|
|
|
$this->assertInstanceOf('Fenom\Error\UnexpectedTokenException', $e);
|
2013-02-07 17:37:16 +04:00
|
|
|
$this->assertStringStartsWith("Unexpected token '3' in expression, expect ':'", $e->getMessage());
|
2013-01-25 18:36:16 +04:00
|
|
|
}
|
|
|
|
$this->assertTrue($tokens->valid());
|
|
|
|
$this->assertSame("3", $tokens->current());
|
2013-09-13 21:19:51 +04:00
|
|
|
|
2013-01-25 18:36:16 +04:00
|
|
|
$this->assertSame(T_LNUMBER, $tokens->key());
|
2013-09-13 21:19:51 +04:00
|
|
|
$this->assertSame($tokens, $tokens->next());
|
2013-01-25 18:36:16 +04:00
|
|
|
$tokens->next();
|
2013-09-13 21:19:51 +04:00
|
|
|
$this->assertSame("double", $tokens->getAndNext());
|
|
|
|
$this->assertSame(")", $tokens->current());
|
|
|
|
$this->assertTrue($tokens->isLast());
|
|
|
|
$this->assertSame($tokens, $tokens->next());
|
2013-09-19 23:10:19 +04:00
|
|
|
$tokens->p = 1000;
|
|
|
|
$this->assertSame($tokens, $tokens->next());
|
|
|
|
$tokens->p = -1000;
|
|
|
|
$this->assertSame($tokens, $tokens->back());
|
2013-01-25 18:36:16 +04:00
|
|
|
}
|
2013-09-15 16:05:18 +04:00
|
|
|
|
2015-02-12 12:13:35 +03:00
|
|
|
public function testFixFloats() {
|
|
|
|
$text = "1..3";
|
|
|
|
$tokens = new Tokenizer($text);
|
|
|
|
$this->assertTrue($tokens->is(T_LNUMBER));
|
|
|
|
$this->assertTrue($tokens->next()->is('.'));
|
|
|
|
$this->assertTrue($tokens->next()->is('.'));
|
|
|
|
$this->assertTrue($tokens->next()->is(T_LNUMBER));
|
|
|
|
}
|
|
|
|
|
2013-01-25 18:36:16 +04:00
|
|
|
}
|