This file is indexed.

/usr/share/php/FSHL/Highlighter.php is in php-fshl 2.1.0-2build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<?php

/**
 * FSHL 2.1.0                                  | Fast Syntax HighLighter |
 * -----------------------------------------------------------------------
 *
 * LICENSE
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */

namespace FSHL;

/**
 * Highlighter.
 *
 * @copyright Copyright (c) 2002-2005 Juraj 'hvge' Durech
 * @copyright Copyright (c) 2011-2012 Jaroslav HanslĂ­k
 * @license http://fshl.kukulich.cz/#license
 */
class Highlighter
{
	/**
	 * No options.
	 *
	 * @var integer
	 */
	const OPTION_DEFAULT = 0x0000;

	/**
	 * Tab indentation option.
	 *
	 * @var integer
	 */
	const OPTION_TAB_INDENT = 0x0010;

	/**
	 * Line counter option.
	 *
	 * @var integer
	 */
	const OPTION_LINE_COUNTER = 0x0020;

	/**
	 * Output mode.
	 *
	 * @var \FSHL\Output
	 */
	private $output = null;

	/**
	 * Options.
	 *
	 * @var integer
	 */
	private $options;

	/**
	 * Tab indent width.
	 *
	 * @var integer
	 */
	private $tabIndentWidth;

	/**
	 * List of already used lexers.
	 *
	 * @var array
	 */
	private $lexers = array();

	/**
	 * Current lexer.
	 *
	 * @var \FSHL\Lexer
	 */
	private $lexer = null;

	/**
	 * Table for tab indentation.
	 *
	 * @var array
	 */
	private $tabs = array();

	/**
	 * States stack.
	 *
	 * @var array
	 */
	private $stack = array();

	/**
	 * Prepares the highlighter.
	 *
	 * @param \FSHL\Output $output
	 * @param integer $options
	 * @param integer $tabIndentWidth
	 */
	public function __construct(Output $output, $options = self::OPTION_DEFAULT, $tabIndentWidth = 4)
	{
		$this->setOutput($output)
			->setOptions($options, $tabIndentWidth);
	}

