PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/public/js/util/docscripts/lib/parser/Text.php

https://github.com/Nerutiz/trades
PHP | 213 lines | 167 code | 23 blank | 23 comment | 40 complexity | be7b45df697da5b6c0661c647da0b2df MD5 | raw file
  1. <?php
  2. class Text
  3. {
  4. public static $variable = '\b[a-zA-Z_.$][\w.$]*';
  5. public static $namespace = '\b[a-zA-Z_.$][\w.$]*(?:\.[a-zA-Z_.$][\w.$]*|\["[^"]+"\])*';
  6. /**
  7. * Blanks out a portion of a string with whitespace
  8. *
  9. * @param $to_blank Portion of the string to be removed
  10. * @param $string Overall string to remove it from
  11. */
  12. public static function blankOut($to_blank, $string) {
  13. $length = strlen($to_blank);
  14. if (!$length) {
  15. return $string;
  16. }
  17. $blanks = array_fill(0, $length, ' ');
  18. return preg_replace('%' . preg_quote($to_blank, '%') . '%', implode($blanks), $string, 1);
  19. }
  20. /**
  21. * Makes sure a variable is formatted namespace.namespace.etc
  22. *
  23. * @param shell
  24. * Called shell because it might be missing some data
  25. */
  26. public static function normalizeVariableName($shell, $source, $start){
  27. if (strpos($shell, '[') !== false) {
  28. $source_line = array_shift(Text::chop($source, $start[0], $start[1], $start[0] + 1));
  29. preg_match('%^\s*([a-zA-Z_.$][\w.$]*(?:\.[a-zA-Z_.$][\w.$]|\["[^"]+"\])*)\s*=\s*function%', $source_line, $match);
  30. $shell = preg_replace('%\["([^"]+)"\]%', '.$1', $match[1]);
  31. }
  32. return $shell;
  33. }
  34. public static function getNextPosition($array, $line_position_pair) {
  35. list($line_number, $position) = $line_position_pair;
  36. ++$position;
  37. if ($position >= strlen($array[$line_number])) {
  38. ++$line_number;
  39. $position = 0;
  40. while (!strlen($array[$line_number])) {
  41. ++$line_number;
  42. }
  43. }
  44. return array($line_number, $position);
  45. }
  46. public static function blankOutAtPositions($to_blank, $start_line, $start_pos, $end_line = -1, $end_pos = -1) {
  47. foreach ($to_blank as $line_number => $line) {
  48. if ($line_number == $start_line) {
  49. $to_blank[$line_number] = Text::blankOutAt($line, $start_pos);
  50. }
  51. if ($line_number > $start_line) {
  52. if ($end_line == -1 || $line_number < $end_line) {
  53. $to_blank[$line_number] = Text::blankOutAt($line, 0);
  54. }
  55. elseif ($line_number == $end_line) {
  56. $to_blank[$line_number] = Text::blankOutAt($line, 0, $end_pos);
  57. }
  58. }
  59. }
  60. return $to_blank;
  61. }
  62. public static function blankOutAt($to_blank, $start, $end = -1) {
  63. if ($end == -1) {
  64. $end = strlen($to_blank) - 1;
  65. }
  66. $length = $end - $start + 1;
  67. if (!$length) {
  68. return $to_blank;
  69. }
  70. if ($length < 0) {
  71. throw new Exception("Length is less than 0");
  72. }
  73. $blanks = array_fill(0, $length, ' ');
  74. return substr($to_blank, 0, $start) . implode($blanks) . substr($to_blank, $end + 1);
  75. }
  76. public static function trim($string) {
  77. return trim(preg_replace('%(^\s*/\*.*\*/\s*?|\s*?/\*.*\*/\s*$|^\s*//.*\n\s*?|\s*?//.*$)%U', '', $string));
  78. }
  79. public static function chop($array, $start_line, $start_position, $end_line = false, $end_position = false, $exclusive = false) {
  80. if (!is_numeric($end_line)) {
  81. $end_line = end(array_keys($array));
  82. }
  83. if (!is_numeric($end_position)) {
  84. $end_position = strlen($array[$end_line]) - 1;
  85. if ($end_position < 0) {
  86. $end_position = 0;
  87. }
  88. }
  89. $lines = array_slice($array, $start_line, $end_line - $start_line + 1, true);
  90. if ($start_position > 0) {
  91. $lines[$start_line] = Text::blankOutAt($lines[$start_line], 0, $start_position - 1);
  92. }
  93. $lines[$end_line] = Text::blankOutAt($lines[$end_line], $end_position + 1, strlen($lines[$end_line]));
  94. if ($exclusive) {
  95. if ($lines[$start_line]{$start_position}) {
  96. $lines[$start_line]{$start_position} = ' ';
  97. }
  98. if ($lines[$end_line]{$end_position}) {
  99. $lines[$end_line]{$end_position} = ' ';
  100. }
  101. }
  102. return $lines;
  103. }
  104. /**
  105. * Always starts at the beginning. If you want a character to be ignored, it shouldn't be passed (see chop and blankOutAt)
  106. */
  107. public static function findTermination($source_array, $termination_characters, $enclosing_characters = '') {
  108. $characters = array();
  109. $terminators = array();
  110. foreach (self::toArray($termination_characters) as $character) {
  111. $terminators[$character] = true;
  112. }
  113. foreach (self::toArray($enclosing_characters) as $index => $character) {
  114. $characters[$character] = ($index % 2) ? -1 : 1;
  115. }
  116. $all_characters = array_merge(array_keys($terminators), array_keys($characters));
  117. $balance = 0;
  118. foreach ($source_array as $line_number => $line) {
  119. $line = self::toArray($line);
  120. foreach (array_intersect($line, $all_characters) as $position => $character) {
  121. if (!$balance && $terminators[$character]) {
  122. return array($line_number, $position);
  123. }
  124. $balance += $characters[$character];
  125. }
  126. }
  127. return array($line_number, $position);
  128. }
  129. public static function toArray($string) {
  130. return array_slice(preg_split('%%', $string), 1, -1);
  131. }
  132. /**
  133. * Splits a comment line up
  134. *
  135. * @returns Array(whitespace, comment data, non-comment data, comment data, has non-comment-data, comment continues on next line)
  136. */
  137. public static function findComments($line, $started = false) {
  138. if (empty($line) && !$started) {
  139. return array(false, false, false, false, false);
  140. }
  141. $first = array();
  142. $middle = array();
  143. $last = array();
  144. $data = false;
  145. $multiline = false;
  146. if ($started) {
  147. // If we're already in a (multi-line) comment, look to close it up
  148. if (($pos = strpos($line, '*/')) !== false) {
  149. $first[] = trim(substr($line, 0, $pos));
  150. $line = substr($line, $pos + 2);
  151. }
  152. else {
  153. // We didn't find a terminator, we're still in a multi-line comment
  154. $multiline = true;
  155. }
  156. }
  157. $single_line = false; // Denotes that we're in a single-line comment.
  158. if (!$multiline) {
  159. // Split by //, /*, and */ while ignoring any surrounding whitespace
  160. $parts = preg_split('%(\s*(?://|/\*|\*/)\s*)%', $line, -1, PREG_SPLIT_DELIM_CAPTURE);
  161. foreach ($parts as $part) {
  162. if (!($trimmed = trim($part))) continue;
  163. if ($multiline && $trimmed == '*/') {
  164. $multiline = false; // Multi-line ends!
  165. }
  166. elseif ($single_line || $multiline) { // If we're within a comment
  167. if (!$data) {
  168. $first[] = $part;
  169. }
  170. else { // If we've found any non-comment data, we start logging this as "after" the data
  171. $last[] = $part;
  172. }
  173. }
  174. elseif ($trimmed == '//') {
  175. $single_line = true;
  176. }
  177. elseif ($trimmed == '/*') {
  178. $multiline = true;
  179. }
  180. else {
  181. $data = true;
  182. $middle = array_merge($middle, $last);
  183. $last = array();
  184. }
  185. }
  186. }
  187. return array(trim(implode('', $first)), trim(implode('', $middle)), trim(implode('', $last)), $data, $multiline);
  188. }
  189. }
  190. ?>