Mode.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /* Copyright (c) 2019 Geert Bergman (geert@scrivo.nl), highlight.php
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright notice,
  10. * this list of conditions and the following disclaimer in the documentation
  11. * and/or other materials provided with the distribution.
  12. * 3. Neither the name of "highlight.js", "highlight.php", nor the names of its
  13. * contributors may be used to endorse or promote products derived from this
  14. * software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. namespace Highlight;
  29. /**
  30. * A PHP representation of a Mode in the JS library.
  31. *
  32. * @internal
  33. *
  34. * @since 9.16.0.0
  35. * @mixin ModeDeprecations
  36. *
  37. * Language definition set via language definition JSON files
  38. *
  39. * @property bool $case_insensitive = false
  40. * @property array $aliases = array()
  41. * @property string $className = ""
  42. * @property string $begin = ""
  43. * @property RegEx|null $beginRe = null
  44. * @property string $end = ""
  45. * @property RegEx|null $endRe = null
  46. * @property string $beginKeywords = ""
  47. * @property bool $endsWithParent = false
  48. * @property bool $endsParent = false
  49. * @property bool $endSameAsBegin = false
  50. * @property string $lexemes = ""
  51. * @property RegEx|null $lexemesRe = null
  52. * @property array<string, array> $keywords = array()
  53. * @property string $illegal = ""
  54. * @property RegEx|null $illegalRe = null
  55. * @property bool $excludeBegin = false
  56. * @property bool $excludeEnd = false
  57. * @property bool $returnBegin = false
  58. * @property bool $returnEnd = false
  59. * @property Mode[] $contains = array()
  60. * @property Mode $starts = ""
  61. * @property array $variants = array()
  62. * @property int|null $relevance = null
  63. * @property string|array|null $subLanguage = null
  64. * @property bool $skip = false
  65. * @property bool $disableAutodetect = false
  66. *
  67. * Properties set at runtime by the language compilation process
  68. * @property array $cachedVariants = array()
  69. * @property Terminators|null $terminators = null
  70. * @property string $terminator_end = ""
  71. * @property bool $compiled = false
  72. * @property Mode|null $parent = null
  73. * @property string $type = ''
  74. *
  75. * @see https://highlightjs.readthedocs.io/en/latest/reference.html
  76. */
  77. abstract class Mode
  78. {
  79. /**
  80. * Fill in the missing properties that this Mode does not have.
  81. *
  82. * @internal
  83. *
  84. * @since 9.16.0.0
  85. */
  86. public static function _normalize(\stdClass &$obj)
  87. {
  88. // Don't overload our Modes if we've already normalized it
  89. if (isset($obj->__IS_COMPLETE)) {
  90. return;
  91. }
  92. if ($obj === null) {
  93. $obj = new \stdClass();
  94. }
  95. $patch = array(
  96. "begin" => true,
  97. "end" => true,
  98. "lexemes" => true,
  99. "illegal" => true,
  100. );
  101. // These values come in from JSON definition files
  102. $defaultValues = array(
  103. "case_insensitive" => false,
  104. "aliases" => array(),
  105. "className" => "",
  106. "begin" => "",
  107. "beginRe" => null,
  108. "end" => "",
  109. "endRe" => null,
  110. "beginKeywords" => "",
  111. "endsWithParent" => false,
  112. "endsParent" => false,
  113. "endSameAsBegin" => false,
  114. "lexemes" => "",
  115. "lexemesRe" => null,
  116. "keywords" => array(),
  117. "illegal" => "",
  118. "illegalRe" => null,
  119. "excludeBegin" => false,
  120. "excludeEnd" => false,
  121. "returnBegin" => false,
  122. "returnEnd" => false,
  123. "contains" => array(),
  124. "starts" => "",
  125. "variants" => array(),
  126. "relevance" => null,
  127. "subLanguage" => null,
  128. "skip" => false,
  129. "disableAutodetect" => false,
  130. );
  131. // These values are set at runtime
  132. $runTimeValues = array(
  133. "cachedVariants" => array(),
  134. "terminators" => null,
  135. "terminator_end" => "",
  136. "compiled" => false,
  137. "parent" => null,
  138. // This value is unique to highlight.php Modes
  139. "__IS_COMPLETE" => true,
  140. );
  141. foreach ($patch as $k => $v) {
  142. if (isset($obj->{$k})) {
  143. $obj->{$k} = str_replace("\\/", "/", $obj->{$k});
  144. $obj->{$k} = str_replace("/", "\\/", $obj->{$k});
  145. }
  146. }
  147. foreach ($defaultValues as $k => $v) {
  148. if (!isset($obj->{$k}) && is_object($obj)) {
  149. $obj->{$k} = $v;
  150. }
  151. }
  152. foreach ($runTimeValues as $k => $v) {
  153. if (is_object($obj)) {
  154. $obj->{$k} = $v;
  155. }
  156. }
  157. }
  158. /**
  159. * Set any deprecated properties values to their replacement values.
  160. *
  161. * @internal
  162. */
  163. public static function _handleDeprecations(\stdClass &$obj)
  164. {
  165. $deprecations = array(
  166. // @TODO Deprecated since 9.16.0.0; remove at 10.x
  167. 'caseInsensitive' => 'case_insensitive',
  168. 'terminatorEnd' => 'terminator_end',
  169. );
  170. foreach ($deprecations as $deprecated => $new) {
  171. $obj->{$deprecated} = &$obj->{$new};
  172. }
  173. }
  174. }