@stxt-lang/core 0.12.0 → 0.13.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.
@@ -46,8 +46,9 @@ export interface FormatResult {
46
46
  * new style; the remainder only survives in documents with errors, which this conversion
47
47
  * neither repairs nor hides.
48
48
  *
49
- * The result is idempotent, round-trips between the two styles, and produces the same canonical
50
- * tree (STXT-TREE-SPEC) as the source. The document is parsed without any schema: formatting
49
+ * These are the rules of STXT-TREE-SPEC 12 (`stxt-impl/core/formatter.txt`). The result is
50
+ * idempotent, round-trips between the two styles, and produces the same canonical tree as the
51
+ * source; an initial BOM is removed. The document is parsed without any schema: formatting
51
52
  * has nothing to do with validation.
52
53
  */
53
54
  export declare class Formatter {
@@ -37,8 +37,9 @@ const NodeWriter_1 = require("./NodeWriter");
37
37
  * new style; the remainder only survives in documents with errors, which this conversion
38
38
  * neither repairs nor hides.
39
39
  *
40
- * The result is idempotent, round-trips between the two styles, and produces the same canonical
41
- * tree (STXT-TREE-SPEC) as the source. The document is parsed without any schema: formatting
40
+ * These are the rules of STXT-TREE-SPEC 12 (`stxt-impl/core/formatter.txt`). The result is
41
+ * idempotent, round-trips between the two styles, and produces the same canonical tree as the
42
+ * source; an initial BOM is removed. The document is parsed without any schema: formatting
42
43
  * has nothing to do with validation.
43
44
  */
44
45
  class Formatter {
@@ -51,6 +52,10 @@ class Formatter {
51
52
  * @returns the formatted text and the syntax errors found; see {@link FormatResult}.
52
53
  */
53
54
  static format(text, style = NodeWriter_1.IndentStyle.TABS) {
55
+ // STXT-TREE-SPEC 12.1: an initial BOM is not kept
56
+ if (text.startsWith("\uFEFF")) {
57
+ text = text.substring(1);
58
+ }
54
59
  const sourceLines = new SourceLines();
55
60
  const parser = new Parser_1.Parser();
56
61
  parser.registerObserver(sourceLines);
@@ -25,6 +25,13 @@ export declare class NodeWriter {
25
25
  * @returns the documents serialized to STXT text.
26
26
  */
27
27
  static toSTXTDocs(docs: ReadonlyArray<Node>, style?: IndentStyle): string;
28
+ /**
29
+ * Writes one node and, recursively, its children, in the canonical text form of
30
+ * STXT-TREE-SPEC 11.1.
31
+ *
32
+ * @param parentNs effective namespace of the parent, "" for a root: the namespace is
33
+ * declared only where it changes (rule 3), wherever the source declared it.
34
+ */
28
35
  private static writeNode;
29
36
  private static indent;
30
37
  }
@@ -23,7 +23,7 @@ class NodeWriter {
23
23
  */
24
24
  static toSTXT(node, style = IndentStyle.TABS) {
25
25
  const out = [];
26
- NodeWriter.writeNode(out, node, 0, style);
26
+ NodeWriter.writeNode(out, node, 0, style, "");
27
27
  return out.join("");
28
28
  }
29
29
  /**
@@ -39,17 +39,22 @@ class NodeWriter {
39
39
  if (i > 0) {
40
40
  out.push("\n");
41
41
  }
42
- NodeWriter.writeNode(out, docs[i], 0, style);
42
+ NodeWriter.writeNode(out, docs[i], 0, style, "");
43
43
  }
44
44
  return out.join("");
45
45
  }
46
- static writeNode(out, n, depth, style) {
46
+ /**
47
+ * Writes one node and, recursively, its children, in the canonical text form of
48
+ * STXT-TREE-SPEC 11.1.
49
+ *
50
+ * @param parentNs effective namespace of the parent, "" for a root: the namespace is
51
+ * declared only where it changes (rule 3), wherever the source declared it.
52
+ */
53
+ static writeNode(out, n, depth, style, parentNs) {
47
54
  NodeWriter.indent(out, depth, style);
48
- // The namespace is written where the node declares it; inherited ones are implicit,
49
- // exactly as in the source (the effective namespace is the same either way)
50
- const ns = n.getDeclaredNamespace();
55
+ const ns = n.getNamespace();
51
56
  out.push(n.getName());
52
- if (ns.length > 0) {
57
+ if (ns !== parentNs) {
53
58
  out.push(" (", ns, ")");
54
59
  }
55
60
  if (n instanceof TextNode_1.TextNode) {
@@ -67,7 +72,7 @@ class NodeWriter {
67
72
  }
68
73
  out.push("\n");
69
74
  for (const child of n.getChildren()) {
70
- NodeWriter.writeNode(out, child, depth + 1, style);
75
+ NodeWriter.writeNode(out, child, depth + 1, style, ns);
71
76
  }
72
77
  }
73
78
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stxt-lang/core",
3
- "version": "0.12.0",
3
+ "version": "0.13.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",