@stxt-lang/core 0.5.2 → 0.6.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/README.md +116 -1
- package/out/all.d.ts +5 -0
- package/out/all.js +10 -4
- package/out/core/Constants.d.ts +8 -0
- package/out/core/Constants.js +8 -0
- package/out/core/Line.d.ts +20 -0
- package/out/core/Line.js +15 -0
- package/out/core/LineParser.d.ts +17 -0
- package/out/core/LineParser.js +27 -10
- package/out/core/NameNamespace.d.ts +9 -0
- package/out/core/NameNamespace.js +9 -0
- package/out/core/NameNamespaceParser.d.ts +11 -0
- package/out/core/NameNamespaceParser.js +16 -5
- package/out/core/NamespaceValidator.d.ts +18 -0
- package/out/core/NamespaceValidator.js +18 -0
- package/out/core/Node.d.ts +55 -0
- package/out/core/Node.js +56 -1
- package/out/core/NodeCreator.d.ts +11 -0
- package/out/core/NodeCreator.js +14 -3
- package/out/core/ParseResult.d.ts +26 -0
- package/out/core/ParseResult.js +26 -0
- package/out/core/Parser.d.ts +31 -0
- package/out/core/Parser.js +50 -19
- package/out/core/StringUtils.d.ts +31 -0
- package/out/core/StringUtils.js +40 -9
- package/out/discovery/DiscoveryEnvironment.d.ts +35 -0
- package/out/discovery/DiscoveryEnvironment.js +3 -0
- package/out/discovery/DiscoveryError.d.ts +30 -0
- package/out/discovery/DiscoveryError.js +36 -0
- package/out/discovery/DiscoveryFileSystem.d.ts +57 -0
- package/out/discovery/DiscoveryFileSystem.js +3 -0
- package/out/discovery/DiscoveryResolver.d.ts +74 -0
- package/out/discovery/DiscoveryResolver.js +232 -0
- package/out/discovery/DiscoveryResult.d.ts +94 -0
- package/out/discovery/DiscoveryResult.js +111 -0
- package/out/exceptions/ParseException.d.ts +14 -0
- package/out/exceptions/ParseException.js +12 -0
- package/out/exceptions/RuntimeException.d.ts +14 -0
- package/out/exceptions/RuntimeException.js +13 -0
- package/out/exceptions/ValidationException.d.ts +8 -0
- package/out/exceptions/ValidationException.js +8 -0
- package/out/processors/Observer.d.ts +29 -0
- package/out/processors/Validator.d.ts +9 -0
- package/out/runtime/ConditionalValidator.d.ts +15 -0
- package/out/runtime/ConditionalValidator.js +16 -2
- package/out/runtime/NodeWriter.d.ts +18 -0
- package/out/runtime/NodeWriter.js +18 -0
- package/out/runtime/UnifiedSchemaProvider.d.ts +23 -5
- package/out/runtime/UnifiedSchemaProvider.js +28 -10
- package/out/schema/ChildDefinition.d.ts +18 -0
- package/out/schema/ChildDefinition.js +18 -0
- package/out/schema/NodeDefinition.d.ts +44 -0
- package/out/schema/NodeDefinition.js +47 -3
- package/out/schema/Schema.d.ts +26 -0
- package/out/schema/Schema.js +26 -1
- package/out/schema/SchemaParser.d.ts +7 -0
- package/out/schema/SchemaParser.js +26 -16
- package/out/schema/SchemaProvider.d.ts +7 -0
- package/out/schema/SchemaProviderMemory.d.ts +27 -0
- package/out/schema/SchemaProviderMemory.js +27 -0
- package/out/schema/SchemaProviderMeta.d.ts +16 -0
- package/out/schema/SchemaProviderMeta.js +16 -0
- package/out/schema/SchemaValidator.d.ts +20 -0
- package/out/schema/SchemaValidator.js +27 -7
- package/out/schema/Type.d.ts +9 -0
- package/out/schema/TypeRegistry.d.ts +13 -0
- package/out/schema/TypeRegistry.js +18 -5
- package/out/schema/type/BASE64.d.ts +1 -0
- package/out/schema/type/BASE64.js +5 -4
- package/out/schema/type/BINARY.d.ts +1 -0
- package/out/schema/type/BINARY.js +2 -1
- package/out/schema/type/BLOCK.d.ts +1 -0
- package/out/schema/type/BLOCK.js +2 -1
- package/out/schema/type/BOOLEAN.d.ts +1 -0
- package/out/schema/type/BOOLEAN.js +1 -0
- package/out/schema/type/DATE.d.ts +1 -0
- package/out/schema/type/DATE.js +1 -0
- package/out/schema/type/EMAIL.d.ts +1 -0
- package/out/schema/type/EMAIL.js +1 -0
- package/out/schema/type/ENUM.d.ts +1 -0
- package/out/schema/type/ENUM.js +2 -1
- package/out/schema/type/GROUP.d.ts +1 -0
- package/out/schema/type/GROUP.js +2 -1
- package/out/schema/type/HEXADECIMAL.d.ts +1 -0
- package/out/schema/type/HEXADECIMAL.js +2 -1
- package/out/schema/type/INLINE.d.ts +1 -0
- package/out/schema/type/INLINE.js +2 -1
- package/out/schema/type/INTEGER.d.ts +1 -0
- package/out/schema/type/INTEGER.js +1 -0
- package/out/schema/type/MARKDOWN.d.ts +4 -0
- package/out/schema/type/MARKDOWN.js +4 -2
- package/out/schema/type/NATURAL.d.ts +1 -0
- package/out/schema/type/NATURAL.js +1 -0
- package/out/schema/type/NUMBER.d.ts +1 -0
- package/out/schema/type/NUMBER.js +1 -0
- package/out/schema/type/TEXT.d.ts +1 -0
- package/out/schema/type/TEXT.js +1 -0
- package/out/schema/type/TIME.d.ts +1 -0
- package/out/schema/type/TIME.js +1 -0
- package/out/schema/type/TIMESTAMP.d.ts +1 -0
- package/out/schema/type/TIMESTAMP.js +1 -0
- package/out/schema/type/URL.d.ts +1 -0
- package/out/schema/type/URL.js +2 -1
- package/out/schema/type/UUID.d.ts +1 -0
- package/out/schema/type/UUID.js +1 -0
- package/out/schema/type/binaryValue.d.ts +9 -0
- package/out/schema/type/binaryValue.js +9 -3
- package/out/schema/type/regexType.d.ts +8 -0
- package/out/schema/type/regexType.js +9 -1
- package/out/template/ChildLine.d.ts +14 -0
- package/out/template/ChildLine.js +14 -0
- package/out/template/ChildLineParser.d.ts +10 -0
- package/out/template/ChildLineParser.js +17 -8
- package/out/template/MetaTemplateSchemaProvider.d.ts +16 -0
- package/out/template/MetaTemplateSchemaProvider.js +17 -1
- package/out/template/TemplateParser.d.ts +8 -0
- package/out/template/TemplateParser.js +49 -39
- package/out/template/TemplateSchemaProviderMemory.d.ts +18 -0
- package/out/template/TemplateSchemaProviderMemory.js +21 -3
- package/package.json +2 -1
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ParseException = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Syntax error detected while parsing (lexical/structural phase, not schema). Every exception
|
|
6
|
+
* carries an UPPERCASE code and the line of the document where it was detected.
|
|
7
|
+
*/
|
|
4
8
|
class ParseException extends Error {
|
|
9
|
+
/**
|
|
10
|
+
* Creates a syntax error located at a line of the document.
|
|
11
|
+
*
|
|
12
|
+
* @param line line number where the error was detected.
|
|
13
|
+
* @param code error code in UPPERCASE.
|
|
14
|
+
* @param message descriptive message.
|
|
15
|
+
*/
|
|
5
16
|
constructor(line, code, message) {
|
|
6
17
|
super(message);
|
|
7
18
|
this.name = "ParseException";
|
|
@@ -9,6 +20,7 @@ class ParseException extends Error {
|
|
|
9
20
|
this.code = code;
|
|
10
21
|
Object.setPrototypeOf(this, ParseException.prototype);
|
|
11
22
|
}
|
|
23
|
+
/** @returns a readable representation of the error, with its line and its code. */
|
|
12
24
|
toString() {
|
|
13
25
|
return `${this.name} [line=${this.line}, code=${this.code}]: ${this.message}`;
|
|
14
26
|
}
|
|
@@ -1,6 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error that is not tied to a line of the document: a wrong use of the API or an inconsistency
|
|
3
|
+
* found at runtime (e.g. an ambiguous child, a type registered twice). It carries an UPPERCASE
|
|
4
|
+
* code, like the rest of the STXT exceptions.
|
|
5
|
+
*/
|
|
1
6
|
export declare class RuntimeException extends Error {
|
|
7
|
+
/** Error code in UPPERCASE (e.g. `AMBIGUOUS_CHILD`). */
|
|
2
8
|
readonly code: string;
|
|
9
|
+
/**
|
|
10
|
+
* Creates an error with an error code and a message.
|
|
11
|
+
*
|
|
12
|
+
* @param code error code in UPPERCASE.
|
|
13
|
+
* @param message descriptive message.
|
|
14
|
+
*/
|
|
3
15
|
constructor(code: string, message: string);
|
|
16
|
+
/** @returns the error code in UPPERCASE. */
|
|
4
17
|
getCode(): string;
|
|
18
|
+
/** @returns a readable representation of the error, with its code. */
|
|
5
19
|
toString(): string;
|
|
6
20
|
}
|
|
@@ -1,16 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RuntimeException = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Error that is not tied to a line of the document: a wrong use of the API or an inconsistency
|
|
6
|
+
* found at runtime (e.g. an ambiguous child, a type registered twice). It carries an UPPERCASE
|
|
7
|
+
* code, like the rest of the STXT exceptions.
|
|
8
|
+
*/
|
|
4
9
|
class RuntimeException extends Error {
|
|
10
|
+
/**
|
|
11
|
+
* Creates an error with an error code and a message.
|
|
12
|
+
*
|
|
13
|
+
* @param code error code in UPPERCASE.
|
|
14
|
+
* @param message descriptive message.
|
|
15
|
+
*/
|
|
5
16
|
constructor(code, message) {
|
|
6
17
|
super(message);
|
|
7
18
|
this.name = "RuntimeException";
|
|
8
19
|
this.code = code;
|
|
9
20
|
Object.setPrototypeOf(this, RuntimeException.prototype);
|
|
10
21
|
}
|
|
22
|
+
/** @returns the error code in UPPERCASE. */
|
|
11
23
|
getCode() {
|
|
12
24
|
return this.code;
|
|
13
25
|
}
|
|
26
|
+
/** @returns a readable representation of the error, with its code. */
|
|
14
27
|
toString() {
|
|
15
28
|
const message = this.message;
|
|
16
29
|
return `${this.name}[${this.code}]${message ? `: ${message}` : ""}`;
|
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
import { ParseException } from "./ParseException";
|
|
2
|
+
/** Semantic validation error (schema, type or cardinality), detected when a node is closed. */
|
|
2
3
|
export declare class ValidationException extends ParseException {
|
|
4
|
+
/**
|
|
5
|
+
* Creates a validation error located at a line of the document.
|
|
6
|
+
*
|
|
7
|
+
* @param line line number where the error was detected.
|
|
8
|
+
* @param code error code in UPPERCASE.
|
|
9
|
+
* @param message descriptive message.
|
|
10
|
+
*/
|
|
3
11
|
constructor(line: number, code: string, message: string);
|
|
4
12
|
}
|
|
@@ -2,7 +2,15 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ValidationException = void 0;
|
|
4
4
|
const ParseException_1 = require("./ParseException");
|
|
5
|
+
/** Semantic validation error (schema, type or cardinality), detected when a node is closed. */
|
|
5
6
|
class ValidationException extends ParseException_1.ParseException {
|
|
7
|
+
/**
|
|
8
|
+
* Creates a validation error located at a line of the document.
|
|
9
|
+
*
|
|
10
|
+
* @param line line number where the error was detected.
|
|
11
|
+
* @param code error code in UPPERCASE.
|
|
12
|
+
* @param message descriptive message.
|
|
13
|
+
*/
|
|
6
14
|
constructor(line, code, message) {
|
|
7
15
|
super(line, code, message);
|
|
8
16
|
this.name = "ValidationException";
|
|
@@ -1,8 +1,37 @@
|
|
|
1
1
|
import { Line } from "../core/Line";
|
|
2
2
|
import { Node } from "../core/Node";
|
|
3
|
+
/**
|
|
4
|
+
* Process hook notified by the {@link Parser} while parsing: when each node is opened and closed,
|
|
5
|
+
* and for every comment and text line it reads. Register it with {@link Parser.registerObserver}.
|
|
6
|
+
*/
|
|
3
7
|
export interface Observer {
|
|
8
|
+
/**
|
|
9
|
+
* Called when a node is opened.
|
|
10
|
+
*
|
|
11
|
+
* @param node node just opened (its children and its text lines are not complete yet).
|
|
12
|
+
* @param line source line that opened the node, as it appears in the document.
|
|
13
|
+
*/
|
|
4
14
|
onCreate(node: Node, line: string): void;
|
|
15
|
+
/**
|
|
16
|
+
* Called when a node is closed.
|
|
17
|
+
*
|
|
18
|
+
* @param node node just closed, with all its children and its value already complete.
|
|
19
|
+
*/
|
|
5
20
|
onFinish(node: Node): void;
|
|
21
|
+
/**
|
|
22
|
+
* Called for every comment line, which produces no node.
|
|
23
|
+
*
|
|
24
|
+
* @param lineNumber line number of the comment.
|
|
25
|
+
* @param line source line of the comment, as it appears in the document.
|
|
26
|
+
*/
|
|
6
27
|
onComment(lineNumber: number, line: string): void;
|
|
28
|
+
/**
|
|
29
|
+
* Called for every text line appended to an open BLOCK node.
|
|
30
|
+
*
|
|
31
|
+
* @param node BLOCK node the line was appended to.
|
|
32
|
+
* @param lineNumber line number of the text line.
|
|
33
|
+
* @param lineString source line, as it appears in the document.
|
|
34
|
+
* @param line the same line already split into indentation and content.
|
|
35
|
+
*/
|
|
7
36
|
onTextLine(node: Node, lineNumber: number, lineString: string, line: Line): void;
|
|
8
37
|
}
|
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { Node } from "../core/Node";
|
|
2
2
|
import { ValidationException } from "../exceptions/ValidationException";
|
|
3
|
+
/** Process hook invoked by the {@link Parser} when each node is closed, to validate in streaming. */
|
|
3
4
|
export interface Validator {
|
|
5
|
+
/**
|
|
6
|
+
* Validates a node and returns every error found (without throwing), letting the caller
|
|
7
|
+
* collect errors from several nodes instead of bailing out on the first one. An empty array
|
|
8
|
+
* means the node is valid.
|
|
9
|
+
*
|
|
10
|
+
* @param node already closed node to validate.
|
|
11
|
+
* @returns the validation errors found, or an empty array if the node is valid.
|
|
12
|
+
*/
|
|
4
13
|
validate(node: Node): ValidationException[];
|
|
5
14
|
}
|
|
@@ -2,8 +2,23 @@ import { Node } from "../core/Node";
|
|
|
2
2
|
import { Validator } from "../processors/Validator";
|
|
3
3
|
import { SchemaValidator } from "../schema/SchemaValidator";
|
|
4
4
|
import { ValidationException } from "../exceptions/ValidationException";
|
|
5
|
+
/**
|
|
6
|
+
* Wrapper around a {@link SchemaValidator} that only validates namespaced nodes, so that a
|
|
7
|
+
* document mixing schema-bound and free nodes does not report the free ones as unknown.
|
|
8
|
+
*/
|
|
5
9
|
export declare class ConditionalValidator implements Validator {
|
|
6
10
|
private readonly schemaValidator;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a validator that delegates to a schema validator.
|
|
13
|
+
*
|
|
14
|
+
* @param schemaValidator validator the namespaced nodes are handed over to.
|
|
15
|
+
*/
|
|
7
16
|
constructor(schemaValidator: SchemaValidator);
|
|
17
|
+
/**
|
|
18
|
+
* Validates a node when it has a namespace, and lets it through otherwise.
|
|
19
|
+
*
|
|
20
|
+
* @param node already closed node to validate.
|
|
21
|
+
* @returns the validation errors found, or an empty array if the node is valid or has no namespace.
|
|
22
|
+
*/
|
|
8
23
|
validate(node: Node): ValidationException[];
|
|
9
24
|
}
|
|
@@ -1,13 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ConditionalValidator = void 0;
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Wrapper around a {@link SchemaValidator} that only validates namespaced nodes, so that a
|
|
6
|
+
* document mixing schema-bound and free nodes does not report the free ones as unknown.
|
|
7
|
+
*/
|
|
5
8
|
class ConditionalValidator {
|
|
9
|
+
/**
|
|
10
|
+
* Creates a validator that delegates to a schema validator.
|
|
11
|
+
*
|
|
12
|
+
* @param schemaValidator validator the namespaced nodes are handed over to.
|
|
13
|
+
*/
|
|
6
14
|
constructor(schemaValidator) {
|
|
7
15
|
this.schemaValidator = schemaValidator;
|
|
8
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Validates a node when it has a namespace, and lets it through otherwise.
|
|
19
|
+
*
|
|
20
|
+
* @param node already closed node to validate.
|
|
21
|
+
* @returns the validation errors found, or an empty array if the node is valid or has no namespace.
|
|
22
|
+
*/
|
|
9
23
|
validate(node) {
|
|
10
|
-
//
|
|
24
|
+
// Only validate the node when it has a namespace
|
|
11
25
|
if (node.getNamespace() !== "") {
|
|
12
26
|
return this.schemaValidator.validate(node);
|
|
13
27
|
}
|
|
@@ -1,11 +1,29 @@
|
|
|
1
1
|
import { Node } from "../core/Node";
|
|
2
|
+
/** Indentation style to use when writing. */
|
|
2
3
|
export declare enum IndentStyle {
|
|
4
|
+
/** One tab character per level. */
|
|
3
5
|
TABS = "TABS",
|
|
6
|
+
/** Four spaces per level. */
|
|
4
7
|
SPACES_4 = "SPACES_4"
|
|
5
8
|
}
|
|
9
|
+
/** Serializes a {@link Node} (or a list of root nodes) back to STXT text. */
|
|
6
10
|
export declare class NodeWriter {
|
|
7
11
|
private constructor();
|
|
12
|
+
/**
|
|
13
|
+
* Serializes a node to STXT text.
|
|
14
|
+
*
|
|
15
|
+
* @param node node to serialize (along with its children).
|
|
16
|
+
* @param style indentation style to use; tabs by default.
|
|
17
|
+
* @returns the node serialized to STXT text.
|
|
18
|
+
*/
|
|
8
19
|
static toSTXT(node: Node, style?: IndentStyle): string;
|
|
20
|
+
/**
|
|
21
|
+
* Serializes a list of root nodes to STXT text, separated by a blank line.
|
|
22
|
+
*
|
|
23
|
+
* @param docs root nodes to serialize.
|
|
24
|
+
* @param style indentation style to use; tabs by default.
|
|
25
|
+
* @returns the documents serialized to STXT text.
|
|
26
|
+
*/
|
|
9
27
|
static toSTXTDocs(docs: ReadonlyArray<Node>, style?: IndentStyle): string;
|
|
10
28
|
private static writeNode;
|
|
11
29
|
private static indent;
|
|
@@ -1,18 +1,36 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.NodeWriter = exports.IndentStyle = void 0;
|
|
4
|
+
/** Indentation style to use when writing. */
|
|
4
5
|
var IndentStyle;
|
|
5
6
|
(function (IndentStyle) {
|
|
7
|
+
/** One tab character per level. */
|
|
6
8
|
IndentStyle["TABS"] = "TABS";
|
|
9
|
+
/** Four spaces per level. */
|
|
7
10
|
IndentStyle["SPACES_4"] = "SPACES_4";
|
|
8
11
|
})(IndentStyle || (exports.IndentStyle = IndentStyle = {}));
|
|
12
|
+
/** Serializes a {@link Node} (or a list of root nodes) back to STXT text. */
|
|
9
13
|
class NodeWriter {
|
|
10
14
|
constructor() { }
|
|
15
|
+
/**
|
|
16
|
+
* Serializes a node to STXT text.
|
|
17
|
+
*
|
|
18
|
+
* @param node node to serialize (along with its children).
|
|
19
|
+
* @param style indentation style to use; tabs by default.
|
|
20
|
+
* @returns the node serialized to STXT text.
|
|
21
|
+
*/
|
|
11
22
|
static toSTXT(node, style = IndentStyle.TABS) {
|
|
12
23
|
const out = [];
|
|
13
24
|
NodeWriter.writeNode(out, node, 0, style, "");
|
|
14
25
|
return out.join("");
|
|
15
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Serializes a list of root nodes to STXT text, separated by a blank line.
|
|
29
|
+
*
|
|
30
|
+
* @param docs root nodes to serialize.
|
|
31
|
+
* @param style indentation style to use; tabs by default.
|
|
32
|
+
* @returns the documents serialized to STXT text.
|
|
33
|
+
*/
|
|
16
34
|
static toSTXTDocs(docs, style = IndentStyle.TABS) {
|
|
17
35
|
const out = [];
|
|
18
36
|
for (let i = 0; i < docs.length; i++) {
|
|
@@ -1,22 +1,40 @@
|
|
|
1
1
|
import { Schema } from "../schema/Schema";
|
|
2
2
|
import { SchemaProvider } from "../schema/SchemaProvider";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* - @stxt.template =>
|
|
7
|
-
* - @stxt.schema =>
|
|
8
|
-
* -
|
|
4
|
+
* Unified provider that handles both schemas and templates.
|
|
5
|
+
* It detects which one it is from the namespace of the root node:
|
|
6
|
+
* - @stxt.template => processed as a template
|
|
7
|
+
* - @stxt.schema => processed as a schema
|
|
8
|
+
* - anything else => ignored
|
|
9
9
|
*/
|
|
10
10
|
export declare class UnifiedSchemaProvider implements SchemaProvider {
|
|
11
11
|
private readonly schemas;
|
|
12
12
|
private readonly schemaMeta;
|
|
13
13
|
private readonly templateMeta;
|
|
14
|
+
/** Creates an empty provider, with the two meta-schemas already loaded. */
|
|
14
15
|
constructor();
|
|
16
|
+
/**
|
|
17
|
+
* Resolves the schema that applies to a namespace, serving the meta-schemas of the two
|
|
18
|
+
* reserved namespaces itself.
|
|
19
|
+
*
|
|
20
|
+
* @param namespace namespace whose schema is wanted.
|
|
21
|
+
* @returns the schema of the namespace, or null/undefined if none has been registered for it.
|
|
22
|
+
*/
|
|
15
23
|
getSchema(namespace: string): Schema | undefined | null;
|
|
24
|
+
/**
|
|
25
|
+
* Parses a document and registers every schema or template it defines, each one under its own
|
|
26
|
+
* namespace. Documents of any other namespace are ignored.
|
|
27
|
+
*
|
|
28
|
+
* @param text text of the document to load.
|
|
29
|
+
* @throws ParseException if the document cannot be parsed, or the first ValidationException if
|
|
30
|
+
* a schema or a template does not validate against its meta-schema.
|
|
31
|
+
*/
|
|
16
32
|
addFile(text: string): void;
|
|
17
33
|
private addTemplateNode;
|
|
18
34
|
private addSchemaNode;
|
|
19
35
|
private static throwIfInvalid;
|
|
36
|
+
/** Removes every schema and template registered in this provider. */
|
|
20
37
|
clear(): void;
|
|
38
|
+
/** @returns every schema registered in this provider, in registration order. */
|
|
21
39
|
getAllSchemas(): ReadonlyArray<Schema>;
|
|
22
40
|
}
|
|
@@ -9,18 +9,26 @@ const SchemaValidator_1 = require("../schema/SchemaValidator");
|
|
|
9
9
|
const MetaTemplateSchemaProvider_1 = require("../template/MetaTemplateSchemaProvider");
|
|
10
10
|
const TemplateParser_1 = require("../template/TemplateParser");
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* - @stxt.template =>
|
|
15
|
-
* - @stxt.schema =>
|
|
16
|
-
* -
|
|
12
|
+
* Unified provider that handles both schemas and templates.
|
|
13
|
+
* It detects which one it is from the namespace of the root node:
|
|
14
|
+
* - @stxt.template => processed as a template
|
|
15
|
+
* - @stxt.schema => processed as a schema
|
|
16
|
+
* - anything else => ignored
|
|
17
17
|
*/
|
|
18
18
|
class UnifiedSchemaProvider {
|
|
19
|
+
/** Creates an empty provider, with the two meta-schemas already loaded. */
|
|
19
20
|
constructor() {
|
|
20
21
|
this.schemas = new Map();
|
|
21
22
|
this.schemaMeta = new SchemaProviderMeta_1.SchemaProviderMeta();
|
|
22
23
|
this.templateMeta = new MetaTemplateSchemaProvider_1.MetaTemplateSchemaProvider();
|
|
23
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Resolves the schema that applies to a namespace, serving the meta-schemas of the two
|
|
27
|
+
* reserved namespaces itself.
|
|
28
|
+
*
|
|
29
|
+
* @param namespace namespace whose schema is wanted.
|
|
30
|
+
* @returns the schema of the namespace, or null/undefined if none has been registered for it.
|
|
31
|
+
*/
|
|
24
32
|
getSchema(namespace) {
|
|
25
33
|
const key = StringUtils_1.StringUtils.lowerCase(namespace);
|
|
26
34
|
if (namespace === "@stxt.template") {
|
|
@@ -32,6 +40,14 @@ class UnifiedSchemaProvider {
|
|
|
32
40
|
let result = this.schemas.get(key);
|
|
33
41
|
return result;
|
|
34
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Parses a document and registers every schema or template it defines, each one under its own
|
|
45
|
+
* namespace. Documents of any other namespace are ignored.
|
|
46
|
+
*
|
|
47
|
+
* @param text text of the document to load.
|
|
48
|
+
* @throws ParseException if the document cannot be parsed, or the first ValidationException if
|
|
49
|
+
* a schema or a template does not validate against its meta-schema.
|
|
50
|
+
*/
|
|
35
51
|
addFile(text) {
|
|
36
52
|
const parser = new Parser_1.Parser();
|
|
37
53
|
const nodes = parser.parse(text);
|
|
@@ -46,32 +62,34 @@ class UnifiedSchemaProvider {
|
|
|
46
62
|
}
|
|
47
63
|
}
|
|
48
64
|
addTemplateNode(node) {
|
|
49
|
-
//
|
|
65
|
+
// Validate against the meta-schema of templates
|
|
50
66
|
const schemaValidator = new SchemaValidator_1.SchemaValidator(this.templateMeta, true);
|
|
51
67
|
UnifiedSchemaProvider.throwIfInvalid(schemaValidator.validate(node));
|
|
52
|
-
//
|
|
68
|
+
// Transform the template into a schema
|
|
53
69
|
const schema = (0, TemplateParser_1.transformTemplateNodeToSchema)(node);
|
|
54
70
|
const key = StringUtils_1.StringUtils.lowerCase(schema.getNamespace());
|
|
55
71
|
this.schemas.set(key, schema);
|
|
56
72
|
}
|
|
57
73
|
addSchemaNode(node) {
|
|
58
|
-
//
|
|
74
|
+
// Validate against the meta-schema of schemas
|
|
59
75
|
const schemaValidator = new SchemaValidator_1.SchemaValidator(this.schemaMeta, true);
|
|
60
76
|
UnifiedSchemaProvider.throwIfInvalid(schemaValidator.validate(node));
|
|
61
|
-
//
|
|
77
|
+
// Transform the node into a schema
|
|
62
78
|
const schema = (0, SchemaParser_1.transformNodeToSchema)(node);
|
|
63
79
|
const key = StringUtils_1.StringUtils.lowerCase(schema.getNamespace());
|
|
64
80
|
this.schemas.set(key, schema);
|
|
65
81
|
}
|
|
66
|
-
//
|
|
82
|
+
// A schema/template that does not validate against its meta-schema must not be loaded
|
|
67
83
|
static throwIfInvalid(errors) {
|
|
68
84
|
if (errors.length > 0) {
|
|
69
85
|
throw errors[0];
|
|
70
86
|
}
|
|
71
87
|
}
|
|
88
|
+
/** Removes every schema and template registered in this provider. */
|
|
72
89
|
clear() {
|
|
73
90
|
this.schemas.clear();
|
|
74
91
|
}
|
|
92
|
+
/** @returns every schema registered in this provider, in registration order. */
|
|
75
93
|
getAllSchemas() {
|
|
76
94
|
return Array.from(this.schemas.values());
|
|
77
95
|
}
|
|
@@ -1,16 +1,34 @@
|
|
|
1
|
+
/** Definition of an expected child inside a {@link NodeDefinition}: name, namespace and min/max cardinality. */
|
|
1
2
|
export declare class ChildDefinition {
|
|
2
3
|
private readonly normalizedName;
|
|
3
4
|
private readonly name;
|
|
4
5
|
private readonly namespace;
|
|
5
6
|
private readonly min;
|
|
6
7
|
private readonly max;
|
|
8
|
+
/**
|
|
9
|
+
* Creates the definition of an expected child.
|
|
10
|
+
*
|
|
11
|
+
* @param name name of the expected child.
|
|
12
|
+
* @param namespace namespace of the expected child (may be null/undefined).
|
|
13
|
+
* @param min minimum cardinality, or null if there is no minimum.
|
|
14
|
+
* @param max maximum cardinality, or null if there is no maximum.
|
|
15
|
+
* @param numLine line number, for the error messages.
|
|
16
|
+
* @throws ValidationException with code `INVALID_NODE_NAME` if the name is not valid.
|
|
17
|
+
*/
|
|
7
18
|
constructor(name: string, namespace: string | null | undefined, min: number | null, max: number | null, numLine: number);
|
|
19
|
+
/** @returns the name of the expected child, as it appears in the schema. */
|
|
8
20
|
getName(): string;
|
|
21
|
+
/** @returns the canonical name of the expected child. */
|
|
9
22
|
getNormalizedName(): string;
|
|
23
|
+
/** @returns the namespace of the expected child, or the empty string if it has none. */
|
|
10
24
|
getNamespace(): string;
|
|
25
|
+
/** @returns the minimum cardinality, or null if there is no minimum. */
|
|
11
26
|
getMin(): number | null;
|
|
27
|
+
/** @returns the maximum cardinality, or null if there is no maximum. */
|
|
12
28
|
getMax(): number | null;
|
|
29
|
+
/** @returns the canonical name prefixed by its namespace, used as the key in {@link NodeDefinition.getChildren}. */
|
|
13
30
|
getQualifiedName(): string;
|
|
31
|
+
/** @returns a plain object with the definition, so that JSON.stringify serializes it. */
|
|
14
32
|
toJSON(): {
|
|
15
33
|
name: string;
|
|
16
34
|
normalizedName: string;
|
|
@@ -4,7 +4,18 @@ exports.ChildDefinition = void 0;
|
|
|
4
4
|
const NamespaceValidator_1 = require("../core/NamespaceValidator");
|
|
5
5
|
const ValidationException_1 = require("../exceptions/ValidationException");
|
|
6
6
|
const StringUtils_1 = require("../core/StringUtils");
|
|
7
|
+
/** Definition of an expected child inside a {@link NodeDefinition}: name, namespace and min/max cardinality. */
|
|
7
8
|
class ChildDefinition {
|
|
9
|
+
/**
|
|
10
|
+
* Creates the definition of an expected child.
|
|
11
|
+
*
|
|
12
|
+
* @param name name of the expected child.
|
|
13
|
+
* @param namespace namespace of the expected child (may be null/undefined).
|
|
14
|
+
* @param min minimum cardinality, or null if there is no minimum.
|
|
15
|
+
* @param max maximum cardinality, or null if there is no maximum.
|
|
16
|
+
* @param numLine line number, for the error messages.
|
|
17
|
+
* @throws ValidationException with code `INVALID_NODE_NAME` if the name is not valid.
|
|
18
|
+
*/
|
|
8
19
|
constructor(name, namespace, min, max, numLine) {
|
|
9
20
|
this.name = StringUtils_1.StringUtils.compactSpaces(name);
|
|
10
21
|
this.normalizedName = StringUtils_1.StringUtils.normalize(name);
|
|
@@ -16,26 +27,33 @@ class ChildDefinition {
|
|
|
16
27
|
throw new ValidationException_1.ValidationException(numLine, "INVALID_NODE_NAME", `Node name not valid: ${name}`);
|
|
17
28
|
}
|
|
18
29
|
}
|
|
30
|
+
/** @returns the name of the expected child, as it appears in the schema. */
|
|
19
31
|
getName() {
|
|
20
32
|
return this.name;
|
|
21
33
|
}
|
|
34
|
+
/** @returns the canonical name of the expected child. */
|
|
22
35
|
getNormalizedName() {
|
|
23
36
|
return this.normalizedName;
|
|
24
37
|
}
|
|
38
|
+
/** @returns the namespace of the expected child, or the empty string if it has none. */
|
|
25
39
|
getNamespace() {
|
|
26
40
|
return this.namespace;
|
|
27
41
|
}
|
|
42
|
+
/** @returns the minimum cardinality, or null if there is no minimum. */
|
|
28
43
|
getMin() {
|
|
29
44
|
return this.min;
|
|
30
45
|
}
|
|
46
|
+
/** @returns the maximum cardinality, or null if there is no maximum. */
|
|
31
47
|
getMax() {
|
|
32
48
|
return this.max;
|
|
33
49
|
}
|
|
50
|
+
/** @returns the canonical name prefixed by its namespace, used as the key in {@link NodeDefinition.getChildren}. */
|
|
34
51
|
getQualifiedName() {
|
|
35
52
|
return this.namespace.length === 0
|
|
36
53
|
? this.normalizedName
|
|
37
54
|
: `${this.namespace}:${this.normalizedName}`;
|
|
38
55
|
}
|
|
56
|
+
/** @returns a plain object with the definition, so that JSON.stringify serializes it. */
|
|
39
57
|
toJSON() {
|
|
40
58
|
return {
|
|
41
59
|
name: this.getName(),
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { ChildDefinition } from "./ChildDefinition";
|
|
2
|
+
/**
|
|
3
|
+
* Definition of a node inside a {@link Schema}: its name, its value type, the children it expects
|
|
4
|
+
* ({@link ChildDefinition}) and, for ENUM, the values it allows.
|
|
5
|
+
*/
|
|
2
6
|
export declare class NodeDefinition {
|
|
3
7
|
private readonly name;
|
|
4
8
|
private readonly normalizedName;
|
|
@@ -6,17 +10,57 @@ export declare class NodeDefinition {
|
|
|
6
10
|
private description;
|
|
7
11
|
private readonly children;
|
|
8
12
|
private readonly values;
|
|
13
|
+
/**
|
|
14
|
+
* Creates the definition of a node.
|
|
15
|
+
*
|
|
16
|
+
* @param name name of the node.
|
|
17
|
+
* @param type name of the type (see {@link TypeRegistry}).
|
|
18
|
+
* @param line line number, for the error message.
|
|
19
|
+
* @param description optional description of the node.
|
|
20
|
+
* @throws ValidationException with code `INVALID_NODE_NAME` if the name is not valid.
|
|
21
|
+
*/
|
|
9
22
|
constructor(name: string, type: string, line: number, description: string | undefined);
|
|
23
|
+
/** @returns the name of the node, as it appears in the schema. */
|
|
10
24
|
getName(): string;
|
|
25
|
+
/** @returns the canonical name of the node. */
|
|
11
26
|
getNormalizedName(): string;
|
|
27
|
+
/** @returns the name of the value type of this node (see {@link TypeRegistry}). */
|
|
12
28
|
getType(): string;
|
|
29
|
+
/** @returns the definitions of the expected children, indexed by their qualified canonical name. */
|
|
13
30
|
getChildren(): ReadonlyMap<string, ChildDefinition>;
|
|
31
|
+
/** @returns the optional description of the node, or undefined if it has none. */
|
|
14
32
|
getDescription(): string | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Sets the optional description of the node.
|
|
35
|
+
*
|
|
36
|
+
* @param description new optional description of the node.
|
|
37
|
+
*/
|
|
15
38
|
setDescription(description: string): void;
|
|
39
|
+
/**
|
|
40
|
+
* Adds the definition of an expected child.
|
|
41
|
+
*
|
|
42
|
+
* @param childDefinition definition of the child to add.
|
|
43
|
+
* @throws ValidationException with code `CHILD_DEF_ALREADY_DEFINED` if a definition for that child already existed.
|
|
44
|
+
*/
|
|
16
45
|
addChildDefinition(childDefinition: ChildDefinition): void;
|
|
46
|
+
/**
|
|
47
|
+
* Adds a value to the list of values allowed for this node.
|
|
48
|
+
*
|
|
49
|
+
* @param value value to add to the list of allowed values.
|
|
50
|
+
* @param line line number, for the error message.
|
|
51
|
+
* @throws ValidationException with code `VALUE_DUPLICATED` if the value (once trimmed) had already been added.
|
|
52
|
+
*/
|
|
17
53
|
addValue(value: string, line?: number): void;
|
|
54
|
+
/**
|
|
55
|
+
* Tells whether a value is allowed for this node.
|
|
56
|
+
*
|
|
57
|
+
* @param value value to check.
|
|
58
|
+
* @returns true if no restricted values are defined, or if the value is among the allowed ones.
|
|
59
|
+
*/
|
|
18
60
|
isAllowedValue(value: string): boolean;
|
|
61
|
+
/** @returns the values allowed for this node (ENUM), or empty if there is no restriction. */
|
|
19
62
|
getValues(): ReadonlySet<string>;
|
|
63
|
+
/** @returns a plain object with the definition, so that JSON.stringify serializes it. */
|
|
20
64
|
toJSON(): {
|
|
21
65
|
name: string;
|
|
22
66
|
normalizedName: string;
|