@stxt-lang/core 0.5.2 → 0.5.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.
Files changed (109) hide show
  1. package/README.md +1 -0
  2. package/out/all.js +3 -3
  3. package/out/core/Constants.d.ts +8 -0
  4. package/out/core/Constants.js +8 -0
  5. package/out/core/Line.d.ts +20 -0
  6. package/out/core/Line.js +15 -0
  7. package/out/core/LineParser.d.ts +17 -0
  8. package/out/core/LineParser.js +27 -10
  9. package/out/core/NameNamespace.d.ts +9 -0
  10. package/out/core/NameNamespace.js +9 -0
  11. package/out/core/NameNamespaceParser.d.ts +11 -0
  12. package/out/core/NameNamespaceParser.js +16 -5
  13. package/out/core/NamespaceValidator.d.ts +18 -0
  14. package/out/core/NamespaceValidator.js +18 -0
  15. package/out/core/Node.d.ts +55 -0
  16. package/out/core/Node.js +56 -1
  17. package/out/core/NodeCreator.d.ts +11 -0
  18. package/out/core/NodeCreator.js +14 -3
  19. package/out/core/ParseResult.d.ts +26 -0
  20. package/out/core/ParseResult.js +26 -0
  21. package/out/core/Parser.d.ts +31 -0
  22. package/out/core/Parser.js +50 -19
  23. package/out/core/StringUtils.d.ts +31 -0
  24. package/out/core/StringUtils.js +40 -9
  25. package/out/exceptions/ParseException.d.ts +14 -0
  26. package/out/exceptions/ParseException.js +12 -0
  27. package/out/exceptions/RuntimeException.d.ts +14 -0
  28. package/out/exceptions/RuntimeException.js +13 -0
  29. package/out/exceptions/ValidationException.d.ts +8 -0
  30. package/out/exceptions/ValidationException.js +8 -0
  31. package/out/processors/Observer.d.ts +29 -0
  32. package/out/processors/Validator.d.ts +9 -0
  33. package/out/runtime/ConditionalValidator.d.ts +15 -0
  34. package/out/runtime/ConditionalValidator.js +16 -2
  35. package/out/runtime/NodeWriter.d.ts +18 -0
  36. package/out/runtime/NodeWriter.js +18 -0
  37. package/out/runtime/UnifiedSchemaProvider.d.ts +23 -5
  38. package/out/runtime/UnifiedSchemaProvider.js +28 -10
  39. package/out/schema/ChildDefinition.d.ts +18 -0
  40. package/out/schema/ChildDefinition.js +18 -0
  41. package/out/schema/NodeDefinition.d.ts +44 -0
  42. package/out/schema/NodeDefinition.js +47 -3
  43. package/out/schema/Schema.d.ts +26 -0
  44. package/out/schema/Schema.js +26 -1
  45. package/out/schema/SchemaParser.d.ts +7 -0
  46. package/out/schema/SchemaParser.js +26 -16
  47. package/out/schema/SchemaProvider.d.ts +7 -0
  48. package/out/schema/SchemaProviderMemory.d.ts +27 -0
  49. package/out/schema/SchemaProviderMemory.js +27 -0
  50. package/out/schema/SchemaProviderMeta.d.ts +16 -0
  51. package/out/schema/SchemaProviderMeta.js +16 -0
  52. package/out/schema/SchemaValidator.d.ts +20 -0
  53. package/out/schema/SchemaValidator.js +27 -7
  54. package/out/schema/Type.d.ts +9 -0
  55. package/out/schema/TypeRegistry.d.ts +13 -0
  56. package/out/schema/TypeRegistry.js +18 -5
  57. package/out/schema/type/BASE64.d.ts +1 -0
  58. package/out/schema/type/BASE64.js +5 -4
  59. package/out/schema/type/BINARY.d.ts +1 -0
  60. package/out/schema/type/BINARY.js +2 -1
  61. package/out/schema/type/BLOCK.d.ts +1 -0
  62. package/out/schema/type/BLOCK.js +2 -1
  63. package/out/schema/type/BOOLEAN.d.ts +1 -0
  64. package/out/schema/type/BOOLEAN.js +1 -0
  65. package/out/schema/type/DATE.d.ts +1 -0
  66. package/out/schema/type/DATE.js +1 -0
  67. package/out/schema/type/EMAIL.d.ts +1 -0
  68. package/out/schema/type/EMAIL.js +1 -0
  69. package/out/schema/type/ENUM.d.ts +1 -0
  70. package/out/schema/type/ENUM.js +2 -1
  71. package/out/schema/type/GROUP.d.ts +1 -0
  72. package/out/schema/type/GROUP.js +2 -1
  73. package/out/schema/type/HEXADECIMAL.d.ts +1 -0
  74. package/out/schema/type/HEXADECIMAL.js +2 -1
  75. package/out/schema/type/INLINE.d.ts +1 -0
  76. package/out/schema/type/INLINE.js +2 -1
  77. package/out/schema/type/INTEGER.d.ts +1 -0
  78. package/out/schema/type/INTEGER.js +1 -0
  79. package/out/schema/type/MARKDOWN.d.ts +4 -0
  80. package/out/schema/type/MARKDOWN.js +4 -2
  81. package/out/schema/type/NATURAL.d.ts +1 -0
  82. package/out/schema/type/NATURAL.js +1 -0
  83. package/out/schema/type/NUMBER.d.ts +1 -0
  84. package/out/schema/type/NUMBER.js +1 -0
  85. package/out/schema/type/TEXT.d.ts +1 -0
  86. package/out/schema/type/TEXT.js +1 -0
  87. package/out/schema/type/TIME.d.ts +1 -0
  88. package/out/schema/type/TIME.js +1 -0
  89. package/out/schema/type/TIMESTAMP.d.ts +1 -0
  90. package/out/schema/type/TIMESTAMP.js +1 -0
  91. package/out/schema/type/URL.d.ts +1 -0
  92. package/out/schema/type/URL.js +2 -1
  93. package/out/schema/type/UUID.d.ts +1 -0
  94. package/out/schema/type/UUID.js +1 -0
  95. package/out/schema/type/binaryValue.d.ts +9 -0
  96. package/out/schema/type/binaryValue.js +9 -3
  97. package/out/schema/type/regexType.d.ts +8 -0
  98. package/out/schema/type/regexType.js +9 -1
  99. package/out/template/ChildLine.d.ts +14 -0
  100. package/out/template/ChildLine.js +14 -0
  101. package/out/template/ChildLineParser.d.ts +10 -0
  102. package/out/template/ChildLineParser.js +17 -8
  103. package/out/template/MetaTemplateSchemaProvider.d.ts +16 -0
  104. package/out/template/MetaTemplateSchemaProvider.js +17 -1
  105. package/out/template/TemplateParser.d.ts +8 -0
  106. package/out/template/TemplateParser.js +49 -39
  107. package/out/template/TemplateSchemaProviderMemory.d.ts +18 -0
  108. package/out/template/TemplateSchemaProviderMemory.js +21 -3
  109. package/package.json +1 -1
