Без опису

PhpStringTokenParser.php 4.3KB

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