Language.php
12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?php
/* Copyright (c)
* - 2006-2013, Ivan Sagalaev (maniacsoftwaremaniacs.org), highlight.js
* (original author)
* - 2013-2019, Geert Bergman (geertscrivo.nl), highlight.php
* - 2014 Daniel Lynge, highlight.php (contributor)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of "highlight.js", "highlight.php", nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
namespace Highlight;
/**
* @todo In highlight.php 10.x, replace the @final attribute with the `final` keyword.
*
* @final
*
* @internal
*
* // Backward compatibility properties
*
* @property Mode $mode (DEPRECATED) All properties traditionally inside of $mode are now available directly from this class.
* @property bool $caseInsensitive (DEPRECATED) Due to compatibility requirements with highlight.js, use `case_insensitive` instead.
*/
class Language extends Mode
{
/** @var string[] */
private static $COMMON_KEYWORDS = array('of', 'and', 'for', 'in', 'not', 'or', 'if', 'then');
/** @var string */
public $name;
/** @var Mode|null */
private $mode = null;
/**
* @param string $lang
* @param string $filePath
*
* @throws \InvalidArgumentException when the given $filePath is inaccessible
*/
public function __construct($lang, $filePath)
{
$this->name = $lang;
// We're loading the JSON definition file as an \stdClass object instead of an associative array. This is being
// done to take advantage of objects being pass by reference automatically in PHP whereas arrays are pass by
// value.
$json = file_get_contents($filePath);
if ($json === false) {
throw new \InvalidArgumentException("Language file inaccessible: $filePath");
}
$this->mode = json_decode($json);
}
/**
* @param string $name
*
* @return bool|Mode|null
*/
public function __get($name)
{
if ($name === 'mode') {
@trigger_error('The "mode" property will be removed in highlight.php 10.x', E_USER_DEPRECATED);
return $this->mode;
}
if ($name === 'caseInsensitive') {
@trigger_error('Due to compatibility requirements with highlight.js, use "case_insensitive" instead.', E_USER_DEPRECATED);
if (isset($this->mode->case_insensitive)) {
return $this->mode->case_insensitive;
}
return false;
}
if (isset($this->mode->{$name})) {
return $this->mode->{$name};
}
return null;
}
/**
* @param string $value
* @param bool $global
*
* @return RegEx
*/
private function langRe($value, $global = false)
{
return RegExUtils::langRe($value, $global, $this->case_insensitive);
}
/**
* Performs a shallow merge of multiple objects into one.
*
* @param Mode $params the objects to merge
* @param array<string, mixed> ...$_
*
* @return Mode
*/
private function inherit($params, $_ = array())
{
/** @var Mode $result */
$result = new \stdClass();
$objects = func_get_args();
$parent = array_shift($objects);
foreach ($parent as $key => $value) {
$result->{$key} = $value;
}
foreach ($objects as $object) {
foreach ($object as $key => $value) {
$result->{$key} = $value;
}
}
return $result;
}
/**
* @param Mode|null $mode
*
* @return bool
*/
private function dependencyOnParent($mode)
{
if (!$mode) {
return false;
}
if (isset($mode->endsWithParent) && $mode->endsWithParent) {
return $mode->endsWithParent;
}
return $this->dependencyOnParent(isset($mode->starts) ? $mode->starts : null);
}
/**
* @param Mode $mode
*
* @return array<int, \stdClass|Mode>
*/
private function expandOrCloneMode($mode)
{
if ($mode->variants && !$mode->cachedVariants) {
$mode->cachedVariants = array();
foreach ($mode->variants as $variant) {
$mode->cachedVariants[] = $this->inherit($mode, array('variants' => null), $variant);
}
}
// EXPAND
// if we have variants then essentially "replace" the mode with the variants
// this happens in compileMode, where this function is called from
if ($mode->cachedVariants) {
return $mode->cachedVariants;
}
// CLONE
// if we have dependencies on parents then we need a unique
// instance of ourselves, so we can be reused with many
// different parents without issue
if ($this->dependencyOnParent($mode)) {
return array($this->inherit($mode, array(
'starts' => $mode->starts ? $this->inherit($mode->starts) : null,
)));
}
// highlight.php does not have a concept freezing our Modes
// no special dependency issues, just return ourselves
return array($mode);
}
/**
* @param Mode $mode
* @param Mode|null $parent
*
* @return void
*/
private function compileMode($mode, $parent = null)
{
Mode::_normalize($mode);
if ($mode->compiled) {
return;
}
$mode->compiled = true;
$mode->keywords = $mode->keywords ? $mode->keywords : $mode->beginKeywords;
if ($mode->keywords) {
$mode->keywords = $this->compileKeywords($mode->keywords, (bool) $this->case_insensitive);
}
$mode->lexemesRe = $this->langRe($mode->lexemes ? $mode->lexemes : "\w+", true);
if ($parent) {
if ($mode->beginKeywords) {
$mode->begin = "\\b(" . implode("|", explode(" ", $mode->beginKeywords)) . ")\\b";
}
if (!$mode->begin) {
$mode->begin = "\B|\b";
}
$mode->beginRe = $this->langRe($mode->begin);
if ($mode->endSameAsBegin) {
$mode->end = $mode->begin;
}
if (!$mode->end && !$mode->endsWithParent) {
$mode->end = "\B|\b";
}
if ($mode->end) {
$mode->endRe = $this->langRe($mode->end);
}
$mode->terminator_end = $mode->end;
if ($mode->endsWithParent && $parent->terminator_end) {
$mode->terminator_end .= ($mode->end ? "|" : "") . $parent->terminator_end;
}
}
if ($mode->illegal) {
$mode->illegalRe = $this->langRe($mode->illegal);
}
if ($mode->relevance === null) {
$mode->relevance = 1;
}
if (!$mode->contains) {
$mode->contains = array();
}
/** @var Mode[] $expandedContains */
$expandedContains = array();
foreach ($mode->contains as &$c) {
if ($c instanceof \stdClass) {
Mode::_normalize($c);
}
$expandedContains = array_merge($expandedContains, $this->expandOrCloneMode(
$c === 'self' ? $mode : $c
));
}
$mode->contains = $expandedContains;
/** @var Mode $contain */
foreach ($mode->contains as $contain) {
$this->compileMode($contain, $mode);
}
if ($mode->starts) {
$this->compileMode($mode->starts, $parent);
}
$terminators = new Terminators($this->case_insensitive);
$mode->terminators = $terminators->_buildModeRegex($mode);
Mode::_handleDeprecations($mode);
}
/**
* @param array<string, string>|string $rawKeywords
* @param bool $caseSensitive
*
* @return array<string, array<int, string|int>>
*/
private function compileKeywords($rawKeywords, $caseSensitive)
{
/** @var array<string, array<int, string|int>> $compiledKeywords */
$compiledKeywords = array();
if (is_string($rawKeywords)) {
$this->splitAndCompile("keyword", $rawKeywords, $compiledKeywords, $caseSensitive);
} else {
foreach ($rawKeywords as $className => $rawKeyword) {
$this->splitAndCompile($className, $rawKeyword, $compiledKeywords, $caseSensitive);
}
}
return $compiledKeywords;
}
/**
* @param string $className
* @param string $str
* @param array<string, array<int, string|int>> $compiledKeywords
* @param bool $caseSensitive
*
* @return void
*/
private function splitAndCompile($className, $str, array &$compiledKeywords, $caseSensitive)
{
if ($caseSensitive) {
$str = strtolower($str);
}
$keywords = explode(' ', $str);
foreach ($keywords as $keyword) {
$pair = explode('|', $keyword);
$providedScore = isset($pair[1]) ? $pair[1] : null;
$compiledKeywords[$pair[0]] = array($className, $this->scoreForKeyword($pair[0], $providedScore));
}
}
/**
* @param string $keyword
* @param string $providedScore
*
* @return int
*/
private function scoreForKeyword($keyword, $providedScore)
{
if ($providedScore) {
return (int) $providedScore;
}
return $this->commonKeyword($keyword) ? 0 : 1;
}
/**
* @param string $word
*
* @return bool
*/
private function commonKeyword($word)
{
return in_array(strtolower($word), self::$COMMON_KEYWORDS);
}
/**
* Compile the Language definition.
*
* @param bool $safeMode
*
* @since 9.17.1.0 The 'safeMode' parameter was added.
*
* @return void
*/
public function compile($safeMode)
{
if ($this->compiled) {
return;
}
$jr = new JsonRef();
$jr->decodeRef($this->mode);
// self is not valid at the top-level
if (isset($this->mode->contains) && !in_array("self", $this->mode->contains)) {
if (!$safeMode) {
throw new \LogicException("`self` is not supported at the top-level of a language.");
}
$this->mode->contains = array_filter($this->mode->contains, function ($mode) {
return $mode !== "self";
});
}
$this->compileMode($this->mode);
}
/**
* @todo Remove in highlight.php 10.x
*
* @deprecated 9.16.0 This method should never have been exposed publicly as part of the API.
*
* @param \stdClass|null $e
*
* @return void
*/
public function complete(&$e)
{
Mode::_normalize($e);
}
}