annotate vendor/symfony/translation/Extractor/PhpStringTokenParser.php @ 5:12f9dff5fda9 tip

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