From 19ca683e31ae5fefaf1568c5e9590828b74ff0b1 Mon Sep 17 00:00:00 2001 From: Dustin Wilson Date: Wed, 5 Jan 2022 10:17:42 -0600 Subject: [PATCH] Integration with Framework --- README.md | 3 + composer.json | 3 +- composer.lock | 229 ++++---------------------------------- lib/Data.php | 1 + lib/FauxReadOnly.php | 21 ---- lib/Grammar.php | 1 + lib/Grammar/Exception.php | 106 ++++++------------ lib/Grammar/Reference.php | 2 - lib/Grammar/Rule.php | 2 +- lib/Scope/Data.php | 9 +- lib/Scope/Node.php | 2 +- 11 files changed, 67 insertions(+), 312 deletions(-) delete mode 100644 lib/FauxReadOnly.php diff --git a/README.md b/README.md index a55289a..aac7168 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,9 @@ Lit is a multilanguage syntax highlighter written in PHP. It takes code as input and returns an HTML pre element containing the code highlighted using span elements with classes based upon tokens in the code. It is loosely based upon [Atom][a]'s [Highlights][b] which is used in the Atom text editor to syntax highlight code. Atom's Highlights is in turn based upon [TextMate][c]'s syntax highlighting using its concepts of scope selectors and common keywords for components of programming languages. Lit is not a port of Atom's Highlights but instead an independent implementation of what I can understand of TextMate's grammar syntax, parsing, and tokenization by analyzing other implementations. It aims to at least have feature parity or better with Atom's Highlights. +## Warning Before Using ## + +This library is experimental. The code is not tested at all, and writing tests for it will be an incredible undertaking because there's no specification whatsoever to test against. It would require creating a specification as well which is beyond the scope of a project that exists just to scratch an itch. Atom's Highlights is also barely tested itself. There's numerous PHP libraries out there without a test suite; not having one is not up to our usual standards. So, that's why this warning exists. ## Documentation ## diff --git a/composer.json b/composer.json index d93738f..cb9915f 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "require": { "php": "^8.0", "ext-dom": "*", - "ext-json": "*" + "ext-json": "*", + "mensbeam/framework": "^1.0" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 20b23c0..4606188 100644 --- a/composer.lock +++ b/composer.lock @@ -4,211 +4,34 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a1e7abc5ea4187862ef68f682eabf335", + "content-hash": "5a9f29fa2c1527f8f786596f93d2039d", "packages": [ { - "name": "mensbeam/html", - "version": "dev-master", + "name": "mensbeam/framework", + "version": "1.0.5", "source": { "type": "git", - "url": "https://code.mensbeam.com/MensBeam/HTML", - "reference": "4a7511dd49e85ad1865a3252176047266d62b087" - }, - "require": { - "ext-dom": "*", - "mensbeam/intl": ">=0.9.0", - "mensbeam/mimesniff": "^0.2.0", - "php": ">=7.1" - }, - "require-dev": { - "bamarni/composer-bin-plugin": "^1.3", - "daux/daux.io": "^0.16.0", - "masterminds/html5": "^2.7" - }, - "suggest": { - "ext-ctype": "Improved performance" - }, - "default-branch": true, - "type": "library", - "autoload": { - "psr-4": { - "MensBeam\\HTML\\": [ - "lib/", - "lib/DOM", - "lib/DOM/traits" - ] - }, - "classmap": [ - "lib/Token.php" - ], - "files": [ - "lib/ctype.php" - ] - }, - "autoload-dev": { - "psr-4": { - "MensBeam\\HTML\\Test\\": "tests/lib/", - "MensBeam\\HTML\\TestCase\\": "tests/cases/" - } - }, - "scripts": { - "post-install-cmd": [ - "@composer bin all install" - ], - "post-update-cmd": [ - "@composer bin all update" - ] - }, - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Dustin Wilson", - "email": "dustin@dustinwilson.com", - "homepage": "https://dustinwilson.com/" - }, - { - "name": "J. King", - "email": "jking@jkingweb.ca", - "homepage": "https://jkingweb.ca/" - } - ], - "description": "Parses modern HTML text into a PHP DOMDocument", - "time": "2021-04-26T21:07:13+00:00" - }, - { - "name": "mensbeam/intl", - "version": "0.9.0", - "source": { - "type": "git", - "url": "https://github.com/mensbeam/intl.git", - "reference": "de037b182ce99aaa90ebc09b0ee0457ddf1d07bc" + "url": "https://github.com/mensbeam/Framework.git", + "reference": "af4b8e72d50c01cf78d3d1b12b5a65f714f9d73b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/mensbeam/intl/zipball/de037b182ce99aaa90ebc09b0ee0457ddf1d07bc", - "reference": "de037b182ce99aaa90ebc09b0ee0457ddf1d07bc", + "url": "https://api.github.com/repos/mensbeam/Framework/zipball/af4b8e72d50c01cf78d3d1b12b5a65f714f9d73b", + "reference": "af4b8e72d50c01cf78d3d1b12b5a65f714f9d73b", "shasum": "" }, "require": { - "php": ">=7.1" + "php": ">=7.4" }, "require-dev": { - "bamarni/composer-bin-plugin": "*", - "ext-intl": "*" + "bamarni/composer-bin-plugin": "^1.3" }, "type": "library", "autoload": { "psr-4": { - "MensBeam\\Intl\\": "lib/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "J. King", - "email": "jking@jkingweb.ca", - "homepage": "https://jkingweb.ca/" - } - ], - "description": "A set of dependency-free basic internationalization tools", - "keywords": [ - "WHATWG", - "charset", - "encoding", - "internationalization", - "intl", - "unicode", - "utf-8", - "utf8" - ], - "support": { - "issues": "https://github.com/mensbeam/intl/issues", - "source": "https://github.com/mensbeam/intl/tree/0.9.0" - }, - "time": "2021-03-25T19:08:04+00:00" - }, - { - "name": "mensbeam/mimesniff", - "version": "0.2.1", - "source": { - "type": "git", - "url": "https://github.com/mensbeam/mime.git", - "reference": "c19be2496ab1e27fbf9c3483c2a9faa2781796cd" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/mensbeam/mime/zipball/c19be2496ab1e27fbf9c3483c2a9faa2781796cd", - "reference": "c19be2496ab1e27fbf9c3483c2a9faa2781796cd", - "shasum": "" - }, - "require": { - "php": ">=7.1", - "psr/http-message": "^1.0" - }, - "require-dev": { - "bamarni/composer-bin-plugin": "^1.3", - "ext-intl": "*" - }, - "type": "library", - "autoload": { - "psr-4": { - "MensBeam\\Mime\\": "lib/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "J. King", - "email": "jking@jkingweb.ca", - "homepage": "https://jkingweb.ca/" - } - ], - "description": "An implementation of the WHATWG MIME Sniffing specification", - "keywords": [ - "WHATWG", - "mime", - "mimesniff" - ], - "support": { - "issues": "https://github.com/mensbeam/mime/issues", - "source": "https://github.com/mensbeam/mime/tree/0.2.1" - }, - "time": "2021-03-07T03:58:00+00:00" - }, - { - "name": "psr/http-message", - "version": "1.0.1", - "source": { - "type": "git", - "url": "https://github.com/php-fig/http-message.git", - "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", - "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", - "shasum": "" - }, - "require": { - "php": ">=5.3.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "psr-4": { - "Psr\\Http\\Message\\": "src/" + "MensBeam\\Framework\\": [ + "lib/" + ] } }, "notification-url": "https://packagist.org/downloads/", @@ -217,38 +40,28 @@ ], "authors": [ { - "name": "PHP-FIG", - "homepage": "http://www.php-fig.org/" + "name": "Dustin Wilson", + "email": "dustin@dustinwilson.com", + "homepage": "https://dustinwilson.com/" } ], - "description": "Common interface for HTTP messages", - "homepage": "https://github.com/php-fig/http-message", - "keywords": [ - "http", - "http-message", - "psr", - "psr-7", - "request", - "response" - ], + "description": "Common classes and traits used in many Mensbeam projects", "support": { - "source": "https://github.com/php-fig/http-message/tree/master" + "issues": "https://github.com/mensbeam/Framework/issues", + "source": "https://github.com/mensbeam/Framework/tree/1.0.5" }, - "time": "2016-08-06T14:39:51+00:00" + "time": "2022-01-05T16:09:09+00:00" } ], "packages-dev": [], "aliases": [], "minimum-stability": "stable", - "stability-flags": { - "mensbeam/html": 20 - }, + "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": "^7.4 || ^8.0", + "php": "^8.0", "ext-dom": "*", - "ext-intl": "*", "ext-json": "*" }, "platform-dev": [], diff --git a/lib/Data.php b/lib/Data.php index c40e200..45801cc 100644 --- a/lib/Data.php +++ b/lib/Data.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace MensBeam\Lit; +use MensBeam\Framework\FauxReadOnly; class Data { diff --git a/lib/FauxReadOnly.php b/lib/FauxReadOnly.php deleted file mode 100644 index 4024a8a..0000000 --- a/lib/FauxReadOnly.php +++ /dev/null @@ -1,21 +0,0 @@ -$prop; - } -} \ No newline at end of file diff --git a/lib/Grammar.php b/lib/Grammar.php index e2fdcc2..eacb0e1 100644 --- a/lib/Grammar.php +++ b/lib/Grammar.php @@ -14,6 +14,7 @@ use MensBeam\Lit\Grammar\{ RepositoryReference, SelfReference }; +use MensBeam\Framework\FauxReadOnly; /** diff --git a/lib/Grammar/Exception.php b/lib/Grammar/Exception.php index 59c2a04..7fce23a 100644 --- a/lib/Grammar/Exception.php +++ b/lib/Grammar/Exception.php @@ -5,81 +5,43 @@ declare(strict_types=1); namespace MensBeam\Lit\Grammar; - -class Exception extends \Exception { - const INVALID_CODE = 100; - const UNKNOWN_ERROR = 101; - const INCORRECT_PARAMETERS_FOR_MESSAGE = 102; - const UNREACHABLE_CODE = 103; - - const JSON_INVALID_FILE = 200; - const JSON_ERROR_STATE_MISMATCH = 201; - const JSON_ERROR_CTRL_CHAR = 202; - const JSON_ERROR_SYNTAX = 203; - const JSON_ERROR_UTF8 = 204; - const JSON_ERROR_RECURSION = 205; - const JSON_ERROR_INF_OR_NAN = 206; - const JSON_ERROR_UNSUPPORTED_TYPE = 207; - const JSON_ERROR_INVALID_PROPERTY_NAME = 208; - const JSON_ERROR_UTF16 = 209; - const JSON_MISSING_PROPERTY = 210; - const JSON_INVALID_TYPE = 211; +use MensBeam\Framework\Exception; + +class Exception extends FrameworkException { + const JSON_INVALID_FILE = 300; + const JSON_ERROR_STATE_MISMATCH = 301; + const JSON_ERROR_CTRL_CHAR = 302; + const JSON_ERROR_SYNTAX = 303; + const JSON_ERROR_UTF8 = 304; + const JSON_ERROR_RECURSION = 305; + const JSON_ERROR_INF_OR_NAN = 306; + const JSON_ERROR_UNSUPPORTED_TYPE = 307; + const JSON_ERROR_INVALID_PROPERTY_NAME = 308; + const JSON_ERROR_UTF16 = 309; + const JSON_MISSING_PROPERTY = 310; + const JSON_INVALID_TYPE = 311; const GRAMMAR_MISSING = 400; - protected static $messages = [ - 100 => 'Invalid error code', - 101 => 'Unknown error; escaping', - 102 => 'Incorrect number of parameters for Exception message; %s expected', - 103 => 'Unreachable code', - - 200 => '"%s" is either not a file or you do not have permission to read the file', - 201 => '"%s" is invalid or malformed JSON', - 202 => 'Invalid control character encountered when parsing "%s"', - 203 => 'Syntax error, malformed JSON when parsing "%s"', - 204 => 'Malformed UTF-8 characters, possibly incorrectly encoded when parsing "%s"', - 205 => 'One or more recursive references could not be encoded when parsing "%s"', - 206 => 'One or more NAN or INF values could not be encoded when parsing "%s"', - 207 => 'Unsupported type encountered when parsing "%s"', - 208 => 'Invalid property name encountered when parsing "%s"', - 209 => 'Malformed UTF-16 characters, possibly incorrectly encoded when parsing "%s"', - 210 => '"%1$s" does not have the required %2$s property', - 211 => '%1$s expected for %2$s, found %3$s in "%4$s"', - - 300 => 'A grammar for scope %s does not exist; one may be added using GrammarRegistry::set' - ]; public function __construct(int $code, ...$args) { - if (!isset(self::$messages[$code])) { - throw new self(self::INVALID_CODE); - } - - $message = self::$messages[$code]; - $previous = null; - - if ($args) { - // Grab a previous exception if there is one. - if ($args[0] instanceof \Throwable) { - $previous = array_shift($args); - } elseif (end($args) instanceof \Throwable) { - $previous = array_pop($args); - } - } - - // Count the number of replacements needed in the message. - preg_match_all('/(\%(?:\d+\$)?s)/', $message, $matches); - $count = count(array_unique($matches[1])); - - // If the number of replacements don't match the arguments then oops. - if (count($args) !== $count) { - throw new self(self::INCORRECT_PARAMETERS_FOR_MESSAGE, $count); - } - - if ($count > 0) { - // Go through each of the arguments and run sprintf on the strings. - $message = call_user_func_array('sprintf', [ $message, ...$args ]); - } - - parent::__construct("$message\n", $code, $previous); + self::$messages = array_replace(parent::$messages, [ + 300 => '"%s" is either not a file or you do not have permission to read the file', + 301 => '"%s" is invalid or malformed JSON', + 302 => 'Invalid control character encountered when parsing "%s"', + 303 => 'Syntax error, malformed JSON when parsing "%s"', + 304 => 'Malformed UTF-8 characters, possibly incorrectly encoded when parsing "%s"', + 305 => 'One or more recursive references could not be encoded when parsing "%s"', + 306 => 'One or more NAN or INF values could not be encoded when parsing "%s"', + 307 => 'Unsupported type encountered when parsing "%s"', + 308 => 'Invalid property name encountered when parsing "%s"', + 309 => 'Malformed UTF-16 characters, possibly incorrectly encoded when parsing "%s"', + 310 => '"%1$s" does not have the required %2$s property', + 311 => '%1$s expected for %2$s, found %3$s in "%4$s"', + + 400 => 'A grammar for scope %s does not exist; one may be added using GrammarRegistry::set' + ]); + + parent::__construct($code, ...$args); } -} +} \ No newline at end of file diff --git a/lib/Grammar/Reference.php b/lib/Grammar/Reference.php index 2208bed..6d11e9d 100644 --- a/lib/Grammar/Reference.php +++ b/lib/Grammar/Reference.php @@ -5,12 +5,10 @@ declare(strict_types=1); namespace MensBeam\Lit\Grammar; -use MensBeam\Lit\FauxReadOnly; /** Acts as a catch-all type for references */ abstract class Reference extends Rule { - use FauxReadOnly; protected string $_ownerGrammarScopeName; diff --git a/lib/Grammar/Rule.php b/lib/Grammar/Rule.php index 59632d6..4418b1a 100644 --- a/lib/Grammar/Rule.php +++ b/lib/Grammar/Rule.php @@ -5,7 +5,7 @@ declare(strict_types=1); namespace MensBeam\Lit\Grammar; -use MensBeam\Lit\FauxReadOnly; +use MensBeam\Framework\FauxReadOnly; /** * Abstract class used as a base class for Pattern and Reference classes diff --git a/lib/Scope/Data.php b/lib/Scope/Data.php index 9dc33fe..d986207 100644 --- a/lib/Scope/Data.php +++ b/lib/Scope/Data.php @@ -5,12 +5,15 @@ declare(strict_types=1); namespace MensBeam\Lit\Scope; +use MensBeam\Framework\FauxReadOnly; /** * Tokenizes scope strings into an array of segments of the original string and * provides an interface for iterating through them. */ class Data { + use FauxReadOnly; + protected array $data; protected int $_position = -1; @@ -48,10 +51,4 @@ class Data { return $this->data[$this->_position + 1][0]; } - - public function __get(string $name) { - if ($name === 'position') { - return $this->_position; - } - } } diff --git a/lib/Scope/Node.php b/lib/Scope/Node.php index f7c8c77..5db86a1 100644 --- a/lib/Scope/Node.php +++ b/lib/Scope/Node.php @@ -5,7 +5,7 @@ declare(strict_types=1); namespace MensBeam\Lit\Scope; -use MensBeam\Lit\FauxReadOnly; +use MensBeam\Framework\FauxReadOnly; class Node { use FauxReadOnly;