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