annotate vendor/symfony/translation/Extractor/PhpStringTokenParser.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@14 1 <?php
Chris@14 2
Chris@14 3 /*
Chris@14 4 * This file is part of the Symfony package.
Chris@14 5 *
Chris@14 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@14 7 *
Chris@14 8 * For the full copyright and license information, please view the LICENSE
Chris@14 9 * file that was distributed with this source code.
Chris@14 10 */
Chris@14 11
Chris@14 12 namespace Symfony\Component\Translation\Extractor;
Chris@14 13
Chris@14 14 /*
Chris@14 15 * The following is derived from code at http://github.com/nikic/PHP-Parser
Chris@14 16 *
Chris@14 17 * Copyright (c) 2011 by Nikita Popov
Chris@14 18 *
Chris@14 19 * Some rights reserved.
Chris@14 20 *
Chris@14 21 * Redistribution and use in source and binary forms, with or without
Chris@14 22 * modification, are permitted provided that the following conditions are
Chris@14 23 * met:
Chris@14 24 *
Chris@14 25 * * Redistributions of source code must retain the above copyright
Chris@14 26 * notice, this list of conditions and the following disclaimer.
Chris@14 27 *
Chris@14 28 * * Redistributions in binary form must reproduce the above
Chris@14 29 * copyright notice, this list of conditions and the following
Chris@14 30 * disclaimer in the documentation and/or other materials provided
Chris@14 31 * with the distribution.
Chris@14 32 *
Chris@14 33 * * The names of the contributors may not be used to endorse or
Chris@14 34 * promote products derived from this software without specific
Chris@14 35 * prior written permission.
Chris@14 36 *
Chris@14 37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@14 38 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Chris@14 39 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Chris@14 40 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Chris@14 41 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Chris@14 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Chris@14 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Chris@14 44 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Chris@14 45 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Chris@14 46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Chris@14 47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@14 48 */
Chris@14 49
Chris@14 50 class PhpStringTokenParser
Chris@14 51 {
Chris@17 52 protected static $replacements = [
Chris@14 53 '\\' => '\\',
Chris@14 54 '$' => '$',
Chris@14 55 'n' => "\n",
Chris@14 56 'r' => "\r",
Chris@14 57 't' => "\t",
Chris@14 58 'f' => "\f",
Chris@14 59 'v' => "\v",
Chris@14 60 'e' => "\x1B",
Chris@17 61 ];
Chris@14 62
Chris@14 63 /**
Chris@14 64 * Parses a string token.
Chris@14 65 *
Chris@14 66 * @param string $str String token content
Chris@14 67 *
Chris@14 68 * @return string The parsed string
Chris@14 69 */
Chris@14 70 public static function parse($str)
Chris@14 71 {
Chris@14 72 $bLength = 0;
Chris@14 73 if ('b' === $str[0]) {
Chris@14 74 $bLength = 1;
Chris@14 75 }
Chris@14 76
Chris@14 77 if ('\'' === $str[$bLength]) {
Chris@14 78 return str_replace(
Chris@17 79 ['\\\\', '\\\''],
Chris@17 80 ['\\', '\''],
Chris@14 81 substr($str, $bLength + 1, -1)
Chris@14 82 );
Chris@14 83 } else {
Chris@14 84 return self::parseEscapeSequences(substr($str, $bLength + 1, -1), '"');
Chris@14 85 }
Chris@14 86 }
Chris@14 87
Chris@14 88 /**
Chris@14 89 * Parses escape sequences in strings (all string types apart from single quoted).
Chris@14 90 *
Chris@14 91 * @param string $str String without quotes
Chris@17 92 * @param string|null $quote Quote type
Chris@14 93 *
Chris@14 94 * @return string String with escape sequences parsed
Chris@14 95 */
Chris@14 96 public static function parseEscapeSequences($str, $quote)
Chris@14 97 {
Chris@14 98 if (null !== $quote) {
Chris@14 99 $str = str_replace('\\'.$quote, $quote, $str);
Chris@14 100 }
Chris@14 101
Chris@14 102 return preg_replace_callback(
Chris@14 103 '~\\\\([\\\\$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3})~',
Chris@17 104 [__CLASS__, 'parseCallback'],
Chris@14 105 $str
Chris@14 106 );
Chris@14 107 }
Chris@14 108
Chris@14 109 private static function parseCallback($matches)
Chris@14 110 {
Chris@14 111 $str = $matches[1];
Chris@14 112
Chris@14 113 if (isset(self::$replacements[$str])) {
Chris@14 114 return self::$replacements[$str];
Chris@14 115 } elseif ('x' === $str[0] || 'X' === $str[0]) {
Chris@17 116 return \chr(hexdec($str));
Chris@14 117 } else {
Chris@17 118 return \chr(octdec($str));
Chris@14 119 }
Chris@14 120 }
Chris@14 121
Chris@14 122 /**
Chris@14 123 * Parses a constant doc string.
Chris@14 124 *
Chris@14 125 * @param string $startToken Doc string start token content (<<<SMTHG)
Chris@14 126 * @param string $str String token content
Chris@14 127 *
Chris@14 128 * @return string Parsed string
Chris@14 129 */
Chris@14 130 public static function parseDocString($startToken, $str)
Chris@14 131 {
Chris@14 132 // strip last newline (thanks tokenizer for sticking it into the string!)
Chris@14 133 $str = preg_replace('~(\r\n|\n|\r)$~', '', $str);
Chris@14 134
Chris@14 135 // nowdoc string
Chris@14 136 if (false !== strpos($startToken, '\'')) {
Chris@14 137 return $str;
Chris@14 138 }
Chris@14 139
Chris@14 140 return self::parseEscapeSequences($str, null);
Chris@14 141 }
Chris@14 142 }