/** * 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; }
/** * Parses a control word token * * @param Jstewmc\Rtf\Token\Control\Word $token the control word token * @param Jstewmc\Rtf\Element\Group $group the current group * @return void * @since 0.1.0 */ protected function parseControlWord(Token\Control\Word $token, Element\Group $group) { // if a class exists for the control word $filename = ucfirst($token->getWord()); $classname = "Jstewmc\\Rtf\\Element\\Control\\Word\\{$filename}"; if (class_exists($classname)) { // instantiate the control word element and break $word = new $classname(); } else { // otherwise, instantiate a generic control word $word = new Element\Control\Word\Word(); $word->setWord($token->getWord()); } // set the element's parameter $word->setParameter($token->getParameter()); $word->setIsSpaceDelimited($token->getIsSpaceDelimited()); // append the element $word->setParent($group); $group->appendChild($word); return; }