示例#1
0
文件: Lexer.php 项目: jstewmc/rtf
 /**
  * Lexes the backslash character ("\")
  *
  * The backslash character ("\") is arguably the most important character in the
  * RTF specification. A backslash character can indicate the following:
  *
  *     1. a control word (e.g., "\foo")
  *     2. a control symbol (e.g., "\-")
  *     3. an escaped special character (e.g., "\\")
  *     4. an escaped new-line or carriage return (e.g., "\\n")
  *
  * @param  Jstewmc\Stream\Stream  $stream  the character stream
  * @return  Jstewmc\Rtf\Token\Token|false
  * @throws  InvalidArgumentException  if the current character in $stream is not
  *     the backslash character ("\")
  * @since  0.2.0
  */
 protected function lexBackslash(\Jstewmc\Stream\Stream $stream)
 {
     if ($stream->current() !== '\\') {
         throw new \InvalidArgumentExeption(__METHOD__ . "() expects the current character in the stream to be a '\\'");
     }
     // look ahead to the next character, it'll determine what we do; just be sure
     //     you rollback to the current character
     //
     $next = $stream->next();
     $stream->previous();
     // if a next character exists
     if ($next !== false) {
         // the next character may be a literal character, an escaped new-line or
         //     carriage-return (i.e., an implicit "\par" control word), a control
         //     word, or a control symbol
         //
         if (in_array($next, ['\\', '{', '}'])) {
             $token = Token\Text::createFromStream($stream);
         } elseif ($next == "\n" || $next == "\r") {
             $token = new Token\Control\Word('par');
             $stream->next();
             // consume the current "\" character
         } elseif (ctype_alpha($next)) {
             $token = Token\Control\Word::createFromStream($stream);
         } else {
             $token = Token\Control\Symbol::createFromStream($stream);
         }
     }
     return $token;
 }