@bhsd/lezer-json 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +42 -0
- package/dist/highlight.d.ts +1 -0
- package/dist/highlight.js +13 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +44 -0
- package/dist/parser.js +24 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2020 by Marijn Haverbeke <marijn@haverbeke.berlin>, Arun Srinivasan <rulfzid@gmail.com>, and others
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# @bhsd/lezer-json
|
|
2
|
+
|
|
3
|
+
This is a fork of [@lezer/json](https://www.npmjs.com/package/@lezer/json) and [@codemirror/lang-json](https://www.npmjs.com/package/@codemirror/lang-json) that adds a dialect for [JSONC](https://jsonc.org/) to provide support for comments and trailing commas.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
You can install the package via npm and import it as a module:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @bhsd/lezer-json
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Language Support
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import {json} from '@bhsd/lezer-json';
|
|
17
|
+
import type {LanguageSupport} from '@codemirror/language';
|
|
18
|
+
|
|
19
|
+
const jsonLanguageSupport: LanguageSupport = json(),
|
|
20
|
+
jsoncLanguageSupport: LanguageSupport = json('jsonc');
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Language
|
|
24
|
+
|
|
25
|
+
You can also import the [LR language](https://codemirror.net/docs/ref/#language.LRLanguage) for JSON or JSONC alone.
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import {jsonLanguage, jsoncLanguage} from '@bhsd/lezer-json';
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Lint Source
|
|
32
|
+
|
|
33
|
+
This package also provides [lint sources](https://codemirror.net/docs/ref/#lint.LintSource) for JSON or JSONC syntax checking.
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import {linter} from '@codemirror/lint';
|
|
37
|
+
import {jsonLinter, jsoncLinter} from '@bhsd/lezer-json';
|
|
38
|
+
import type {Extension} from '@codemirror/state';
|
|
39
|
+
|
|
40
|
+
const jsonLintExtension: Extension = linter(jsonLinter),
|
|
41
|
+
jsoncLintExtension: Extension = linter(jsoncLinter);
|
|
42
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const jsonHighlighting: import("@lezer/common").NodePropSource;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { styleTags, tags as t } from '@lezer/highlight';
|
|
2
|
+
export const jsonHighlighting = styleTags({
|
|
3
|
+
LineComment: t.lineComment,
|
|
4
|
+
BlockComment: t.blockComment,
|
|
5
|
+
String: t.string,
|
|
6
|
+
Number: t.number,
|
|
7
|
+
'True False': t.bool,
|
|
8
|
+
PropertyName: t.propertyName,
|
|
9
|
+
Null: t.null,
|
|
10
|
+
', :': t.separator,
|
|
11
|
+
'[ ]': t.squareBracket,
|
|
12
|
+
'{ }': t.brace,
|
|
13
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { LRLanguage, LanguageSupport } from '@codemirror/language';
|
|
2
|
+
import type { LintSource } from '@codemirror/lint';
|
|
3
|
+
/** LR language for JSON. */
|
|
4
|
+
export declare const jsonLanguage: LRLanguage;
|
|
5
|
+
/** LR language for JSONC. */
|
|
6
|
+
export declare const jsoncLanguage: LRLanguage;
|
|
7
|
+
/**
|
|
8
|
+
* Get language support for JSON or JSONC.
|
|
9
|
+
* @param dialect The dialect to use, either omitted or `'jsonc'` for JSON with comments.
|
|
10
|
+
*/
|
|
11
|
+
export declare const json: (dialect?: "jsonc") => LanguageSupport;
|
|
12
|
+
/** Lint source for JSON */
|
|
13
|
+
export declare const jsonLinter: LintSource;
|
|
14
|
+
/** Lint source for JSONC */
|
|
15
|
+
export declare const jsoncLinter: LintSource;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { continuedIndent, indentNodeProp, foldNodeProp, foldInside, LRLanguage, LanguageSupport, } from '@codemirror/language';
|
|
2
|
+
import { lintJSON, lintJSONC } from '@bhsd/common';
|
|
3
|
+
import { parser } from './parser.js';
|
|
4
|
+
const props = [
|
|
5
|
+
indentNodeProp.add({
|
|
6
|
+
Object: continuedIndent({ except: /^\s*\}/u }),
|
|
7
|
+
Array: continuedIndent({ except: /^\s*\]/u }),
|
|
8
|
+
}),
|
|
9
|
+
foldNodeProp.add({
|
|
10
|
+
'Object Array': foldInside,
|
|
11
|
+
}),
|
|
12
|
+
], languageData = {
|
|
13
|
+
closeBrackets: { brackts: ['[', '{', '"'] },
|
|
14
|
+
indentOnInput: /^\s*[}\]]$/u,
|
|
15
|
+
};
|
|
16
|
+
/** LR language for JSON. */
|
|
17
|
+
export const jsonLanguage = LRLanguage.define({
|
|
18
|
+
name: 'json',
|
|
19
|
+
parser: parser.configure({ props }),
|
|
20
|
+
languageData,
|
|
21
|
+
});
|
|
22
|
+
/** LR language for JSONC. */
|
|
23
|
+
export const jsoncLanguage = LRLanguage.define({
|
|
24
|
+
name: 'jsonc',
|
|
25
|
+
parser: parser.configure({ props, dialect: 'jsonc' }),
|
|
26
|
+
languageData: {
|
|
27
|
+
...languageData,
|
|
28
|
+
commentTokens: {
|
|
29
|
+
line: '//',
|
|
30
|
+
block: { open: '/*', close: '*/' },
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
/**
|
|
35
|
+
* Get language support for JSON or JSONC.
|
|
36
|
+
* @param dialect The dialect to use, either omitted or `'jsonc'` for JSON with comments.
|
|
37
|
+
*/
|
|
38
|
+
export const json = (dialect) => new LanguageSupport(dialect === 'jsonc' ? jsoncLanguage : jsonLanguage);
|
|
39
|
+
const getLintSource = (lint) => ({ state: { doc } }) => lint(doc.toString())
|
|
40
|
+
.map(({ message, from, to = from, severity }) => ({ message, severity, from, to }));
|
|
41
|
+
/** Lint source for JSON */
|
|
42
|
+
export const jsonLinter = /* @__PURE__ */ getLintSource(lintJSON);
|
|
43
|
+
/** Lint source for JSONC */
|
|
44
|
+
export const jsoncLinter = /* @__PURE__ */ getLintSource(lintJSONC);
|
package/dist/parser.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// This file was generated by lezer-generator. You probably shouldn't edit it.
|
|
2
|
+
import {LRParser, LocalTokenGroup} from "@lezer/lr"
|
|
3
|
+
import {jsonHighlighting} from "./highlight.js"
|
|
4
|
+
export const parser = LRParser.deserialize({
|
|
5
|
+
version: 14,
|
|
6
|
+
states: "&YO]QPOOPtOPOOOOQO'#Cd'#CdOyQPO'#CgO!UQPO'#CoOOQO'#Cy'#CyQOQPOOP!`OSO'#C^POOO)C>j)C>jOOQO'#Ci'#CiO!hQPO'#ChO!mQQO'#C{OOQO'#C{'#C{OOQO,59R,59RO!uQPO,59RO!zQQO'#C|OOQO'#C|'#C|OOQO,59Z,59ZO#SQPO,59ZPOOO'#Cp'#CpP#XOSO,58xPOOO,58x,58xO]QPO,59SO#aQPO,59gO#iQQO,59gOOQO1G.m1G.mO#qQPO,59hO#xQQO,59hOOQO1G.u1G.uPOOO-E6n-E6nPOOO1G.d1G.dOOQO1G.n1G.nOOQO,59],59]O$QQPO1G/ROOQO-E6o-E6oOOQO,59^,59^O$YQPO1G/SOOQO-E6p-E6pP!PQPO'#CqP]QPO'#Cr",
|
|
7
|
+
stateData: "$e~OiOSPOSjPQ~OSTOTTOUTOVTOYRObSOnQO~OjVO~OX]O`[OnXO~O``OaaO~P]OkcOleO~O^fO~O_gOXoX~OXiO~O_jOapX~OalO~OkcOlnO~OnXOXoa~O_qOXoa~Oapa~P]O_tOapa~OnXOXoi~Oapi~P]OPj`~",
|
|
8
|
+
goto: "#TqPPrPPPPPuPPu}!VPPPPPu!]!c!iPPPPPP!oP!}#QRWP]TOSfjtwQZRVpgqvXYRgqvQdVRmdQhZRrhQk_RukQUOQ_SQofVsjtwR^RRbS",
|
|
9
|
+
nodeNames: "⚠ LineComment BlockComment JsonText True False Null Number String } { Object Property PropertyName : , , ] [ Array",
|
|
10
|
+
maxTerm: 32,
|
|
11
|
+
nodeProps: [
|
|
12
|
+
["isolate", -2,8,13,""],
|
|
13
|
+
["openedBy", 9,"{",17,"["],
|
|
14
|
+
["closedBy", 10,"}",18,"]"]
|
|
15
|
+
],
|
|
16
|
+
propSources: [jsonHighlighting],
|
|
17
|
+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description": "Lezer-based JSON/JSONC grammar",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"codemirror",
|
|
7
|
+
"lezer",
|
|
8
|
+
"json",
|
|
9
|
+
"jsonc"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/bhsd-harry/lezer-json#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/bhsd-harry/lezer-json/issues"
|
|
14
|
+
},
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"contributors": [
|
|
17
|
+
"Arun Srinivasan <rulfzid@gmail.com>",
|
|
18
|
+
"Marijn Haverbeke <marijn@haverbeke.berlin>",
|
|
19
|
+
"Bhsd"
|
|
20
|
+
],
|
|
21
|
+
"files": [
|
|
22
|
+
"dist/"
|
|
23
|
+
],
|
|
24
|
+
"browser": "dist/index.js",
|
|
25
|
+
"main": "dist/index.js",
|
|
26
|
+
"types": "dist/index.d.ts",
|
|
27
|
+
"type": "module",
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/bhsd-harry/lezer-json.git"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"ls": "npm i --package-lock-only && npm ls --package-lock-only --all --omit=dev",
|
|
35
|
+
"prepublishOnly": "npm run build",
|
|
36
|
+
"build:parser": "lezer-generator src/json.grammar -o src/parser.js",
|
|
37
|
+
"build:core": "tsc && cp src/parser.js dist/",
|
|
38
|
+
"build": "npm run build:parser && npm run build:core",
|
|
39
|
+
"lint:ts": "tsc --noEmit && eslint --cache .",
|
|
40
|
+
"lint:md": "markdownlint-cli2 '**/*.md'",
|
|
41
|
+
"lint": "npm run lint:ts && npm run lint:md"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@bhsd/common": "^2.2.1",
|
|
45
|
+
"@codemirror/language": "^6.12.3",
|
|
46
|
+
"@codemirror/lint": "^6.9.5"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@bhsd/code-standard": "^2.4.2",
|
|
50
|
+
"@lezer/generator": "^1.8.0",
|
|
51
|
+
"@typescript-eslint/parser": "^8.59.2",
|
|
52
|
+
"eslint": "^10.3.0",
|
|
53
|
+
"markdownlint-cli2": "^0.22.1",
|
|
54
|
+
"typescript": "^6.0.3"
|
|
55
|
+
}
|
|
56
|
+
}
|