tree-sitter-okf 0.2.0__tar.gz
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.
- tree_sitter_okf-0.2.0/LICENSE +21 -0
- tree_sitter_okf-0.2.0/NOTICE +27 -0
- tree_sitter_okf-0.2.0/PKG-INFO +281 -0
- tree_sitter_okf-0.2.0/README.md +261 -0
- tree_sitter_okf-0.2.0/bindings/python/tree_sitter_okf/__init__.py +40 -0
- tree_sitter_okf-0.2.0/bindings/python/tree_sitter_okf/__init__.pyi +8 -0
- tree_sitter_okf-0.2.0/bindings/python/tree_sitter_okf/binding.c +35 -0
- tree_sitter_okf-0.2.0/bindings/python/tree_sitter_okf/okf.py +553 -0
- tree_sitter_okf-0.2.0/bindings/python/tree_sitter_okf/py.typed +0 -0
- tree_sitter_okf-0.2.0/bindings/python/tree_sitter_okf.egg-info/PKG-INFO +281 -0
- tree_sitter_okf-0.2.0/bindings/python/tree_sitter_okf.egg-info/SOURCES.txt +17 -0
- tree_sitter_okf-0.2.0/bindings/python/tree_sitter_okf.egg-info/dependency_links.txt +1 -0
- tree_sitter_okf-0.2.0/bindings/python/tree_sitter_okf.egg-info/not-zip-safe +1 -0
- tree_sitter_okf-0.2.0/bindings/python/tree_sitter_okf.egg-info/requires.txt +3 -0
- tree_sitter_okf-0.2.0/bindings/python/tree_sitter_okf.egg-info/top_level.txt +2 -0
- tree_sitter_okf-0.2.0/pyproject.toml +29 -0
- tree_sitter_okf-0.2.0/queries/highlights.scm +241 -0
- tree_sitter_okf-0.2.0/queries/injections-fullyaml.scm +24 -0
- tree_sitter_okf-0.2.0/queries/injections.scm +18 -0
- tree_sitter_okf-0.2.0/queries/locals.scm +23 -0
- tree_sitter_okf-0.2.0/queries/okf/citations.scm +16 -0
- tree_sitter_okf-0.2.0/queries/okf/computation.scm +26 -0
- tree_sitter_okf-0.2.0/queries/okf/fields.scm +133 -0
- tree_sitter_okf-0.2.0/queries/okf/links.scm +59 -0
- tree_sitter_okf-0.2.0/queries/okf/scalars.scm +29 -0
- tree_sitter_okf-0.2.0/queries/okf/sections.scm +34 -0
- tree_sitter_okf-0.2.0/queries/okf/sources.scm +165 -0
- tree_sitter_okf-0.2.0/queries/tags.scm +27 -0
- tree_sitter_okf-0.2.0/setup.cfg +4 -0
- tree_sitter_okf-0.2.0/setup.py +79 -0
- tree_sitter_okf-0.2.0/src/parser.c +211942 -0
- tree_sitter_okf-0.2.0/src/scanner.c +3812 -0
- tree_sitter_okf-0.2.0/src/tree_sitter/alloc.h +54 -0
- tree_sitter_okf-0.2.0/src/tree_sitter/array.h +291 -0
- tree_sitter_okf-0.2.0/src/tree_sitter/parser.h +286 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 tree-sitter-okf contributors
|
|
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.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
tree-sitter-okf
|
|
2
|
+
Copyright (c) 2026 tree-sitter-okf contributors
|
|
3
|
+
Licensed under the MIT License (see LICENSE).
|
|
4
|
+
|
|
5
|
+
This project includes third-party material:
|
|
6
|
+
|
|
7
|
+
1. tree-sitter-markdown
|
|
8
|
+
https://github.com/tree-sitter-grammars/tree-sitter-markdown
|
|
9
|
+
Copyright (c) 2021 Matthias Deiml
|
|
10
|
+
Licensed under the MIT License (vendor/tree-sitter-markdown/LICENSE).
|
|
11
|
+
|
|
12
|
+
The markdown rules in grammar/block.js, grammar/inline.js and
|
|
13
|
+
grammar/common.js, the markdown half of src/scanner.c, and the files in
|
|
14
|
+
vendor/tree-sitter-markdown/ are derived from or copied from this project
|
|
15
|
+
at commit a0a00f817d02412bd92c54d316f164d827b57b5c. The changes are
|
|
16
|
+
listed in vendor/tree-sitter-markdown/MERGE.md.
|
|
17
|
+
|
|
18
|
+
2. Open Knowledge Format example bundles
|
|
19
|
+
https://github.com/GoogleCloudPlatform/open-knowledge-format
|
|
20
|
+
Copyright 2026 Google LLC
|
|
21
|
+
Licensed under the Apache License, Version 2.0
|
|
22
|
+
(test/fixtures/bundles/LICENSE.md).
|
|
23
|
+
|
|
24
|
+
The files in test/fixtures/bundles/ are unmodified copies of the
|
|
25
|
+
`bundles/` directory at commit ad30107c31c06aec8a7d5636e0d1058118604e6f,
|
|
26
|
+
used as test fixtures only. They are not part of the distributed
|
|
27
|
+
packages. test/fixtures/bundles/LOCK.json records each file's SHA-256.
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tree-sitter-okf
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Open Knowledge Format (OKF) grammar for tree-sitter: markdown concept documents with YAML frontmatter
|
|
5
|
+
Author: tree-sitter-okf contributors
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/antstanley/tree-sitter-okf
|
|
8
|
+
Keywords: incremental,parsing,tree-sitter,okf
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Topic :: Software Development :: Compilers
|
|
11
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
12
|
+
Classifier: Typing :: Typed
|
|
13
|
+
Requires-Python: >=3.10
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
License-File: NOTICE
|
|
17
|
+
Provides-Extra: core
|
|
18
|
+
Requires-Dist: tree-sitter~=0.24; extra == "core"
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# tree-sitter-okf
|
|
22
|
+
|
|
23
|
+
A [tree-sitter](https://tree-sitter.github.io/) grammar for
|
|
24
|
+
[Open Knowledge Format (OKF)](https://github.com/GoogleCloudPlatform/open-knowledge-format)
|
|
25
|
+
v0.2 documents: markdown with YAML frontmatter, organised into knowledge
|
|
26
|
+
bundles.
|
|
27
|
+
|
|
28
|
+
**Status: 0.2.0.** The version follows OKF: 0.2.x targets OKF v0.2 (see
|
|
29
|
+
[`docs/releasing.md`](docs/releasing.md)). It implements the whole
|
|
30
|
+
[grammar specification](docs/grammar-spec.md) (milestones M0–M5). See
|
|
31
|
+
[`CHANGELOG.md`](CHANGELOG.md) for what is verified and the known
|
|
32
|
+
limitations.
|
|
33
|
+
|
|
34
|
+
## Why a grammar of its own?
|
|
35
|
+
|
|
36
|
+
`.md` files in an OKF bundle are more than markdown:
|
|
37
|
+
|
|
38
|
+
1. **The document shape is OKF's.** A frontmatter block plus a body, with
|
|
39
|
+
rules about where frontmatter is and isn't allowed.
|
|
40
|
+
2. **The profile is OKF's.** GFM tables (for `# Schema`), footnotes (for
|
|
41
|
+
per-claim attribution), fenced and indented code, and cross-links.
|
|
42
|
+
`tree-sitter-markdown` has no footnotes and gates GFM behind build flags.
|
|
43
|
+
3. **The semantic join crosses the frontmatter/body boundary.** Frontmatter
|
|
44
|
+
`sources[].id` is joined to footnote labels in the body. Only a single
|
|
45
|
+
tree can express that in one query.
|
|
46
|
+
|
|
47
|
+
So this grammar parses the whole document into one tree:
|
|
48
|
+
|
|
49
|
+
```markdown
|
|
50
|
+
---
|
|
51
|
+
type: Metric
|
|
52
|
+
tags: [finance]
|
|
53
|
+
sources:
|
|
54
|
+
- id: gl
|
|
55
|
+
resource: /tables/gl
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
# Gross Margin
|
|
59
|
+
|
|
60
|
+
Revenue minus COGS.[^gl]
|
|
61
|
+
|
|
62
|
+
[^gl]: General ledger.
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
```query
|
|
66
|
+
(source_file
|
|
67
|
+
frontmatter: (frontmatter
|
|
68
|
+
(block_mapping
|
|
69
|
+
(block_mapping_pair key: (plain_scalar) value: (plain_scalar))
|
|
70
|
+
(block_mapping_pair key: (plain_scalar) value: (flow_sequence (plain_scalar)))
|
|
71
|
+
(block_mapping_pair key: (plain_scalar)
|
|
72
|
+
value: (block_sequence
|
|
73
|
+
(block_sequence_item
|
|
74
|
+
(block_mapping
|
|
75
|
+
(block_mapping_pair key: (plain_scalar) value: (plain_scalar))
|
|
76
|
+
(block_mapping_pair key: (plain_scalar) value: (plain_scalar))))))))
|
|
77
|
+
body: (body
|
|
78
|
+
(section
|
|
79
|
+
(atx_heading (atx_h1_marker) heading_content: (inline))
|
|
80
|
+
(paragraph (inline (footnote_reference label: (footnote_label))))
|
|
81
|
+
(footnote_definition label: (footnote_label) (paragraph (inline))))))
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
* **Frontmatter** is parsed natively as [OKF-YAML](docs/okf-yaml.md), a
|
|
85
|
+
documented subset of YAML. Anything outside it becomes a
|
|
86
|
+
`yaml_unsupported` node, never an `ERROR`. A missing closing `---` is a
|
|
87
|
+
`MISSING "---"`, so tools can say exactly what is wrong.
|
|
88
|
+
* **The body** uses the rules of
|
|
89
|
+
[tree-sitter-markdown](https://github.com/tree-sitter-grammars/tree-sitter-markdown),
|
|
90
|
+
with block and inline merged into one grammar. GFM is always on, footnotes
|
|
91
|
+
are added, and node names match upstream. Existing markdown queries and
|
|
92
|
+
habits carry over. The deltas are listed in
|
|
93
|
+
[`vendor/tree-sitter-markdown/MERGE.md`](vendor/tree-sitter-markdown/MERGE.md).
|
|
94
|
+
* **Every node type** is documented in [`docs/node-types.md`](docs/node-types.md).
|
|
95
|
+
|
|
96
|
+
## Installing
|
|
97
|
+
|
|
98
|
+
| Ecosystem | Package | Language |
|
|
99
|
+
|---|---|---|
|
|
100
|
+
| Node | `tree-sitter-okf` (npm) | `require('tree-sitter-okf')` |
|
|
101
|
+
| Rust | `tree-sitter-okf` (crates.io) | `tree_sitter_okf::LANGUAGE` |
|
|
102
|
+
| Python | `tree-sitter-okf` (PyPI) | `tree_sitter_okf.language()` |
|
|
103
|
+
| Go | `github.com/antstanley/tree-sitter-okf/bindings/go` | `tree_sitter_okf.Language()` |
|
|
104
|
+
| Swift | `TreeSitterOkf` (SwiftPM) | `tree_sitter_okf()` |
|
|
105
|
+
| C | `make && make install` (`libtree-sitter-okf`, `tree-sitter-okf.pc`) | `tree_sitter_okf()` |
|
|
106
|
+
|
|
107
|
+
The packages are not published yet. Until they are, install from this
|
|
108
|
+
repository (for example `npm install github:antstanley/tree-sitter-okf`,
|
|
109
|
+
or `pip install git+https://github.com/antstanley/tree-sitter-okf`).
|
|
110
|
+
|
|
111
|
+
The generated parser uses tree-sitter **ABI 14** (spec D12). That is the
|
|
112
|
+
highest ABI Neovim 0.10 loads, and every current runtime supports it.
|
|
113
|
+
|
|
114
|
+
```js
|
|
115
|
+
const Parser = require('tree-sitter');
|
|
116
|
+
const OKF = require('tree-sitter-okf');
|
|
117
|
+
|
|
118
|
+
const parser = new Parser();
|
|
119
|
+
parser.setLanguage(OKF);
|
|
120
|
+
const tree = parser.parse(source);
|
|
121
|
+
console.log(tree.rootNode.toString());
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
import tree_sitter, tree_sitter_okf
|
|
126
|
+
|
|
127
|
+
parser = tree_sitter.Parser(tree_sitter.Language(tree_sitter_okf.language()))
|
|
128
|
+
tree = parser.parse(source.encode())
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Queries
|
|
132
|
+
|
|
133
|
+
| File | Purpose |
|
|
134
|
+
|---|---|
|
|
135
|
+
| `queries/highlights.scm` | Highlighting, with `@markup.*` capture names (Neovim, Helix) |
|
|
136
|
+
| `queries/injections.scm` | Fenced code by info string, HTML. Frontmatter is native, not injected |
|
|
137
|
+
| `queries/injections-fullyaml.scm` | Opt-in: injects the frontmatter as `yaml` for full-YAML hosts |
|
|
138
|
+
| `queries/locals.scm` | Heading scopes (outlines, folding) |
|
|
139
|
+
| `queries/tags.scm` | Concept definitions and cross-link references |
|
|
140
|
+
| `queries/okf/fields.scm` | One capture per well-known frontmatter key (`@okf.field.type`, …) |
|
|
141
|
+
| `queries/okf/links.scm` | Link classification: URI, bundle-relative, relative, fragment |
|
|
142
|
+
| `queries/okf/sources.scm` | `sources[]` entries and their fields |
|
|
143
|
+
| `queries/okf/citations.scm` | Footnote labels, for the `sources[].id` join |
|
|
144
|
+
| `queries/okf/scalars.scm` | A replaceable typing policy for scalars (integer, timestamp, boolean) |
|
|
145
|
+
| `queries/okf/sections.scm` | Conventional headings and `log.md` date headings |
|
|
146
|
+
| `queries/okf/computation.scm` | The `# Computation` heading → code block pairing |
|
|
147
|
+
|
|
148
|
+
The OKF queries use only node patterns and `#match?`, so they give the same
|
|
149
|
+
results in every host.
|
|
150
|
+
|
|
151
|
+
## Host helpers
|
|
152
|
+
|
|
153
|
+
Some OKF facts need something the parse tree does not have: a filename, a
|
|
154
|
+
clock, the absence of a key, or what a value means. The Node and Python
|
|
155
|
+
bindings ship reference helpers for them. The helpers classify documents,
|
|
156
|
+
compute concept ids, turn frontmatter into a plain value, and report trust
|
|
157
|
+
tier, lifecycle status and staleness. They also join citations to footnotes
|
|
158
|
+
and produce OKF conformance findings.
|
|
159
|
+
|
|
160
|
+
```js
|
|
161
|
+
const { okf } = require('tree-sitter-okf');
|
|
162
|
+
okf.classify('metrics/gross-margin.md', tree); // 'concept'
|
|
163
|
+
okf.trustTier(tree); // 'human-reviewed'
|
|
164
|
+
okf.citations(tree).unresolved; // footnotes with no source
|
|
165
|
+
okf.conformance('metrics/gross-margin.md', tree);
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
The helpers are specified in [`docs/host-helpers.md`](docs/host-helpers.md)
|
|
169
|
+
and pinned by shared fixtures, so the two implementations agree.
|
|
170
|
+
|
|
171
|
+
## Editor setup
|
|
172
|
+
|
|
173
|
+
OKF documents are `.md` files, and `okf` is a superset of the markdown that
|
|
174
|
+
`tree-sitter-markdown` parses. It is safe to register `okf` for markdown
|
|
175
|
+
buffers, ideally only in OKF bundles (spec D11).
|
|
176
|
+
|
|
177
|
+
### Neovim (nvim-treesitter)
|
|
178
|
+
|
|
179
|
+
```lua
|
|
180
|
+
-- nvim-treesitter `master` branch API
|
|
181
|
+
local parsers = require('nvim-treesitter.parsers').get_parser_configs()
|
|
182
|
+
parsers.okf = {
|
|
183
|
+
install_info = {
|
|
184
|
+
url = 'https://github.com/antstanley/tree-sitter-okf',
|
|
185
|
+
files = { 'src/parser.c', 'src/scanner.c' },
|
|
186
|
+
branch = 'main',
|
|
187
|
+
},
|
|
188
|
+
}
|
|
189
|
+
vim.treesitter.language.register('okf', 'markdown')
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Then run `:TSInstall okf`, and copy `queries/*.scm` into
|
|
193
|
+
`~/.config/nvim/queries/okf/`. nvim-treesitter does not fetch queries for
|
|
194
|
+
parsers it does not ship. To use `okf` only inside bundles, register it from
|
|
195
|
+
an autocommand that checks for the bundle's root `index.md`, instead of
|
|
196
|
+
globally.
|
|
197
|
+
|
|
198
|
+
### Helix
|
|
199
|
+
|
|
200
|
+
```toml
|
|
201
|
+
# languages.toml
|
|
202
|
+
[[language]]
|
|
203
|
+
name = "okf"
|
|
204
|
+
scope = "source.okf"
|
|
205
|
+
file-types = ["md"]
|
|
206
|
+
roots = ["index.md"]
|
|
207
|
+
injection-regex = "okf"
|
|
208
|
+
|
|
209
|
+
[[grammar]]
|
|
210
|
+
name = "okf"
|
|
211
|
+
source = { git = "https://github.com/antstanley/tree-sitter-okf", rev = "<commit sha>" }
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Helix needs a commit SHA for `rev`, so use the commit of a release tag.
|
|
215
|
+
|
|
216
|
+
Then run `hx --grammar fetch && hx --grammar build`, and copy `queries/*.scm`
|
|
217
|
+
into `~/.config/helix/runtime/queries/okf/`.
|
|
218
|
+
|
|
219
|
+
### Command line
|
|
220
|
+
|
|
221
|
+
```sh
|
|
222
|
+
npx tree-sitter parse path/to/concept.md
|
|
223
|
+
npx tree-sitter query queries/okf/fields.scm path/to/concept.md
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## Development
|
|
227
|
+
|
|
228
|
+
```sh
|
|
229
|
+
npm install # tree-sitter CLI and Node bindings
|
|
230
|
+
npm run generate # tree-sitter generate --abi 14
|
|
231
|
+
npm test # corpus: tree-sitter test
|
|
232
|
+
script/test # every suite (below); --bench adds the benchmark
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
`script/test` needs a Python environment with `tree-sitter`, `PyYAML`,
|
|
236
|
+
`tree-sitter-yaml` and this package installed (`pip install -e .`).
|
|
237
|
+
|
|
238
|
+
| Suite | What it checks |
|
|
239
|
+
|---|---|
|
|
240
|
+
| `tree-sitter test` | Hand-reviewed trees in `test/corpus/` (frontmatter, body, documents, errors) |
|
|
241
|
+
| `script/diff-upstream` | tree-sitter-markdown's own corpus through this grammar. Every difference is a recorded divergence |
|
|
242
|
+
| `script/diff-yaml` | Every frontmatter scalar value against PyYAML |
|
|
243
|
+
| `script/test-queries` | Query captures on fixture documents, native and full-YAML modes |
|
|
244
|
+
| `script/helpers-fixtures.js` | Host helpers against `test/helpers/cases.json` (Node and Python tests run the same cases) |
|
|
245
|
+
| `script/property-test` | No `ERROR` on fuzzed inputs, lossless leaves, incremental == full parse, CRLF/BOM, linear time |
|
|
246
|
+
| `script/node-types --check` | `docs/node-types.md` covers every node type |
|
|
247
|
+
| `script/verify-vendor` | Vendored markdown sources and the OKF fixture bundles match their SHA-256 locks |
|
|
248
|
+
| `script/bench` | Parse throughput and incremental latency against `bench/baseline.json` |
|
|
249
|
+
|
|
250
|
+
Test fixtures in `test/fixtures/bundles/` are the four official OKF example
|
|
251
|
+
bundles, pinned by `LOCK.json` and refreshed with `script/sync-fixtures`.
|
|
252
|
+
Upstream markdown is refreshed with `script/revendor` (see `MERGE.md`).
|
|
253
|
+
|
|
254
|
+
Releases are cut with [changesets](https://github.com/changesets/changesets).
|
|
255
|
+
Run `npx changeset` for every user-facing change. Tagging the merged
|
|
256
|
+
"version packages" PR publishes to npm, crates.io and PyPI by trusted
|
|
257
|
+
publishing (see [`docs/releasing.md`](docs/releasing.md)).
|
|
258
|
+
|
|
259
|
+
The experimental `[[wiki link]]` and `#tag` dialects are off by default.
|
|
260
|
+
Build them with `OKF_DIALECT_WIKILINK=1 OKF_DIALECT_TAGS=1 npm run generate`.
|
|
261
|
+
|
|
262
|
+
## Repository layout
|
|
263
|
+
|
|
264
|
+
```
|
|
265
|
+
grammar.js assembles the layers below
|
|
266
|
+
grammar/ block.js, inline.js, common.js (merged markdown), frontmatter.js (OKF-YAML)
|
|
267
|
+
src/scanner.c the external scanner: markdown block + inline, OKF-YAML frontmatter
|
|
268
|
+
queries/ editor queries and the OKF query library
|
|
269
|
+
bindings/ C, Go, Node, Python, Rust, Swift (+ host helpers for Node, Python)
|
|
270
|
+
docs/ grammar spec, OKF-YAML subset, node types, host helpers
|
|
271
|
+
test/ corpus, fixture bundles, query and helper fixtures
|
|
272
|
+
vendor/tree-sitter-markdown/ pinned upstream sources and MERGE.md
|
|
273
|
+
script/ test, differential, property, bench and maintenance scripts
|
|
274
|
+
bench/ benchmark baseline
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
## License
|
|
278
|
+
|
|
279
|
+
MIT (see [`LICENSE`](LICENSE)). The markdown rules derive from
|
|
280
|
+
tree-sitter-markdown (MIT). The OKF example bundles used as test fixtures
|
|
281
|
+
are Apache-2.0. See [`NOTICE`](NOTICE).
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
# tree-sitter-okf
|
|
2
|
+
|
|
3
|
+
A [tree-sitter](https://tree-sitter.github.io/) grammar for
|
|
4
|
+
[Open Knowledge Format (OKF)](https://github.com/GoogleCloudPlatform/open-knowledge-format)
|
|
5
|
+
v0.2 documents: markdown with YAML frontmatter, organised into knowledge
|
|
6
|
+
bundles.
|
|
7
|
+
|
|
8
|
+
**Status: 0.2.0.** The version follows OKF: 0.2.x targets OKF v0.2 (see
|
|
9
|
+
[`docs/releasing.md`](docs/releasing.md)). It implements the whole
|
|
10
|
+
[grammar specification](docs/grammar-spec.md) (milestones M0–M5). See
|
|
11
|
+
[`CHANGELOG.md`](CHANGELOG.md) for what is verified and the known
|
|
12
|
+
limitations.
|
|
13
|
+
|
|
14
|
+
## Why a grammar of its own?
|
|
15
|
+
|
|
16
|
+
`.md` files in an OKF bundle are more than markdown:
|
|
17
|
+
|
|
18
|
+
1. **The document shape is OKF's.** A frontmatter block plus a body, with
|
|
19
|
+
rules about where frontmatter is and isn't allowed.
|
|
20
|
+
2. **The profile is OKF's.** GFM tables (for `# Schema`), footnotes (for
|
|
21
|
+
per-claim attribution), fenced and indented code, and cross-links.
|
|
22
|
+
`tree-sitter-markdown` has no footnotes and gates GFM behind build flags.
|
|
23
|
+
3. **The semantic join crosses the frontmatter/body boundary.** Frontmatter
|
|
24
|
+
`sources[].id` is joined to footnote labels in the body. Only a single
|
|
25
|
+
tree can express that in one query.
|
|
26
|
+
|
|
27
|
+
So this grammar parses the whole document into one tree:
|
|
28
|
+
|
|
29
|
+
```markdown
|
|
30
|
+
---
|
|
31
|
+
type: Metric
|
|
32
|
+
tags: [finance]
|
|
33
|
+
sources:
|
|
34
|
+
- id: gl
|
|
35
|
+
resource: /tables/gl
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
# Gross Margin
|
|
39
|
+
|
|
40
|
+
Revenue minus COGS.[^gl]
|
|
41
|
+
|
|
42
|
+
[^gl]: General ledger.
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```query
|
|
46
|
+
(source_file
|
|
47
|
+
frontmatter: (frontmatter
|
|
48
|
+
(block_mapping
|
|
49
|
+
(block_mapping_pair key: (plain_scalar) value: (plain_scalar))
|
|
50
|
+
(block_mapping_pair key: (plain_scalar) value: (flow_sequence (plain_scalar)))
|
|
51
|
+
(block_mapping_pair key: (plain_scalar)
|
|
52
|
+
value: (block_sequence
|
|
53
|
+
(block_sequence_item
|
|
54
|
+
(block_mapping
|
|
55
|
+
(block_mapping_pair key: (plain_scalar) value: (plain_scalar))
|
|
56
|
+
(block_mapping_pair key: (plain_scalar) value: (plain_scalar))))))))
|
|
57
|
+
body: (body
|
|
58
|
+
(section
|
|
59
|
+
(atx_heading (atx_h1_marker) heading_content: (inline))
|
|
60
|
+
(paragraph (inline (footnote_reference label: (footnote_label))))
|
|
61
|
+
(footnote_definition label: (footnote_label) (paragraph (inline))))))
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
* **Frontmatter** is parsed natively as [OKF-YAML](docs/okf-yaml.md), a
|
|
65
|
+
documented subset of YAML. Anything outside it becomes a
|
|
66
|
+
`yaml_unsupported` node, never an `ERROR`. A missing closing `---` is a
|
|
67
|
+
`MISSING "---"`, so tools can say exactly what is wrong.
|
|
68
|
+
* **The body** uses the rules of
|
|
69
|
+
[tree-sitter-markdown](https://github.com/tree-sitter-grammars/tree-sitter-markdown),
|
|
70
|
+
with block and inline merged into one grammar. GFM is always on, footnotes
|
|
71
|
+
are added, and node names match upstream. Existing markdown queries and
|
|
72
|
+
habits carry over. The deltas are listed in
|
|
73
|
+
[`vendor/tree-sitter-markdown/MERGE.md`](vendor/tree-sitter-markdown/MERGE.md).
|
|
74
|
+
* **Every node type** is documented in [`docs/node-types.md`](docs/node-types.md).
|
|
75
|
+
|
|
76
|
+
## Installing
|
|
77
|
+
|
|
78
|
+
| Ecosystem | Package | Language |
|
|
79
|
+
|---|---|---|
|
|
80
|
+
| Node | `tree-sitter-okf` (npm) | `require('tree-sitter-okf')` |
|
|
81
|
+
| Rust | `tree-sitter-okf` (crates.io) | `tree_sitter_okf::LANGUAGE` |
|
|
82
|
+
| Python | `tree-sitter-okf` (PyPI) | `tree_sitter_okf.language()` |
|
|
83
|
+
| Go | `github.com/antstanley/tree-sitter-okf/bindings/go` | `tree_sitter_okf.Language()` |
|
|
84
|
+
| Swift | `TreeSitterOkf` (SwiftPM) | `tree_sitter_okf()` |
|
|
85
|
+
| C | `make && make install` (`libtree-sitter-okf`, `tree-sitter-okf.pc`) | `tree_sitter_okf()` |
|
|
86
|
+
|
|
87
|
+
The packages are not published yet. Until they are, install from this
|
|
88
|
+
repository (for example `npm install github:antstanley/tree-sitter-okf`,
|
|
89
|
+
or `pip install git+https://github.com/antstanley/tree-sitter-okf`).
|
|
90
|
+
|
|
91
|
+
The generated parser uses tree-sitter **ABI 14** (spec D12). That is the
|
|
92
|
+
highest ABI Neovim 0.10 loads, and every current runtime supports it.
|
|
93
|
+
|
|
94
|
+
```js
|
|
95
|
+
const Parser = require('tree-sitter');
|
|
96
|
+
const OKF = require('tree-sitter-okf');
|
|
97
|
+
|
|
98
|
+
const parser = new Parser();
|
|
99
|
+
parser.setLanguage(OKF);
|
|
100
|
+
const tree = parser.parse(source);
|
|
101
|
+
console.log(tree.rootNode.toString());
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
import tree_sitter, tree_sitter_okf
|
|
106
|
+
|
|
107
|
+
parser = tree_sitter.Parser(tree_sitter.Language(tree_sitter_okf.language()))
|
|
108
|
+
tree = parser.parse(source.encode())
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Queries
|
|
112
|
+
|
|
113
|
+
| File | Purpose |
|
|
114
|
+
|---|---|
|
|
115
|
+
| `queries/highlights.scm` | Highlighting, with `@markup.*` capture names (Neovim, Helix) |
|
|
116
|
+
| `queries/injections.scm` | Fenced code by info string, HTML. Frontmatter is native, not injected |
|
|
117
|
+
| `queries/injections-fullyaml.scm` | Opt-in: injects the frontmatter as `yaml` for full-YAML hosts |
|
|
118
|
+
| `queries/locals.scm` | Heading scopes (outlines, folding) |
|
|
119
|
+
| `queries/tags.scm` | Concept definitions and cross-link references |
|
|
120
|
+
| `queries/okf/fields.scm` | One capture per well-known frontmatter key (`@okf.field.type`, …) |
|
|
121
|
+
| `queries/okf/links.scm` | Link classification: URI, bundle-relative, relative, fragment |
|
|
122
|
+
| `queries/okf/sources.scm` | `sources[]` entries and their fields |
|
|
123
|
+
| `queries/okf/citations.scm` | Footnote labels, for the `sources[].id` join |
|
|
124
|
+
| `queries/okf/scalars.scm` | A replaceable typing policy for scalars (integer, timestamp, boolean) |
|
|
125
|
+
| `queries/okf/sections.scm` | Conventional headings and `log.md` date headings |
|
|
126
|
+
| `queries/okf/computation.scm` | The `# Computation` heading → code block pairing |
|
|
127
|
+
|
|
128
|
+
The OKF queries use only node patterns and `#match?`, so they give the same
|
|
129
|
+
results in every host.
|
|
130
|
+
|
|
131
|
+
## Host helpers
|
|
132
|
+
|
|
133
|
+
Some OKF facts need something the parse tree does not have: a filename, a
|
|
134
|
+
clock, the absence of a key, or what a value means. The Node and Python
|
|
135
|
+
bindings ship reference helpers for them. The helpers classify documents,
|
|
136
|
+
compute concept ids, turn frontmatter into a plain value, and report trust
|
|
137
|
+
tier, lifecycle status and staleness. They also join citations to footnotes
|
|
138
|
+
and produce OKF conformance findings.
|
|
139
|
+
|
|
140
|
+
```js
|
|
141
|
+
const { okf } = require('tree-sitter-okf');
|
|
142
|
+
okf.classify('metrics/gross-margin.md', tree); // 'concept'
|
|
143
|
+
okf.trustTier(tree); // 'human-reviewed'
|
|
144
|
+
okf.citations(tree).unresolved; // footnotes with no source
|
|
145
|
+
okf.conformance('metrics/gross-margin.md', tree);
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
The helpers are specified in [`docs/host-helpers.md`](docs/host-helpers.md)
|
|
149
|
+
and pinned by shared fixtures, so the two implementations agree.
|
|
150
|
+
|
|
151
|
+
## Editor setup
|
|
152
|
+
|
|
153
|
+
OKF documents are `.md` files, and `okf` is a superset of the markdown that
|
|
154
|
+
`tree-sitter-markdown` parses. It is safe to register `okf` for markdown
|
|
155
|
+
buffers, ideally only in OKF bundles (spec D11).
|
|
156
|
+
|
|
157
|
+
### Neovim (nvim-treesitter)
|
|
158
|
+
|
|
159
|
+
```lua
|
|
160
|
+
-- nvim-treesitter `master` branch API
|
|
161
|
+
local parsers = require('nvim-treesitter.parsers').get_parser_configs()
|
|
162
|
+
parsers.okf = {
|
|
163
|
+
install_info = {
|
|
164
|
+
url = 'https://github.com/antstanley/tree-sitter-okf',
|
|
165
|
+
files = { 'src/parser.c', 'src/scanner.c' },
|
|
166
|
+
branch = 'main',
|
|
167
|
+
},
|
|
168
|
+
}
|
|
169
|
+
vim.treesitter.language.register('okf', 'markdown')
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Then run `:TSInstall okf`, and copy `queries/*.scm` into
|
|
173
|
+
`~/.config/nvim/queries/okf/`. nvim-treesitter does not fetch queries for
|
|
174
|
+
parsers it does not ship. To use `okf` only inside bundles, register it from
|
|
175
|
+
an autocommand that checks for the bundle's root `index.md`, instead of
|
|
176
|
+
globally.
|
|
177
|
+
|
|
178
|
+
### Helix
|
|
179
|
+
|
|
180
|
+
```toml
|
|
181
|
+
# languages.toml
|
|
182
|
+
[[language]]
|
|
183
|
+
name = "okf"
|
|
184
|
+
scope = "source.okf"
|
|
185
|
+
file-types = ["md"]
|
|
186
|
+
roots = ["index.md"]
|
|
187
|
+
injection-regex = "okf"
|
|
188
|
+
|
|
189
|
+
[[grammar]]
|
|
190
|
+
name = "okf"
|
|
191
|
+
source = { git = "https://github.com/antstanley/tree-sitter-okf", rev = "<commit sha>" }
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
Helix needs a commit SHA for `rev`, so use the commit of a release tag.
|
|
195
|
+
|
|
196
|
+
Then run `hx --grammar fetch && hx --grammar build`, and copy `queries/*.scm`
|
|
197
|
+
into `~/.config/helix/runtime/queries/okf/`.
|
|
198
|
+
|
|
199
|
+
### Command line
|
|
200
|
+
|
|
201
|
+
```sh
|
|
202
|
+
npx tree-sitter parse path/to/concept.md
|
|
203
|
+
npx tree-sitter query queries/okf/fields.scm path/to/concept.md
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## Development
|
|
207
|
+
|
|
208
|
+
```sh
|
|
209
|
+
npm install # tree-sitter CLI and Node bindings
|
|
210
|
+
npm run generate # tree-sitter generate --abi 14
|
|
211
|
+
npm test # corpus: tree-sitter test
|
|
212
|
+
script/test # every suite (below); --bench adds the benchmark
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
`script/test` needs a Python environment with `tree-sitter`, `PyYAML`,
|
|
216
|
+
`tree-sitter-yaml` and this package installed (`pip install -e .`).
|
|
217
|
+
|
|
218
|
+
| Suite | What it checks |
|
|
219
|
+
|---|---|
|
|
220
|
+
| `tree-sitter test` | Hand-reviewed trees in `test/corpus/` (frontmatter, body, documents, errors) |
|
|
221
|
+
| `script/diff-upstream` | tree-sitter-markdown's own corpus through this grammar. Every difference is a recorded divergence |
|
|
222
|
+
| `script/diff-yaml` | Every frontmatter scalar value against PyYAML |
|
|
223
|
+
| `script/test-queries` | Query captures on fixture documents, native and full-YAML modes |
|
|
224
|
+
| `script/helpers-fixtures.js` | Host helpers against `test/helpers/cases.json` (Node and Python tests run the same cases) |
|
|
225
|
+
| `script/property-test` | No `ERROR` on fuzzed inputs, lossless leaves, incremental == full parse, CRLF/BOM, linear time |
|
|
226
|
+
| `script/node-types --check` | `docs/node-types.md` covers every node type |
|
|
227
|
+
| `script/verify-vendor` | Vendored markdown sources and the OKF fixture bundles match their SHA-256 locks |
|
|
228
|
+
| `script/bench` | Parse throughput and incremental latency against `bench/baseline.json` |
|
|
229
|
+
|
|
230
|
+
Test fixtures in `test/fixtures/bundles/` are the four official OKF example
|
|
231
|
+
bundles, pinned by `LOCK.json` and refreshed with `script/sync-fixtures`.
|
|
232
|
+
Upstream markdown is refreshed with `script/revendor` (see `MERGE.md`).
|
|
233
|
+
|
|
234
|
+
Releases are cut with [changesets](https://github.com/changesets/changesets).
|
|
235
|
+
Run `npx changeset` for every user-facing change. Tagging the merged
|
|
236
|
+
"version packages" PR publishes to npm, crates.io and PyPI by trusted
|
|
237
|
+
publishing (see [`docs/releasing.md`](docs/releasing.md)).
|
|
238
|
+
|
|
239
|
+
The experimental `[[wiki link]]` and `#tag` dialects are off by default.
|
|
240
|
+
Build them with `OKF_DIALECT_WIKILINK=1 OKF_DIALECT_TAGS=1 npm run generate`.
|
|
241
|
+
|
|
242
|
+
## Repository layout
|
|
243
|
+
|
|
244
|
+
```
|
|
245
|
+
grammar.js assembles the layers below
|
|
246
|
+
grammar/ block.js, inline.js, common.js (merged markdown), frontmatter.js (OKF-YAML)
|
|
247
|
+
src/scanner.c the external scanner: markdown block + inline, OKF-YAML frontmatter
|
|
248
|
+
queries/ editor queries and the OKF query library
|
|
249
|
+
bindings/ C, Go, Node, Python, Rust, Swift (+ host helpers for Node, Python)
|
|
250
|
+
docs/ grammar spec, OKF-YAML subset, node types, host helpers
|
|
251
|
+
test/ corpus, fixture bundles, query and helper fixtures
|
|
252
|
+
vendor/tree-sitter-markdown/ pinned upstream sources and MERGE.md
|
|
253
|
+
script/ test, differential, property, bench and maintenance scripts
|
|
254
|
+
bench/ benchmark baseline
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
## License
|
|
258
|
+
|
|
259
|
+
MIT (see [`LICENSE`](LICENSE)). The markdown rules derive from
|
|
260
|
+
tree-sitter-markdown (MIT). The OKF example bundles used as test fixtures
|
|
261
|
+
are Apache-2.0. See [`NOTICE`](NOTICE).
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""Open Knowledge Format (OKF) grammar for tree-sitter: markdown concept documents with YAML frontmatter"""
|
|
2
|
+
|
|
3
|
+
from importlib.resources import files as _files
|
|
4
|
+
|
|
5
|
+
from ._binding import language
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def _get_query(name, file):
|
|
9
|
+
query = _files(f"{__package__}.queries") / file
|
|
10
|
+
globals()[name] = query.read_text()
|
|
11
|
+
return globals()[name]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def __getattr__(name):
|
|
15
|
+
if name == "HIGHLIGHTS_QUERY":
|
|
16
|
+
return _get_query("HIGHLIGHTS_QUERY", "highlights.scm")
|
|
17
|
+
if name == "INJECTIONS_QUERY":
|
|
18
|
+
return _get_query("INJECTIONS_QUERY", "injections.scm")
|
|
19
|
+
if name == "LOCALS_QUERY":
|
|
20
|
+
return _get_query("LOCALS_QUERY", "locals.scm")
|
|
21
|
+
if name == "TAGS_QUERY":
|
|
22
|
+
return _get_query("TAGS_QUERY", "tags.scm")
|
|
23
|
+
|
|
24
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
__all__ = [
|
|
28
|
+
"language",
|
|
29
|
+
"HIGHLIGHTS_QUERY",
|
|
30
|
+
"INJECTIONS_QUERY",
|
|
31
|
+
"LOCALS_QUERY",
|
|
32
|
+
"TAGS_QUERY",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def __dir__():
|
|
37
|
+
return sorted(__all__ + [
|
|
38
|
+
"__all__", "__builtins__", "__cached__", "__doc__", "__file__",
|
|
39
|
+
"__loader__", "__name__", "__package__", "__path__", "__spec__",
|
|
40
|
+
])
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#include <Python.h>
|
|
2
|
+
|
|
3
|
+
typedef struct TSLanguage TSLanguage;
|
|
4
|
+
|
|
5
|
+
TSLanguage *tree_sitter_okf(void);
|
|
6
|
+
|
|
7
|
+
static PyObject* _binding_language(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) {
|
|
8
|
+
return PyCapsule_New(tree_sitter_okf(), "tree_sitter.Language", NULL);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
static struct PyModuleDef_Slot slots[] = {
|
|
12
|
+
#ifdef Py_GIL_DISABLED
|
|
13
|
+
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
|
|
14
|
+
#endif
|
|
15
|
+
{0, NULL}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
static PyMethodDef methods[] = {
|
|
19
|
+
{"language", _binding_language, METH_NOARGS,
|
|
20
|
+
"Get the tree-sitter language for this grammar."},
|
|
21
|
+
{NULL, NULL, 0, NULL}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
static struct PyModuleDef module = {
|
|
25
|
+
.m_base = PyModuleDef_HEAD_INIT,
|
|
26
|
+
.m_name = "_binding",
|
|
27
|
+
.m_doc = NULL,
|
|
28
|
+
.m_size = 0,
|
|
29
|
+
.m_methods = methods,
|
|
30
|
+
.m_slots = slots,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
PyMODINIT_FUNC PyInit__binding(void) {
|
|
34
|
+
return PyModuleDef_Init(&module);
|
|
35
|
+
}
|