xnl-core 0.1.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/LICENSE +21 -0
- package/README.md +67 -0
- package/dist/index.cjs +2176 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +154 -0
- package/dist/index.d.ts +154 -0
- package/dist/index.js +2156 -0
- package/dist/index.js.map +1 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 kongweixian
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# XNL Parser (TypeScript)
|
|
2
|
+
|
|
3
|
+
Node + browser-friendly parser for the XNL (Extensible Notation Language) format described in `doc/ai-guide/XnlDataFormatIntroduce.md`.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install xnl-core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { XNL, parseXnl, stringify } from "xnl-core";
|
|
15
|
+
|
|
16
|
+
// Parse many
|
|
17
|
+
const { nodes, warnings } = XNL.parseMany(`<item a=1 { b = 2 } [ 3 4 ]>`);
|
|
18
|
+
|
|
19
|
+
// Parse one
|
|
20
|
+
const { node } = XNL.parseSingle(`<text a=1 {b=2} #>hello</#>`);
|
|
21
|
+
|
|
22
|
+
// Unique children (extend-like)
|
|
23
|
+
const { node: unique } = XNL.parseUnique("root", `<a {x=1}> <a {x=2}> <b>`);
|
|
24
|
+
|
|
25
|
+
// Stringify (compact by default)
|
|
26
|
+
const compact = stringify({ nodes });
|
|
27
|
+
|
|
28
|
+
// Pretty stringify
|
|
29
|
+
const pretty = XNL.stringify({ nodes }, { pretty: true, indent: 2 });
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## API
|
|
33
|
+
|
|
34
|
+
- `parseXnl(input: string): XnlDocument` – parse a full XNL string into an AST of nodes plus optional `warnings`.
|
|
35
|
+
- `parseXnlSingleNode(input: string): { node: XnlNode; warnings?: ParseWarning[] }` – parse exactly one node; errors if extra content remains.
|
|
36
|
+
- `parseUniqueChildren(name: string, input: string, metadata?: AttributeMap, attributes?: AttributeMap): { node: XnlNode; warnings?: ParseWarning[] }` – parse multiple sibling elements into a node whose `extend` children overwrite duplicates with a warning.
|
|
37
|
+
- `XNL` namespace: `XNL.parseMany`, `XNL.parseSingle`, `XNL.parseUnique` (wrappers around the above) and `XNL.stringify`.
|
|
38
|
+
- `XNL.stringify(value: XnlDocument | XnlNode, options?)` – serialize a document or node to XNL. Defaults to compact single-line; pass `{ pretty: true, indent: 2 }` (or string indent) for pretty output.
|
|
39
|
+
- AST nodes carry `tag`, `metadata` (inline `key=value`), optional `{}` `attributes`, optional `body` array (`[]` block), optional unique `extend` children (`()` block with warn+overwrite on duplicate tag names), and optional `text`/`textMarker` for `<tag ... #>...</#...>`.
|
|
40
|
+
- `ValueLiteral` is primitive-only (`String`/`Boolean`/`Null`/`Number`). `ObjectValue.entries`, `ArrayValue.items`, `metadata`/`attributes`, and `body` all hold `XnlNode`, so object/array/attribute entries can themselves be elements, values, or comments.
|
|
41
|
+
- Value literals keep numeric kind metadata (`Integer` vs `Float`) while using `number` values in JS/TS.
|
|
42
|
+
- Multiline text blocks dedent like C# triple-quoted strings: drop a leading blank line, then strip the closing tag’s indentation prefix (spaces/tabs) from each line.
|
|
43
|
+
|
|
44
|
+
### Errors
|
|
45
|
+
|
|
46
|
+
Errors are thrown as `XnlParseError` with `code`, line/column, and tag/marker context in the message:
|
|
47
|
+
|
|
48
|
+
- `UNEXPECTED_EOF` – input ended before a structure closed (message names the tag/delimiter).
|
|
49
|
+
- `MISMATCHED_TAG` – closing text marker did not match opener (message shows expected vs found).
|
|
50
|
+
- `DUPLICATE_CHILD` – extend `( ... )` block repeated a child name (later overwrote earlier).
|
|
51
|
+
- `INVALID_CONTENT` – disallowed content for the current body type (e.g., text with `[]`/`()`), message cites parent tag.
|
|
52
|
+
- `INVALID_LITERAL` – malformed or unsupported literal.
|
|
53
|
+
- `UNEXPECTED_TOKEN` – unexpected character while parsing.
|
|
54
|
+
- Warnings (returned, not logged): `DUPLICATE_CHILD` when extend children repeat names (later overwrites earlier).
|
|
55
|
+
|
|
56
|
+
Example:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { parseXnl, XnlParseError } from "xnl-core";
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
parseXnl("<wrap ( <a> <a> )>");
|
|
63
|
+
} catch (err) {
|
|
64
|
+
const e = err as XnlParseError;
|
|
65
|
+
console.error(e.code, e.message); // DUPLICATE_CHILD ...
|
|
66
|
+
}
|
|
67
|
+
```
|