{"id":16758166,"url":"https://github.com/foo123/syntaxhighlighter-grammar","last_synced_at":"2025-04-10T17:11:55.845Z","repository":{"id":143020786,"uuid":"45670120","full_name":"foo123/syntaxhighlighter-grammar","owner":"foo123","description":"Transform a grammar in JSON format to a highlight brush for SyntaxHighlighter","archived":false,"fork":false,"pushed_at":"2022-08-08T07:01:20.000Z","size":474,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T14:51:23.603Z","etag":null,"topics":["grammar-parser","syntax-highlighting"],"latest_commit_sha":null,"homepage":"https://foo123.github.io/examples/syntaxhighlighter-grammar/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/foo123.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-11-06T08:46:00.000Z","updated_at":"2024-05-22T09:41:02.000Z","dependencies_parsed_at":"2023-08-31T07:21:13.723Z","dependency_job_id":null,"html_url":"https://github.com/foo123/syntaxhighlighter-grammar","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foo123%2Fsyntaxhighlighter-grammar","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foo123%2Fsyntaxhighlighter-grammar/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foo123%2Fsyntaxhighlighter-grammar/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foo123%2Fsyntaxhighlighter-grammar/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/foo123","download_url":"https://codeload.github.com/foo123/syntaxhighlighter-grammar/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248261799,"owners_count":21074224,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["grammar-parser","syntax-highlighting"],"created_at":"2024-10-13T04:04:21.382Z","updated_at":"2025-04-10T17:11:55.837Z","avatar_url":"https://github.com/foo123.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"syntaxhighlighter-grammar\n=============\n\n\n__Transform a JSON grammar into a syntax-highlight brush for SyntaxHighlighter__\n\n\n![SyntaxHighlighter Grammar](/sh-grammar.png)\n\nA simple and light-weight (~ 44kB minified, ~ 15kB zipped) [SyntaxHighlighter](https://github.com/syntaxhighlighter/syntaxhighlighter) add-on\n\nto generate syntaxhighlighter-compatible brushes from a grammar specification in JSON format.\n\n**see also:**\n\n* [Abacus](https://github.com/foo123/Abacus) advanced Combinatorics and Algebraic Number Theory Symbolic Computation library for JavaScript, Python\n* [Plot.js](https://github.com/foo123/Plot.js) simple and small library which can plot graphs of functions and various simple charts and can render to Canvas, SVG and plain HTML\n* [HAAR.js](https://github.com/foo123/HAAR.js) image feature detection based on Haar Cascades in JavaScript (Viola-Jones-Lienhart et al Algorithm)\n* [HAARPHP](https://github.com/foo123/HAARPHP) image feature detection based on Haar Cascades in PHP (Viola-Jones-Lienhart et al Algorithm)\n* [FILTER.js](https://github.com/foo123/FILTER.js) video and image processing and computer vision Library in pure JavaScript (browser and node)\n* [Xpresion](https://github.com/foo123/Xpresion) a simple and flexible eXpression parser engine (with custom functions and variables support), based on [GrammarTemplate](https://github.com/foo123/GrammarTemplate), for PHP, JavaScript, Python\n* [Regex Analyzer/Composer](https://github.com/foo123/RegexAnalyzer) Regular Expression Analyzer and Composer for PHP, JavaScript, Python\n* [GrammarTemplate](https://github.com/foo123/GrammarTemplate) grammar-based templating for PHP, JavaScript, Python\n* [codemirror-grammar](https://github.com/foo123/codemirror-grammar) transform a formal grammar in JSON format into a syntax-highlight parser for CodeMirror editor\n* [ace-grammar](https://github.com/foo123/ace-grammar) transform a formal grammar in JSON format into a syntax-highlight parser for ACE editor\n* [prism-grammar](https://github.com/foo123/prism-grammar) transform a formal grammar in JSON format into a syntax-highlighter for Prism code highlighter\n* [highlightjs-grammar](https://github.com/foo123/highlightjs-grammar) transform a formal grammar in JSON format into a syntax-highlight mode for Highlight.js code highlighter\n* [syntaxhighlighter-grammar](https://github.com/foo123/syntaxhighlighter-grammar) transform a formal grammar in JSON format to a highlight brush for SyntaxHighlighter code highlighter\n* [SortingAlgorithms](https://github.com/foo123/SortingAlgorithms) implementations of Sorting Algorithms in JavaScript\n* [PatternMatchingAlgorithms](https://github.com/foo123/PatternMatchingAlgorithms) implementations of Pattern Matching Algorithms in JavaScript\n\n\n\n**Note:** The invariant codebase for all the `*-grammar` add-ons resides at [editor-grammar](https://github.com/foo123/editor-grammar) repository (used as a `git submodule`)\n\n\n### Contents\n\n* [Live Playground Example](https://foo123.github.io/examples/syntaxhighlighter-grammar)\n* [Todo](#todo)\n* [Features](#features)\n* [How To use](#how-to-use)\n* [API Reference](/api-reference.md)\n* [Grammar Reference](https://github.com/foo123/editor-grammar/blob/master/grammar-reference.md)\n* [Other Examples](#other-examples)\n\n\n[![Build your own syntax-highlight mode on the fly](/test/screenshot.png)](https://foo123.github.io/examples/syntaxhighlighter-grammar)\n\n\n### Todo\n\nsee [Modularity and Future Directions](https://github.com/foo123/editor-grammar/blob/master/grammar-reference.md#modularity-and-future-directions)\n\n* enable grammar add-on to pre-compile a grammar specification directly into mode source code, so it can be used without the add-on as standalone mode [TODO, maybe]\n\n\n### Features\n\n* A [`Grammar`](https://github.com/foo123/editor-grammar/blob/master/grammar-reference.md) can **extend other `Grammars`** (so arbitrary `variations` and `dialects` can be handled more easily)\n* `Grammar` includes: [`Style Model`](/https://github.com/foo123/editor-grammar/blob/master/grammar-reference.md#style-model) , [`Lex Model`](https://github.com/foo123/editor-grammar/blob/master/grammar-reference.md#lexical-model) and [`Syntax Model` (optional)](https://github.com/foo123/editor-grammar/blob/master/grammar-reference.md#syntax-model), plus a couple of [*settings*](https://github.com/foo123/editor-grammar/blob/master/grammar-reference.md#extra-settings) (see examples)\n* **`Grammar` specification can be minimal**, defaults will be used (see example grammars)\n* [`Grammar.Syntax Model`](https://github.com/foo123/editor-grammar/blob/master/grammar-reference.md#syntax-model) can enable highlight in a more *context-specific* way, plus detect possible *syntax errors*\n* [`Grammar.Syntax Model`](https://github.com/foo123/editor-grammar/blob/master/grammar-reference.md#syntax-model) can contain **recursive references**\n* [`Grammar.Syntax Model`](https://github.com/foo123/editor-grammar/blob/master/grammar-reference.md#syntax-pegbnf-like-notations) can be (fully) specificed using [`PEG`](https://en.wikipedia.org/wiki/Parsing_expression_grammar)-like notation or [`BNF`](https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form)-like notation  (**NEW feature**)\n* [`Grammar.Syntax Model`](https://github.com/foo123/editor-grammar/blob/master/grammar-reference.md#syntax-pegbnf-like-notations) implements **positive / negative lookahead tokens** (analogous to `PEG` `and-`/`not-` predicates)  (**NEW feature**)\n* [`Grammar.Syntax Model`](https://github.com/foo123/editor-grammar/blob/master/grammar-reference.md#syntax-model) can include **external (sub-)grammars so that new multiplexed / mixed grammars** are created easily and intuitively (see test examples) (**NEW feature**)\n* `Grammar` can define [*action* tokens](https://github.com/foo123/editor-grammar/blob/master/grammar-reference.md#action-tokens) to perform *complex context-specific* parsing functionality, including **associated tag matching** and **duplicate identifiers** (see for example `xml.grammar` example) (**NEW feature**)\n* Generated highlighters are **optimized for speed and size**\n* Can generate a syntax-highlighter from a grammar **interactively and on-the-fly** ( see example, http://foo123.github.io/examples/syntaxhighlighter-grammar )\n* see also [Modularity and Future Directions](https://github.com/foo123/editor-grammar/blob/master/grammar-reference.md#modularity-and-future-directions)\n\n\n### How to use:\n\nAn example for XML:\n\n```javascript\n// 1. a partial xml grammar in simple JSON format\nvar xml_grammar = {\n    \n// prefix ID for regular expressions, represented as strings, used in the grammar\n\"RegExpID\"                          : \"RE::\",\n\n// Style model\n\"Style\"                             : {\n    \n     \"declaration\"                  : \"keyword\"\n    ,\"doctype\"                      : \"variable\"\n    ,\"meta\"                         : \"variable\"\n    ,\"comment\"                      : \"comments\"\n    ,\"cdata\"                        : \"color2\"\n    ,\"atom\"                         : \"constants\"\n    ,\"tag\"                          : \"keyword\"\n    ,\"attribute\"                    : \"color1\"\n    ,\"string\"                       : \"string\"\n    ,\"number\"                       : \"constants\"\n\n},\n\n// Lexical model\n\"Lex\"                               : {\n     \n     \"comment:comment\"              : [\"\u003c!--\", \"--\u003e\"]\n    ,\"declaration:block\"            : [\"\u003c?xml\", \"?\u003e\"]\n    ,\"doctype:block\"                : [\"RE::/\u003c!doctype\\\\b/i\", \"\u003e\"]\n    ,\"meta:block\"                   : [\"RE::/\u003c\\\\?[_a-zA-Z][\\\\w\\\\._\\\\-]*/\", \"?\u003e\"]\n    ,\"cdata:block\"                  : [\"\u003c![CDATA[\", \"]]\u003e\"]\n    ,\"open_tag\"                     : \"RE::/\u003c([_a-zA-Z][_a-zA-Z0-9\\\\-]*)/\"\n    ,\"close_tag\"                    : \"RE::/\u003c\\\\/([_a-zA-Z][_a-zA-Z0-9\\\\-]*)\u003e/\"\n    ,\"attribute\"                    : \"RE::/[_a-zA-Z][_a-zA-Z0-9\\\\-]*/\"\n    ,\"string:line-block\"            : [[\"\\\"\"], [\"'\"]]\n    ,\"number\"                       : [\"RE::/[0-9]\\\\d*/\", \"RE::/#[0-9a-fA-F]+/\"]\n    ,\"atom\"                         : [\"RE::/\u0026#x[a-fA-F\\\\d]+;/\", \"RE::/\u0026#[\\\\d]+;/\", \"RE::/\u0026[a-zA-Z][a-zA-Z0-9]*;/\"]\n    ,\"text\"                         : \"RE::/[^\u003c\u0026]+/\"\n    \n    // actions\n    ,\"tag_ctx:action\"               : {\"context\":true}\n    ,\"\\\\tag_ctx:action\"             : {\"context\":false}\n    ,\"unique_id:action\"             : {\"unique\":[\"xml\", \"$1\"],\"msg\":\"Duplicate id value \\\"$0\\\"\"}\n    ,\"unique_att:action\"            : {\"unique\":[\"tag\", \"$0\"],\"msg\":\"Duplicate attribute \\\"$0\\\"\",\"in-context\":true}\n    ,\"tag_opened:action\"            : {\"push\":\"\u003c$1\u003e\",\"ci\":true}\n    ,\"tag_closed:action\"            : {\"pop\":\"\u003c$1\u003e\",\"ci\":true,\"msg\":\"Tags \\\"$0\\\" and \\\"$1\\\" do not match\"}\n    ,\"tag_autoclosed:action\"        : {\"pop\":null}\n    ,\"out_of_place:error\"           : \"\\\"$2$3\\\" can only be at the beginning of XML document\"\n    \n},\n    \n// Syntax model (optional)\n\"Syntax\"                            : {\n     \n     \"tag_att\"                      : \"'id'.attribute unique_att '=' string unique_id | attribute unique_att '=' (string | number)\"\n    ,\"start_tag\"                    : \"open_tag.tag tag_ctx tag_opened tag_att* ('\u003e'.tag | '/\u003e'.tag tag_autoclosed) \\\\tag_ctx\"\n    ,\"end_tag\"                      : \"close_tag.tag tag_closed\"\n    ,\"xml\"                          : \"(^^1 declaration? doctype?) (declaration.error out_of_place | doctype.error out_of_place | comment | meta | cdata | start_tag | end_tag | atom | text)*\"\n    \n},\n    \n// what to parse and in what order\n\"Parser\"                            : [ [\"xml\"] ]\n\n};\n\n// 2. parse the grammar into a SyntaxHighlighter-compatible Brush\nvar xml_mode = SyntaxHighlighterGrammar.getMode( xml_grammar, SyntaxHighlighter );\n\n// 3. use it with SyntaxHighlighter\nxml_mode.aliases = [ \"xml\" ];\nSyntaxHighlighter.brushes[ \"Xml\" ] = xml_mode;\nSyntaxHighlighter.highlight( {brush: \"xml\"}, document.getElementById(\"code\") );\n\n\n```\n\n\nResult:\n\n![xml-grammar](/test/grammar-xml.png)\n\n\n### Other Examples:\n\n![htmlmixed-grammar](/test/grammar-htmlmixed.png)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoo123%2Fsyntaxhighlighter-grammar","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffoo123%2Fsyntaxhighlighter-grammar","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoo123%2Fsyntaxhighlighter-grammar/lists"}