{"id":16758129,"url":"https://github.com/foo123/ace-grammar","last_synced_at":"2025-03-21T23:31:47.779Z","repository":{"id":45665072,"uuid":"15138782","full_name":"foo123/ace-grammar","owner":"foo123","description":"Transform a JSON grammar into a syntax-highlight parser for ACE Editor","archived":false,"fork":false,"pushed_at":"2022-08-08T06:47:29.000Z","size":6023,"stargazers_count":36,"open_issues_count":3,"forks_count":8,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-18T05:51:21.505Z","etag":null,"topics":["ace-editor","bnf","grammar-parser","peg","syntax-highlighter"],"latest_commit_sha":null,"homepage":"https://foo123.github.io/examples/ace-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}},"created_at":"2013-12-12T14:58:38.000Z","updated_at":"2024-06-27T06:16:24.000Z","dependencies_parsed_at":"2022-09-02T18:30:43.244Z","dependency_job_id":null,"html_url":"https://github.com/foo123/ace-grammar","commit_stats":null,"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foo123%2Face-grammar","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foo123%2Face-grammar/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foo123%2Face-grammar/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foo123%2Face-grammar/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/foo123","download_url":"https://codeload.github.com/foo123/ace-grammar/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244885134,"owners_count":20526282,"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":["ace-editor","bnf","grammar-parser","peg","syntax-highlighter"],"created_at":"2024-10-13T04:04:14.379Z","updated_at":"2025-03-21T23:31:47.428Z","avatar_url":"https://github.com/foo123.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"ace-grammar\r\n===========\r\n\r\n__Transform a JSON grammar into an ACE syntax-highlight parser__\r\n\r\n\r\n![Ace Grammar](/ace-grammar.png)\r\n\r\nA simple and light-weight (~ 57kB minified, ~ 19kB zipped) [ACE](https://github.com/ajaxorg/ace) add-on\r\n\r\nto generate syntax-highlight parsers (ace modes) from a grammar specification in JSON format.\r\n\r\n\r\n**see also:**\r\n\r\n* [Abacus](https://github.com/foo123/Abacus) advanced Combinatorics and Algebraic Number Theory Symbolic Computation library for JavaScript, Python\r\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\r\n* [HAAR.js](https://github.com/foo123/HAAR.js) image feature detection based on Haar Cascades in JavaScript (Viola-Jones-Lienhart et al Algorithm)\r\n* [HAARPHP](https://github.com/foo123/HAARPHP) image feature detection based on Haar Cascades in PHP (Viola-Jones-Lienhart et al Algorithm)\r\n* [FILTER.js](https://github.com/foo123/FILTER.js) video and image processing and computer vision Library in pure JavaScript (browser and node)\r\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\r\n* [Regex Analyzer/Composer](https://github.com/foo123/RegexAnalyzer) Regular Expression Analyzer and Composer for PHP, JavaScript, Python\r\n* [GrammarTemplate](https://github.com/foo123/GrammarTemplate) grammar-based templating for PHP, JavaScript, Python\r\n* [codemirror-grammar](https://github.com/foo123/codemirror-grammar) transform a formal grammar in JSON format into a syntax-highlight parser for CodeMirror editor\r\n* [ace-grammar](https://github.com/foo123/ace-grammar) transform a formal grammar in JSON format into a syntax-highlight parser for ACE editor\r\n* [prism-grammar](https://github.com/foo123/prism-grammar) transform a formal grammar in JSON format into a syntax-highlighter for Prism code highlighter\r\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\r\n* [syntaxhighlighter-grammar](https://github.com/foo123/syntaxhighlighter-grammar) transform a formal grammar in JSON format to a highlight brush for SyntaxHighlighter code highlighter\r\n* [SortingAlgorithms](https://github.com/foo123/SortingAlgorithms) implementations of Sorting Algorithms in JavaScript\r\n* [PatternMatchingAlgorithms](https://github.com/foo123/PatternMatchingAlgorithms) implementations of Pattern Matching Algorithms in JavaScript\r\n\r\n\r\n\r\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`)\r\n\r\n\r\n\r\n### Contents\r\n\r\n* [Live Playground Example](https://foo123.github.io/examples/ace-grammar)\r\n* [Todo](#todo)\r\n* [Features](#features)\r\n* [How To use](#how-to-use)\r\n* [API Reference](/api-reference.md)\r\n* [Grammar Reference](https://github.com/foo123/editor-grammar/blob/master/grammar-reference.md)\r\n* [Other Examples](#other-examples)\r\n\r\n[![Build your own syntax-highlight mode on the fly](/test/screenshot.png)](https://foo123.github.io/examples/ace-grammar)\r\n\r\n\r\n### Todo\r\n\r\nCode Indentation is ACE default, see [Modularity and Future Directions](https://github.com/foo123/editor-grammar/blob/master/grammar-reference.md#modularity-and-future-directions)\r\n\r\n\r\n* handle arbitrary, user-defined, code `(de-)indentation` in the `grammar` specification (e.g via `indent action` tokens)\r\n* handle arbitrary, user-defined, code `matching` (e.g `brackets`, `tags`, etc..) in the `grammar` specification (e.g via `match action` tokens) [DONE]\r\n\u003c!--* handle arbitrary, user-defined, `(operator) precedence` relations in the `grammar` specification (e.g via `precedence action` tokens)--\u003e\r\n* handle arbitrary, user-defined, `local/global/scoped` relations in the `grammar` specification (e.g via `scope action` tokens) [DONE]\r\n* and so on..\r\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]\r\n\r\n\r\n\r\n### Features\r\n\r\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)\r\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)\r\n* **`Grammar` specification can be minimal**, defaults will be used (see example grammars)\r\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* and display appropriate *error messages* (see below)\r\n* [`Grammar.Syntax Model`](https://github.com/foo123/editor-grammar/blob/master/grammar-reference.md#syntax-model) can contain **recursive references** (see `/test/grammar-js-recursion.html`)\r\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**)\r\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**)\r\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**)\r\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**)\r\n* Generated highlight modes can support **toggle comments** and **keyword autocompletion** functionality if defined in the grammar\r\n* **Context-sensitive autocompletion** extracted directly from the grammar specification  (**NEW feature**)\r\n* **Dynamic (Context-sensitive) autocompletion** from typed user actions like code/token/symbols  (**NEW feature**)\r\n* Generated highlight modes can support **lint-like syntax-annotation** functionality generated from the grammar\r\n* Generated highlight modes can support custom, user-defined, **code folding** functionality from the [grammar `fold` model](https://github.com/foo123/editor-grammar/blob/master/grammar-reference.md#code-folding)  (**NEW feature**)\r\n* Generated highlight modes can support custom, user-defined, **code token matching** functionality from the [grammar `match` model](https://github.com/foo123/editor-grammar/blob/master/grammar-reference.md#code-matching)  (**NEW feature**)\r\n* Generated parsers are **optimized for speed and size**\r\n* Can generate a syntax-highlight parser from a grammar **interactively and on-the-fly** ( see example, http://foo123.github.io/examples/ace-grammar )\r\n* see also [Modularity and Future Directions](https://github.com/foo123/editor-grammar/blob/master/grammar-reference.md#modularity-and-future-directions)\r\n\r\n\r\n### How to use:\r\n\r\nSee working examples under [/test](/test) folder.\r\n\r\nAn example for XML:\r\n\r\n\r\n```javascript\r\n// 1. a partial xml grammar in simple JSON format\r\nvar xml_grammar = {\r\n    \r\n// prefix ID for regular expressions, represented as strings, used in the grammar\r\n\"RegExpID\"                          : \"RE::\",\r\n\r\n\"Extra\"                     : {\r\n    \r\n    \"fold\"                  : \"xml\"\r\n    \r\n},\r\n    \r\n// Style model\r\n\"Style\"                             : {\r\n    \r\n     \"declaration\"                  : \"keyword\"\r\n    ,\"doctype\"                      : \"string\"\r\n    ,\"meta\"                         : \"string\"\r\n    ,\"comment\"                      : \"comment\"\r\n    ,\"cdata\"                        : \"string\"\r\n    ,\"atom\"                         : \"string\"\r\n    ,\"tag\"                          : \"keyword.tag\"\r\n    ,\"attribute\"                    : \"variable\"\r\n    ,\"number\"                       : \"constant.numeric\"\r\n    ,\"string\"                       : \"string\"\r\n    ,\"error\"                        : \"invalid\"\r\n\r\n},\r\n\r\n// Lexical model\r\n\"Lex\"                               : {\r\n     \r\n     \"comment:comment\"              : [\"\u003c!--\", \"--\u003e\"]\r\n    ,\"declaration:block\"            : [\"\u003c?xml\", \"?\u003e\"]\r\n    ,\"doctype:block\"                : [\"RE::/\u003c!doctype\\\\b/i\", \"\u003e\"]\r\n    ,\"meta:block\"                   : [\"RE::/\u003c\\\\?[_a-zA-Z][\\\\w\\\\._\\\\-]*/\", \"?\u003e\"]\r\n    ,\"cdata:block\"                  : [\"\u003c![CDATA[\", \"]]\u003e\"]\r\n    ,\"open_tag\"                     : \"RE::/\u003c((?:[_a-zA-Z][_a-zA-Z0-9\\\\-]*:)?[_a-zA-Z][_a-zA-Z0-9\\\\-]*)\\\\b/\"\r\n    ,\"close_tag\"                    : \"RE::/\u003c\\\\/((?:[_a-zA-Z][_a-zA-Z0-9\\\\-]*:)?[_a-zA-Z][_a-zA-Z0-9\\\\-]*)\u003e/\"\r\n    ,\"attribute\"                    : \"RE::/[_a-zA-Z][_a-zA-Z0-9\\\\-]*/\"\r\n    ,\"string:line-block\"            : [[\"\\\"\"], [\"'\"]]\r\n    ,\"number\"                       : [\"RE::/[0-9]\\\\d*/\", \"RE::/#[0-9a-fA-F]+/\"]\r\n    ,\"atom\"                         : [\"RE::/\u0026#x[a-fA-F\\\\d]+;/\", \"RE::/\u0026#[\\\\d]+;/\", \"RE::/\u0026[a-zA-Z][a-zA-Z0-9]*;/\"]\r\n    ,\"text\"                         : \"RE::/[^\u003c\u0026]+/\"\r\n    \r\n    // actions\r\n    ,\"@tag:action\"                  : {\"context\":true}\r\n    ,\"tag@:action\"                  : {\"context\":false}\r\n    ,\"@unique_id:action\"            : {\"unique\":[\"xml\", \"$1\"],\"msg\":\"Duplicate id value \\\"$0\\\"\",\"mode\":\"hash\"}\r\n    ,\"@unique_att:action\"           : {\"unique\":[\"att\", \"$0\"],\"msg\":\"Duplicate attribute \\\"$0\\\"\",\"mode\":\"hash\",\"in-context\":true}\r\n    ,\"@tag_opened:action\"           : {\"push\":\"\u003c$1\u003e\",\"ci\":true}\r\n    ,\"@tag_closed:action\"           : {\"pop\":\"\u003c$1\u003e\",\"ci\":true,\"msg\":\"Tags \\\"$0\\\" and \\\"$1\\\" do not match\"}\r\n    ,\"@tag_autoclosed:action\"       : {\"pop\":null}\r\n    ,\"@autocomplete:action\"         : {\"define\":[\"autocomplete\",\"$1\"],\"msg\":false,\"autocomplete\":true,\"mode\":\"hash\"}\r\n    ,\"@out_of_place:error\"          : \"\\\"$2$3\\\" can only be at the beginning of XML document\"\r\n    \r\n},\r\n    \r\n// Syntax model (optional)\r\n\"Syntax\"                            : {\r\n     \r\n     \"tag_att\"                      : \"'id'.attribute @unique_att '=' string @unique_id | attribute @unique_att '=' (string | number)\"\r\n    ,\"start_tag\"                    : \"open_tag.tag @tag @autocomplete @tag_opened tag_att* ('\u003e'.tag | '/\u003e'.tag @tag_autoclosed) tag@\"\r\n    ,\"end_tag\"                      : \"close_tag.tag @autocomplete @tag_closed\"\r\n    ,\"xml\"                          : \"(^^1 declaration? doctype?) (declaration.error @out_of_place | doctype.error @out_of_place | comment | meta | cdata | start_tag | end_tag | atom | text)*\"\r\n    \r\n},\r\n    \r\n// what to parse and in what order\r\n\"Parser\"                            : [ [\"xml\"] ]\r\n\r\n};\r\n\r\n// 2. parse the grammar into an ACE syntax-highlight mode\r\nvar xml_mode = AceGrammar.getMode( xml_grammar );\r\n\r\n// enable user-defined code folding in the specification (new feature)\r\nxml_mode.supportCodeFolding = true;\r\n\r\n// enable syntax lint-like validation in the grammar\r\nxml_mode.supportGrammarAnnotations = true;\r\n\r\n// enable user-defined autocompletion (if defined)\r\nxml_mode.supportAutoCompletion = true;\r\nxml_mode.autocompleter.options = {prefixMatch:true, caseInsensitiveMatch:false};\r\n// or for context-sensitive autocompletion, extracted from the grammar\r\nxml_mode.autocompleter.options = {prefixMatch:true, caseInsensitiveMatch:false, inContext:true};\r\n// or for dynamic (context-sensitive) autocompletion, extracted from user actions\r\nxml_mode.autocompleter.options = {prefixMatch:true, caseInsensitiveMatch:false, inContext:true|false, dynamic:true};\r\n\r\n// 3. use it with ACE\r\nvar editor = ace.edit(\"editor\");\r\neditor.setValue( document.getElementById(\"code\").value, -1 );\r\neditor.getSession().setMode( xml_mode );\r\neditor.getSession().setFoldStyle(\"markbegin\"); // code-folding\r\neditor.getSession().setOptions({useWorker: true}); // lint-like annotations through workers\r\n\r\n```\r\n\r\n\r\nResult:\r\n\r\n![xml-grammar-1](/test/grammar-xml.png)\r\n![xml-grammar-2](/test/grammar-xml-2.png)\r\n![xml-grammar-2](/test/grammar-xml-3.png)\r\n\r\n\r\n\r\n### Other Examples:\r\n\r\n\r\n![htmlmixed-grammar](/test/grammar-htmlmixed.png)\r\n\r\n![js-recursive-grammar](/test/grammar-js-recursion.png)\r\n![js-recursive-grammar-autocomplete](/test/grammar-js-recursion-2.png)\r\n\r\n![js-scoped-grammar](/test/grammar-js-scoped.png)\r\n![js-scoped-grammar](/test/grammar-js-scoped-2.png)\r\n\r\n![css-grammar](/test/grammar-css.png)\r\n\r\n![python-grammar](/test/grammar-python.png)\r\n\r\n![php-grammar](/test/grammar-php.png)\r\n\r\n![scheme-grammar](/test/grammar-scheme.png)\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoo123%2Face-grammar","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffoo123%2Face-grammar","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoo123%2Face-grammar/lists"}