Exemplo n.º 1
0
 /**
  * Test if the `parse` method throws an exception if the closing parentheses is missing
  * and the end of the stream is reached.
  *
  * @expectedException \com\mohiva\common\exceptions\SyntaxErrorException
  */
 public function testParseThrowsExceptionIfEndOfStreamIsReached()
 {
     $tokenStream = new TokenStream();
     $tokenStream->push(new Token(Lexer::T_OPEN_PARENTHESIS, '(', 1));
     $tokenStream->push(new Token(Lexer::T_NUMBER, 100, 1));
     $tokenStream->rewind();
     $grammar = new Grammar();
     $grammar->addOperand(new NumberOperand());
     $operand = new ParenthesesOperand();
     $operand->parse($grammar, $tokenStream);
 }
Exemplo n.º 2
0
 /**
  * Test if the parse method returns the `OperandNode` with the correct number.
  */
 public function testParseReturnsNodeWithCorrectNumber()
 {
     $number = mt_rand(1, 100);
     $tokenStream = new TokenStream();
     $tokenStream->push(new Token(Lexer::T_NUMBER, $number, 1));
     $tokenStream->rewind();
     $operand = new NumberOperand();
     $node = $operand->parse(new Grammar(), $tokenStream);
     $this->assertSame($number, $node->evaluate());
 }
Exemplo n.º 3
0
 /**
  * Transform the input string into a token stream.
  *
  * @param string $input The string input to tokenize.
  * @return TokenStream The resulting token stream.
  */
 private function tokenize($input)
 {
     $stream = new TokenStream();
     $stream->setSource($input);
     $pattern = '/' . implode('|', $this->lexemes) . '/';
     $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
     $matches = preg_split($pattern, $input, -1, $flags);
     foreach ($matches as $match) {
         $value = strtolower($match[0]);
         if ($value[0] == "'" || $value[0] == '"' || $value == 'true' || $value == 'false' || $value == 'null' || is_numeric($value)) {
             $code = self::T_VALUE;
         } else {
             if (isset($this->constMap[$value])) {
                 $code = $this->constMap[$value];
             } else {
                 if (preg_match('/[a-z0-9_]+/', $value)) {
                     $code = self::T_NAME;
                 } else {
                     if (ctype_space($value)) {
                         continue;
                     } else {
                         $code = self::T_NONE;
                     }
                 }
             }
         }
         $stream->push(new AnnotationToken($code, $match[0], $match[1]));
     }
     return $stream;
 }
Exemplo n.º 4
0
 /**
  * Data provider which returns a TokenStream instance.
  *
  * @return array An array containing a TokenStream instance.
  */
 public function tokenStreamProvider()
 {
     // user.name.split(" ").join("-")
     $stream = new TokenStream();
     $stream->push(new TestToken(self::T_NAME));
     $stream->push(new TestToken(self::T_POINT));
     $stream->push(new TestToken(self::T_NAME));
     $stream->push(new TestToken(self::T_POINT));
     $stream->push(new TestToken(self::T_NAME));
     $stream->push(new TestToken(self::T_OPEN_PARENTHESIS));
     $stream->push(new TestToken(self::T_VALUE));
     $stream->push(new TestToken(self::T_CLOSE_PARENTHESIS));
     $stream->push(new TestToken(self::T_POINT));
     $stream->push(new TestToken(self::T_NAME));
     $stream->push(new TestToken(self::T_OPEN_PARENTHESIS));
     $stream->push(new TestToken(self::T_VALUE));
     $stream->push(new TestToken(self::T_CLOSE_PARENTHESIS));
     $stream->rewind();
     return array(array($stream));
 }
Exemplo n.º 5
0
 /**
  * Transform the input string into a token stream.
  *
  * @param string $input The string input to tokenize.
  * @return TokenStream The resulting token stream.
  */
 private function tokenize($input)
 {
     $stream = new TokenStream();
     $stream->setSource($input);
     $pattern = '/' . implode('|', $this->lexemes) . '/';
     $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
     $matches = preg_split($pattern, $input, -1, $flags);
     foreach ($matches as $match) {
         $value = strtolower($match[0]);
         if (is_numeric($value)) {
             $code = self::T_NUMBER;
         } else {
             if (isset($this->constTokenMap[$value])) {
                 $code = $this->constTokenMap[$value];
             } else {
                 if (ctype_space($value)) {
                     continue;
                 } else {
                     $code = self::T_NONE;
                 }
             }
         }
         $stream->push(new Token($code, $match[0], $match[1]));
     }
     return $stream;
 }