@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.
Files changed (120) hide show
  1. package/README.md +116 -1
  2. package/out/all.d.ts +5 -0
  3. package/out/all.js +10 -4
  4. package/out/core/Constants.d.ts +8 -0
  5. package/out/core/Constants.js +8 -0
  6. package/out/core/Line.d.ts +20 -0
  7. package/out/core/Line.js +15 -0
  8. package/out/core/LineParser.d.ts +17 -0
  9. package/out/core/LineParser.js +27 -10
  10. package/out/core/NameNamespace.d.ts +9 -0
  11. package/out/core/NameNamespace.js +9 -0
  12. package/out/core/NameNamespaceParser.d.ts +11 -0
  13. package/out/core/NameNamespaceParser.js +16 -5
  14. package/out/core/NamespaceValidator.d.ts +18 -0
  15. package/out/core/NamespaceValidator.js +18 -0
  16. package/out/core/Node.d.ts +55 -0
  17. package/out/core/Node.js +56 -1
  18. package/out/core/NodeCreator.d.ts +11 -0
  19. package/out/core/NodeCreator.js +14 -3
  20. package/out/core/ParseResult.d.ts +26 -0
  21. package/out/core/ParseResult.js +26 -0
  22. package/out/core/Parser.d.ts +31 -0
  23. package/out/core/Parser.js +50 -19
  24. package/out/core/StringUtils.d.ts +31 -0
  25. package/out/core/StringUtils.js +40 -9
  26. package/out/discovery/DiscoveryEnvironment.d.ts +35 -0
  27. package/out/discovery/DiscoveryEnvironment.js +3 -0
  28. package/out/discovery/DiscoveryError.d.ts +30 -0
  29. package/out/discovery/DiscoveryError.js +36 -0
  30. package/out/discovery/DiscoveryFileSystem.d.ts +57 -0
  31. package/out/discovery/DiscoveryFileSystem.js +3 -0
  32. package/out/discovery/DiscoveryResolver.d.ts +74 -0
  33. package/out/discovery/DiscoveryResolver.js +232 -0
  34. package/out/discovery/DiscoveryResult.d.ts +94 -0
  35. package/out/discovery/DiscoveryResult.js +111 -0
  36. package/out/exceptions/ParseException.d.ts +14 -0
  37. package/out/exceptions/ParseException.js +12 -0
  38. package/out/exceptions/RuntimeException.d.ts +14 -0
  39. package/out/exceptions/RuntimeException.js +13 -0
  40. package/out/exceptions/ValidationException.d.ts +8 -0
  41. package/out/exceptions/ValidationException.js +8 -0
  42. package/out/processors/Observer.d.ts +29 -0
  43. package/out/processors/Validator.d.ts +9 -0
  44. package/out/runtime/ConditionalValidator.d.ts +15 -0
  45. package/out/runtime/ConditionalValidator.js +16 -2
  46. package/out/runtime/NodeWriter.d.ts +18 -0
  47. package/out/runtime/NodeWriter.js +18 -0
  48. package/out/runtime/UnifiedSchemaProvider.d.ts +23 -5
  49. package/out/runtime/UnifiedSchemaProvider.js +28 -10
  50. package/out/schema/ChildDefinition.d.ts +18 -0
  51. package/out/schema/ChildDefinition.js +18 -0
  52. package/out/schema/NodeDefinition.d.ts +44 -0
  53. package/out/schema/NodeDefinition.js +47 -3
  54. package/out/schema/Schema.d.ts +26 -0
  55. package/out/schema/Schema.js +26 -1
  56. package/out/schema/SchemaParser.d.ts +7 -0
  57. package/out/schema/SchemaParser.js +26 -16
  58. package/out/schema/SchemaProvider.d.ts +7 -0
  59. package/out/schema/SchemaProviderMemory.d.ts +27 -0
  60. package/out/schema/SchemaProviderMemory.js +27 -0
  61. package/out/schema/SchemaProviderMeta.d.ts +16 -0
  62. package/out/schema/SchemaProviderMeta.js +16 -0
  63. package/out/schema/SchemaValidator.d.ts +20 -0
  64. package/out/schema/SchemaValidator.js +27 -7
  65. package/out/schema/Type.d.ts +9 -0
  66. package/out/schema/TypeRegistry.d.ts +13 -0
  67. package/out/schema/TypeRegistry.js +18 -5
  68. package/out/schema/type/BASE64.d.ts +1 -0
  69. package/out/schema/type/BASE64.js +5 -4
  70. package/out/schema/type/BINARY.d.ts +1 -0
  71. package/out/schema/type/BINARY.js +2 -1
  72. package/out/schema/type/BLOCK.d.ts +1 -0
  73. package/out/schema/type/BLOCK.js +2 -1
  74. package/out/schema/type/BOOLEAN.d.ts +1 -0
  75. package/out/schema/type/BOOLEAN.js +1 -0
  76. package/out/schema/type/DATE.d.ts +1 -0
  77. package/out/schema/type/DATE.js +1 -0
  78. package/out/schema/type/EMAIL.d.ts +1 -0
  79. package/out/schema/type/EMAIL.js +1 -0
  80. package/out/schema/type/ENUM.d.ts +1 -0
  81. package/out/schema/type/ENUM.js +2 -1
  82. package/out/schema/type/GROUP.d.ts +1 -0
  83. package/out/schema/type/GROUP.js +2 -1
  84. package/out/schema/type/HEXADECIMAL.d.ts +1 -0
  85. package/out/schema/type/HEXADECIMAL.js +2 -1
  86. package/out/schema/type/INLINE.d.ts +1 -0
  87. package/out/schema/type/INLINE.js +2 -1
  88. package/out/schema/type/INTEGER.d.ts +1 -0
  89. package/out/schema/type/INTEGER.js +1 -0
  90. package/out/schema/type/MARKDOWN.d.ts +4 -0
  91. package/out/schema/type/MARKDOWN.js +4 -2
  92. package/out/schema/type/NATURAL.d.ts +1 -0
  93. package/out/schema/type/NATURAL.js +1 -0
  94. package/out/schema/type/NUMBER.d.ts +1 -0
  95. package/out/schema/type/NUMBER.js +1 -0
  96. package/out/schema/type/TEXT.d.ts +1 -0
  97. package/out/schema/type/TEXT.js +1 -0
  98. package/out/schema/type/TIME.d.ts +1 -0
  99. package/out/schema/type/TIME.js +1 -0
  100. package/out/schema/type/TIMESTAMP.d.ts +1 -0
  101. package/out/schema/type/TIMESTAMP.js +1 -0
  102. package/out/schema/type/URL.d.ts +1 -0
  103. package/out/schema/type/URL.js +2 -1
  104. package/out/schema/type/UUID.d.ts +1 -0
  105. package/out/schema/type/UUID.js +1 -0
  106. package/out/schema/type/binaryValue.d.ts +9 -0
  107. package/out/schema/type/binaryValue.js +9 -3
  108. package/out/schema/type/regexType.d.ts +8 -0
  109. package/out/schema/type/regexType.js +9 -1
  110. package/out/template/ChildLine.d.ts +14 -0
  111. package/out/template/ChildLine.js +14 -0
  112. package/out/template/ChildLineParser.d.ts +10 -0
  113. package/out/template/ChildLineParser.js +17 -8
  114. package/out/template/MetaTemplateSchemaProvider.d.ts +16 -0
  115. package/out/template/MetaTemplateSchemaProvider.js +17 -1
  116. package/out/template/TemplateParser.d.ts +8 -0
  117. package/out/template/TemplateParser.js +49 -39
  118. package/out/template/TemplateSchemaProviderMemory.d.ts +18 -0
  119. package/out/template/TemplateSchemaProviderMemory.js +21 -3
  120. package/package.json +2 -1
