@speclynx/apidom-parser-adapter-json 4.0.1 → 4.0.3
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/CHANGELOG.md +10 -0
- package/package.json +6 -7
- package/src/adapter.cjs +71 -0
- package/src/adapter.mjs +61 -0
- package/src/adapter.ts +80 -0
- package/src/media-types.cjs +20 -0
- package/src/media-types.mjs +16 -0
- package/src/media-types.ts +17 -0
- package/src/native/index.cjs +37 -0
- package/src/native/index.mjs +32 -0
- package/src/native/index.ts +36 -0
- package/src/tree-sitter/index.cjs +56 -0
- package/src/tree-sitter/index.mjs +50 -0
- package/src/tree-sitter/index.ts +57 -0
- package/src/tree-sitter/lexical-analysis/index.cjs +43 -0
- package/src/tree-sitter/lexical-analysis/index.mjs +39 -0
- package/src/tree-sitter/lexical-analysis/index.ts +48 -0
- package/src/tree-sitter/syntactic-analysis/index.cjs +280 -0
- package/src/tree-sitter/syntactic-analysis/index.mjs +276 -0
- package/src/tree-sitter/syntactic-analysis/index.ts +350 -0
- package/src/wasm.d.ts +18 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,16 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [4.0.3](https://github.com/speclynx/apidom/compare/v4.0.2...v4.0.3) (2026-03-11)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- **release:** fix v4.0.2 failed release ([b4dc1c4](https://github.com/speclynx/apidom/commit/b4dc1c48e8d9b2986a70e49b5554eb0a166d7528))
|
|
11
|
+
|
|
12
|
+
## [4.0.2](https://github.com/speclynx/apidom/compare/v4.0.1...v4.0.2) (2026-03-11)
|
|
13
|
+
|
|
14
|
+
**Note:** Version bump only for package @speclynx/apidom-parser-adapter-json
|
|
15
|
+
|
|
6
16
|
## [4.0.1](https://github.com/speclynx/apidom/compare/v4.0.0...v4.0.1) (2026-03-11)
|
|
7
17
|
|
|
8
18
|
**Note:** Version bump only for package @speclynx/apidom-parser-adapter-json
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@speclynx/apidom-parser-adapter-json",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.3",
|
|
4
4
|
"description": "Parser adapter for parsing JSON documents into base namespace.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"apidom",
|
|
@@ -58,9 +58,9 @@
|
|
|
58
58
|
"license": "Apache-2.0",
|
|
59
59
|
"dependencies": {
|
|
60
60
|
"@babel/runtime-corejs3": "^7.28.4",
|
|
61
|
-
"@speclynx/apidom-core": "4.0.
|
|
62
|
-
"@speclynx/apidom-datamodel": "4.0.
|
|
63
|
-
"@speclynx/apidom-error": "4.0.
|
|
61
|
+
"@speclynx/apidom-core": "4.0.3",
|
|
62
|
+
"@speclynx/apidom-datamodel": "4.0.3",
|
|
63
|
+
"@speclynx/apidom-error": "4.0.3",
|
|
64
64
|
"web-tree-sitter": "=0.26.6"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
@@ -68,8 +68,7 @@
|
|
|
68
68
|
"tree-sitter-json": "=0.24.8"
|
|
69
69
|
},
|
|
70
70
|
"files": [
|
|
71
|
-
"src
|
|
72
|
-
"src/**/*.cjs",
|
|
71
|
+
"src/",
|
|
73
72
|
"dist/",
|
|
74
73
|
"types/apidom-parser-adapter-json.d.ts",
|
|
75
74
|
"LICENSES",
|
|
@@ -77,5 +76,5 @@
|
|
|
77
76
|
"README.md",
|
|
78
77
|
"CHANGELOG.md"
|
|
79
78
|
],
|
|
80
|
-
"gitHead": "
|
|
79
|
+
"gitHead": "6ccfa09c02232516215e7de3ead276641957e626"
|
|
81
80
|
}
|
package/src/adapter.cjs
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
|
4
|
+
var _interopRequireWildcard = require("@babel/runtime-corejs3/helpers/interopRequireWildcard").default;
|
|
5
|
+
exports.__esModule = true;
|
|
6
|
+
exports.syntacticAnalysis = exports.parse = exports.namespace = exports.mediaTypes = exports.lexicalAnalysis = exports.detectionRegExp = exports.detect = void 0;
|
|
7
|
+
var _apidomDatamodel = require("@speclynx/apidom-datamodel");
|
|
8
|
+
var _apidomError = require("@speclynx/apidom-error");
|
|
9
|
+
var native = _interopRequireWildcard(require("./native/index.cjs"));
|
|
10
|
+
var treeSitter = _interopRequireWildcard(require("./tree-sitter/index.cjs"));
|
|
11
|
+
exports.lexicalAnalysis = treeSitter.lexicalAnalysis;
|
|
12
|
+
exports.syntacticAnalysis = treeSitter.syntacticAnalysis;
|
|
13
|
+
var _mediaTypes = _interopRequireDefault(require("./media-types.cjs"));
|
|
14
|
+
exports.mediaTypes = _mediaTypes.default;
|
|
15
|
+
/**
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
const namespace = exports.namespace = new _apidomDatamodel.Namespace();
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
const detectionRegExp = exports.detectionRegExp = treeSitter.detectionRegExp;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
const detect = async (source, {
|
|
37
|
+
strict = false
|
|
38
|
+
} = {}) => {
|
|
39
|
+
if (strict) {
|
|
40
|
+
return native.detect(source);
|
|
41
|
+
}
|
|
42
|
+
return treeSitter.detect(source);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
exports.detect = detect;
|
|
49
|
+
/**
|
|
50
|
+
* @public
|
|
51
|
+
*/
|
|
52
|
+
const parse = async (source, {
|
|
53
|
+
sourceMap = false,
|
|
54
|
+
style = false,
|
|
55
|
+
strict = false
|
|
56
|
+
} = {}) => {
|
|
57
|
+
if (strict && sourceMap) {
|
|
58
|
+
throw new _apidomError.UnsupportedOperationError('Cannot use sourceMap with strict parsing. Strict parsing does not support source maps.');
|
|
59
|
+
}
|
|
60
|
+
if (strict && style) {
|
|
61
|
+
throw new _apidomError.UnsupportedOperationError('Cannot use style with strict parsing. Strict parsing does not support style preservation.');
|
|
62
|
+
}
|
|
63
|
+
if (strict) {
|
|
64
|
+
return native.parse(source);
|
|
65
|
+
}
|
|
66
|
+
return treeSitter.parse(source, {
|
|
67
|
+
sourceMap,
|
|
68
|
+
style
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
exports.parse = parse;
|
package/src/adapter.mjs
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Namespace } from '@speclynx/apidom-datamodel';
|
|
2
|
+
import { UnsupportedOperationError } from '@speclynx/apidom-error';
|
|
3
|
+
import * as native from "./native/index.mjs";
|
|
4
|
+
import * as treeSitter from "./tree-sitter/index.mjs";
|
|
5
|
+
export { lexicalAnalysis, syntacticAnalysis } from "./tree-sitter/index.mjs";
|
|
6
|
+
/**
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export { default as mediaTypes } from "./media-types.mjs";
|
|
10
|
+
/**
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export const namespace = new Namespace();
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
export const detectionRegExp = treeSitter.detectionRegExp;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
export const detect = async (source, {
|
|
28
|
+
strict = false
|
|
29
|
+
} = {}) => {
|
|
30
|
+
if (strict) {
|
|
31
|
+
return native.detect(source);
|
|
32
|
+
}
|
|
33
|
+
return treeSitter.detect(source);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @public
|
|
42
|
+
*/
|
|
43
|
+
export const parse = async (source, {
|
|
44
|
+
sourceMap = false,
|
|
45
|
+
style = false,
|
|
46
|
+
strict = false
|
|
47
|
+
} = {}) => {
|
|
48
|
+
if (strict && sourceMap) {
|
|
49
|
+
throw new UnsupportedOperationError('Cannot use sourceMap with strict parsing. Strict parsing does not support source maps.');
|
|
50
|
+
}
|
|
51
|
+
if (strict && style) {
|
|
52
|
+
throw new UnsupportedOperationError('Cannot use style with strict parsing. Strict parsing does not support style preservation.');
|
|
53
|
+
}
|
|
54
|
+
if (strict) {
|
|
55
|
+
return native.parse(source);
|
|
56
|
+
}
|
|
57
|
+
return treeSitter.parse(source, {
|
|
58
|
+
sourceMap,
|
|
59
|
+
style
|
|
60
|
+
});
|
|
61
|
+
};
|
package/src/adapter.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { ParseResultElement, Namespace } from '@speclynx/apidom-datamodel';
|
|
2
|
+
import { UnsupportedOperationError } from '@speclynx/apidom-error';
|
|
3
|
+
|
|
4
|
+
import * as native from './native/index.ts';
|
|
5
|
+
import * as treeSitter from './tree-sitter/index.ts';
|
|
6
|
+
|
|
7
|
+
export type { JSONMediaTypes } from './media-types.ts';
|
|
8
|
+
export type { Tree } from './tree-sitter/index.ts';
|
|
9
|
+
|
|
10
|
+
export { lexicalAnalysis, syntacticAnalysis } from './tree-sitter/index.ts';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
export { default as mediaTypes } from './media-types.ts';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
export const namespace = new Namespace();
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
export const detectionRegExp = treeSitter.detectionRegExp;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
export interface ParseDetectOptions {
|
|
31
|
+
strict?: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @public
|
|
36
|
+
*/
|
|
37
|
+
export const detect = async (
|
|
38
|
+
source: string,
|
|
39
|
+
{ strict = false }: ParseDetectOptions = {},
|
|
40
|
+
): Promise<boolean> => {
|
|
41
|
+
if (strict) {
|
|
42
|
+
return native.detect(source);
|
|
43
|
+
}
|
|
44
|
+
return treeSitter.detect(source);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @public
|
|
49
|
+
*/
|
|
50
|
+
export interface ParseOptions {
|
|
51
|
+
sourceMap?: boolean;
|
|
52
|
+
style?: boolean;
|
|
53
|
+
strict?: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @public
|
|
58
|
+
*/
|
|
59
|
+
export const parse = async (
|
|
60
|
+
source: string,
|
|
61
|
+
{ sourceMap = false, style = false, strict = false }: ParseOptions = {},
|
|
62
|
+
): Promise<ParseResultElement> => {
|
|
63
|
+
if (strict && sourceMap) {
|
|
64
|
+
throw new UnsupportedOperationError(
|
|
65
|
+
'Cannot use sourceMap with strict parsing. Strict parsing does not support source maps.',
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (strict && style) {
|
|
70
|
+
throw new UnsupportedOperationError(
|
|
71
|
+
'Cannot use style with strict parsing. Strict parsing does not support style preservation.',
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (strict) {
|
|
76
|
+
return native.parse(source);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return treeSitter.parse(source, { sourceMap, style });
|
|
80
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = exports.JSONMediaTypes = void 0;
|
|
5
|
+
var _apidomCore = require("@speclynx/apidom-core");
|
|
6
|
+
/**
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
class JSONMediaTypes extends _apidomCore.MediaTypes {
|
|
10
|
+
latest() {
|
|
11
|
+
return this[0];
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
exports.JSONMediaTypes = JSONMediaTypes;
|
|
19
|
+
const mediaTypes = new JSONMediaTypes('application/json');
|
|
20
|
+
var _default = exports.default = mediaTypes;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { MediaTypes } from '@speclynx/apidom-core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export class JSONMediaTypes extends MediaTypes {
|
|
7
|
+
latest() {
|
|
8
|
+
return this[0];
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
const mediaTypes = new JSONMediaTypes('application/json');
|
|
16
|
+
export default mediaTypes;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { MediaTypes } from '@speclynx/apidom-core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export class JSONMediaTypes extends MediaTypes<string> {
|
|
7
|
+
latest() {
|
|
8
|
+
return this[0];
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
const mediaTypes = new JSONMediaTypes('application/json');
|
|
16
|
+
|
|
17
|
+
export default mediaTypes;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.parse = exports.detect = void 0;
|
|
5
|
+
var _apidomDatamodel = require("@speclynx/apidom-datamodel");
|
|
6
|
+
/**
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
const detect = async source => {
|
|
10
|
+
if (source.trim().length === 0) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
try {
|
|
14
|
+
JSON.parse(source);
|
|
15
|
+
return true;
|
|
16
|
+
} catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @public
|
|
23
|
+
*/
|
|
24
|
+
exports.detect = detect;
|
|
25
|
+
const parse = async source => {
|
|
26
|
+
const parseResult = new _apidomDatamodel.ParseResultElement();
|
|
27
|
+
if (source.trim().length === 0) {
|
|
28
|
+
return parseResult;
|
|
29
|
+
}
|
|
30
|
+
let pojo = JSON.parse(source);
|
|
31
|
+
const element = (0, _apidomDatamodel.refract)(pojo);
|
|
32
|
+
pojo = null; // allow GC to reclaim POJO
|
|
33
|
+
element.classes.push('result');
|
|
34
|
+
parseResult.push(element);
|
|
35
|
+
return parseResult;
|
|
36
|
+
};
|
|
37
|
+
exports.parse = parse;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ParseResultElement, refract } from '@speclynx/apidom-datamodel';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export const detect = async source => {
|
|
7
|
+
if (source.trim().length === 0) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
try {
|
|
11
|
+
JSON.parse(source);
|
|
12
|
+
return true;
|
|
13
|
+
} catch {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
export const parse = async source => {
|
|
22
|
+
const parseResult = new ParseResultElement();
|
|
23
|
+
if (source.trim().length === 0) {
|
|
24
|
+
return parseResult;
|
|
25
|
+
}
|
|
26
|
+
let pojo = JSON.parse(source);
|
|
27
|
+
const element = refract(pojo);
|
|
28
|
+
pojo = null; // allow GC to reclaim POJO
|
|
29
|
+
element.classes.push('result');
|
|
30
|
+
parseResult.push(element);
|
|
31
|
+
return parseResult;
|
|
32
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ParseResultElement, refract } from '@speclynx/apidom-datamodel';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export const detect = async (source: string): Promise<boolean> => {
|
|
7
|
+
if (source.trim().length === 0) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
JSON.parse(source);
|
|
13
|
+
return true;
|
|
14
|
+
} catch {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
export const parse = async (source: string): Promise<ParseResultElement> => {
|
|
23
|
+
const parseResult = new ParseResultElement();
|
|
24
|
+
|
|
25
|
+
if (source.trim().length === 0) {
|
|
26
|
+
return parseResult;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let pojo: unknown = JSON.parse(source);
|
|
30
|
+
const element = refract(pojo);
|
|
31
|
+
pojo = null; // allow GC to reclaim POJO
|
|
32
|
+
element.classes.push('result');
|
|
33
|
+
parseResult.push(element);
|
|
34
|
+
|
|
35
|
+
return parseResult;
|
|
36
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
|
4
|
+
exports.__esModule = true;
|
|
5
|
+
exports.parse = exports.detectionRegExp = exports.detect = void 0;
|
|
6
|
+
var _index = _interopRequireDefault(require("./lexical-analysis/index.cjs"));
|
|
7
|
+
exports.lexicalAnalysis = _index.default;
|
|
8
|
+
var _index2 = _interopRequireDefault(require("./syntactic-analysis/index.cjs"));
|
|
9
|
+
exports.syntacticAnalysis = _index2.default;
|
|
10
|
+
/**
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
const detectionRegExp = exports.detectionRegExp =
|
|
14
|
+
// eslint-disable-next-line no-control-regex
|
|
15
|
+
/(?<true>^\s*true\s*$)|(?<false>^\s*false\s*$)|(?<null>^\s*null\s*$)|(?<number>^\s*\d+\s*$)|(?<object>^\s*{\s*)|(?<array>^\s*\[\s*)|(?<string>^\s*"(((?=\\)\\(["\\/bfnrt]|u[0-9a-fA-F]{4}))|[^"\\\x00-\x1F\x7F])*"\s*$)/;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
const detect = async source => {
|
|
21
|
+
if (!detectionRegExp.test(source)) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
let cst = null;
|
|
25
|
+
try {
|
|
26
|
+
cst = await (0, _index.default)(source);
|
|
27
|
+
return cst.rootNode.type !== 'ERROR';
|
|
28
|
+
} catch {
|
|
29
|
+
return false;
|
|
30
|
+
} finally {
|
|
31
|
+
cst?.delete();
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
exports.detect = detect;
|
|
39
|
+
/**
|
|
40
|
+
* @public
|
|
41
|
+
*/
|
|
42
|
+
const parse = async (source, {
|
|
43
|
+
sourceMap = false,
|
|
44
|
+
style = false
|
|
45
|
+
} = {}) => {
|
|
46
|
+
const cst = await (0, _index.default)(source);
|
|
47
|
+
try {
|
|
48
|
+
return (0, _index2.default)(cst, {
|
|
49
|
+
sourceMap,
|
|
50
|
+
style
|
|
51
|
+
});
|
|
52
|
+
} finally {
|
|
53
|
+
cst.delete();
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
exports.parse = parse;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import lexicalAnalysis from "./lexical-analysis/index.mjs";
|
|
2
|
+
import syntacticAnalysis from "./syntactic-analysis/index.mjs";
|
|
3
|
+
export { lexicalAnalysis, syntacticAnalysis };
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
export const detectionRegExp =
|
|
9
|
+
// eslint-disable-next-line no-control-regex
|
|
10
|
+
/(?<true>^\s*true\s*$)|(?<false>^\s*false\s*$)|(?<null>^\s*null\s*$)|(?<number>^\s*\d+\s*$)|(?<object>^\s*{\s*)|(?<array>^\s*\[\s*)|(?<string>^\s*"(((?=\\)\\(["\\/bfnrt]|u[0-9a-fA-F]{4}))|[^"\\\x00-\x1F\x7F])*"\s*$)/;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
export const detect = async source => {
|
|
16
|
+
if (!detectionRegExp.test(source)) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
let cst = null;
|
|
20
|
+
try {
|
|
21
|
+
cst = await lexicalAnalysis(source);
|
|
22
|
+
return cst.rootNode.type !== 'ERROR';
|
|
23
|
+
} catch {
|
|
24
|
+
return false;
|
|
25
|
+
} finally {
|
|
26
|
+
cst?.delete();
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @public
|
|
36
|
+
*/
|
|
37
|
+
export const parse = async (source, {
|
|
38
|
+
sourceMap = false,
|
|
39
|
+
style = false
|
|
40
|
+
} = {}) => {
|
|
41
|
+
const cst = await lexicalAnalysis(source);
|
|
42
|
+
try {
|
|
43
|
+
return syntacticAnalysis(cst, {
|
|
44
|
+
sourceMap,
|
|
45
|
+
style
|
|
46
|
+
});
|
|
47
|
+
} finally {
|
|
48
|
+
cst.delete();
|
|
49
|
+
}
|
|
50
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { ParseResultElement } from '@speclynx/apidom-datamodel';
|
|
2
|
+
import type { Tree } from 'web-tree-sitter';
|
|
3
|
+
|
|
4
|
+
import lexicalAnalysis from './lexical-analysis/index.ts';
|
|
5
|
+
import syntacticAnalysis from './syntactic-analysis/index.ts';
|
|
6
|
+
|
|
7
|
+
export type { Tree };
|
|
8
|
+
export { lexicalAnalysis, syntacticAnalysis };
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export const detectionRegExp =
|
|
14
|
+
// eslint-disable-next-line no-control-regex
|
|
15
|
+
/(?<true>^\s*true\s*$)|(?<false>^\s*false\s*$)|(?<null>^\s*null\s*$)|(?<number>^\s*\d+\s*$)|(?<object>^\s*{\s*)|(?<array>^\s*\[\s*)|(?<string>^\s*"(((?=\\)\\(["\\/bfnrt]|u[0-9a-fA-F]{4}))|[^"\\\x00-\x1F\x7F])*"\s*$)/;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
export const detect = async (source: string): Promise<boolean> => {
|
|
21
|
+
if (!detectionRegExp.test(source)) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let cst: Tree | null = null;
|
|
26
|
+
try {
|
|
27
|
+
cst = await lexicalAnalysis(source);
|
|
28
|
+
return cst.rootNode.type !== 'ERROR';
|
|
29
|
+
} catch {
|
|
30
|
+
return false;
|
|
31
|
+
} finally {
|
|
32
|
+
cst?.delete();
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
39
|
+
export interface ParseOptions {
|
|
40
|
+
sourceMap?: boolean;
|
|
41
|
+
style?: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @public
|
|
46
|
+
*/
|
|
47
|
+
export const parse = async (
|
|
48
|
+
source: string,
|
|
49
|
+
{ sourceMap = false, style = false }: ParseOptions = {},
|
|
50
|
+
): Promise<ParseResultElement> => {
|
|
51
|
+
const cst = await lexicalAnalysis(source);
|
|
52
|
+
try {
|
|
53
|
+
return syntacticAnalysis(cst, { sourceMap, style });
|
|
54
|
+
} finally {
|
|
55
|
+
cst.delete();
|
|
56
|
+
}
|
|
57
|
+
};
|