@stxt-lang/core 0.11.1 → 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.
@@ -12,7 +12,7 @@ import { Line } from "./Line";
12
12
  *
13
13
  * @param line source line, with its indentation.
14
14
  * @param lastNodeBlock true if the node currently open is a BLOCK text node.
15
- * @param lastLevel indentation level of the node currently open.
15
+ * @param lastLevel indentation level of the node currently open, -1 when there is none.
16
16
  * @param numLine line number, for the error messages.
17
17
  * @param validate false to split the line without enforcing the indentation rules.
18
18
  * @returns the line already split into indentation and content.
@@ -18,7 +18,7 @@ const Line_1 = require("./Line");
18
18
  *
19
19
  * @param line source line, with its indentation.
20
20
  * @param lastNodeBlock true if the node currently open is a BLOCK text node.
21
- * @param lastLevel indentation level of the node currently open.
21
+ * @param lastLevel indentation level of the node currently open, -1 when there is none.
22
22
  * @param numLine line number, for the error messages.
23
23
  * @param validate false to split the line without enforcing the indentation rules.
24
24
  * @returns the line already split into indentation and content.
@@ -86,7 +86,9 @@ class Parser {
86
86
  try {
87
87
  const lastNode = stack.length === 0 ? null : stack[stack.length - 1];
88
88
  // The stack holds the open nodes, one per level: its size is the level of the next line's parent
89
- const lastLevel = lastNode ? stack.length - 1 : 0;
89
+ // With no open node the reference level is -1 (spec 8.3): the first line of the
90
+ // document, and the first after every node has been closed, must be at level 0.
91
+ const lastLevel = lastNode ? stack.length - 1 : -1;
90
92
  const lastNodeText = lastNode instanceof TextNode_1.TextNode;
91
93
  // Parse the line
92
94
  const line = (0, LineParser_1.parseLine)(lineString, lastNodeText, lastLevel, lineNumber);
@@ -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.11.1",
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",