@@ -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
- // Wrapper del validador que solo valida nodos con namespace
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
- // Solo validar si tiene namespace
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
- * Provider unificado que maneja tanto schemas como templates.
5
- * Detecta automáticamente el tipo según el namespace del nodo raíz:
6
- * - @stxt.template => procesa como template
7
- * - @stxt.schema => procesa como schema
8
- * - otros => no hace nada
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
- * Provider unificado que maneja tanto schemas como templates.
13
- * Detecta automáticamente el tipo según el namespace del nodo raíz:
14
- * - @stxt.template => procesa como template
15
- * - @stxt.schema => procesa como schema
16
- * - otros => no hace nada
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
- // Validar contra el meta-schema de templates
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
- // Transformar el template a schema
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
- // Validar contra el meta-schema de schemas
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
- // Transformar el nodo a schema
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
- // Un schema/template que no valida contra su meta-schema no debe cargarse
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;
@@ -3,7 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NodeDefinition = void 0;
4
4
  const ValidationException_1 = require("../exceptions/ValidationException");
5
5
  const StringUtils_1 = require("../core/StringUtils");
6
+ /**
7
+ * Definition of a node inside a {@link Schema}: its name, its value type, the children it expects
8
+ * ({@link ChildDefinition}) and, for ENUM, the values it allows.
9
+ */
6
10
  class NodeDefinition {
11
+ /**
12
+ * Creates the definition of a node.
13
+ *
14
+ * @param name name of the node.
15
+ * @param type name of the type (see {@link TypeRegistry}).
16
+ * @param line line number, for the error message.
17
+ * @param description optional description of the node.
18
+ * @throws ValidationException with code `INVALID_NODE_NAME` if the name is not valid.
19
+ */
7
20
  constructor(name, type, line, description) {
8
21
  this.children = new Map();
9
22
  this.values = new Set();
@@ -15,24 +28,40 @@ class NodeDefinition {
15
28
  throw new ValidationException_1.ValidationException(line, "INVALID_NODE_NAME", `Node name not valid: ${name}`);
16
29
  }
17
30
  }
31
+ /** @returns the name of the node, as it appears in the schema. */
18
32
  getName() {
19
33
  return this.name;
20
34
  }
35
+ /** @returns the canonical name of the node. */
21
36
  getNormalizedName() {
22
37
  return this.normalizedName;
23
38
  }
39
+ /** @returns the name of the value type of this node (see {@link TypeRegistry}). */
24
40
  getType() {
25
41
  return this.type;
26
42
  }
43
+ /** @returns the definitions of the expected children, indexed by their qualified canonical name. */
27
44
  getChildren() {
28
45
  return this.children;
29
46
  }
47
+ /** @returns the optional description of the node, or undefined if it has none. */
30
48
  getDescription() {
31
49
  return this.description;
32
50
  }
51
+ /**
52
+ * Sets the optional description of the node.
53
+ *
54
+ * @param description new optional description of the node.
55
+ */
33
56
  setDescription(description) {
34
57
  this.description = description;
35
58
  }
59
+ /**
60
+ * Adds the definition of an expected child.
61
+ *
62
+ * @param childDefinition definition of the child to add.
63
+ * @throws ValidationException with code `CHILD_DEF_ALREADY_DEFINED` if a definition for that child already existed.
64
+ */
36
65
  addChildDefinition(childDefinition) {
37
66
  const qname = childDefinition.getQualifiedName();
38
67
  if (this.children.has(qname)) {
@@ -40,9 +69,16 @@ class NodeDefinition {
40
69
  }
41
70
  this.children.set(qname, childDefinition);
42
71
  }
43
- // STXT-SCHEMA-SPEC 13.9 / STXT-TEMPLATE-SPEC 14.14: no puede haber valores duplicados
44
- // tras la normalización por trim. Mismo código que ChildLineParser: es la misma condición
45
- // por la otra puerta de entrada.
72
+ // STXT-SCHEMA-SPEC 13.9 / STXT-TEMPLATE-SPEC 14.14: there can be no duplicated values
73
+ // after the trim normalization. Same code as ChildLineParser: it is the same condition
74
+ // coming through the other entry point.
75
+ /**
76
+ * Adds a value to the list of values allowed for this node.
77
+ *
78
+ * @param value value to add to the list of allowed values.
79
+ * @param line line number, for the error message.
80
+ * @throws ValidationException with code `VALUE_DUPLICATED` if the value (once trimmed) had already been added.
81
+ */
46
82
  addValue(value, line) {
47
83
  const trimmed = value?.trim() ?? "";
48
84
  if (this.values.has(trimmed)) {
@@ -50,15 +86,23 @@ class NodeDefinition {
50
86
  }
51
87
  this.values.add(trimmed);
52
88
  }
89
+ /**
90
+ * Tells whether a value is allowed for this node.
91
+ *
92
+ * @param value value to check.
93
+ * @returns true if no restricted values are defined, or if the value is among the allowed ones.
94
+ */
53
95
  isAllowedValue(value) {
54
96
  if (this.values.size === 0) {
55
97
  return true;
56
98
  }
57
99
  return this.values.has(value);
58
100
  }
101
+ /** @returns the values allowed for this node (ENUM), or empty if there is no restriction. */
59
102
  getValues() {
60
103
  return this.values;
61
104
  }
105
+ /** @returns a plain object with the definition, so that JSON.stringify serializes it. */
62
106
  toJSON() {
63
107
  return {
64
108
  name: this.getName(),
@@ -1,14 +1,39 @@
1
1
  import { NodeDefinition } from "./NodeDefinition";
2
+ /** Schema of a namespace: the set of {@link NodeDefinition} valid for the nodes of that namespace. */
2
3
  export declare class Schema {
4
+ /** Namespace of the schema language itself, `@stxt.schema`. */
3
5
  static readonly SCHEMA_NAMESPACE = "@stxt.schema";
4
6
  private readonly nodes;
5
7
  private readonly namespace;
6
8
  private readonly description;
9
+ /**
10
+ * Creates an empty schema for a namespace.
11
+ *
12
+ * @param namespace namespace this schema applies to.
13
+ * @param line line number, for the error message.
14
+ * @param description optional description of the schema.
15
+ * @throws ParseException if the namespace is not well formed.
16
+ */
7
17
  constructor(namespace: string | null | undefined, line: number, description: string | undefined);
18
+ /** @returns the node definitions, indexed by their canonical name. */
8
19
  getNodes(): ReadonlyMap<string, NodeDefinition>;
20
+ /**
21
+ * Looks up the definition of a node by name.
22
+ *
23
+ * @param name name of the node to look for.
24
+ * @returns the definition of the node with that name, or undefined if it is not defined in this schema.
25
+ */
9
26
  getNodeDefinition(name: string): NodeDefinition | undefined;
27
+ /**
28
+ * Adds the definition of a node to this schema.
29
+ *
30
+ * @param nodeDefinition node definition to add.
31
+ * @throws ValidationException with code `NODE_DEF_ALREADY_DEFINED` if there already was a node definition with the same name.
32
+ */
10
33
  addNodeDefinition(nodeDefinition: NodeDefinition): void;
34
+ /** @returns the namespace this schema applies to. */
11
35
  getNamespace(): string;
36
+ /** @returns a plain object with the schema, so that JSON.stringify serializes it. */
12
37
  toJSON(): {
13
38
  namespace: string;
14
39
  nodes: {
@@ -26,5 +51,6 @@ export declare class Schema {
26
51
  values: string[];
27
52
  }[];
28
53
  };
54
+ /** @returns the schema as pretty-printed JSON, for debugging. */
29
55
  toString(): string;
30
56
  }
@@ -4,19 +4,41 @@ exports.Schema = void 0;
4
4
  const NamespaceValidator_1 = require("../core/NamespaceValidator");
5
5
  const StringUtils_1 = require("../core/StringUtils");
6
6
  const ValidationException_1 = require("../exceptions/ValidationException");
7
+ /** Schema of a namespace: the set of {@link NodeDefinition} valid for the nodes of that namespace. */
7
8
  class Schema {
9
+ /**
10
+ * Creates an empty schema for a namespace.
11
+ *
12
+ * @param namespace namespace this schema applies to.
13
+ * @param line line number, for the error message.
14
+ * @param description optional description of the schema.
15
+ * @throws ParseException if the namespace is not well formed.
16
+ */
8
17
  constructor(namespace, line, description) {
9
18
  this.nodes = new Map();
10
19
  this.namespace = StringUtils_1.StringUtils.lowerCase(namespace);
11
20
  this.description = description;
12
21
  NamespaceValidator_1.NamespaceValidator.validateNamespaceFormat(this.namespace, line);
13
22
  }
23
+ /** @returns the node definitions, indexed by their canonical name. */
14
24
  getNodes() {
15
25
  return this.nodes;
16
26
  }
27
+ /**
28
+ * Looks up the definition of a node by name.
29
+ *
30
+ * @param name name of the node to look for.
31
+ * @returns the definition of the node with that name, or undefined if it is not defined in this schema.
32
+ */
17
33
  getNodeDefinition(name) {
18
34
  return this.nodes.get(StringUtils_1.StringUtils.normalize(name));
19
35
  }
36
+ /**
37
+ * Adds the definition of a node to this schema.
38
+ *
39
+ * @param nodeDefinition node definition to add.
40
+ * @throws ValidationException with code `NODE_DEF_ALREADY_DEFINED` if there already was a node definition with the same name.
41
+ */
20
42
  addNodeDefinition(nodeDefinition) {
21
43
  const qname = nodeDefinition.getNormalizedName();
22
44
  if (this.nodes.has(qname)) {
@@ -24,20 +46,23 @@ class Schema {
24
46
  }
25
47
  this.nodes.set(qname, nodeDefinition);
26
48
  }
49
+ /** @returns the namespace this schema applies to. */
27
50
  getNamespace() {
28
51
  return this.namespace;
29
52
  }
30
- // Dentro de la clase Schema
53
+ /** @returns a plain object with the schema, so that JSON.stringify serializes it. */
31
54
  toJSON() {
32
55
  return {
33
56
  namespace: this.namespace,
34
57
  nodes: Array.from(this.nodes.values()).map(n => n.toJSON()),
35
58
  };
36
59
  }
60
+ /** @returns the schema as pretty-printed JSON, for debugging. */
37
61
  toString() {
38
62
  return JSON.stringify(this, null, 2); // pretty print
39
63
  }
40
64
  }
41
65
  exports.Schema = Schema;
66
+ /** Namespace of the schema language itself, `@stxt.schema`. */
42
67
  Schema.SCHEMA_NAMESPACE = "@stxt.schema";
43
68
  //# sourceMappingURL=Schema.js.map
@@ -1,3 +1,10 @@
1
1
  import { Schema } from "./Schema";
2
2
  import { Node } from "../core/Node";
3
+ /**
4
+ * Turns the tree of an already parsed `@stxt.schema` document into a {@link Schema}.
5
+ *
6
+ * @param node root node of the schema document, `Schema (@stxt.schema): ...`.
7
+ * @returns the schema the document defines.
8
+ * @throws ValidationException if the document is not a valid `@stxt.schema` one.
9
+ */
3
10
  export declare function transformNodeToSchema(node: Node): Schema;