	/**
	 * Highlightes a string.
	 *
	 * @param string $text
	 * @param \FSHL\Lexer $lexer
	 * @return string
	 * @throws \RuntimeException If no lexer is set.
	 */
	public function highlight($text, Lexer $lexer = null)
	{
		// Sets the lexer
		$initialLexer = $this->lexer;
		if (null !== $lexer) {
			$this->setLexer($lexer);
		}

		// No lexer
		if (null === $this->lexer) {
			throw new \RuntimeException('No lexer set');
		}

		// Prepares the text
		$text = str_replace(array("\r\n", "\r"), "\n", (string) $text);
		$textLength = strlen($text);
		$textPos = 0;

		// Parses the text
		$output = array();
		$fragment = '';
		$maxLineWidth = 0;
		$line = 1;
		$char = 0;
		if ($this->options & self::OPTION_LINE_COUNTER) {
			// Right aligment of line counter
			$maxLineWidth = strlen(substr_count($text, "\n") + 1);
			$fragment .= $this->line($line, $maxLineWidth);
		}
		$newLexerName = $lexerName = $this->lexer->language;
		$newState = $state = $this->lexer->initialState;
		$this->stack = array();

		while (true) {
			list($transitionId, $delimiter, $buffer) = $this->lexer->{'findDelimiter' . $state}($text, $textLength, $textPos);

			// Some data may be collected before getPart reaches the delimiter, we must output this before other processing
			if (false !== $buffer) {
				$bufferLength = strlen($buffer);
				$textPos += $bufferLength;
				$char += $bufferLength;
				$fragment .= $this->template($buffer, $state);
				if (isset($fragment[8192])) {
					$output[] = $fragment;
					$fragment = '';
				}
			}

			if (-1 === $transitionId) {
				// End of stream
				break;
			}

			// Processes received delimiter as string
			$prevLine = $line;
			$prevChar = $char;
			$prevTextPos = $textPos;

			$delimiterLength = strlen($delimiter);
			$textPos += $delimiterLength;
			$char += $delimiterLength;

			// Adds line counter and tab indentation
			$addLine = false;
			if ("\n" === $delimiter[$delimiterLength - 1]) {
				// Line counter
				$line++;
				$char = 0;
				if ($this->options & self::OPTION_LINE_COUNTER) {
					$addLine = true;
					$actualLine = $line;
				}
			} elseif ("\t" === $delimiter && ($this->options & self::OPTION_TAB_INDENT)) {
				// Tab indentation
				$i = $char % $this->tabIndentWidth;
				$delimiter = $this->tabs[$i][0];
				$char += $this->tabs[$i][1];
			}

			// Gets new state from the transitions table
			$newState = $this->lexer->trans[$state][$transitionId][Generator::STATE_DIAGRAM_INDEX_STATE];
			if ($newState === $this->lexer->returnState) {
				// Chooses mode of delimiter processing
				if (Generator::BACK === $this->lexer->trans[$state][$transitionId][Generator::STATE_DIAGRAM_INDEX_MODE]) {
					$line = $prevLine;
					$char = $prevChar;
					$textPos = $prevTextPos;
				} else {
					$fragment .= $this->template($delimiter, $state);
					if ($addLine) {
						$fragment .= $this->line($actualLine, $maxLineWidth);
					}
					if (isset($fragment[8192])) {
						$output[] = $fragment;
						$fragment = '';
					}
				}

				// Get state from the context stack
				if ($item = $this->popState()) {
					list($newLexerName, $state) = $item;
					// If previous context was in a different lexer, switch the lexer too
					if ($newLexerName !== $lexerName) {
						$this->setLexerByName($newLexerName);
						$lexerName = $newLexerName;
					}
				} else {
					$state = $this->lexer->initialState;
				}

				continue;
			}

			// Chooses mode of delimiter processing
			$type = $this->lexer->trans[$state][$transitionId][Generator::STATE_DIAGRAM_INDEX_MODE];
			if (Generator::BACK === $type) {
				$line = $prevLine;
				$char = $prevChar;
				$textPos = $prevTextPos;
			} else {
				$fragment .= $this->template($delimiter, Generator::NEXT === $type ? $newState : $state);
				if ($addLine) {
					$fragment .= $this->line($actualLine, $maxLineWidth);
				}
				if (isset($fragment[8192])) {
					$output[] = $fragment;
					$fragment = '';
				}
			}

			// Switches between lexers (transition to embedded language)
			if ($this->lexer->flags[$newState] & Generator::STATE_FLAG_NEWLEXER) {
				if ($newState === $this->lexer->quitState) {
					// Returns to the previous lexer
					if ($item = $this->popState()) {
						list($newLexerName, $state) = $item;
						if ($newLexerName !== $lexerName) {
							$this->setLexerByName($newLexerName);
							$lexerName = $newLexerName;
						}
					} else {
						$state = $this->lexer->initialState;
					}
				} else {
					// Switches to the embedded language
					$newLexerName = $this->lexer->data[$newState];
					$this->pushState($lexerName, $this->lexer->trans[$newState] ? $newState : $state);
					$this->setLexerByName($newLexerName);
					$lexerName = $newLexerName;
					$state = $this->lexer->initialState;
				}

				continue;
			}

			// If newState is marked with recursion flag (alias call), push current state to the context stack
			if (($this->lexer->flags[$newState] & Generator::STATE_FLAG_RECURSION) && $state !== $newState) {
				$this->pushState($lexerName, $state);
			}

			// Change the state
			$state = $newState;
		}

		// Adds template end
		$fragment .= $this->output->template('', null);
		$output[] = $fragment;

		// Restore lexer
		$this->lexer = $initialLexer;

		return implode('', $output);
	}

	/**
	 * Sets the output mode.
	 *
	 * @param \FSHL\Output $output
	 * @return \FSHL\Highlighter
	 */
	public function setOutput(Output $output)
	{
		$this->output = $output;

		return $this;
	}

	/**
	 * Sets options.
	 *
	 * @param integer $options
	 * @param integer $tabIndentWidth
	 * @return \FSHL\Highlighter
	 */
	public function setOptions($options = self::OPTION_DEFAULT, $tabIndentWidth = 4)
	{
		$this->options = $options;

		if (($this->options & self::OPTION_TAB_INDENT) && $tabIndentWidth > 0) {
			// Precalculate a table for tab indentation
			$t = ' ';
			$ti = 0;
			for ($i = $tabIndentWidth; $i; $i--) {
				$this->tabs[$i % $tabIndentWidth] = array($t, $ti++);
				$t .= ' ';
			}
			$this->tabIndentWidth = $tabIndentWidth;
		} else {
			$this->options &= ~self::OPTION_TAB_INDENT;
		}

		return $this;
	}

