@sinclair/typebox 0.33.19 → 0.33.21
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/build/cjs/index.d.ts +1 -0
- package/build/cjs/index.js +5 -1
- package/build/cjs/parse/index.d.ts +1 -0
- package/build/cjs/parse/index.js +18 -0
- package/build/cjs/parse/parse.d.ts +11 -0
- package/build/cjs/parse/parse.js +18 -0
- package/build/cjs/parse/parsebox/index.d.ts +2 -0
- package/build/cjs/parse/parsebox/index.js +6 -0
- package/build/cjs/parse/parsebox/runtime/guard.d.ts +17 -0
- package/build/cjs/parse/parsebox/runtime/guard.js +75 -0
- package/build/cjs/parse/parsebox/runtime/index.d.ts +5 -0
- package/build/cjs/parse/parsebox/runtime/index.js +23 -0
- package/build/cjs/parse/parsebox/runtime/module.d.ts +9 -0
- package/build/cjs/parse/parsebox/runtime/module.js +22 -0
- package/build/cjs/parse/parsebox/runtime/parse.d.ts +9 -0
- package/build/cjs/parse/parsebox/runtime/parse.js +99 -0
- package/build/cjs/parse/parsebox/runtime/token.d.ts +8 -0
- package/build/cjs/parse/parsebox/runtime/token.js +230 -0
- package/build/cjs/parse/parsebox/runtime/types.d.ts +68 -0
- package/build/cjs/parse/parsebox/runtime/types.js +45 -0
- package/build/cjs/parse/parsebox/static/index.d.ts +3 -0
- package/build/cjs/parse/parsebox/static/index.js +21 -0
- package/build/cjs/parse/parsebox/static/parse.d.ts +17 -0
- package/build/cjs/parse/parsebox/static/parse.js +3 -0
- package/build/cjs/parse/parsebox/static/token.d.ts +108 -0
- package/build/cjs/parse/parsebox/static/token.js +3 -0
- package/build/cjs/parse/parsebox/static/types.d.ts +46 -0
- package/build/cjs/parse/parsebox/static/types.js +3 -0
- package/build/cjs/parse/runtime.d.ts +55 -0
- package/build/cjs/parse/runtime.js +621 -0
- package/build/cjs/parse/static.d.ts +471 -0
- package/build/cjs/parse/static.js +3 -0
- package/build/esm/index.d.mts +1 -0
- package/build/esm/index.mjs +5 -1
- package/build/esm/parse/index.d.mts +1 -0
- package/build/esm/parse/index.mjs +1 -0
- package/build/esm/parse/parse.d.mts +11 -0
- package/build/esm/parse/parse.mjs +13 -0
- package/build/esm/parse/parsebox/index.d.mts +2 -0
- package/build/esm/parse/parsebox/index.mjs +2 -0
- package/build/esm/parse/parsebox/runtime/guard.d.mts +17 -0
- package/build/esm/parse/parsebox/runtime/guard.mjs +64 -0
- package/build/esm/parse/parsebox/runtime/index.d.mts +5 -0
- package/build/esm/parse/parsebox/runtime/index.mjs +5 -0
- package/build/esm/parse/parsebox/runtime/module.d.mts +9 -0
- package/build/esm/parse/parsebox/runtime/module.mjs +17 -0
- package/build/esm/parse/parsebox/runtime/parse.d.mts +9 -0
- package/build/esm/parse/parsebox/runtime/parse.mjs +95 -0
- package/build/esm/parse/parsebox/runtime/token.d.mts +8 -0
- package/build/esm/parse/parsebox/runtime/token.mjs +223 -0
- package/build/esm/parse/parsebox/runtime/types.d.mts +68 -0
- package/build/esm/parse/parsebox/runtime/types.mjs +32 -0
- package/build/esm/parse/parsebox/static/index.d.mts +3 -0
- package/build/esm/parse/parsebox/static/index.mjs +3 -0
- package/build/esm/parse/parsebox/static/parse.d.mts +17 -0
- package/build/esm/parse/parsebox/static/parse.mjs +1 -0
- package/build/esm/parse/parsebox/static/token.d.mts +108 -0
- package/build/esm/parse/parsebox/static/token.mjs +1 -0
- package/build/esm/parse/parsebox/static/types.d.mts +46 -0
- package/build/esm/parse/parsebox/static/types.mjs +1 -0
- package/build/esm/parse/runtime.d.mts +55 -0
- package/build/esm/parse/runtime.mjs +617 -0
- package/build/esm/parse/static.d.mts +471 -0
- package/build/esm/parse/static.mjs +1 -0
- package/package.json +11 -1
- package/parse/package.json +4 -0
- package/readme.md +5 -4
package/build/cjs/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export * from './type/patterns/index';
|
|
|
7
7
|
export * from './type/registry/index';
|
|
8
8
|
export * from './type/sets/index';
|
|
9
9
|
export * from './type/symbols/index';
|
|
10
|
+
export * from './parse/index';
|
|
10
11
|
export * from './type/any/index';
|
|
11
12
|
export * from './type/array/index';
|
|
12
13
|
export * from './type/async-iterator/index';
|
package/build/cjs/index.js
CHANGED
|
@@ -28,6 +28,10 @@ __exportStar(require("./type/registry/index"), exports);
|
|
|
28
28
|
__exportStar(require("./type/sets/index"), exports);
|
|
29
29
|
__exportStar(require("./type/symbols/index"), exports);
|
|
30
30
|
// ------------------------------------------------------------------
|
|
31
|
+
// Parse
|
|
32
|
+
// ------------------------------------------------------------------
|
|
33
|
+
__exportStar(require("./parse/index"), exports);
|
|
34
|
+
// ------------------------------------------------------------------
|
|
31
35
|
// Types
|
|
32
36
|
// ------------------------------------------------------------------
|
|
33
37
|
__exportStar(require("./type/any/index"), exports);
|
|
@@ -91,6 +95,6 @@ __exportStar(require("./type/unknown/index"), exports);
|
|
|
91
95
|
__exportStar(require("./type/unsafe/index"), exports);
|
|
92
96
|
__exportStar(require("./type/void/index"), exports);
|
|
93
97
|
// ------------------------------------------------------------------
|
|
94
|
-
//
|
|
98
|
+
// Type.*
|
|
95
99
|
// ------------------------------------------------------------------
|
|
96
100
|
__exportStar(require("./type/type/index"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './parse';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
15
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
16
|
+
};
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
__exportStar(require("./parse"), exports);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Static } from './parsebox/index';
|
|
2
|
+
import { TSchema, SchemaOptions } from '../type/schema/index';
|
|
3
|
+
import { Type } from './static';
|
|
4
|
+
/** `[Experimental]` Parses a TypeScript type annotation as an inferred TypeBox type */
|
|
5
|
+
export declare function Parse<Code extends string, Context extends Record<PropertyKey, TSchema> = {}>(context: Context, code: Code, options?: SchemaOptions): Static.Parse<Type, Code, Context>[0];
|
|
6
|
+
/** `[Experimental]` Parses a TypeScript type annotation as an inferred TypeBox type */
|
|
7
|
+
export declare function Parse<Code extends string>(code: Code, options?: SchemaOptions): Static.Parse<Type, Code, {}>[0];
|
|
8
|
+
/** `[Experimental]` Parses a TypeScript type annotation as TSchema */
|
|
9
|
+
export declare function ParseOnly<Code extends string, Context extends Record<PropertyKey, TSchema> = {}>(context: Context, code: Code, options?: SchemaOptions): TSchema | undefined;
|
|
10
|
+
/** `[Experimental]` Parses a TypeScript type annotation as TSchema */
|
|
11
|
+
export declare function ParseOnly<Code extends string>(code: Code, options?: SchemaOptions): TSchema | undefined;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Parse = Parse;
|
|
5
|
+
exports.ParseOnly = ParseOnly;
|
|
6
|
+
const type_1 = require("../type/create/type");
|
|
7
|
+
const runtime_1 = require("./runtime");
|
|
8
|
+
/** `[Experimental]` Parses a TypeScript type annotation as an inferred TypeBox type */
|
|
9
|
+
function Parse(...args) {
|
|
10
|
+
return ParseOnly.apply(null, args);
|
|
11
|
+
}
|
|
12
|
+
/** `[Experimental]` Parses a TypeScript type annotation as TSchema */
|
|
13
|
+
function ParseOnly(...args) {
|
|
14
|
+
const withContext = typeof args[0] === 'string' ? false : true;
|
|
15
|
+
const [context, code, options] = withContext ? [args[0], args[1], args[2] || {}] : [{}, args[0], args[1] || {}];
|
|
16
|
+
const type = runtime_1.Module.Parse('Type', code, context)[0];
|
|
17
|
+
return (type !== undefined ? (0, type_1.CreateType)(type, options) : undefined);
|
|
18
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { IIdent, INumber, IRef, IString, IConst, ITuple, IUnion } from './types';
|
|
2
|
+
/** Returns true if the value is a Tuple Parser */
|
|
3
|
+
export declare function IsTuple(value: unknown): value is ITuple;
|
|
4
|
+
/** Returns true if the value is a Union Parser */
|
|
5
|
+
export declare function IsUnion(value: unknown): value is IUnion;
|
|
6
|
+
/** Returns true if the value is a Const Parser */
|
|
7
|
+
export declare function IsConst(value: unknown): value is IConst;
|
|
8
|
+
/** Returns true if the value is a Ident Parser */
|
|
9
|
+
export declare function IsIdent(value: unknown): value is IIdent;
|
|
10
|
+
/** Returns true if the value is a Number Parser */
|
|
11
|
+
export declare function IsNumber(value: unknown): value is INumber;
|
|
12
|
+
/** Returns true if the value is a Ref Parser */
|
|
13
|
+
export declare function IsRef(value: unknown): value is IRef;
|
|
14
|
+
/** Returns true if the value is a String Parser */
|
|
15
|
+
export declare function IsString(value: unknown): value is IString;
|
|
16
|
+
/** Returns true if the value is a Parser */
|
|
17
|
+
export declare function IsParser(value: unknown): value is IUnion<unknown> | ITuple<unknown> | IConst<unknown> | IIdent<unknown> | INumber<unknown> | IRef<unknown> | IString<unknown>;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.IsTuple = IsTuple;
|
|
5
|
+
exports.IsUnion = IsUnion;
|
|
6
|
+
exports.IsConst = IsConst;
|
|
7
|
+
exports.IsIdent = IsIdent;
|
|
8
|
+
exports.IsNumber = IsNumber;
|
|
9
|
+
exports.IsRef = IsRef;
|
|
10
|
+
exports.IsString = IsString;
|
|
11
|
+
exports.IsParser = IsParser;
|
|
12
|
+
// ------------------------------------------------------------------
|
|
13
|
+
// Value Guard
|
|
14
|
+
// ------------------------------------------------------------------
|
|
15
|
+
// prettier-ignore
|
|
16
|
+
function HasPropertyKey(value, key) {
|
|
17
|
+
return key in value;
|
|
18
|
+
}
|
|
19
|
+
// prettier-ignore
|
|
20
|
+
function IsObjectValue(value) {
|
|
21
|
+
return typeof value === 'object' && value !== null;
|
|
22
|
+
}
|
|
23
|
+
// prettier-ignore
|
|
24
|
+
function IsArrayValue(value) {
|
|
25
|
+
return globalThis.Array.isArray(value);
|
|
26
|
+
}
|
|
27
|
+
// ------------------------------------------------------------------
|
|
28
|
+
// Parser Guard
|
|
29
|
+
// ------------------------------------------------------------------
|
|
30
|
+
/** Returns true if the value is a Tuple Parser */
|
|
31
|
+
// prettier-ignore
|
|
32
|
+
function IsTuple(value) {
|
|
33
|
+
return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Tuple' && HasPropertyKey(value, 'parsers') && IsArrayValue(value.parsers);
|
|
34
|
+
}
|
|
35
|
+
/** Returns true if the value is a Union Parser */
|
|
36
|
+
// prettier-ignore
|
|
37
|
+
function IsUnion(value) {
|
|
38
|
+
return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Union' && HasPropertyKey(value, 'parsers') && IsArrayValue(value.parsers);
|
|
39
|
+
}
|
|
40
|
+
/** Returns true if the value is a Const Parser */
|
|
41
|
+
// prettier-ignore
|
|
42
|
+
function IsConst(value) {
|
|
43
|
+
return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Const' && HasPropertyKey(value, 'value') && typeof value.value === 'string';
|
|
44
|
+
}
|
|
45
|
+
/** Returns true if the value is a Ident Parser */
|
|
46
|
+
// prettier-ignore
|
|
47
|
+
function IsIdent(value) {
|
|
48
|
+
return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Ident';
|
|
49
|
+
}
|
|
50
|
+
/** Returns true if the value is a Number Parser */
|
|
51
|
+
// prettier-ignore
|
|
52
|
+
function IsNumber(value) {
|
|
53
|
+
return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Number';
|
|
54
|
+
}
|
|
55
|
+
/** Returns true if the value is a Ref Parser */
|
|
56
|
+
// prettier-ignore
|
|
57
|
+
function IsRef(value) {
|
|
58
|
+
return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'Ref' && HasPropertyKey(value, 'ref') && typeof value.ref === 'string';
|
|
59
|
+
}
|
|
60
|
+
/** Returns true if the value is a String Parser */
|
|
61
|
+
// prettier-ignore
|
|
62
|
+
function IsString(value) {
|
|
63
|
+
return IsObjectValue(value) && HasPropertyKey(value, 'type') && value.type === 'String' && HasPropertyKey(value, 'options') && IsArrayValue(value.options);
|
|
64
|
+
}
|
|
65
|
+
/** Returns true if the value is a Parser */
|
|
66
|
+
// prettier-ignore
|
|
67
|
+
function IsParser(value) {
|
|
68
|
+
return (IsTuple(value) ||
|
|
69
|
+
IsUnion(value) ||
|
|
70
|
+
IsConst(value) ||
|
|
71
|
+
IsIdent(value) ||
|
|
72
|
+
IsNumber(value) ||
|
|
73
|
+
IsRef(value) ||
|
|
74
|
+
IsString(value));
|
|
75
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
15
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
16
|
+
};
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.Token = exports.Guard = void 0;
|
|
19
|
+
exports.Guard = require("./guard");
|
|
20
|
+
exports.Token = require("./token");
|
|
21
|
+
__exportStar(require("./types"), exports);
|
|
22
|
+
__exportStar(require("./module"), exports);
|
|
23
|
+
__exportStar(require("./parse"), exports);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as Types from './types';
|
|
2
|
+
export declare class Module<Properties extends Types.IModuleProperties = Types.IModuleProperties> {
|
|
3
|
+
private readonly properties;
|
|
4
|
+
constructor(properties: Properties);
|
|
5
|
+
/** Parses using one of the parsers defined on this instance */
|
|
6
|
+
Parse<Key extends keyof Properties>(key: Key, code: string, context: unknown): [] | [Types.StaticParser<Properties[Key]>, string];
|
|
7
|
+
/** Parses using one of the parsers defined on this instance */
|
|
8
|
+
Parse<Key extends keyof Properties>(key: Key, code: string): [] | [Types.StaticParser<Properties[Key]>, string];
|
|
9
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Module = void 0;
|
|
5
|
+
const parse_1 = require("./parse");
|
|
6
|
+
// ------------------------------------------------------------------
|
|
7
|
+
// Module
|
|
8
|
+
// ------------------------------------------------------------------
|
|
9
|
+
// prettier-ignore
|
|
10
|
+
class Module {
|
|
11
|
+
constructor(properties) {
|
|
12
|
+
this.properties = properties;
|
|
13
|
+
}
|
|
14
|
+
/** Parses using one of the parsers defined on this instance */
|
|
15
|
+
Parse(...args) {
|
|
16
|
+
const [key, code, context] = args.length === 3 ? [args[0], args[1], args[2]] :
|
|
17
|
+
args.length === 2 ? [args[0], args[1], undefined] :
|
|
18
|
+
(() => { throw Error('Invalid parse arguments'); })();
|
|
19
|
+
return (0, parse_1.Parse)(this.properties[key], this.properties, code, context);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.Module = Module;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as Types from './types';
|
|
2
|
+
/** Parses content using the given parser */
|
|
3
|
+
export declare function Parse<Parser extends Types.IParser>(parser: Parser, properties: Types.IModuleProperties, code: string, context: unknown): [] | [Types.StaticParser<Parser>, string];
|
|
4
|
+
/** Parses content using the given parser */
|
|
5
|
+
export declare function Parse<Parser extends Types.IParser>(parser: Parser, properties: Types.IModuleProperties, code: string): [] | [Types.StaticParser<Parser>, string];
|
|
6
|
+
/** Parses content using the given parser */
|
|
7
|
+
export declare function Parse<Parser extends Types.IParser>(parser: Parser, code: string, context: unknown): [] | [Types.StaticParser<Parser>, string];
|
|
8
|
+
/** Parses content using the given parser */
|
|
9
|
+
export declare function Parse<Parser extends Types.IParser>(parser: Parser, code: string): [] | [Types.StaticParser<Parser>, string];
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Parse = Parse;
|
|
5
|
+
const Guard = require("./guard");
|
|
6
|
+
const Token = require("./token");
|
|
7
|
+
// ------------------------------------------------------------------
|
|
8
|
+
// Tuple
|
|
9
|
+
// ------------------------------------------------------------------
|
|
10
|
+
// prettier-ignore
|
|
11
|
+
function ParseTuple(parsers, properties, code, context) {
|
|
12
|
+
const buffer = [];
|
|
13
|
+
let rest = code;
|
|
14
|
+
for (const parser of parsers) {
|
|
15
|
+
const result = ParseParser(parser, properties, rest, context);
|
|
16
|
+
if (result.length === 0)
|
|
17
|
+
return [];
|
|
18
|
+
buffer.push(result[0]);
|
|
19
|
+
rest = result[1];
|
|
20
|
+
}
|
|
21
|
+
return [buffer, rest];
|
|
22
|
+
}
|
|
23
|
+
// ------------------------------------------------------------------
|
|
24
|
+
// Union
|
|
25
|
+
// ------------------------------------------------------------------
|
|
26
|
+
// prettier-ignore
|
|
27
|
+
function ParseUnion(parsers, properties, code, context) {
|
|
28
|
+
for (const parser of parsers) {
|
|
29
|
+
const result = ParseParser(parser, properties, code, context);
|
|
30
|
+
if (result.length === 0)
|
|
31
|
+
continue;
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
36
|
+
// ------------------------------------------------------------------
|
|
37
|
+
// Const
|
|
38
|
+
// ------------------------------------------------------------------
|
|
39
|
+
// prettier-ignore
|
|
40
|
+
function ParseConst(value, code, context) {
|
|
41
|
+
return Token.Const(value, code);
|
|
42
|
+
}
|
|
43
|
+
// ------------------------------------------------------------------
|
|
44
|
+
// Ref
|
|
45
|
+
// ------------------------------------------------------------------
|
|
46
|
+
// prettier-ignore
|
|
47
|
+
function ParseRef(ref, properties, code, context) {
|
|
48
|
+
const parser = properties[ref];
|
|
49
|
+
if (!Guard.IsParser(parser))
|
|
50
|
+
throw Error(`Cannot dereference parser '${ref}'`);
|
|
51
|
+
return ParseParser(parser, properties, code, context);
|
|
52
|
+
}
|
|
53
|
+
// ------------------------------------------------------------------
|
|
54
|
+
// String
|
|
55
|
+
// ------------------------------------------------------------------
|
|
56
|
+
// prettier-ignore
|
|
57
|
+
function ParseString(options, code, _context) {
|
|
58
|
+
return Token.String(options, code);
|
|
59
|
+
}
|
|
60
|
+
// ------------------------------------------------------------------
|
|
61
|
+
// Number
|
|
62
|
+
// ------------------------------------------------------------------
|
|
63
|
+
// prettier-ignore
|
|
64
|
+
function ParseNumber(code, _context) {
|
|
65
|
+
return Token.Number(code);
|
|
66
|
+
}
|
|
67
|
+
// ------------------------------------------------------------------
|
|
68
|
+
// Ident
|
|
69
|
+
// ------------------------------------------------------------------
|
|
70
|
+
// prettier-ignore
|
|
71
|
+
function ParseIdent(code, _context) {
|
|
72
|
+
return Token.Ident(code);
|
|
73
|
+
}
|
|
74
|
+
// ------------------------------------------------------------------
|
|
75
|
+
// Parser
|
|
76
|
+
// ------------------------------------------------------------------
|
|
77
|
+
// prettier-ignore
|
|
78
|
+
function ParseParser(parser, properties, code, context) {
|
|
79
|
+
const result = (Guard.IsTuple(parser) ? ParseTuple(parser.parsers, properties, code, context) :
|
|
80
|
+
Guard.IsUnion(parser) ? ParseUnion(parser.parsers, properties, code, context) :
|
|
81
|
+
Guard.IsConst(parser) ? ParseConst(parser.value, code, context) :
|
|
82
|
+
Guard.IsRef(parser) ? ParseRef(parser.ref, properties, code, context) :
|
|
83
|
+
Guard.IsString(parser) ? ParseString(parser.options, code, context) :
|
|
84
|
+
Guard.IsIdent(parser) ? ParseIdent(code, context) :
|
|
85
|
+
Guard.IsNumber(parser) ? ParseNumber(code, context) :
|
|
86
|
+
[]);
|
|
87
|
+
return (result.length === 2
|
|
88
|
+
? [parser.mapping(result[0], context), result[1]]
|
|
89
|
+
: result);
|
|
90
|
+
}
|
|
91
|
+
/** Parses content using the given parser */
|
|
92
|
+
// prettier-ignore
|
|
93
|
+
function Parse(...args) {
|
|
94
|
+
const withProperties = typeof args[1] === 'string' ? false : true;
|
|
95
|
+
const [parser, properties, code, context] = withProperties
|
|
96
|
+
? [args[0], args[1], args[2], args[3]]
|
|
97
|
+
: [args[0], {}, args[1], args[2]];
|
|
98
|
+
return ParseParser(parser, properties, code, context);
|
|
99
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Takes the next constant string value skipping any whitespace */
|
|
2
|
+
export declare function Const(value: string, code: string): [] | [string, string];
|
|
3
|
+
/** Scans for the next Ident token */
|
|
4
|
+
export declare function Ident(code: string): [] | [string, string];
|
|
5
|
+
/** Scans for the next number token */
|
|
6
|
+
export declare function Number(code: string): [string, string] | [];
|
|
7
|
+
/** Scans the next Literal String value */
|
|
8
|
+
export declare function String(options: string[], code: string): [string, string] | [];
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Const = Const;
|
|
5
|
+
exports.Ident = Ident;
|
|
6
|
+
exports.Number = Number;
|
|
7
|
+
exports.String = String;
|
|
8
|
+
// ------------------------------------------------------------------
|
|
9
|
+
// Chars
|
|
10
|
+
// ------------------------------------------------------------------
|
|
11
|
+
// prettier-ignore
|
|
12
|
+
var Chars;
|
|
13
|
+
(function (Chars) {
|
|
14
|
+
/** Returns true if the char code is a whitespace */
|
|
15
|
+
function IsWhitespace(value) {
|
|
16
|
+
return value === 32;
|
|
17
|
+
}
|
|
18
|
+
Chars.IsWhitespace = IsWhitespace;
|
|
19
|
+
/** Returns true if the char code is a newline */
|
|
20
|
+
function IsNewline(value) {
|
|
21
|
+
return value === 10;
|
|
22
|
+
}
|
|
23
|
+
Chars.IsNewline = IsNewline;
|
|
24
|
+
/** Returns true if the char code is a alpha */
|
|
25
|
+
function IsAlpha(value) {
|
|
26
|
+
return ((value >= 65 && value <= 90) || // A-Z
|
|
27
|
+
(value >= 97 && value <= 122) // a-z
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
Chars.IsAlpha = IsAlpha;
|
|
31
|
+
/** Returns true if the char code is zero */
|
|
32
|
+
function IsZero(value) {
|
|
33
|
+
return value === 48;
|
|
34
|
+
}
|
|
35
|
+
Chars.IsZero = IsZero;
|
|
36
|
+
/** Returns true if the char code is non-zero */
|
|
37
|
+
function IsNonZero(value) {
|
|
38
|
+
return value >= 49 && value <= 57;
|
|
39
|
+
}
|
|
40
|
+
Chars.IsNonZero = IsNonZero;
|
|
41
|
+
/** Returns true if the char code is a digit */
|
|
42
|
+
function IsDigit(value) {
|
|
43
|
+
return (IsNonZero(value) ||
|
|
44
|
+
IsZero(value));
|
|
45
|
+
}
|
|
46
|
+
Chars.IsDigit = IsDigit;
|
|
47
|
+
/** Returns true if the char code is a dot */
|
|
48
|
+
function IsDot(value) {
|
|
49
|
+
return value === 46;
|
|
50
|
+
}
|
|
51
|
+
Chars.IsDot = IsDot;
|
|
52
|
+
/** Returns true if this char code is a underscore */
|
|
53
|
+
function IsUnderscore(value) {
|
|
54
|
+
return value === 95;
|
|
55
|
+
}
|
|
56
|
+
Chars.IsUnderscore = IsUnderscore;
|
|
57
|
+
/** Returns true if this char code is a dollar sign */
|
|
58
|
+
function IsDollarSign(value) {
|
|
59
|
+
return value === 36;
|
|
60
|
+
}
|
|
61
|
+
Chars.IsDollarSign = IsDollarSign;
|
|
62
|
+
})(Chars || (Chars = {}));
|
|
63
|
+
// ------------------------------------------------------------------
|
|
64
|
+
// Trim
|
|
65
|
+
// ------------------------------------------------------------------
|
|
66
|
+
// prettier-ignore
|
|
67
|
+
var Trim;
|
|
68
|
+
(function (Trim) {
|
|
69
|
+
/** Trims Whitespace and retains Newline, Tabspaces, etc. */
|
|
70
|
+
function TrimWhitespaceOnly(code) {
|
|
71
|
+
for (let i = 0; i < code.length; i++) {
|
|
72
|
+
if (Chars.IsWhitespace(code.charCodeAt(i)))
|
|
73
|
+
continue;
|
|
74
|
+
return code.slice(i);
|
|
75
|
+
}
|
|
76
|
+
return code;
|
|
77
|
+
}
|
|
78
|
+
Trim.TrimWhitespaceOnly = TrimWhitespaceOnly;
|
|
79
|
+
/** Trims Whitespace including Newline, Tabspaces, etc. */
|
|
80
|
+
function TrimAll(code) {
|
|
81
|
+
return code.trimStart();
|
|
82
|
+
}
|
|
83
|
+
Trim.TrimAll = TrimAll;
|
|
84
|
+
})(Trim || (Trim = {}));
|
|
85
|
+
// ------------------------------------------------------------------
|
|
86
|
+
// Const
|
|
87
|
+
// ------------------------------------------------------------------
|
|
88
|
+
/** Checks the value matches the next string */
|
|
89
|
+
// prettier-ignore
|
|
90
|
+
function NextTokenCheck(value, code) {
|
|
91
|
+
if (value.length > code.length)
|
|
92
|
+
return false;
|
|
93
|
+
for (let i = 0; i < value.length; i++) {
|
|
94
|
+
if (value.charCodeAt(i) !== code.charCodeAt(i))
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
/** Gets the next constant string value or empty if no match */
|
|
100
|
+
// prettier-ignore
|
|
101
|
+
function NextConst(value, code) {
|
|
102
|
+
return NextTokenCheck(value, code)
|
|
103
|
+
? [code.slice(0, value.length), code.slice(value.length)]
|
|
104
|
+
: [];
|
|
105
|
+
}
|
|
106
|
+
/** Takes the next constant string value skipping any whitespace */
|
|
107
|
+
// prettier-ignore
|
|
108
|
+
function Const(value, code) {
|
|
109
|
+
if (value.length === 0)
|
|
110
|
+
return ['', code];
|
|
111
|
+
const char_0 = value.charCodeAt(0);
|
|
112
|
+
return (Chars.IsNewline(char_0) ? NextConst(value, Trim.TrimWhitespaceOnly(code)) :
|
|
113
|
+
Chars.IsWhitespace(char_0) ? NextConst(value, code) :
|
|
114
|
+
NextConst(value, Trim.TrimAll(code)));
|
|
115
|
+
}
|
|
116
|
+
// ------------------------------------------------------------------
|
|
117
|
+
// Ident
|
|
118
|
+
// ------------------------------------------------------------------
|
|
119
|
+
// prettier-ignore
|
|
120
|
+
function IdentIsFirst(char) {
|
|
121
|
+
return (Chars.IsAlpha(char) ||
|
|
122
|
+
Chars.IsDollarSign(char) ||
|
|
123
|
+
Chars.IsUnderscore(char));
|
|
124
|
+
}
|
|
125
|
+
// prettier-ignore
|
|
126
|
+
function IdentIsRest(char) {
|
|
127
|
+
return (Chars.IsAlpha(char) ||
|
|
128
|
+
Chars.IsDigit(char) ||
|
|
129
|
+
Chars.IsDollarSign(char) ||
|
|
130
|
+
Chars.IsUnderscore(char));
|
|
131
|
+
}
|
|
132
|
+
// prettier-ignore
|
|
133
|
+
function NextIdent(code) {
|
|
134
|
+
if (!IdentIsFirst(code.charCodeAt(0)))
|
|
135
|
+
return [];
|
|
136
|
+
for (let i = 1; i < code.length; i++) {
|
|
137
|
+
const char = code.charCodeAt(i);
|
|
138
|
+
if (IdentIsRest(char))
|
|
139
|
+
continue;
|
|
140
|
+
const slice = code.slice(0, i);
|
|
141
|
+
const rest = code.slice(i);
|
|
142
|
+
return [slice, rest];
|
|
143
|
+
}
|
|
144
|
+
return [code, ''];
|
|
145
|
+
}
|
|
146
|
+
/** Scans for the next Ident token */
|
|
147
|
+
// prettier-ignore
|
|
148
|
+
function Ident(code) {
|
|
149
|
+
return NextIdent(Trim.TrimAll(code));
|
|
150
|
+
}
|
|
151
|
+
// ------------------------------------------------------------------
|
|
152
|
+
// Number
|
|
153
|
+
// ------------------------------------------------------------------
|
|
154
|
+
/** Checks that the next number is not a leading zero */
|
|
155
|
+
// prettier-ignore
|
|
156
|
+
function NumberLeadingZeroCheck(code, index) {
|
|
157
|
+
const char_0 = code.charCodeAt(index + 0);
|
|
158
|
+
const char_1 = code.charCodeAt(index + 1);
|
|
159
|
+
return ((
|
|
160
|
+
// 1-9
|
|
161
|
+
Chars.IsNonZero(char_0)) || (
|
|
162
|
+
// 0
|
|
163
|
+
Chars.IsZero(char_0) &&
|
|
164
|
+
!Chars.IsDigit(char_1)) || (
|
|
165
|
+
// 0.
|
|
166
|
+
Chars.IsZero(char_0) &&
|
|
167
|
+
Chars.IsDot(char_1)) || (
|
|
168
|
+
// .0
|
|
169
|
+
Chars.IsDot(char_0) &&
|
|
170
|
+
Chars.IsDigit(char_1)));
|
|
171
|
+
}
|
|
172
|
+
/** Gets the next number token */
|
|
173
|
+
// prettier-ignore
|
|
174
|
+
function NextNumber(code) {
|
|
175
|
+
const negated = code.charAt(0) === '-';
|
|
176
|
+
const index = negated ? 1 : 0;
|
|
177
|
+
if (!NumberLeadingZeroCheck(code, index)) {
|
|
178
|
+
return [];
|
|
179
|
+
}
|
|
180
|
+
const dash = negated ? '-' : '';
|
|
181
|
+
let hasDot = false;
|
|
182
|
+
for (let i = index; i < code.length; i++) {
|
|
183
|
+
const char_i = code.charCodeAt(i);
|
|
184
|
+
if (Chars.IsDigit(char_i)) {
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (Chars.IsDot(char_i)) {
|
|
188
|
+
if (hasDot) {
|
|
189
|
+
const slice = code.slice(index, i);
|
|
190
|
+
const rest = code.slice(i);
|
|
191
|
+
return [`${dash}${slice}`, rest];
|
|
192
|
+
}
|
|
193
|
+
hasDot = true;
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
const slice = code.slice(index, i);
|
|
197
|
+
const rest = code.slice(i);
|
|
198
|
+
return [`${dash}${slice}`, rest];
|
|
199
|
+
}
|
|
200
|
+
return [code, ''];
|
|
201
|
+
}
|
|
202
|
+
/** Scans for the next number token */
|
|
203
|
+
// prettier-ignore
|
|
204
|
+
function Number(code) {
|
|
205
|
+
return NextNumber(Trim.TrimAll(code));
|
|
206
|
+
}
|
|
207
|
+
// ------------------------------------------------------------------
|
|
208
|
+
// String
|
|
209
|
+
// ------------------------------------------------------------------
|
|
210
|
+
// prettier-ignore
|
|
211
|
+
function NextString(options, code) {
|
|
212
|
+
const first = code.charAt(0);
|
|
213
|
+
if (!options.includes(first))
|
|
214
|
+
return [];
|
|
215
|
+
const quote = first;
|
|
216
|
+
for (let i = 1; i < code.length; i++) {
|
|
217
|
+
const char = code.charAt(i);
|
|
218
|
+
if (char === quote) {
|
|
219
|
+
const slice = code.slice(1, i);
|
|
220
|
+
const rest = code.slice(i + 1);
|
|
221
|
+
return [slice, rest];
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return [];
|
|
225
|
+
}
|
|
226
|
+
/** Scans the next Literal String value */
|
|
227
|
+
// prettier-ignore
|
|
228
|
+
function String(options, code) {
|
|
229
|
+
return NextString(options, Trim.TrimAll(code));
|
|
230
|
+
}
|