/** * Finalizes the processor. * * @return string */ public function finalize() { static $classes = array(T_VARIABLE => 'variable', T_CLASS => 'keyword', T_INTERFACE => 'keyword', T_EXTENDS => 'keyword', T_IMPLEMENTS => 'keyword', T_CATCH => 'keyword', T_THROW => 'keyword', T_TRY => 'keyword', T_NEW => 'keyword', T_FUNCTION => 'keyword', T_FOR => 'keyword', T_IF => 'keyword', T_ELSE => 'keyword', T_SWITCH => 'keyword', T_WHILE => 'keyword', T_FOREACH => 'keyword', T_RETURN => 'keyword', T_STATIC => 'modifier', T_ABSTRACT => 'modifier', T_PUBLIC => 'modifier', T_PRIVATE => 'modifier', T_PROTECTED => 'modifier', T_FINAL => 'modifier', T_DNUMBER => 'number', T_LNUMBER => 'number', T_CONSTANT_ENCAPSED_STRING => 'string', T_COMMENT => 'comment', '{' => 'bracket', '}' => 'bracket', '(' => 'bracket', ')' => 'bracket'); // Tokenize buffer $tokens = token_get_all('<?php ' . trim($this->buffer, "\r\n") . '?>'); if (!is_array($tokens) || xp::errorAt(__FILE__, __LINE__ - 1)) { $e = new FormatException('Cannot parse "' . $this->buffer . '"'); xp::gc(__FILE__); throw $e; } // Create HTML $current = NULL; $out = ''; for ($i = 1, $s = sizeof($tokens) - 1; $i < $s; $i++) { $token = $tokens[$i]; $class = isset($classes[$token[0]]) ? $classes[$token[0]] : 'default'; // Handle annotations if (is_array($token) && T_COMMENT === $token[0] && '#' === $token[1][0]) { $class = 'annotation'; } if ($current != $class) { $out .= '</span><span class="' . $class . '">'; $current = $class; } $out .= strtr(htmlspecialchars(is_array($token) ? $token[1] : $token), array("\n" => '<br/>', "\r" => '')); } // Skip leading "</span>" (7) return '</p><code>' . substr($out, 7) . ($current ? '</span>' : '') . '</code><p>'; }
/** * Constructor * * @param var arg either a string or an int * @param string charset default NULL */ public function __construct($arg, $charset = NULL) { if (is_int($arg)) { $this->buffer = iconv('UCS-4BE', 'utf-8', pack('N', $arg)); return; } $charset = strtoupper($charset ? $charset : iconv_get_encoding('input_encoding')); // Convert the input to internal encoding $this->buffer = iconv($charset, 'utf-8', $arg); if (xp::errorAt(__FILE__, __LINE__ - 1)) { $message = key(xp::$errors[__FILE__][__LINE__ - 2]); xp::gc(__FILE__); throw new FormatException($message . ($charset == 'utf-8' ? ' with charset ' . $charset : $message . ' while converting input from ' . $charset . ' to ' . 'utf-8')); } if (1 != ($l = iconv_strlen($this->buffer, 'utf-8'))) { throw new IllegalArgumentException('Given argument is too long (' . $l . ')'); } }
/** * Constructor. Creates a new date object through either a * <ul> * <li>integer - interpreted as timestamp</li> * <li>string - parsed into a date</li> * <li>php.DateTime object - will be used as is</li> * <li>NULL - creates a date representing the current instance</li> * </ul> * * Timezone assignment works through these rules: * . If the time is given as string and contains a parseable timezone identifier * that one is used. * . If no timezone could be determined, the timezone given by the * second parameter is used * . If no timezone has been given as second parameter, the system's default * timezone is used. * * @param var in default NULL either a string or a Unix timestamp or DateTime object, defaulting to now * @param string timezone default NULL string of timezone * @throws lang.IllegalArgumentException in case the date is unparseable */ public function __construct($in = NULL, TimeZone $timezone = NULL) { if ($in instanceof DateTime) { $this->date = $in; } else { if ((string) (int) $in === (string) $in) { // Specially mark timestamps for parsing (we assume here that strings // containing only digits are timestamps) $this->date = date_create('@' . $in, timezone_open('UTC')); date_timezone_set($this->date, $timezone ? $timezone->getHandle() : timezone_open(date_default_timezone_get())); } else { $this->date = $timezone ? date_create($in, $timezone->getHandle()) : date_create($in); if (FALSE === $this->date || xp::errorAt(__FILE__, __LINE__ - 1)) { $e = new IllegalArgumentException('Given argument is neither a timestamp nor a well-formed timestring: ' . xp::stringOf($in)); xp::gc(__FILE__); throw $e; } } } }
/** * Returns whether the file pointer is at the end of the file * * Hint: * Use isOpen() to check if the file is open * * @see php://feof * @return bool TRUE when the end of the file is reached * @throws io.IOException in case of an error (e.g., the file's not been opened) */ public function eof() { $result = feof($this->_fd); if (xp::errorAt(__FILE__, __LINE__ - 1)) { $e = new IOException('Cannot determine eof of ' . $this->uri); xp::gc(__FILE__); throw $e; } return $result; }
/** * Flip keys and values. Note that the values may only consists * of scalar values, else the operation will fail (and no key/value * pair in question will *not* be flipped. If a value has several * occurrences, the latest key will be used as its value * * Example: * <pre> * before after * ------ ----- * a => b b => b * b => b 1 => c * c => 1 * </pre> * * @return bool all keys/values have been flipped * @throws lang.FormatException in case this hash contains non-scalar values */ public function flip() { $h = array_flip($this->_hash); if (\xp::errorAt(__FILE__, __LINE__ - 1)) { $e = new \lang\FormatException('hash contains values which are not scalar'); \xp::gc(__FILE__); throw $e; } $this->_hash = $h; return true; }
/** * Retrieve file pointer position * * @throws io.IOException in case of an error * @return int position */ public function tell($position = 0, $mode = SEEK_SET) { $result = gztell($this->_fd); if (FALSE === $result && xp::errorAt(__FILE__, __LINE__ - 1)) { throw new IOException('retrieve file pointer\'s position ' . $this->uri); } return $result; }
public function triggered_error_at_file_and_line() { trigger_error('Test'); $this->assertEquals(array('Test' => array('class' => NULL, 'method' => 'trigger_error', 'cnt' => 1)), xp::errorAt(__FILE__, __LINE__ - 3)); xp::gc(); }
/** * Decode a name from a list of given character sets * * @param string name * @param string[] charsets * @return string */ protected function decodeName($name, $charsets) { xp::gc(__FILE__); foreach ($charsets as $charset) { $decoded = iconv($charset, xp::ENCODING, $name); if (!xp::errorAt(__FILE__, __LINE__ - 1)) { return $decoded; } xp::gc(__FILE__); // Clean up and try next charset } return $name; }
/** * Parser main method. Maintains a state and a value stack, * currently with fixed maximum size. * * @param text.parser.generic.AbstractLexer lexer . * @return mixed result of the last reduction, if any. */ public function yyparse($yyLex) { $yyVal = null; $yyStates = $yyVals = []; $yyToken = -1; $yyState = $yyErrorFlag = 0; while (1) { for ($yyTop = 0;; $yyTop++) { $yyStates[$yyTop] = $yyState; $yyVals[$yyTop] = $yyVal; for (;;) { if (($yyN = self::$yyDefRed[$yyState]) == 0) { // Check whether it's necessary to fetch the next token $yyToken < 0 && ($yyToken = $yyLex->advance() ? $yyLex->token : 0); if (($yyN = self::$yySindex[$yyState]) != 0 && ($yyN += $yyToken) >= 0 && $yyN < self::$yyTableCount && self::$yyCheck[$yyN] == $yyToken) { $yyState = self::$yyTable[$yyN]; // shift to yyN $yyVal = $yyLex->value; $yyToken = -1; $yyErrorFlag > 0 && $yyErrorFlag--; continue 2; } if (($yyN = self::$yyRindex[$yyState]) != 0 && ($yyN += $yyToken) >= 0 && $yyN < self::$yyTableCount && self::$yyCheck[$yyN] == $yyToken) { $yyN = self::$yyTable[$yyN]; // reduce (yyN) } else { switch ($yyErrorFlag) { case 0: return $this->error(E_PARSE, sprintf('Syntax error at %s, line %d (offset %d): Unexpected %s', $yyLex->fileName, $yyLex->position[0], $yyLex->position[1], $this->yyName($yyToken)), $this->yyExpecting($yyState)); case 1: case 2: $yyErrorFlag = 3; do { if (($yyN = @self::$yySindex[$yyStates[$yyTop]]) != 0 && ($yyN += TOKEN_YY_ERRORCODE) >= 0 && $yyN < self::$yyTableCount && self::$yyCheck[$yyN] == TOKEN_YY_ERRORCODE) { $yyState = self::$yyTable[$yyN]; $yyVal = $yyLex->value; break 3; } } while ($yyTop-- >= 0); throw new ParseException(sprintf('Irrecoverable syntax error at %s, line %d (offset %d)', $yyLex->fileName, $yyLex->position[0], $yyLex->position[1])); case 3: if (0 == $yyToken) { throw new ParseException(sprintf('Irrecoverable syntax error at end-of-file at %s, line %d (offset %d)', $yyLex->fileName, $yyLex->position[0], $yyLex->position[1])); } $yyToken = -1; break 1; } } } $yyV = $yyTop + 1 - self::$yyLen[$yyN]; $yyVal = $yyV > $yyTop ? null : $yyVals[$yyV]; // Actions switch ($yyN) { case 5: $yyVal = $yyVals[-1 + $yyTop]; break; case 6: $yyVal = []; break; case 8: $yyVal = $yyVals[-2 + $yyTop] + $yyVals[0 + $yyTop]; break; case 9: $yyVal = [$yyVals[-2 + $yyTop] => $yyVals[0 + $yyTop]]; break; case 10: $yyVal = iconv($this->targetEncoding, \xp::ENCODING, $yyVals[-1 + $yyTop]); break; case 11: $yyVal = ''; break; case 12: $yyVal = $yyVals[-1 + $yyTop]; break; case 13: $yyVal = []; break; case 14: $yyVal = [$yyVals[0 + $yyTop]]; break; case 15: $yyVal = array_merge($yyVals[-2 + $yyTop], [$yyVals[0 + $yyTop]]); break; case 16: if (\xp::errorAt(__FILE__)) { $e = new \lang\FormatException('Cannot decode string ' . (new \lang\types\Bytes($yyVals[-2 + $yyTop]))->toString() . ' to ' . $this->targetEncoding); \xp::gc(__FILE__); throw $e; } $yyVal = $yyVals[-1 + $yyTop]; break; case 17: $yyVal = ''; break; case 19: $yyVal = $yyVals[-1 + $yyTop] . $yyVals[0 + $yyTop]; break; case 20: $yyVal = iconv('utf-8', $this->targetEncoding, $yyVals[0 + $yyTop]); break; case 21: $yyVal = '"'; break; case 22: $yyVal = "\\"; break; case 23: $yyVal = "/"; break; case 24: $yyVal = "\\b"; break; case 25: $yyVal = "\f"; break; case 26: $yyVal = "\n"; break; case 27: $yyVal = "\r"; break; case 28: $yyVal = "\t"; break; case 29: $yyVal = iconv('ucs-4be', $this->targetEncoding, pack('N', hexdec(substr($yyVals[0 + $yyTop], 2)))); break; case 30: $yyVal = true; break; case 31: $yyVal = false; break; case 32: $yyVal = null; break; case 33: $yyVal = doubleval($yyVals[0 + $yyTop]); break; case 34: $yyVal = intval($yyVals[0 + $yyTop]); break; #line 410 "-" } $yyTop -= self::$yyLen[$yyN]; $yyState = $yyStates[$yyTop]; $yyM = self::$yyLhs[$yyN]; if (0 == $yyState && 0 == $yyM) { $yyState = self::$yyFinal; // Check whether it's necessary to fetch the next token $yyToken < 0 && ($yyToken = $yyLex->advance() ? $yyLex->token : 0); // We've reached the final token! if (0 == $yyToken) { return $yyVal; } continue 2; } $yyState = ($yyN = self::$yyGindex[$yyM]) != 0 && ($yyN += $yyState) >= 0 && $yyN < self::$yyTableCount && self::$yyCheck[$yyN] == $yyState ? self::$yyTable[$yyN] : self::$yyDgoto[$yyM]; continue 2; } } } }
public function errorAtFileAndLine() { $a .= ''; // E_NOTICE: Undefined variable: a $this->assertTrue((bool) xp::errorAt(__FILE__, __LINE__ - 1)); }
public function errorAt_given_a_file_and_line_with_error() { trigger_error('Test error'); $this->assertTrue((bool) \xp::errorAt(__FILE__, __LINE__ - 1)); }
/** * Decode a name from a list of given character sets * * @param string name * @param string[] charsets * @return string */ protected function decodeName($name, $charsets) { \xp::gc(__FILE__); foreach ($charsets as $charset => $target) { $decoded = iconv($charset, $target, $name); if (!\xp::errorAt(__FILE__, __LINE__ - 1)) { return $target === \xp::ENCODING ? $decoded : iconv($target, \xp::ENCODING, $decoded); } \xp::gc(__FILE__); // Clean up and try next charset } return $name; }
/** * Read a number of characters * * @param int size default 8192 * @return string NULL when end of data is reached */ public function read($size = 8192) { if (0 === $size) { return ''; } while (strlen($this->buf) < $size) { $c = fread($this->in, $size - strlen($this->buf)); if ('' === $c) { if (xp::errorAt(__FILE__, __LINE__ - 2)) { $message = key(xp::$errors[__FILE__][__LINE__ - 3]); xp::gc(__FILE__); throw new FormatException($message); } break; } $this->buf .= $c; } if ('' === $this->buf) { return NULL; } $chunk = $this->buf; $this->buf = ''; return $chunk; }