Chris@14: Chris@14: * Chris@14: * For the full copyright and license information, please view the LICENSE Chris@14: * file that was distributed with this source code. Chris@14: */ Chris@14: Chris@14: namespace Symfony\Component\Translation\Extractor; Chris@14: Chris@14: /* Chris@14: * The following is derived from code at http://github.com/nikic/PHP-Parser Chris@14: * Chris@14: * Copyright (c) 2011 by Nikita Popov Chris@14: * Chris@14: * Some rights reserved. Chris@14: * Chris@14: * Redistribution and use in source and binary forms, with or without Chris@14: * modification, are permitted provided that the following conditions are Chris@14: * met: Chris@14: * Chris@14: * * Redistributions of source code must retain the above copyright Chris@14: * notice, this list of conditions and the following disclaimer. Chris@14: * Chris@14: * * Redistributions in binary form must reproduce the above Chris@14: * copyright notice, this list of conditions and the following Chris@14: * disclaimer in the documentation and/or other materials provided Chris@14: * with the distribution. Chris@14: * Chris@14: * * The names of the contributors may not be used to endorse or Chris@14: * promote products derived from this software without specific Chris@14: * prior written permission. Chris@14: * Chris@14: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS Chris@14: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT Chris@14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR Chris@14: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT Chris@14: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, Chris@14: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT Chris@14: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, Chris@14: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY Chris@14: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT Chris@14: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE Chris@14: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Chris@14: */ Chris@14: Chris@14: class PhpStringTokenParser Chris@14: { Chris@17: protected static $replacements = [ Chris@14: '\\' => '\\', Chris@14: '$' => '$', Chris@14: 'n' => "\n", Chris@14: 'r' => "\r", Chris@14: 't' => "\t", Chris@14: 'f' => "\f", Chris@14: 'v' => "\v", Chris@14: 'e' => "\x1B", Chris@17: ]; Chris@14: Chris@14: /** Chris@14: * Parses a string token. Chris@14: * Chris@14: * @param string $str String token content Chris@14: * Chris@14: * @return string The parsed string Chris@14: */ Chris@14: public static function parse($str) Chris@14: { Chris@14: $bLength = 0; Chris@14: if ('b' === $str[0]) { Chris@14: $bLength = 1; Chris@14: } Chris@14: Chris@14: if ('\'' === $str[$bLength]) { Chris@14: return str_replace( Chris@17: ['\\\\', '\\\''], Chris@17: ['\\', '\''], Chris@14: substr($str, $bLength + 1, -1) Chris@14: ); Chris@14: } else { Chris@14: return self::parseEscapeSequences(substr($str, $bLength + 1, -1), '"'); Chris@14: } Chris@14: } Chris@14: Chris@14: /** Chris@14: * Parses escape sequences in strings (all string types apart from single quoted). Chris@14: * Chris@14: * @param string $str String without quotes Chris@17: * @param string|null $quote Quote type Chris@14: * Chris@14: * @return string String with escape sequences parsed Chris@14: */ Chris@14: public static function parseEscapeSequences($str, $quote) Chris@14: { Chris@14: if (null !== $quote) { Chris@14: $str = str_replace('\\'.$quote, $quote, $str); Chris@14: } Chris@14: Chris@14: return preg_replace_callback( Chris@14: '~\\\\([\\\\$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3})~', Chris@17: [__CLASS__, 'parseCallback'], Chris@14: $str Chris@14: ); Chris@14: } Chris@14: Chris@14: private static function parseCallback($matches) Chris@14: { Chris@14: $str = $matches[1]; Chris@14: Chris@14: if (isset(self::$replacements[$str])) { Chris@14: return self::$replacements[$str]; Chris@14: } elseif ('x' === $str[0] || 'X' === $str[0]) { Chris@17: return \chr(hexdec($str)); Chris@14: } else { Chris@17: return \chr(octdec($str)); Chris@14: } Chris@14: } Chris@14: Chris@14: /** Chris@14: * Parses a constant doc string. Chris@14: * Chris@14: * @param string $startToken Doc string start token content (<<