/** * 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 symbol token * * @param Jstewnc\Rtf\Token\Control\Symbol $token the control symbol token * @param Jstewmc\Rtf\Element\Group $group the current group * @return void * @since 0.1.0 */ protected function parseControlSymbol(Token\Control\Symbol $token, Element\Group $group) { // if a class exists for the symbol, instantiate it; otherwise, instantiate // a generic control symbol element // keep in mind, class_exists() requires a fully-qualified namespace // if (array_key_exists($token->getSymbol(), self::$symbols)) { // get the symbol's name $name = self::$symbols[$token->getSymbol()]; $name = ucfirst($name); $classname = "Jstewmc\\Rtf\\Element\\Control\\Symbol\\{$name}"; if (class_exists($classname)) { $symbol = new $classname(); } else { $symbol = new Element\Control\Symbol\Symbol(); $symbol->setSymbol($token->getSymbol()); } } else { $symbol = new Element\Control\Symbol\Symbol(); $symbol->setSymbol($token->getSymbol()); } // set the symbol's parameter $symbol->setParameter($token->getParameter()); $symbol->setIsSpaceDelimited($token->getIsSpaceDelimited()); // append the element $symbol->setParent($group); $group->appendChild($symbol); return; }