Example #1
0
 /**
  * "\" で始まる文字列を対応する文字に変換します.
  * 
  * @param  Context $context
  * @return string
  */
 private function decodeEscapedChar(Context $context)
 {
     // @codeCoverageIgnoreStart
     static $specials = null;
     if ($specials === null) {
         $specials = array("\\" => "\\", '"' => '"', "/" => "/", "b" => chr(0x8), "f" => chr(0xc), "n" => "\n", "r" => "\r", "t" => "\t");
     }
     // @codeCoverageIgnoreEnd
     $current = $context->current();
     if (array_key_exists($current, $specials)) {
         $context->next();
         return $specials[$current];
     }
     // decode \uXXXX
     if ($current !== "u") {
         $context->throwException("Invalid escape sequence ('\\{$current}')");
     }
     $context->next();
     $hex = $context->getSequence(4);
     if (!preg_match("/^[0-9A-Fa-f]{4}\$/", $hex)) {
         $context->throwException("Invalid hexadecimal sequence (Expected: \\uXXXX)");
     }
     $context->skip(4);
     return $context->encodeCodepoint(hexdec($hex));
 }
Example #2
0
 /**
  * リテラル null, true, false をデコードします.
  * 
  * @param Context $context
  * @param string  $literal
  * @param mixed   $value
  */
 private function decodeLiteral(Context $context, $literal, $value)
 {
     $count = strlen($literal);
     if ($context->getSequence($count) !== $literal) {
         $current = $context->current();
         $context->throwException("Unexpected character found ('{$current}')");
     }
     $this->result = $value;
     $context->skip($count);
 }
Example #3
0
 /**
  * 
  * @param Context $context
  * @throws DecodeException 期待されている文字以外の文字を検知した場合
  */
 private function handleChar(Context $context)
 {
     $chr = $context->current();
     $expected = $this->expected;
     if (in_array($chr, $expected, true)) {
         $this->result = $chr;
         $context->next();
         return;
     }
     $quote = function ($chr) {
         return "'{$chr}'";
     };
     $expectedList = implode(", ", array_map($quote, $expected));
     $context->throwException("'{$chr}' is not allowed (expected: {$expectedList})");
 }