@@ -5,7 +5,16 @@ const Parser_1 = require("../core/Parser");
5
5
  const ValidationException_1 = require("../exceptions/ValidationException");
6
6
  const RuntimeException_1 = require("../exceptions/RuntimeException");
7
7
  const TemplateParser_1 = require("./TemplateParser");
8
+ /**
9
+ * {@link SchemaProvider} that defines in code the meta-schema of the template language itself
10
+ * (`@stxt.template`), so that a loaded template can validate itself.
11
+ */
8
12
  class MetaTemplateSchemaProvider {
13
+ /**
14
+ * Parses the meta-template and keeps the schema it produces ready to be served.
15
+ *
16
+ * @throws ValidationException with code `META_SCHEMA_INVALID` if the meta-template does not produce exactly one document.
17
+ */
9
18
  constructor() {
10
19
  const parser = new Parser_1.Parser();
11
20
  const nodes = parser.parse(MetaTemplateSchemaProvider.META_TEXT);
@@ -14,11 +23,18 @@ class MetaTemplateSchemaProvider {
14
23
  }
15
24
  this.meta = (0, TemplateParser_1.transformTemplateNodeToSchema)(nodes[0]);
16
25
  }
26
+ /**
27
+ * Serves the meta-schema of the template language.
28
+ *
29
+ * @param namespace namespace whose schema is wanted; only `@stxt.template` is served.
30
+ * @returns the meta-schema of the template language.
31
+ * @throws RuntimeException with code `RESOURCE_NOT_FOUND` if any other namespace is asked for.
32
+ */
17
33
  getSchema(namespace) {
18
34
  if (namespace !== "@stxt.template") {
19
35
  throw new RuntimeException_1.RuntimeException("RESOURCE_NOT_FOUND", `Not found '${namespace}' in namespace: @stxt.template`);
20
36
  }
21
- // meta siempre existe si el constructor terminó, pero lo dejamos equivalente al Java
37
+ // meta always exists once the constructor finished, but this mirrors the Java version
22
38
  if (!this.meta) {
23
39
  throw new ValidationException_1.ValidationException(0, "META_SCHEMA_NOT_AVAILABLE", "Meta schema not available");
24
40
  }
@@ -1,3 +1,11 @@
1
1
  import { Node } from "../core/Node";
2
2
  import { Schema } from "../schema/Schema";
3
+ /**
4
+ * Turns the tree of an already parsed `@stxt.template` document into an equivalent {@link Schema}.
5
+ *
6
+ * @param node root of the already parsed `@stxt.template` document.
7
+ * @returns the resulting {@link Schema}.
8
+ * @throws ValidationException if the template is not valid, with the line already shifted to the
9
+ * one of the original document.
10
+ */
3
11
  export declare function transformTemplateNodeToSchema(node: Node): Schema;
@@ -10,29 +10,37 @@ const StringUtils_1 = require("../core/StringUtils");
10
10
  const ChildLineParser_1 = require("./ChildLineParser");
11
11
  const ParseException_1 = require("../exceptions/ParseException");
12
12
  const TypeRegistry_1 = require("../schema/TypeRegistry");
13
+ /**
14
+ * Turns the tree of an already parsed `@stxt.template` document into an equivalent {@link Schema}.
15
+ *
16
+ * @param node root of the already parsed `@stxt.template` document.
17
+ * @returns the resulting {@link Schema}.
18
+ * @throws ValidationException if the template is not valid, with the line already shifted to the
19
+ * one of the original document.
20
+ */
13
21
  function transformTemplateNodeToSchema(node) {
14
- // Insertamos namespace
22
+ // Set the namespace
15
23
  const result = new Schema_1.Schema(node.getValue(), node.getLine(), undefined);
16
- // Buscamos nodo structure
24
+ // Look for the structure node
17
25
  const structure = node.getChild("structure");
18
26
  if (!structure) {
19
27
  throw new ValidationException_1.ValidationException(node.getLine(), "TEMPLATE_STRUCTURE_REQUIRED", "Template must define 'Structure >>'");
20
28
  }
21
29
  const text = structure.getText();
22
30
  const offset = structure.getLine();
23
- // Creamos un parser simple
31
+ // Build a plain parser
24
32
  const parser = new Parser_1.Parser();
25
- // Parseamos para los nodos
33
+ // Parse to get the nodes
26
34
  try {
27
35
  const nodes = parser.parse(text);
28
- // Vamos iterando todos los nodos insertando
36
+ // Walk every node adding it to the schema
29
37
  for (const n of nodes) {
30
38
  addToSchema(result, n);
31
39
  }
32
40
  }
33
41
  catch (e) {
34
- // ValidationException extiende ParseException: comprobar primero para no degradar
35
- // la severidad (la extensión pinta ValidationException como Warning)
42
+ // ValidationException extends ParseException: check it first so the severity is not
43
+ // downgraded (the extension paints ValidationException as a Warning)
36
44
  if (e instanceof ValidationException_1.ValidationException) {
37
45
  throw new ValidationException_1.ValidationException(e.line + offset, e.code, e.message);
38
46
  }
@@ -41,7 +49,7 @@ function transformTemplateNodeToSchema(node) {
41
49
  }
42
50
  throw e;
43
51
  }
44
- // Buscamos descripciones
52
+ // Look for the descriptions
45
53
  const description = node.getChild("description");
46
54
  if (description) {
47
55
  const text = description.getText();
@@ -59,21 +67,22 @@ function transformTemplateNodeToSchema(node) {
59
67
  throw e;
60
68
  }
61
69
  }
62
- // Retornamos resultado
70
+ // Return the result
63
71
  return result;
64
72
  }
73
+ /** Adds to the schema the definition a node of the structure declares, along with its children. */
65
74
  function addToSchema(schema, node) {
66
- // Obtenemos nombre qualificado
75
+ // Get the qualified name
67
76
  let namespace = node.getNamespace();
68
77
  const name = node.getName();
69
- // Miramos datos
78
+ // Look at the data
70
79
  let cl = ChildLineParser_1.ChildLineParser.parse(node.getValue(), node.getLine());
71
80
  if (!namespace || namespace === "") {
72
81
  namespace = schema.getNamespace();
73
82
  }
74
83
  if (namespace !== schema.getNamespace()) {
75
- // Un nodo externo solo puede declarar cardinalidad: ni tipo, ni valores ENUM,
76
- // ni hijos (STXT-TEMPLATE-SPEC 6.4, 10 y 14.15)
84
+ // An external node may only declare cardinality: no type, no ENUM values
85
+ // and no children (STXT-TEMPLATE-SPEC 6.4, 10 and 14.15)
77
86
  const type = cl.getType();
78
87
  if (type && type.trim().length > 0) {
79
88
  throw new ValidationException_1.ValidationException(node.getLine(), "TYPE_DEFINITION_NOT_ALLOWED", "Not allowed type definition in external namespaces");
@@ -85,17 +94,17 @@ function addToSchema(schema, node) {
85
94
  if (node.getChildren().length > 0) {
86
95
  throw new ValidationException_1.ValidationException(node.getLine(), "CHILDREN_NOT_ALLOWED_IN_EXTERNAL_NAMESPACE", `Not allowed children in external namespaces (node ${node.getName()})`);
87
96
  }
88
- // No hacemos nada con creación de nodos que no son de @stxt.template!!
97
+ // Nodes that do not belong to @stxt.template create nothing here!!
89
98
  return;
90
99
  }
91
- // Miramos si es nuevo y añadimos en listado
100
+ // Check whether it is new and add it to the list
92
101
  let schemaNode = schema.getNodeDefinition(name);
93
102
  if (!schemaNode) {
94
- // Nuevo
103
+ // New one
95
104
  const type = cl.getType() ?? "INLINE";
96
- // En este punto el schema ya contiene tanto las definiciones previas ya cerradas
97
- // como los ancestros abiertos, así que una referencia que no resuelve aquí no
98
- // resuelve a nada (STXT-TEMPLATE-SPEC 6.4 y 14.11)
105
+ // At this point the schema already holds both the previous definitions, already closed,
106
+ // and the open ancestors, so a reference that does not resolve here resolves to
107
+ // nothing (STXT-TEMPLATE-SPEC 6.4 and 14.11)
99
108
  if (type.startsWith("@")) {
100
109
  throw new ValidationException_1.ValidationException(node.getLine(), "REFERENCE_NOT_FOUND", `Reference '${type}' does not point to a previous definition or an open ancestor`);
101
110
  }
@@ -107,16 +116,16 @@ function addToSchema(schema, node) {
107
116
  const values = cl.getValues();
108
117
  if (values) {
109
118
  if (type !== "ENUM") {
110
- // Mismo código que SchemaParser: un template es azúcar equivalente a un schema
111
- // (STXT-TEMPLATE-SPEC 13), así que la misma condición no debe cambiar de código
112
- // según la puerta de entrada
119
+ // Same code as SchemaParser: a template is sugar equivalent to a schema
120
+ // (STXT-TEMPLATE-SPEC 13), so the same condition must not change its code
121
+ // depending on the entry point
113
122
  throw new ValidationException_1.ValidationException(node.getLine(), "VALUES_ONLY_SUPPORTED_BY_ENUM", `Values only supported for type ENUM, not for type ${type}`);
114
123
  }
115
124
  for (const v of values) {
116
125
  schemaNode.addValue(v, node.getLine());
117
126
  }
118
127
  }
119
- // Un ENUM sin lista de valores es un template inválido (STXT-TEMPLATE-SPEC 9 y 13.7)
128
+ // An ENUM with no list of values is an invalid template (STXT-TEMPLATE-SPEC 9 and 13.7)
120
129
  if (type === "ENUM" && (!values || values.length === 0)) {
121
130
  throw new ValidationException_1.ValidationException(node.getLine(), "VALUES_EMPTY_FOR_ENUM", "ENUM Type must include values");
122
131
  }
@@ -127,7 +136,7 @@ function addToSchema(schema, node) {
127
136
  throw new ValidationException_1.ValidationException(node.getLine(), "NODE_DEFINED_MULTIPLE_TIMES", `Multiple node reference must start with @: ${node.getName()}`);
128
137
  }
129
138
  const reference = type.substring(1).trim();
130
- // Referencia y tipo explícito en la misma línea (STXT-TEMPLATE-SPEC 14.13)
139
+ // Reference and explicit type on the same line (STXT-TEMPLATE-SPEC 14.13)
131
140
  const explicitType = referenceType(reference, node.getNormalizedName());
132
141
  if (explicitType) {
133
142
  throw new ValidationException_1.ValidationException(node.getLine(), "REFERENCE_WITH_TYPE_NOT_ALLOWED", `Reference '@${node.getName()}' can not declare a type: ${explicitType}`);
@@ -135,8 +144,8 @@ function addToSchema(schema, node) {
135
144
  if (StringUtils_1.StringUtils.normalize(reference) !== node.getNormalizedName()) {
136
145
  throw new ValidationException_1.ValidationException(node.getLine(), "NODE_REFERENCE_NOT_VALID", `Reference must be '@${node.getName()}', not '${reference}'`);
137
146
  }
138
- // La referencia puede sobrescribir la cardinalidad, pero no redefinir valores
139
- // ENUM ni hijos (STXT-TEMPLATE-SPEC 6.4)
147
+ // A reference may override the cardinality, but it may redefine neither the ENUM
148
+ // values nor the children (STXT-TEMPLATE-SPEC 6.4)
140
149
  const values = cl.getValues();
141
150
  if (values) {
142
151
  throw new ValidationException_1.ValidationException(node.getLine(), "VALUES_NOT_ALLOWED_IN_REFERENCE", `Reference '@${node.getName()}' can not redefine ENUM values`);
@@ -146,13 +155,13 @@ function addToSchema(schema, node) {
146
155
  }
147
156
  return; // OK Definition
148
157
  }
149
- // Una vez ya existe, si tiene hijos los intentamos crear.
158
+ // Once it exists, try to create its children if it has any.
150
159
  const childrenNode = node.getChildren();
151
- // Error de template 14.9: nodo con hijos y tipo efectivo que no admite hijos
160
+ // Template error 14.9: node with children and an effective type that does not admit children
152
161
  if (childrenNode.length > 0 && !TypeRegistry_1.TypeRegistry.admitsChildren(schemaNode.getType())) {
153
162
  throw new ValidationException_1.ValidationException(node.getLine(), "CHILDREN_NOT_ALLOWED_FOR_TYPE", `Type ${schemaNode.getType()} does not allow children (node ${node.getName()})`);
154
163
  }
155
- // Insertamos childs
164
+ // Add the children
156
165
  for (const child of childrenNode) {
157
166
  cl = ChildLineParser_1.ChildLineParser.parse(child.getValue(), child.getLine());
158
167
  const childName = child.getName();
@@ -166,11 +175,11 @@ function addToSchema(schema, node) {
166
175
  }
167
176
  }
168
177
  /**
169
- * Distingue `@Nombre Nodo TIPO` (referencia + tipo, error 14.13) de `@Otro Nombre`
170
- * (referencia con nombre distinto, error 14.12). Como los nombres de nodo admiten
171
- * espacios, la única lectura fiable es: si el último token es un tipo conocido y lo
172
- * que queda delante es el nombre del propio nodo, la línea declara ambas cosas.
173
- * Devuelve el tipo declarado, o null si la referencia no lleva tipo.
178
+ * Tells `@Node Name TYPE` (reference + type, error 14.13) apart from `@Another Name`
179
+ * (reference with a different name, error 14.12). Since node names may contain spaces,
180
+ * the only reliable reading is: if the last token is a known type and what comes before
181
+ * it is the name of the node itself, the line is declaring both things.
182
+ * Returns the declared type, or null when the reference carries no type.
174
183
  */
175
184
  function referenceType(reference, normalizedName) {
176
185
  const cut = reference.lastIndexOf(" ");
@@ -184,27 +193,28 @@ function referenceType(reference, normalizedName) {
184
193
  }
185
194
  return null;
186
195
  }
196
+ /** Attaches to the node definitions the descriptions declared in the `Description >>` block. */
187
197
  function addDescriptions(schema, nodes) {
188
198
  nodes.forEach((node) => {
189
- // Obtenemos namespace
199
+ // Get the namespace
190
200
  let namespace = node.getNamespace();
191
201
  if (!namespace || namespace === "") {
192
202
  namespace = schema.getNamespace();
193
203
  }
194
- // Validamos no external description
204
+ // No external descriptions
195
205
  if (namespace !== schema.getNamespace()) {
196
206
  throw new ValidationException_1.ValidationException(node.getLine(), "EXTERNAL_DESCRIPTION_NOT_ALLOWED", "Not allowed description in external namespaces");
197
207
  }
198
- // Validamos sin hijos
208
+ // No children either
199
209
  if (node.getChildren().length > 0) {
200
210
  throw new ValidationException_1.ValidationException(node.getLine(), "CHILDREN_DESCRIPTION_NOT_ALLOWED", "Not allowed children in description");
201
211
  }
202
- // Buscamos nodo de esquema
212
+ // Look for the node in the schema
203
213
  const nodeDef = schema.getNodeDefinition(node.getName());
204
214
  if (!nodeDef) {
205
215
  throw new ValidationException_1.ValidationException(node.getLine(), "NODE_NOT_FOUND", `Not found node with name: ${node.getName()}`);
206
216
  }
207
- // No se permite más de una entrada por nodo (STXT-TEMPLATE-SPEC 12)
217
+ // No more than one entry per node is allowed (STXT-TEMPLATE-SPEC 12)
208
218
  if (nodeDef.getDescription() !== undefined) {
209
219
  throw new ValidationException_1.ValidationException(node.getLine(), "DESCRIPTION_ALREADY_DEFINED", `Exists a previous description for node: ${node.getName()}`);
210
220
  }
@@ -1,6 +1,24 @@
1
1
  import { SchemaProviderMemory } from "../schema/SchemaProviderMemory";
2
2
  import { SchemaProvider } from "../schema/SchemaProvider";
3
+ /**
4
+ * In-memory {@link SchemaProvider} fed with `@stxt.template` documents: each template is turned
5
+ * into its equivalent {@link Schema} and registered under its own namespace.
6
+ */
3
7
  export declare class TemplateSchemaProviderMemory extends SchemaProviderMemory {
8
+ /**
9
+ * Creates an empty provider.
10
+ *
11
+ * @param parent provider to fall back to when a namespace is not registered here; the
12
+ * template meta-schema provider when omitted.
13
+ */
4
14
  constructor(parent?: SchemaProvider | null | undefined);
15
+ /**
16
+ * Parses a template document, validates it against the template meta-schema and registers the
17
+ * schema it produces.
18
+ *
19
+ * @param template text of the `@stxt.template` document.
20
+ * @throws ValidationException with code `INVALID_SCHEMA` if the document does not hold exactly
21
+ * one template, or if the resulting schema has no namespace.
22
+ */
5
23
  addTemplate(template: string): void;
6
24
  }
@@ -8,25 +8,43 @@ const ValidationException_1 = require("../exceptions/ValidationException");
8
8
  const MetaTemplateSchemaProvider_1 = require("./MetaTemplateSchemaProvider");
9
9
  const SchemaProviderMemory_1 = require("../schema/SchemaProviderMemory");
10
10
  const TemplateParser_1 = require("./TemplateParser");
11
+ /**
12
+ * In-memory {@link SchemaProvider} fed with `@stxt.template` documents: each template is turned
13
+ * into its equivalent {@link Schema} and registered under its own namespace.
14
+ */
11
15
  class TemplateSchemaProviderMemory extends SchemaProviderMemory_1.SchemaProviderMemory {
16
+ /**
17
+ * Creates an empty provider.
18
+ *
19
+ * @param parent provider to fall back to when a namespace is not registered here; the
20
+ * template meta-schema provider when omitted.
21
+ */
12
22
  constructor(parent) {
13
23
  if (!parent) {
14
24
  parent = new MetaTemplateSchemaProvider_1.MetaTemplateSchemaProvider();
15
25
  }
16
26
  super(parent);
17
27
  }
28
+ /**
29
+ * Parses a template document, validates it against the template meta-schema and registers the
30
+ * schema it produces.
31
+ *
32
+ * @param template text of the `@stxt.template` document.
33
+ * @throws ValidationException with code `INVALID_SCHEMA` if the document does not hold exactly
34
+ * one template, or if the resulting schema has no namespace.
35
+ */
18
36
  addTemplate(template) {
19
37
  const parser = new Parser_1.Parser();
20
38
  const nodes = parser.parse(template);
21
39
  if (nodes.length !== 1) {
22
40
  throw new ValidationException_1.ValidationException(0, "INVALID_SCHEMA", `There are ${nodes.length}, and expected is 1`);
23
41
  }
24
- // Validamos el template contra el meta-schema de templates
42
+ // Validate the template against the template meta-schema
25
43
  const schemaValidator = new SchemaValidator_1.SchemaValidator(new MetaTemplateSchemaProvider_1.MetaTemplateSchemaProvider(), true);
26
44
  schemaValidator.validate(nodes[0]);
27
- // Generamos schema desde el template
45
+ // Build the schema out of the template
28
46
  const sch = (0, TemplateParser_1.transformTemplateNodeToSchema)(nodes[0]);
29
- // Check mínimo de seguridad (en Java también se controlaba el namespace esperado)
47
+ // Minimum safety check (Java checked the expected namespace here too)
30
48
  if (!sch.getNamespace() || sch.getNamespace().trim().length === 0) {
31
49
  throw new ValidationException_1.ValidationException(0, "INVALID_SCHEMA", "Schema namespace is empty");
32
50
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stxt-lang/core",
3
- "version": "0.5.2",
3
+ "version": "0.6.0",
4
4
  "description": "Parser and schema validator for STXT, an indentation-based structured-text format.",
5
5
  "main": "out/all.js",
6
6
  "types": "out/all.d.ts",
@@ -11,6 +11,7 @@
11
11
  "out/all.js",
12
12
  "out/all.d.ts",
13
13
  "out/core",
14
+ "out/discovery",
14
15
  "out/exceptions",
15
16
  "out/processors",
16
17
  "out/runtime",