Browse Source

Added some documentation

main
Dustin Wilson 3 years ago
parent
commit
641e9fa547
  1. 6
      lib/Grammar.php
  2. 13
      lib/GrammarRegistry.php
  3. 23
      lib/Highlight.php
  4. 14
      lib/Tokenizer.php

6
lib/Grammar.php

@ -42,7 +42,11 @@ class Grammar {
} }
/** Imports an Atom JSON grammar into the Grammar object */ /**
* Imports an Atom JSON grammar into the Grammar object
*
* @param string $filename - The JSON file to be imported
*/
public function loadJSON(string $filename) { public function loadJSON(string $filename) {
if (!is_file($filename)) { if (!is_file($filename)) {
throw new Exception(Exception::JSON_INVALID_FILE, $filename); throw new Exception(Exception::JSON_INVALID_FILE, $filename);

13
lib/GrammarRegistry.php

@ -16,6 +16,12 @@ class GrammarRegistry {
return true; return true;
} }
/**
* Retrieves a grammar from the registry
*
* @param string $scopeName - The scope name (eg: text.html.php) of the grammar that is being requested
* @return dW\Lit\Grammar|false
*/
public static function get(string $scopeName): Grammar|false { public static function get(string $scopeName): Grammar|false {
if (array_key_exists($scopeName, self::$storage)) { if (array_key_exists($scopeName, self::$storage)) {
return self::$storage[$scopeName]; return self::$storage[$scopeName];
@ -31,6 +37,13 @@ class GrammarRegistry {
return false; return false;
} }
/**
* Sets a grammar in the registry.
*
* @param string $scopeName - The scope name (eg: text.html.php) of the grammar that is being set
* @param dW\Lit\Grammar - The grammar to be put into the registry
* @return bool
*/
public static function set(string $scopeName, Grammar $grammar): bool { public static function set(string $scopeName, Grammar $grammar): bool {
try { try {
self::$storage[$scopeName] = $grammar; self::$storage[$scopeName] = $grammar;

23
lib/Highlight.php

@ -13,10 +13,27 @@ use MensBeam\HTML\{
class Highlight { class Highlight {
public static function toDOM(string $data, string $scopeName, ?Document $document = null, string $encoding = 'windows-1252'): Element { /**
* Highlights incoming string data and outputs an HTML DOM Mensbeam\HTML\Element.
*
* @param string $data - The input data string.
* @param string $scopeName - The scope name (eg: text.html.php) of the grammar that's needed to highlight the input data.
* @param ?Mensbeam\HTML\Document [$document = null] - An existing MensBeam\HTML\Document to use as the owner document of the returned MensBeam\HTML\Element; if omitted one will be created instead.
* @param string [$encoding = 'windows-1252'] - If a document isn't provided an encoding may be provided for the new document; the HTML standard default windows-1252 is used if no encoding is provided.
* @return Mensbeam\HTML\Element
*/
public static function toElement(string $data, string $scopeName, ?Document $document = null, string $encoding = 'windows-1252'): Element {
return self::highlight($data, $scopeName, $document, $encoding); return self::highlight($data, $scopeName, $document, $encoding);
} }
/**
* Highlights incoming string data and outputs an HTML string.
*
* @param string $data - The input data string.
* @param string $scopeName - The scope name (eg: text.html.php) of the grammar that's needed to highlight the input data.
* @param string [$encoding = 'windows-1252'] - Encoding for the input string data; the HTML standard default windows-1252 is used if no encoding is provided.
* @return string
*/
public static function toString(string $data, string $scopeName, string $encoding = 'windows-1252'): string { public static function toString(string $data, string $scopeName, string $encoding = 'windows-1252'): string {
return (string)self::highlight($data, $scopeName, null, $encoding); return (string)self::highlight($data, $scopeName, null, $encoding);
} }
@ -38,7 +55,7 @@ class Highlight {
$pre = $document->createElement('pre'); $pre = $document->createElement('pre');
$code = $document->createElement('code'); $code = $document->createElement('code');
$code->setAttribute('class', str_replace('.', ' ', $scopeName)); $code->setAttribute('class', implode(' ', array_unique(explode('.', $scopeName))));
$pre->appendChild($code); $pre->appendChild($code);
$elementStack = [ $code ]; $elementStack = [ $code ];
@ -56,7 +73,7 @@ class Highlight {
} }
$span = $document->createElement('span'); $span = $document->createElement('span');
$span->setAttribute('class', str_replace('.', ' ', $scope)); $span->setAttribute('class', implode(' ', array_unique(explode('.', $scope))));
end($elementStack)->appendChild($span); end($elementStack)->appendChild($span);
$scopeStack[] = $scope; $scopeStack[] = $scope;
$elementStack[] = $span; $elementStack[] = $span;

14
lib/Tokenizer.php

@ -17,14 +17,22 @@ use dW\Lit\Scope\{
}; };
/** Class for tokenizing input data */
class Tokenizer { class Tokenizer {
// Used for debugging; assertions (`ini_set('zend.assertions', '1')`) must be
// enabled to see debug output.
public static bool $debug = false; public static bool $debug = false;
// The input Data class.
protected Data $data; protected Data $data;
// The supplied Grammar used to highlight the input data.
protected Grammar $grammar; protected Grammar $grammar;
// The offset/position on the line the tokenizer is currently at.
protected int $offset = 0; protected int $offset = 0;
// Flag used to tell the tokenizer an injection is on the rule stack.
protected bool $activeInjection = false; protected bool $activeInjection = false;
// The current line being tokenized.
protected string $line = ''; protected string $line = '';
// The current line number of the input data being tokenized.
protected int $lineNumber = 1; protected int $lineNumber = 1;
// Cache of rule lists which have had references spliced to keep from having to // Cache of rule lists which have had references spliced to keep from having to
// repeatedly splice in the same reference. It needs to be in two arrays because // repeatedly splice in the same reference. It needs to be in two arrays because
@ -32,7 +40,9 @@ class Tokenizer {
// itself. // itself.
protected array $ruleCacheIndexes = []; protected array $ruleCacheIndexes = [];
protected array $ruleCacheValues = []; protected array $ruleCacheValues = [];
// The stack of rules
protected array $ruleStack; protected array $ruleStack;
// The stack of scopes
protected array $scopeStack; protected array $scopeStack;
protected const SCOPE_RESOLVE_REGEX = '/\$(\d+)|\${(\d+):\/(downcase|upcase)}/S'; protected const SCOPE_RESOLVE_REGEX = '/\$(\d+)|\${(\d+):\/(downcase|upcase)}/S';
@ -46,7 +56,7 @@ class Tokenizer {
$this->scopeStack = [ $this->grammar->scopeName ]; $this->scopeStack = [ $this->grammar->scopeName ];
} }
/** Receives lines from the Data object and yields an array of tokens */
public function tokenize(): \Generator { public function tokenize(): \Generator {
foreach ($this->data->get() as $lineNumber => $line) { foreach ($this->data->get() as $lineNumber => $line) {
$this->lineNumber = $lineNumber; $this->lineNumber = $lineNumber;

Loading…
Cancel
Save