@ldclabs/kip-lang 0.4.0 → 2.0.1
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/CHANGELOG.md +53 -0
- package/LICENSE +21 -0
- package/README.md +89 -64
- package/dist/ast.d.ts +511 -145
- package/dist/ast.d.ts.map +1 -1
- package/dist/diagnostics.d.ts +8 -2
- package/dist/diagnostics.d.ts.map +1 -1
- package/dist/diagnostics.js +32 -3
- package/dist/diagnostics.js.map +1 -1
- package/dist/exec-ast.d.ts +514 -149
- package/dist/exec-ast.d.ts.map +1 -1
- package/dist/exec-ast.js +8 -7
- package/dist/exec-ast.js.map +1 -1
- package/dist/formatter.d.ts.map +1 -1
- package/dist/formatter.js +870 -479
- package/dist/formatter.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/lexer.d.ts.map +1 -1
- package/dist/lexer.js +12 -30
- package/dist/lexer.js.map +1 -1
- package/dist/lower.d.ts +1 -2
- package/dist/lower.d.ts.map +1 -1
- package/dist/lower.js +1355 -594
- package/dist/lower.js.map +1 -1
- package/dist/parser.d.ts.map +1 -1
- package/dist/parser.js +2410 -1300
- package/dist/parser.js.map +1 -1
- package/dist/semantics.d.ts +11 -7
- package/dist/semantics.d.ts.map +1 -1
- package/dist/semantics.js +295 -180
- package/dist/semantics.js.map +1 -1
- package/dist/token.d.ts +130 -40
- package/dist/token.d.ts.map +1 -1
- package/dist/token.js +264 -83
- package/dist/token.js.map +1 -1
- package/dist/version.d.ts +2 -2
- package/dist/version.js +2 -2
- package/package.json +35 -5
- package/src/ast.ts +914 -0
- package/src/budget.ts +108 -0
- package/src/diagnostics.ts +182 -0
- package/src/errors.ts +42 -0
- package/src/exec-ast.ts +614 -0
- package/src/formatter.ts +1339 -0
- package/src/index.ts +226 -0
- package/src/lexer.ts +459 -0
- package/src/lower.ts +2094 -0
- package/src/parser.ts +3506 -0
- package/src/semantics.ts +392 -0
- package/src/token.ts +408 -0
- package/src/version.ts +13 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@ldclabs/kip-lang` are documented here.
|
|
4
|
+
|
|
5
|
+
## 2.0.0
|
|
6
|
+
|
|
7
|
+
Targets KIP specification revision `2.0-draft`. This is a rewrite of the
|
|
8
|
+
language surface, not an increment: KIP 2.0 command text is not backward
|
|
9
|
+
compatible with the 1.0 grammar the 0.x line implemented.
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- **Executable AST.** `lower` / `lowerAll` / `lowerStatement` turn a syntax
|
|
14
|
+
tree into the closed `Command` AST an engine runs, rejecting what the
|
|
15
|
+
grammar admits but the language does not with a KIP error code.
|
|
16
|
+
- **KQL 2.0**: the five Core element patterns (`CONCEPT`, `PROPOSITION`,
|
|
17
|
+
`ASSERTION`, `EVIDENCE`, `ACTIVITY`), `STRUCTURAL` topology patterns,
|
|
18
|
+
`BELIEF` / `BELIEF SLOT` projection patterns, the independent `AS OF`
|
|
19
|
+
(`SEQ` / `TX`) and `FOR TIME` axes, `WITH EPISTEMIC`, multi-key `ORDER BY`,
|
|
20
|
+
and `CURSOR` paging.
|
|
21
|
+
- **KML 2.0**: `MUTATE` transactions, `CREATE`/`UPSERT CONCEPT`,
|
|
22
|
+
`ENSURE PROPOSITION`, the `ASSERT` sugar with `SUPERSEDING`,
|
|
23
|
+
`CREATE EVIDENCE | ASSERTION | ACTIVITY`, generic `UPDATE`, the
|
|
24
|
+
`RETRACT` / `SUPERSEDE` / `CORRECT` / `TRANSITION` lifecycle,
|
|
25
|
+
`SET RETENTION`, `ARCHIVE`, `TOMBSTONE`, `PURGE ... CONFIRM "PURGE"`, and
|
|
26
|
+
`MERGE CONCEPT`, with `EXPECT VERSION` / `EXPECT STATE` preconditions.
|
|
27
|
+
- **`UNSET STRUCTURAL`**, so every `SET` clause has its removal counterpart.
|
|
28
|
+
- **META 2.0**: every `DESCRIBE`, `LIST`, `SEARCH`, `VERIFY`, `VALIDATE`,
|
|
29
|
+
`PREVIEW`, `HISTORY`, `CHANGES`, `SNAPSHOT` and `EXPORT CAPSULE` target.
|
|
30
|
+
- **`validateExecutable(program)`**, chained into `diagnose`, so the editor
|
|
31
|
+
reports any statement that parses but cannot lower.
|
|
32
|
+
- **`checkBudget` / `checkBatchBudget`** (`KIP_4002`) matching `anda_kip`'s
|
|
33
|
+
ceilings, so a command one engine refuses on size is refused by all.
|
|
34
|
+
- `KipSyntaxError` carrying KIP error codes; `PARSER_VERSION` and
|
|
35
|
+
`KIP_SPEC_REVISION` exports.
|
|
36
|
+
- `src/` ships alongside `dist/`, so published source maps resolve.
|
|
37
|
+
|
|
38
|
+
### Changed
|
|
39
|
+
|
|
40
|
+
- Keywords are ASCII case-insensitive and **contextual, not reserved** — `by`,
|
|
41
|
+
`mode`, `type`, `status` remain usable as object keys and dot-path steps.
|
|
42
|
+
- The parser enforces the canonical KQL trailing-clause order (`AS OF`,
|
|
43
|
+
`FOR TIME`, `WITH EPISTEMIC`, `ORDER BY`, `LIMIT`, `CURSOR`) while still
|
|
44
|
+
recovering from a mistake.
|
|
45
|
+
- Proposition subjects lower as Element references only; KML predicates and
|
|
46
|
+
structural edges take an exact quoted name or a `:parameter`, never a
|
|
47
|
+
`?variable`.
|
|
48
|
+
- Unbound `MUTATE` references (`KIP_2102`) are errors, not warnings.
|
|
49
|
+
- The formatter keeps a comment attached to the body clause it annotates.
|
|
50
|
+
|
|
51
|
+
## 0.4.0 and earlier
|
|
52
|
+
|
|
53
|
+
Implemented the KIP 1.0 command-text surface. See the git history.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 LDC Labs
|
|
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
CHANGED
|
@@ -1,18 +1,32 @@
|
|
|
1
1
|
# @ldclabs/kip-lang
|
|
2
2
|
|
|
3
|
-
TypeScript toolkit for **KIP** (Knowledge Interaction Protocol) —
|
|
3
|
+
TypeScript toolkit for **KIP** (Knowledge Interaction Protocol) — the cognitive
|
|
4
|
+
state protocol between an agent and a persistent Cognitive Nexus.
|
|
4
5
|
|
|
5
6
|
Provides a full-featured **lexer → parser → AST → formatter / diagnostics**
|
|
6
7
|
pipeline for `.kip` files, plus **`lower`**, which turns a syntax tree into the
|
|
7
8
|
executable AST a KIP engine runs.
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
This package targets the normative **KIP 2.0 draft** command-text surface. It
|
|
11
|
+
implements the KQL, KML, and META syntax surfaces, but it does not claim a
|
|
12
|
+
complete KIP conformance profile by itself: Schema resolution, Governance,
|
|
13
|
+
transactions, persistence, projection, history, Capsules, and runtime
|
|
14
|
+
envelopes remain engine responsibilities.
|
|
15
|
+
|
|
16
|
+
The parser targets the KIP 2.0 command-text syntax — all three languages:
|
|
17
|
+
**KQL** (`FIND`) to read, **KML** (`ASSERT`, `MUTATE`, `CREATE`, `UPSERT`,
|
|
18
|
+
`UPDATE`, and the retract/supersede/archive/tombstone/purge lifecycle) to
|
|
19
|
+
change cognition, and **META** (`DESCRIBE`, `LIST`, `SEARCH`, `VERIFY`,
|
|
20
|
+
`VALIDATE`, `PREVIEW`, `HISTORY`, `CHANGES`, `SNAPSHOT`, `EXPORT CAPSULE`) to
|
|
21
|
+
ground and introspect. That includes the five Core element kinds (Concept,
|
|
22
|
+
Proposition, Assertion, Evidence, Activity), `BELIEF` / `BELIEF SLOT`
|
|
23
|
+
projection patterns, `STRUCTURAL` topology patterns, facets, the independent
|
|
24
|
+
`AS OF` / `FOR TIME` time axes, `:parameter` placeholders in full value
|
|
25
|
+
positions, ASCII case-insensitive keywords, JSON-compatible object literals
|
|
26
|
+
with unquoted identifier keys, and batch-friendly multi-command source text.
|
|
27
|
+
The request envelope (`kip`, `request_id`, `space`, `execution`, `ingest`,
|
|
28
|
+
`operations`) is JSON outside the `.kip` language and should be validated by
|
|
29
|
+
the caller.
|
|
16
30
|
|
|
17
31
|
## Two trees, two audiences
|
|
18
32
|
|
|
@@ -21,13 +35,14 @@ text — everything a formatter or an editor needs to reproduce the source, and
|
|
|
21
35
|
error recovery that keeps producing a tree after a mistake.
|
|
22
36
|
|
|
23
37
|
`lower` returns the **executable AST**: every construct collapsed to the one
|
|
24
|
-
shape it means, with the open-ended parts of the grammar closed.
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
38
|
+
shape it means, with the open-ended parts of the grammar closed. The `ASSERT`
|
|
39
|
+
sugar desugars to exactly the `ENSURE PROPOSITION` + `CREATE ASSERTION`
|
|
40
|
+
(+ `SUPERSEDE`) it stands for, and nothing more; a filter becomes a
|
|
41
|
+
comparison, a logical node, a negation, or a call to a registered function; a
|
|
42
|
+
variable becomes a name and a dot path. Anything the syntax admits but the
|
|
43
|
+
language does not — an unknown filter function, an `UPSERT` identified by name
|
|
44
|
+
alone, an `UPDATE` that reaches into Assertion epistemic payload — is rejected
|
|
45
|
+
here, with a KIP error code.
|
|
31
46
|
|
|
32
47
|
That split is why the two exist: an editor wants the loosest tree it can get,
|
|
33
48
|
an engine wants the tightest.
|
|
@@ -49,16 +64,20 @@ const tokens = tokenize('FIND(?x.name) WHERE { ?x {type: "Person"} }')
|
|
|
49
64
|
// Token[] with type, value, line, column, offset
|
|
50
65
|
```
|
|
51
66
|
|
|
67
|
+
Keywords are ASCII case-insensitive, so `find` and `FIND` produce the same
|
|
68
|
+
token type; schema symbols and string values keep their own case-sensitive
|
|
69
|
+
contracts.
|
|
70
|
+
|
|
52
71
|
### Parse
|
|
53
72
|
|
|
54
73
|
```ts
|
|
55
74
|
import { parse } from '@ldclabs/kip-lang'
|
|
56
75
|
|
|
57
76
|
const { ast, diagnostics } = parse(`
|
|
58
|
-
FIND(?
|
|
77
|
+
FIND(?belief.status, ?tz)
|
|
59
78
|
WHERE {
|
|
60
|
-
?
|
|
61
|
-
(?
|
|
79
|
+
?person {type: "Person", name: "Alice"}
|
|
80
|
+
?belief BELIEF (?person, "timezone", ?tz)
|
|
62
81
|
}
|
|
63
82
|
LIMIT 10
|
|
64
83
|
`)
|
|
@@ -72,35 +91,35 @@ console.log(diagnostics) // [] (no errors)
|
|
|
72
91
|
```ts
|
|
73
92
|
import { format } from '@ldclabs/kip-lang'
|
|
74
93
|
|
|
75
|
-
const source = `
|
|
94
|
+
const source = `CREATE CONCEPT ?exp { TYPE "Experience" NAME "Deploy v2 failure" SET ATTRIBUTES { goal: :goal, outcome_status: "failure" } }`
|
|
76
95
|
|
|
77
96
|
const formatted = format(source, {
|
|
78
|
-
indentSize: 4,
|
|
79
|
-
sortAttributes:
|
|
97
|
+
indentSize: 4, // default: 4
|
|
98
|
+
sortAttributes: false // default: false — author key order is preserved
|
|
80
99
|
})
|
|
81
100
|
```
|
|
82
101
|
|
|
83
102
|
Output:
|
|
84
103
|
|
|
85
104
|
```kip
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
105
|
+
CREATE CONCEPT ?exp {
|
|
106
|
+
TYPE "Experience"
|
|
107
|
+
NAME "Deploy v2 failure"
|
|
108
|
+
SET ATTRIBUTES {goal: :goal, outcome_status: "failure"}
|
|
91
109
|
}
|
|
92
110
|
```
|
|
93
111
|
|
|
94
|
-
The formatter preserves comments and quoted/unquoted key styles from the
|
|
95
|
-
|
|
96
|
-
|
|
112
|
+
The formatter preserves comments and quoted/unquoted key styles from the
|
|
113
|
+
original source. `sortAttributes` alphabetizes `SET ATTRIBUTES` keys, and skips
|
|
114
|
+
any block holding a comment, since reordering would detach the comment from
|
|
115
|
+
its key.
|
|
97
116
|
|
|
98
117
|
### Diagnose
|
|
99
118
|
|
|
100
119
|
```ts
|
|
101
120
|
import { diagnose } from '@ldclabs/kip-lang'
|
|
102
121
|
|
|
103
|
-
const diagnostics = diagnose('
|
|
122
|
+
const diagnostics = diagnose('CREATE CONCEPT ?x { TYPE "Experience"')
|
|
104
123
|
// [{ severity: "error", message: "Unclosed '{'", ... }]
|
|
105
124
|
```
|
|
106
125
|
|
|
@@ -123,13 +142,14 @@ function command(source: string) {
|
|
|
123
142
|
return lower(ast) // throws KipSyntaxError
|
|
124
143
|
}
|
|
125
144
|
|
|
126
|
-
command('FIND(?
|
|
127
|
-
//
|
|
145
|
+
command('FIND(?e.name) WHERE { ?e {type: "Experience"} } LIMIT 10')
|
|
146
|
+
// → the executable Command AST
|
|
128
147
|
```
|
|
129
148
|
|
|
130
|
-
`lower` requires the program to hold exactly one command
|
|
131
|
-
|
|
132
|
-
|
|
149
|
+
`lower` requires the program to hold exactly one command. In KML that means
|
|
150
|
+
one statement — `MUTATE { ... }` is how several mutations become one
|
|
151
|
+
all-or-nothing cognitive transition, so a transaction is still one command.
|
|
152
|
+
Use `lowerAll` for source text that is genuinely a sequence of commands.
|
|
133
153
|
|
|
134
154
|
The `diagnostics` check above is not decoration. `parse` recovers from a
|
|
135
155
|
malformed string or an unquoted value by reading it leniently, and `lower`
|
|
@@ -144,45 +164,50 @@ refuses on size is refused by every other engine too.
|
|
|
144
164
|
|
|
145
165
|
## API Reference
|
|
146
166
|
|
|
147
|
-
| Export
|
|
148
|
-
|
|
|
149
|
-
| `tokenize(source)`
|
|
150
|
-
| `parse(source)`
|
|
151
|
-
| `format(source, options?)`
|
|
152
|
-
| `
|
|
153
|
-
| `
|
|
154
|
-
| `analyzeSemantics(program)`
|
|
155
|
-
| `lower(program)`
|
|
156
|
-
| `lowerAll(program)`
|
|
157
|
-
| `lowerStatement(statement)`
|
|
158
|
-
| `checkBudget(source)`
|
|
159
|
-
| `checkBatchBudget(count)`
|
|
160
|
-
| `KipSyntaxError`
|
|
161
|
-
| `TokenType`
|
|
162
|
-
| `KEYWORDS` / `FUNCTIONS`
|
|
167
|
+
| Export | Description |
|
|
168
|
+
| ---------------------------------- | -------------------------------------------------------- |
|
|
169
|
+
| `tokenize(source)` | Tokenize KIP source into `Token[]` |
|
|
170
|
+
| `parse(source)` | Parse into `{ ast: Program, diagnostics: Diagnostic[] }` |
|
|
171
|
+
| `format(source, options?)` | AST-based formatting with comment preservation |
|
|
172
|
+
| `diagnose(source)` | Return syntax, static-semantic, and executable diagnostics |
|
|
173
|
+
| `validateExecutable(program)` | Check every parsed statement can lower to executable AST |
|
|
174
|
+
| `analyzeSemantics(program)` | Spec SHOULD/MUST checks decidable without a live schema |
|
|
175
|
+
| `lower(program)` | Lower one command to the executable `Command` AST |
|
|
176
|
+
| `lowerAll(program)` | Lower every command in a multi-statement program |
|
|
177
|
+
| `lowerStatement(statement)` | Lower a single statement |
|
|
178
|
+
| `checkBudget(source)` | Enforce length and nesting ceilings (`KIP_4002`) |
|
|
179
|
+
| `checkBatchBudget(count)` | Enforce the command-count ceiling (`KIP_4002`) |
|
|
180
|
+
| `KipSyntaxError` | Thrown by `lower` / `checkBudget`, carries a KIP code |
|
|
181
|
+
| `TokenType` | Enum of all token types |
|
|
182
|
+
| `KEYWORDS` / `FUNCTIONS` | Keyword map and built-in function set |
|
|
183
|
+
| `PARSER_VERSION` / `KIP_SPEC_REVISION` | Grammar version and the spec revision it targets |
|
|
163
184
|
|
|
164
185
|
### AST Node Types
|
|
165
186
|
|
|
166
187
|
All AST types are exported for downstream consumption:
|
|
167
188
|
|
|
168
|
-
- **
|
|
169
|
-
- **
|
|
170
|
-
- **
|
|
171
|
-
- **
|
|
189
|
+
- **KQL**: `FindStatement`, with `AsOfClause`, `ForTimeClause`, `EpistemicClause`, `OrderByClause`, `LimitClause`, `CursorClause`
|
|
190
|
+
- **KML**: `MutateStatement` plus every `MutationClause` — `CreateConceptStatement`, `UpsertConceptStatement`, `EnsurePropositionStatement`, `AssertStatement`, `CreateEvidenceStatement`, `CreateAssertionStatement`, `CreateActivityStatement`, `UpdateStatement`, `RetractAssertionStatement`, `SupersedeAssertionStatement`, `CorrectEvidenceStatement`, `TransitionActivityStatement`, `SetRetentionStatement`, `ArchiveStatement`, `TombstoneStatement`, `PurgeStatement`, `MergeConceptStatement`
|
|
191
|
+
- **META**: `DescribeStatement`, `ListStatement`, `SearchStatement`, `VerifyStatement`, `ValidateStatement`, `PreviewStatement`, `HistoryStatement`, `ChangesStatement`, `SnapshotStatement`, `ExportCapsuleStatement`
|
|
192
|
+
- **Clauses**: `TypeClause`, `ClientKeyClause`, `NameClause`, `MatchClause`, `SetFieldsClause`, `SetAttributesClause`, `SetFacetClause`, `UnsetAttributesClause`, `UnsetFacetClause`, `SetStructuralClause`, `UnsetStructuralClause`, `ExpectVersionClause`, `ExpectStateClause`
|
|
193
|
+
- **Patterns**: `ConceptPattern`, `PropositionPattern`, `AssertionPattern`, `EvidencePattern`, `ActivityPattern`, `StructuralPattern`, `BeliefPattern`, `BeliefSlotPattern`, `FilterClause`, `NotClause`, `OptionalClause`, `UnionClause`
|
|
194
|
+
- **Expressions**: `Expression`, `FieldAccess`, `AggregateExpr`, `FunctionCallExpr`, `ObjectLiteral`, `ObjectPattern`, `ArrayLiteral`, `ObjectEntry`
|
|
172
195
|
|
|
173
|
-
The executable AST is exported separately
|
|
174
|
-
|
|
175
|
-
`TargetTerm`, `PredTerm`, `FilterExpression`, `UpdateValue`, `KipValue`, and
|
|
176
|
-
friends. Names that collide with a syntax-tree node carry an `Exec` prefix.
|
|
196
|
+
The executable AST is exported separately, and names that collide with a
|
|
197
|
+
syntax-tree node carry an `Exec` prefix.
|
|
177
198
|
|
|
178
|
-
String-or-parameter
|
|
179
|
-
`SearchStatement.
|
|
180
|
-
|
|
181
|
-
|
|
199
|
+
String-or-parameter operands stay `ScalarValue` nodes — `SearchStatement.term`,
|
|
200
|
+
`SearchStatement.withType`, `DescribeStatement.value`, `ClientKeyClause.value`
|
|
201
|
+
— so the tree still says whether the source wrote a quoted string or a
|
|
202
|
+
`:parameter` placeholder.
|
|
182
203
|
|
|
183
204
|
## KIP Language
|
|
184
205
|
|
|
185
|
-
KIP is
|
|
206
|
+
KIP 2.0 is the cognitive state protocol between an agent and a persistent
|
|
207
|
+
Cognitive Nexus: you read with KQL, change cognition with KML, and ground or
|
|
208
|
+
introspect with META. See the [KIP 2.0 Specification](https://github.com/ldclabs/KIP/blob/main/v2/KIP-2.0-SPECIFICATION.md)
|
|
209
|
+
for full syntax details, or the [syntax card](https://github.com/ldclabs/KIP/blob/main/v2/KIPSyntax.md) for a
|
|
210
|
+
condensed reference.
|
|
186
211
|
|
|
187
212
|
## License
|
|
188
213
|
|