	/**
	 * Sets the default lexer.
	 *
	 * @param \FSHL\Lexer $lexer
	 * @return \FSHL\Highlighter
	 */
	public function setLexer(Lexer $lexer)
	{
		// Generates the lexer cache on fly, if the lexer cache doesn't exist
		if (!$this->findCache($lexer->getLanguage())) {
			$this->generateCache($lexer);
		}

		return $this;
	}

	/**
	 * Sets the current lexer by name.
	 *
	 * @param string $lexerName
	 * @return \FSHL\Highlighter
	 * @throws \InvalidArgumentException If the class for given lexer doesn't exist.
	 */
	private function setLexerByName($lexerName)
	{
		// Generates the lexer cache on fly, if the lexer cache doesn't exist
		if (!$this->findCache($lexerName)) {
			// Finds the lexer
			$lexerClass = '\\FSHL\\Lexer\\' . $lexerName;
			if (!class_exists($lexerClass)) {
				throw new \InvalidArgumentException(sprintf('The class for "%s" lexer doesn\'t exist', $lexerName));
			}
			$lexer = new $lexerClass();

			// Generates the lexer cache on fly
			$this->generateCache($lexer);
		}

		return $this;
	}

	/**
	 * Tries to find the lexer cache.
	 *
	 * @param string $lexerName
	 * @return boolean
	 */
	private function findCache($lexerName)
	{
		// Lexer has been used before
		if (isset($this->lexers[$lexerName])) {
			$this->lexer = $this->lexers[$lexerName];
			return true;
		}

		// Loads lexer cache
		$lexerCacheClass = '\\FSHL\\Lexer\\Cache\\' . $lexerName;
		if (class_exists($lexerCacheClass)) {
			$this->lexers[$lexerName] = new $lexerCacheClass();
			$this->lexer = $this->lexers[$lexerName];
			return true;
		}

		return false;
	}

	/**
	 * Generates the lexer cache on fly.
	 *
	 * @param \FSHL\Lexer $lexer
	 * @return \FSHL\Highlighter
	 */
	private function generateCache(Lexer $lexer)
	{
		$generator = new Generator($lexer);
		try {
			$generator->saveToCache();
		} catch (\RuntimeException $e) {
			$file = tempnam(sys_get_temp_dir(), 'fshl');
			file_put_contents($file, $generator->getSource());
			require_once $file;
			unlink($file);
		}

		$lexerName = $lexer->getLanguage();
		$lexerCacheClass = '\\FSHL\\Lexer\\Cache\\' . $lexerName;
		$this->lexers[$lexerName] = new $lexerCacheClass();
		$this->lexer = $this->lexers[$lexerName];

		return $this;
	}

	/**
	 * Outputs a word.
	 *
	 * @param string $part
	 * @param string $state
	 * @return string
	 */
	private function template($part, $state)
	{
		if ($this->lexer->flags[$state] & Generator::STATE_FLAG_KEYWORD) {
			$normalized = Generator::CASE_SENSITIVE === $this->lexer->keywords[Generator::KEYWORD_INDEX_CASE_SENSITIVE] ? $part : strtolower($part);

			if (isset($this->lexer->keywords[Generator::KEYWORD_INDEX_LIST][$normalized])) {
				return $this->output->keyword($part, $this->lexer->keywords[Generator::KEYWORD_INDEX_CLASS] . $this->lexer->keywords[Generator::KEYWORD_INDEX_LIST][$normalized]);
			}
		}

		return $this->output->template($part, $this->lexer->classes[$state]);
	}

	/**
	 * Outputs a line.
	 *
	 * @param integer $line
	 * @param integer $maxLineWidth
	 * @return string
	 */
	private function line($line, $maxLineWidth)
	{
		return $this->output->template(str_pad($line, $maxLineWidth, ' ', STR_PAD_LEFT) . ': ', 'line');
	}

	/**
	 * Pushes a state to the context stack.
	 *
	 * @param string $lexerName
	 * @param string $state
	 * @return \FSHL\Highlighter
	 */
	private function pushState($lexerName, $state)
	{
		array_unshift($this->stack, array($lexerName, $state));
		return $this;
	}

	/**
	 * Pops a state from the context stack.
	 *
	 * @return array|null
	 */
	private function popState()
	{
		return array_shift($this->stack);
	}
}