Add v0.1 scaffold: tree-sitter-p8-cart grammar and Zed extension
The grammar parses the .p8 cartridge container ( header, version, and the named __lua__/__gfx__/__gff__/__label__/__map__/__sfx__/__music__ sections, plus a fallback unknown_section ). The Zed language definition hands the __lua__ body to Zed's built-in Lua via injections.scm, so non-dialect Lua code highlights correctly today; PICO-8-specific syntax (?, +=, !=, single-line if, etc.) will fall back to error highlighting in those regions only — see README roadmap for the dialect grammar fork. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
node_modules/
|
||||
build/
|
||||
*.wasm
|
||||
|
||||
# tree-sitter-cli scratch
|
||||
.tree-sitter/
|
||||
@@ -1,3 +1,154 @@
|
||||
# zed-p8
|
||||
|
||||
The Pico-8 dialect of Lua for the Zed IDE.
|
||||
A Zed extension for the [PICO-8](https://www.lexaloffle.com/pico-8.php) fantasy
|
||||
console. The goal is reasonable editor support for the entire `.p8` cartridge
|
||||
file format and for PICO-8's Lua dialect — even where PICO-8 deviates from
|
||||
standard Lua 5.2 (compound assignments, `?` print shorthand, single-line
|
||||
`if (cond) ...`, `!=`, binary literals, peek operators, and so on).
|
||||
|
||||
## Status — v0.1 scaffold
|
||||
|
||||
Working today:
|
||||
|
||||
- A small tree-sitter grammar (`p8_cart`, in this repo's root) that parses
|
||||
the `.p8` cartridge container: the magic header line, `version` line, and
|
||||
the named sections `__lua__`, `__gfx__`, `__gff__`, `__label__`, `__map__`,
|
||||
`__sfx__`, `__music__`. Unknown `__name__` markers are accepted as a
|
||||
fallback `unknown_section`.
|
||||
- A Zed language definition `Pico-8 Cartridge` (file suffix `.p8`) that uses
|
||||
this grammar for outline, section-marker highlighting, and an outline view.
|
||||
- An injection that hands the body of the `__lua__` section to Zed's
|
||||
built-in Lua language for syntax highlighting.
|
||||
|
||||
Known limitations:
|
||||
|
||||
- **PICO-8 Lua dialect is not fully parsed.** The injected grammar is plain
|
||||
Lua 5.2, which does not understand `?` (print shorthand), `+=` and friends,
|
||||
`!=`, `0b...` literals, the `\` integer-divide operator, the `@`/`%`/`$`
|
||||
peek prefixes, or the single-line `if (cond) stmt` / `while (cond) stmt`
|
||||
forms. Code that uses any of those will show parse-error highlighting in
|
||||
those regions only — surrounding code remains correctly highlighted. See
|
||||
Roadmap below.
|
||||
- **No language server.** No completion, hover docs, or diagnostics for
|
||||
PICO-8 builtins yet. See Roadmap.
|
||||
- **No `.p8.png` support.** Only the plain-text `.p8` format is handled.
|
||||
|
||||
## Repository layout
|
||||
|
||||
```
|
||||
zed-p8/
|
||||
extension.toml ← Zed extension manifest
|
||||
grammar.js ← tree-sitter-p8-cart grammar source
|
||||
src/ ← generated parser ( committed; regenerate after grammar.js edits )
|
||||
package.json ← tree-sitter-cli devDependency
|
||||
tree-sitter.json ← tree-sitter-cli config ( auto-managed )
|
||||
languages/
|
||||
pico-8-cart/ ← Pico-8 Cartridge language files
|
||||
config.toml
|
||||
highlights.scm
|
||||
injections.scm
|
||||
outline.scm
|
||||
examples/
|
||||
hello.p8 ← minimal test cart
|
||||
references/ ← upstream PICO-8 manual + Zed docs links
|
||||
```
|
||||
|
||||
The cart grammar lives at the repo root rather than as a separate sibling
|
||||
repository. This keeps everything in one place during early development; if
|
||||
the grammar grows or wants to be reused outside Zed it can be split out
|
||||
later — the only file that needs to move with it is `grammar.js` plus the
|
||||
generated `src/`, and the `[grammars.p8_cart]` URL in `extension.toml` would
|
||||
need updating.
|
||||
|
||||
## Local development
|
||||
|
||||
Prerequisites: Node.js (for `tree-sitter-cli`) and Zed. Rust is NOT required
|
||||
unless/until we add a language-server harness.
|
||||
|
||||
### Edit-and-reload loop
|
||||
|
||||
1. Edit `grammar.js`.
|
||||
2. Regenerate the parser:
|
||||
|
||||
```sh
|
||||
npx tree-sitter generate
|
||||
```
|
||||
|
||||
3. Sanity-check on a real cart:
|
||||
|
||||
```sh
|
||||
npx tree-sitter parse examples/hello.p8
|
||||
```
|
||||
|
||||
4. Commit. The `[grammars.p8_cart]` block in `extension.toml` references this
|
||||
repo by `file://` URL and pins a commit SHA — Zed clones the grammar
|
||||
from that pinned revision, so changes only take effect after they're
|
||||
committed.
|
||||
|
||||
5. Update `extension.toml`'s `rev` field to the new SHA, then in Zed run
|
||||
`zed: install dev extension` (or click *Install Dev Extension* on the
|
||||
Extensions page) and select this directory. Reinstall after every commit
|
||||
that should be picked up.
|
||||
|
||||
Logs: `zed: open log`. Run `zed --foreground` for live stdout.
|
||||
|
||||
### Editing language queries
|
||||
|
||||
Files under `languages/pico-8-cart/` (`highlights.scm`, `injections.scm`,
|
||||
`outline.scm`) are loaded directly by Zed — no regeneration needed. Reinstall
|
||||
the dev extension to pick up changes.
|
||||
|
||||
## Roadmap
|
||||
|
||||
### v0.2 — PICO-8 Lua dialect grammar
|
||||
|
||||
Fork [`tree-sitter-grammars/tree-sitter-lua`](https://github.com/tree-sitter-grammars/tree-sitter-lua)
|
||||
into `tree-sitter-pico8` and add the dialect extensions documented in the
|
||||
PICO-8 manual:
|
||||
|
||||
- Compound-assignment operators: `+= -= *= /= \= %= ^= ..= &= |= ^^= <<= >>= >>>= <<>= >><=`
|
||||
- `!=` as alias for `~=`
|
||||
- `\` (integer divide) and the rotate / logical-shift operators `<<>` `>><` `>>>`
|
||||
- Binary literals `0b...` and hex fractional literals `0x1.4p0` style
|
||||
- Single-line `if (cond) stmt` and `while (cond) stmt` ( no `then`/`do`/`end` )
|
||||
- `?` as a statement-level shorthand for `print`
|
||||
- The peek-prefix unary operators `@addr` `%addr` `$addr`
|
||||
|
||||
Then add a second language `Pico-8 Lua` here (separate from Zed's built-in
|
||||
`Lua`) and switch `injections.scm` to inject `pico-8-lua` instead of `lua`.
|
||||
|
||||
### v0.3 — Language server integration
|
||||
|
||||
Wire up [`japhib/pico8-ls`](https://github.com/japhib/pico8-ls) (or whichever
|
||||
PICO-8 LSP is most maintained at the time) for:
|
||||
|
||||
- Completion of PICO-8 builtins (`spr`, `circfill`, `btn`, `flr`, …)
|
||||
- Signature help and hover docs sourced from the manual
|
||||
- Cart-aware analysis ( the LSP already understands `.p8` section markers
|
||||
and only analyzes the `__lua__` body )
|
||||
- Per-cart diagnostics
|
||||
|
||||
This will require a Rust component ( the `zed_extension_api` crate ) to
|
||||
download the language-server binary and define
|
||||
`language_server_command` — see [Zed's developing-extensions docs](https://zed.dev/docs/extensions/developing-extensions).
|
||||
|
||||
### v0.4 — Polish
|
||||
|
||||
- LuaCATS / EmmyLua stub file enumerating PICO-8's ~110 globals, for users
|
||||
who'd rather wire up `lua-language-server` against their `#include`-d
|
||||
`.lua` files.
|
||||
- Highlight rules for hex sections (`__gfx__`, `__map__`, `__sfx__`, etc.)
|
||||
so palette indices and note pitches show up distinctly.
|
||||
- Snippets for common idioms (`for x=0,127 do ... end`, the `_init`/`_update`/
|
||||
`_draw` triad).
|
||||
|
||||
## References
|
||||
|
||||
- PICO-8 manual: `references/pico-8_manual.txt`
|
||||
- Zed extension docs: see links in `references/zed-doc-links.md`
|
||||
- Cart file-format spec ( community wiki, not in the official manual ):
|
||||
https://pico-8.fandom.com/wiki/P8FileFormat
|
||||
|
||||
## License
|
||||
|
||||
0BSD — see `LICENSE`.
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
pico-8 cartridge // http://www.pico-8.com
|
||||
version 42
|
||||
__lua__
|
||||
-- hello cartridge
|
||||
function _init()
|
||||
cls()
|
||||
end
|
||||
|
||||
function _draw()
|
||||
cls(1)
|
||||
print("hello pico-8", 30, 60, 7)
|
||||
end
|
||||
__gfx__
|
||||
00000000111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
__gff__
|
||||
00000000000000000000000000000000
|
||||
__map__
|
||||
0000000000000000
|
||||
__sfx__
|
||||
010100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
__music__
|
||||
00 41424344
|
||||
@@ -0,0 +1,15 @@
|
||||
id = "pico-8"
|
||||
name = "Pico-8"
|
||||
version = "0.0.1"
|
||||
schema_version = 1
|
||||
authors = ["Kistaro Windrider <kistaro@gmail.com>"]
|
||||
description = "Pico-8 cartridge (.p8) and Lua dialect support for Zed"
|
||||
repository = "https://github.com/kistaro/zed-p8"
|
||||
|
||||
# The .p8 cart container grammar lives at the root of this repo.
|
||||
# During local development, set `repository` to `file://` + the absolute
|
||||
# path of your clone and pin `rev` to a committed SHA. When publishing,
|
||||
# this should point at a public Git URL.
|
||||
[grammars.p8_cart]
|
||||
repository = "file:///Users/norberg/gitea-repos/zed-p8"
|
||||
rev = "HEAD"
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* tree-sitter grammar for the PICO-8 .p8 cartridge text format.
|
||||
*
|
||||
* The .p8 format is a flat text container divided into named sections
|
||||
* delimited by lines of the form `__name__`. The first section is
|
||||
* always `__lua__` and contains the cart's Lua source; the remaining
|
||||
* sections (`__gfx__`, `__gff__`, `__label__`, `__map__`, `__sfx__`,
|
||||
* `__music__`) hold hex-encoded asset data. The file begins with a
|
||||
* fixed magic header line and a `version N` line.
|
||||
*
|
||||
* This grammar is intentionally minimal: it parses the section
|
||||
* structure and exposes each section's body as a single named node
|
||||
* so that injection queries (see languages/pico8-cart/injections.scm)
|
||||
* can hand the contents off to other languages — most importantly
|
||||
* Lua for the `__lua__` section.
|
||||
*/
|
||||
|
||||
module.exports = grammar({
|
||||
name: 'p8_cart',
|
||||
|
||||
// Whitespace is significant inside hex sections, so we don't skip it.
|
||||
extras: $ => [],
|
||||
|
||||
rules: {
|
||||
cartridge: $ => seq(
|
||||
optional($.header),
|
||||
optional($.version),
|
||||
repeat($.section),
|
||||
),
|
||||
|
||||
header: $ => /pico-8 cartridge \/\/[^\n]*\n/,
|
||||
version: $ => /version[ \t]+\d+\n/,
|
||||
|
||||
section: $ => choice(
|
||||
$.lua_section,
|
||||
$.gfx_section,
|
||||
$.gff_section,
|
||||
$.label_section,
|
||||
$.map_section,
|
||||
$.sfx_section,
|
||||
$.music_section,
|
||||
$.unknown_section,
|
||||
),
|
||||
|
||||
lua_section: $ => seq($.lua_marker, optional($.lua_content)),
|
||||
gfx_section: $ => seq($.gfx_marker, optional($.body)),
|
||||
gff_section: $ => seq($.gff_marker, optional($.body)),
|
||||
label_section: $ => seq($.label_marker, optional($.body)),
|
||||
map_section: $ => seq($.map_marker, optional($.body)),
|
||||
sfx_section: $ => seq($.sfx_marker, optional($.body)),
|
||||
music_section: $ => seq($.music_marker, optional($.body)),
|
||||
unknown_section: $ => seq($.section_marker, optional($.body)),
|
||||
|
||||
lua_marker: $ => token(prec(2, '__lua__\n')),
|
||||
gfx_marker: $ => token(prec(2, '__gfx__\n')),
|
||||
gff_marker: $ => token(prec(2, '__gff__\n')),
|
||||
label_marker: $ => token(prec(2, '__label__\n')),
|
||||
map_marker: $ => token(prec(2, '__map__\n')),
|
||||
sfx_marker: $ => token(prec(2, '__sfx__\n')),
|
||||
music_marker: $ => token(prec(2, '__music__\n')),
|
||||
section_marker: $ => token(prec(1, /__[a-z][a-z0-9_]*__\n/)),
|
||||
|
||||
lua_content: $ => repeat1($.line),
|
||||
body: $ => repeat1($.line),
|
||||
|
||||
// A single physical line. The lexer prefers section markers over
|
||||
// generic lines via the precedence above, so a line that happens
|
||||
// to be exactly `__name__\n` will tokenize as a marker, not a line.
|
||||
line: $ => choice(
|
||||
token(prec(0, /[^\n]*\n/)),
|
||||
token(prec(0, /[^\n]+/)), // final line with no trailing newline
|
||||
),
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,6 @@
|
||||
name = "Pico-8 Cartridge"
|
||||
grammar = "p8_cart"
|
||||
path_suffixes = ["p8"]
|
||||
line_comments = []
|
||||
hard_tabs = false
|
||||
tab_size = 1
|
||||
@@ -0,0 +1,13 @@
|
||||
; The cartridge magic header and version line are metadata, not content.
|
||||
(header) @comment.doc
|
||||
(version) @comment.doc
|
||||
|
||||
; Section markers ( __lua__, __gfx__, etc. ) are structural tokens.
|
||||
(lua_marker) @keyword
|
||||
(gfx_marker) @keyword
|
||||
(gff_marker) @keyword
|
||||
(label_marker) @keyword
|
||||
(map_marker) @keyword
|
||||
(sfx_marker) @keyword
|
||||
(music_marker) @keyword
|
||||
(section_marker) @keyword
|
||||
@@ -0,0 +1,11 @@
|
||||
; Inject Lua syntax highlighting into the __lua__ section.
|
||||
;
|
||||
; NOTE: This injects Zed's built-in Lua grammar, which does not
|
||||
; understand Pico-8 dialect extensions ( ?, +=, !=, single-line
|
||||
; `if (cond) stmt`, binary literals, memory peek prefix operators,
|
||||
; etc. ). Code that uses those forms will produce parse errors
|
||||
; locally, with degraded highlighting in those regions only — the
|
||||
; rest of the file will still render correctly. A future Pico-8
|
||||
; Lua grammar fork will replace this; see README for status.
|
||||
((lua_content) @injection.content
|
||||
(#set! injection.language "lua"))
|
||||
@@ -0,0 +1,9 @@
|
||||
; Show each cart section as an outline entry, named by its marker.
|
||||
(lua_section (lua_marker) @name) @item
|
||||
(gfx_section (gfx_marker) @name) @item
|
||||
(gff_section (gff_marker) @name) @item
|
||||
(label_section (label_marker) @name) @item
|
||||
(map_section (map_marker) @name) @item
|
||||
(sfx_section (sfx_marker) @name) @item
|
||||
(music_section (music_marker) @name) @item
|
||||
(unknown_section (section_marker) @name) @item
|
||||
Generated
+30
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"name": "tree-sitter-p8-cart",
|
||||
"version": "0.0.1",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "tree-sitter-p8-cart",
|
||||
"version": "0.0.1",
|
||||
"license": "0BSD",
|
||||
"devDependencies": {
|
||||
"tree-sitter-cli": "^0.24.7"
|
||||
}
|
||||
},
|
||||
"node_modules/tree-sitter-cli": {
|
||||
"version": "0.24.7",
|
||||
"resolved": "https://registry.npmjs.org/tree-sitter-cli/-/tree-sitter-cli-0.24.7.tgz",
|
||||
"integrity": "sha512-o4gnE82pVmMMhJbWwD6+I9yr4lXii5Ci5qEQ2pFpUbVy1YiD8cizTJaqdcznA0qEbo7l2OneI1GocChPrI4YGQ==",
|
||||
"dev": true,
|
||||
"hasInstallScript": true,
|
||||
"license": "MIT",
|
||||
"bin": {
|
||||
"tree-sitter": "cli.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12.0.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "tree-sitter-p8-cart",
|
||||
"version": "0.0.1",
|
||||
"description": "tree-sitter grammar for the PICO-8 .p8 cartridge text format",
|
||||
"main": "bindings/node",
|
||||
"types": "bindings/node",
|
||||
"keywords": [
|
||||
"tree-sitter",
|
||||
"parser",
|
||||
"pico-8",
|
||||
"pico8"
|
||||
],
|
||||
"license": "0BSD",
|
||||
"devDependencies": {
|
||||
"tree-sitter-cli": "^0.24.7"
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,3 @@
|
||||
* Zed docs, top level: https://zed.dev/docs/
|
||||
* Zed docs on language extensions: https://zed.dev/docs/extensions/languages
|
||||
* Zed docs on its standard Lua support: https://zed.dev/docs/languages/lua
|
||||
@@ -0,0 +1,390 @@
|
||||
{
|
||||
"$schema": "https://tree-sitter.github.io/tree-sitter/assets/schemas/grammar.schema.json",
|
||||
"name": "p8_cart",
|
||||
"rules": {
|
||||
"cartridge": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "header"
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "version"
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "REPEAT",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "section"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"header": {
|
||||
"type": "PATTERN",
|
||||
"value": "pico-8 cartridge \\/\\/[^\\n]*\\n"
|
||||
},
|
||||
"version": {
|
||||
"type": "PATTERN",
|
||||
"value": "version[ \\t]+\\d+\\n"
|
||||
},
|
||||
"section": {
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "lua_section"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "gfx_section"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "gff_section"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "label_section"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "map_section"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "sfx_section"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "music_section"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "unknown_section"
|
||||
}
|
||||
]
|
||||
},
|
||||
"lua_section": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "lua_marker"
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "lua_content"
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"gfx_section": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "gfx_marker"
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "body"
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"gff_section": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "gff_marker"
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "body"
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"label_section": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "label_marker"
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "body"
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"map_section": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "map_marker"
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "body"
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"sfx_section": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "sfx_marker"
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "body"
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"music_section": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "music_marker"
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "body"
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"unknown_section": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "section_marker"
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "body"
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"lua_marker": {
|
||||
"type": "TOKEN",
|
||||
"content": {
|
||||
"type": "PREC",
|
||||
"value": 2,
|
||||
"content": {
|
||||
"type": "STRING",
|
||||
"value": "__lua__\n"
|
||||
}
|
||||
}
|
||||
},
|
||||
"gfx_marker": {
|
||||
"type": "TOKEN",
|
||||
"content": {
|
||||
"type": "PREC",
|
||||
"value": 2,
|
||||
"content": {
|
||||
"type": "STRING",
|
||||
"value": "__gfx__\n"
|
||||
}
|
||||
}
|
||||
},
|
||||
"gff_marker": {
|
||||
"type": "TOKEN",
|
||||
"content": {
|
||||
"type": "PREC",
|
||||
"value": 2,
|
||||
"content": {
|
||||
"type": "STRING",
|
||||
"value": "__gff__\n"
|
||||
}
|
||||
}
|
||||
},
|
||||
"label_marker": {
|
||||
"type": "TOKEN",
|
||||
"content": {
|
||||
"type": "PREC",
|
||||
"value": 2,
|
||||
"content": {
|
||||
"type": "STRING",
|
||||
"value": "__label__\n"
|
||||
}
|
||||
}
|
||||
},
|
||||
"map_marker": {
|
||||
"type": "TOKEN",
|
||||
"content": {
|
||||
"type": "PREC",
|
||||
"value": 2,
|
||||
"content": {
|
||||
"type": "STRING",
|
||||
"value": "__map__\n"
|
||||
}
|
||||
}
|
||||
},
|
||||
"sfx_marker": {
|
||||
"type": "TOKEN",
|
||||
"content": {
|
||||
"type": "PREC",
|
||||
"value": 2,
|
||||
"content": {
|
||||
"type": "STRING",
|
||||
"value": "__sfx__\n"
|
||||
}
|
||||
}
|
||||
},
|
||||
"music_marker": {
|
||||
"type": "TOKEN",
|
||||
"content": {
|
||||
"type": "PREC",
|
||||
"value": 2,
|
||||
"content": {
|
||||
"type": "STRING",
|
||||
"value": "__music__\n"
|
||||
}
|
||||
}
|
||||
},
|
||||
"section_marker": {
|
||||
"type": "TOKEN",
|
||||
"content": {
|
||||
"type": "PREC",
|
||||
"value": 1,
|
||||
"content": {
|
||||
"type": "PATTERN",
|
||||
"value": "__[a-z][a-z0-9_]*__\\n"
|
||||
}
|
||||
}
|
||||
},
|
||||
"lua_content": {
|
||||
"type": "REPEAT1",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "line"
|
||||
}
|
||||
},
|
||||
"body": {
|
||||
"type": "REPEAT1",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "line"
|
||||
}
|
||||
},
|
||||
"line": {
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "TOKEN",
|
||||
"content": {
|
||||
"type": "PREC",
|
||||
"value": 0,
|
||||
"content": {
|
||||
"type": "PATTERN",
|
||||
"value": "[^\\n]*\\n"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "TOKEN",
|
||||
"content": {
|
||||
"type": "PREC",
|
||||
"value": 0,
|
||||
"content": {
|
||||
"type": "PATTERN",
|
||||
"value": "[^\\n]+"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"extras": [],
|
||||
"conflicts": [],
|
||||
"precedences": [],
|
||||
"externals": [],
|
||||
"inline": [],
|
||||
"supertypes": []
|
||||
}
|
||||
@@ -0,0 +1,296 @@
|
||||
[
|
||||
{
|
||||
"type": "body",
|
||||
"named": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": true,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "line",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "cartridge",
|
||||
"named": true,
|
||||
"root": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": true,
|
||||
"required": false,
|
||||
"types": [
|
||||
{
|
||||
"type": "header",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "section",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "version",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "gff_section",
|
||||
"named": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": true,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "body",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "gff_marker",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "gfx_section",
|
||||
"named": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": true,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "body",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "gfx_marker",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "label_section",
|
||||
"named": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": true,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "body",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "label_marker",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "line",
|
||||
"named": true,
|
||||
"fields": {}
|
||||
},
|
||||
{
|
||||
"type": "lua_content",
|
||||
"named": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": true,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "line",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "lua_section",
|
||||
"named": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": true,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "lua_content",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "lua_marker",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "map_section",
|
||||
"named": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": true,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "body",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "map_marker",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "music_section",
|
||||
"named": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": true,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "body",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "music_marker",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "section",
|
||||
"named": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": false,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "gff_section",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "gfx_section",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "label_section",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "lua_section",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "map_section",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "music_section",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "sfx_section",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "unknown_section",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "sfx_section",
|
||||
"named": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": true,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "body",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "sfx_marker",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "unknown_section",
|
||||
"named": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": true,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "body",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "section_marker",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "gff_marker",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "gfx_marker",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "header",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "label_marker",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "lua_marker",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "map_marker",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "music_marker",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "section_marker",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "sfx_marker",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "version",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
+1683
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,54 @@
|
||||
#ifndef TREE_SITTER_ALLOC_H_
|
||||
#define TREE_SITTER_ALLOC_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Allow clients to override allocation functions
|
||||
#ifdef TREE_SITTER_REUSE_ALLOCATOR
|
||||
|
||||
extern void *(*ts_current_malloc)(size_t size);
|
||||
extern void *(*ts_current_calloc)(size_t count, size_t size);
|
||||
extern void *(*ts_current_realloc)(void *ptr, size_t size);
|
||||
extern void (*ts_current_free)(void *ptr);
|
||||
|
||||
#ifndef ts_malloc
|
||||
#define ts_malloc ts_current_malloc
|
||||
#endif
|
||||
#ifndef ts_calloc
|
||||
#define ts_calloc ts_current_calloc
|
||||
#endif
|
||||
#ifndef ts_realloc
|
||||
#define ts_realloc ts_current_realloc
|
||||
#endif
|
||||
#ifndef ts_free
|
||||
#define ts_free ts_current_free
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
#ifndef ts_malloc
|
||||
#define ts_malloc malloc
|
||||
#endif
|
||||
#ifndef ts_calloc
|
||||
#define ts_calloc calloc
|
||||
#endif
|
||||
#ifndef ts_realloc
|
||||
#define ts_realloc realloc
|
||||
#endif
|
||||
#ifndef ts_free
|
||||
#define ts_free free
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // TREE_SITTER_ALLOC_H_
|
||||
@@ -0,0 +1,291 @@
|
||||
#ifndef TREE_SITTER_ARRAY_H_
|
||||
#define TREE_SITTER_ARRAY_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "./alloc.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4101)
|
||||
#elif defined(__GNUC__) || defined(__clang__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wunused-variable"
|
||||
#endif
|
||||
|
||||
#define Array(T) \
|
||||
struct { \
|
||||
T *contents; \
|
||||
uint32_t size; \
|
||||
uint32_t capacity; \
|
||||
}
|
||||
|
||||
/// Initialize an array.
|
||||
#define array_init(self) \
|
||||
((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL)
|
||||
|
||||
/// Create an empty array.
|
||||
#define array_new() \
|
||||
{ NULL, 0, 0 }
|
||||
|
||||
/// Get a pointer to the element at a given `index` in the array.
|
||||
#define array_get(self, _index) \
|
||||
(assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index])
|
||||
|
||||
/// Get a pointer to the first element in the array.
|
||||
#define array_front(self) array_get(self, 0)
|
||||
|
||||
/// Get a pointer to the last element in the array.
|
||||
#define array_back(self) array_get(self, (self)->size - 1)
|
||||
|
||||
/// Clear the array, setting its size to zero. Note that this does not free any
|
||||
/// memory allocated for the array's contents.
|
||||
#define array_clear(self) ((self)->size = 0)
|
||||
|
||||
/// Reserve `new_capacity` elements of space in the array. If `new_capacity` is
|
||||
/// less than the array's current capacity, this function has no effect.
|
||||
#define array_reserve(self, new_capacity) \
|
||||
_array__reserve((Array *)(self), array_elem_size(self), new_capacity)
|
||||
|
||||
/// Free any memory allocated for this array. Note that this does not free any
|
||||
/// memory allocated for the array's contents.
|
||||
#define array_delete(self) _array__delete((Array *)(self))
|
||||
|
||||
/// Push a new `element` onto the end of the array.
|
||||
#define array_push(self, element) \
|
||||
(_array__grow((Array *)(self), 1, array_elem_size(self)), \
|
||||
(self)->contents[(self)->size++] = (element))
|
||||
|
||||
/// Increase the array's size by `count` elements.
|
||||
/// New elements are zero-initialized.
|
||||
#define array_grow_by(self, count) \
|
||||
do { \
|
||||
if ((count) == 0) break; \
|
||||
_array__grow((Array *)(self), count, array_elem_size(self)); \
|
||||
memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \
|
||||
(self)->size += (count); \
|
||||
} while (0)
|
||||
|
||||
/// Append all elements from one array to the end of another.
|
||||
#define array_push_all(self, other) \
|
||||
array_extend((self), (other)->size, (other)->contents)
|
||||
|
||||
/// Append `count` elements to the end of the array, reading their values from the
|
||||
/// `contents` pointer.
|
||||
#define array_extend(self, count, contents) \
|
||||
_array__splice( \
|
||||
(Array *)(self), array_elem_size(self), (self)->size, \
|
||||
0, count, contents \
|
||||
)
|
||||
|
||||
/// Remove `old_count` elements from the array starting at the given `index`. At
|
||||
/// the same index, insert `new_count` new elements, reading their values from the
|
||||
/// `new_contents` pointer.
|
||||
#define array_splice(self, _index, old_count, new_count, new_contents) \
|
||||
_array__splice( \
|
||||
(Array *)(self), array_elem_size(self), _index, \
|
||||
old_count, new_count, new_contents \
|
||||
)
|
||||
|
||||
/// Insert one `element` into the array at the given `index`.
|
||||
#define array_insert(self, _index, element) \
|
||||
_array__splice((Array *)(self), array_elem_size(self), _index, 0, 1, &(element))
|
||||
|
||||
/// Remove one element from the array at the given `index`.
|
||||
#define array_erase(self, _index) \
|
||||
_array__erase((Array *)(self), array_elem_size(self), _index)
|
||||
|
||||
/// Pop the last element off the array, returning the element by value.
|
||||
#define array_pop(self) ((self)->contents[--(self)->size])
|
||||
|
||||
/// Assign the contents of one array to another, reallocating if necessary.
|
||||
#define array_assign(self, other) \
|
||||
_array__assign((Array *)(self), (const Array *)(other), array_elem_size(self))
|
||||
|
||||
/// Swap one array with another
|
||||
#define array_swap(self, other) \
|
||||
_array__swap((Array *)(self), (Array *)(other))
|
||||
|
||||
/// Get the size of the array contents
|
||||
#define array_elem_size(self) (sizeof *(self)->contents)
|
||||
|
||||
/// Search a sorted array for a given `needle` value, using the given `compare`
|
||||
/// callback to determine the order.
|
||||
///
|
||||
/// If an existing element is found to be equal to `needle`, then the `index`
|
||||
/// out-parameter is set to the existing value's index, and the `exists`
|
||||
/// out-parameter is set to true. Otherwise, `index` is set to an index where
|
||||
/// `needle` should be inserted in order to preserve the sorting, and `exists`
|
||||
/// is set to false.
|
||||
#define array_search_sorted_with(self, compare, needle, _index, _exists) \
|
||||
_array__search_sorted(self, 0, compare, , needle, _index, _exists)
|
||||
|
||||
/// Search a sorted array for a given `needle` value, using integer comparisons
|
||||
/// of a given struct field (specified with a leading dot) to determine the order.
|
||||
///
|
||||
/// See also `array_search_sorted_with`.
|
||||
#define array_search_sorted_by(self, field, needle, _index, _exists) \
|
||||
_array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists)
|
||||
|
||||
/// Insert a given `value` into a sorted array, using the given `compare`
|
||||
/// callback to determine the order.
|
||||
#define array_insert_sorted_with(self, compare, value) \
|
||||
do { \
|
||||
unsigned _index, _exists; \
|
||||
array_search_sorted_with(self, compare, &(value), &_index, &_exists); \
|
||||
if (!_exists) array_insert(self, _index, value); \
|
||||
} while (0)
|
||||
|
||||
/// Insert a given `value` into a sorted array, using integer comparisons of
|
||||
/// a given struct field (specified with a leading dot) to determine the order.
|
||||
///
|
||||
/// See also `array_search_sorted_by`.
|
||||
#define array_insert_sorted_by(self, field, value) \
|
||||
do { \
|
||||
unsigned _index, _exists; \
|
||||
array_search_sorted_by(self, field, (value) field, &_index, &_exists); \
|
||||
if (!_exists) array_insert(self, _index, value); \
|
||||
} while (0)
|
||||
|
||||
// Private
|
||||
|
||||
typedef Array(void) Array;
|
||||
|
||||
/// This is not what you're looking for, see `array_delete`.
|
||||
static inline void _array__delete(Array *self) {
|
||||
if (self->contents) {
|
||||
ts_free(self->contents);
|
||||
self->contents = NULL;
|
||||
self->size = 0;
|
||||
self->capacity = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// This is not what you're looking for, see `array_erase`.
|
||||
static inline void _array__erase(Array *self, size_t element_size,
|
||||
uint32_t index) {
|
||||
assert(index < self->size);
|
||||
char *contents = (char *)self->contents;
|
||||
memmove(contents + index * element_size, contents + (index + 1) * element_size,
|
||||
(self->size - index - 1) * element_size);
|
||||
self->size--;
|
||||
}
|
||||
|
||||
/// This is not what you're looking for, see `array_reserve`.
|
||||
static inline void _array__reserve(Array *self, size_t element_size, uint32_t new_capacity) {
|
||||
if (new_capacity > self->capacity) {
|
||||
if (self->contents) {
|
||||
self->contents = ts_realloc(self->contents, new_capacity * element_size);
|
||||
} else {
|
||||
self->contents = ts_malloc(new_capacity * element_size);
|
||||
}
|
||||
self->capacity = new_capacity;
|
||||
}
|
||||
}
|
||||
|
||||
/// This is not what you're looking for, see `array_assign`.
|
||||
static inline void _array__assign(Array *self, const Array *other, size_t element_size) {
|
||||
_array__reserve(self, element_size, other->size);
|
||||
self->size = other->size;
|
||||
memcpy(self->contents, other->contents, self->size * element_size);
|
||||
}
|
||||
|
||||
/// This is not what you're looking for, see `array_swap`.
|
||||
static inline void _array__swap(Array *self, Array *other) {
|
||||
Array swap = *other;
|
||||
*other = *self;
|
||||
*self = swap;
|
||||
}
|
||||
|
||||
/// This is not what you're looking for, see `array_push` or `array_grow_by`.
|
||||
static inline void _array__grow(Array *self, uint32_t count, size_t element_size) {
|
||||
uint32_t new_size = self->size + count;
|
||||
if (new_size > self->capacity) {
|
||||
uint32_t new_capacity = self->capacity * 2;
|
||||
if (new_capacity < 8) new_capacity = 8;
|
||||
if (new_capacity < new_size) new_capacity = new_size;
|
||||
_array__reserve(self, element_size, new_capacity);
|
||||
}
|
||||
}
|
||||
|
||||
/// This is not what you're looking for, see `array_splice`.
|
||||
static inline void _array__splice(Array *self, size_t element_size,
|
||||
uint32_t index, uint32_t old_count,
|
||||
uint32_t new_count, const void *elements) {
|
||||
uint32_t new_size = self->size + new_count - old_count;
|
||||
uint32_t old_end = index + old_count;
|
||||
uint32_t new_end = index + new_count;
|
||||
assert(old_end <= self->size);
|
||||
|
||||
_array__reserve(self, element_size, new_size);
|
||||
|
||||
char *contents = (char *)self->contents;
|
||||
if (self->size > old_end) {
|
||||
memmove(
|
||||
contents + new_end * element_size,
|
||||
contents + old_end * element_size,
|
||||
(self->size - old_end) * element_size
|
||||
);
|
||||
}
|
||||
if (new_count > 0) {
|
||||
if (elements) {
|
||||
memcpy(
|
||||
(contents + index * element_size),
|
||||
elements,
|
||||
new_count * element_size
|
||||
);
|
||||
} else {
|
||||
memset(
|
||||
(contents + index * element_size),
|
||||
0,
|
||||
new_count * element_size
|
||||
);
|
||||
}
|
||||
}
|
||||
self->size += new_count - old_count;
|
||||
}
|
||||
|
||||
/// A binary search routine, based on Rust's `std::slice::binary_search_by`.
|
||||
/// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`.
|
||||
#define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \
|
||||
do { \
|
||||
*(_index) = start; \
|
||||
*(_exists) = false; \
|
||||
uint32_t size = (self)->size - *(_index); \
|
||||
if (size == 0) break; \
|
||||
int comparison; \
|
||||
while (size > 1) { \
|
||||
uint32_t half_size = size / 2; \
|
||||
uint32_t mid_index = *(_index) + half_size; \
|
||||
comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \
|
||||
if (comparison <= 0) *(_index) = mid_index; \
|
||||
size -= half_size; \
|
||||
} \
|
||||
comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \
|
||||
if (comparison == 0) *(_exists) = true; \
|
||||
else if (comparison < 0) *(_index) += 1; \
|
||||
} while (0)
|
||||
|
||||
/// Helper macro for the `_sorted_by` routines below. This takes the left (existing)
|
||||
/// parameter by reference in order to work with the generic sorting function above.
|
||||
#define _compare_int(a, b) ((int)*(a) - (int)(b))
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#elif defined(__GNUC__) || defined(__clang__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // TREE_SITTER_ARRAY_H_
|
||||
@@ -0,0 +1,266 @@
|
||||
#ifndef TREE_SITTER_PARSER_H_
|
||||
#define TREE_SITTER_PARSER_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define ts_builtin_sym_error ((TSSymbol)-1)
|
||||
#define ts_builtin_sym_end 0
|
||||
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
|
||||
|
||||
#ifndef TREE_SITTER_API_H_
|
||||
typedef uint16_t TSStateId;
|
||||
typedef uint16_t TSSymbol;
|
||||
typedef uint16_t TSFieldId;
|
||||
typedef struct TSLanguage TSLanguage;
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
TSFieldId field_id;
|
||||
uint8_t child_index;
|
||||
bool inherited;
|
||||
} TSFieldMapEntry;
|
||||
|
||||
typedef struct {
|
||||
uint16_t index;
|
||||
uint16_t length;
|
||||
} TSFieldMapSlice;
|
||||
|
||||
typedef struct {
|
||||
bool visible;
|
||||
bool named;
|
||||
bool supertype;
|
||||
} TSSymbolMetadata;
|
||||
|
||||
typedef struct TSLexer TSLexer;
|
||||
|
||||
struct TSLexer {
|
||||
int32_t lookahead;
|
||||
TSSymbol result_symbol;
|
||||
void (*advance)(TSLexer *, bool);
|
||||
void (*mark_end)(TSLexer *);
|
||||
uint32_t (*get_column)(TSLexer *);
|
||||
bool (*is_at_included_range_start)(const TSLexer *);
|
||||
bool (*eof)(const TSLexer *);
|
||||
void (*log)(const TSLexer *, const char *, ...);
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
TSParseActionTypeShift,
|
||||
TSParseActionTypeReduce,
|
||||
TSParseActionTypeAccept,
|
||||
TSParseActionTypeRecover,
|
||||
} TSParseActionType;
|
||||
|
||||
typedef union {
|
||||
struct {
|
||||
uint8_t type;
|
||||
TSStateId state;
|
||||
bool extra;
|
||||
bool repetition;
|
||||
} shift;
|
||||
struct {
|
||||
uint8_t type;
|
||||
uint8_t child_count;
|
||||
TSSymbol symbol;
|
||||
int16_t dynamic_precedence;
|
||||
uint16_t production_id;
|
||||
} reduce;
|
||||
uint8_t type;
|
||||
} TSParseAction;
|
||||
|
||||
typedef struct {
|
||||
uint16_t lex_state;
|
||||
uint16_t external_lex_state;
|
||||
} TSLexMode;
|
||||
|
||||
typedef union {
|
||||
TSParseAction action;
|
||||
struct {
|
||||
uint8_t count;
|
||||
bool reusable;
|
||||
} entry;
|
||||
} TSParseActionEntry;
|
||||
|
||||
typedef struct {
|
||||
int32_t start;
|
||||
int32_t end;
|
||||
} TSCharacterRange;
|
||||
|
||||
struct TSLanguage {
|
||||
uint32_t version;
|
||||
uint32_t symbol_count;
|
||||
uint32_t alias_count;
|
||||
uint32_t token_count;
|
||||
uint32_t external_token_count;
|
||||
uint32_t state_count;
|
||||
uint32_t large_state_count;
|
||||
uint32_t production_id_count;
|
||||
uint32_t field_count;
|
||||
uint16_t max_alias_sequence_length;
|
||||
const uint16_t *parse_table;
|
||||
const uint16_t *small_parse_table;
|
||||
const uint32_t *small_parse_table_map;
|
||||
const TSParseActionEntry *parse_actions;
|
||||
const char * const *symbol_names;
|
||||
const char * const *field_names;
|
||||
const TSFieldMapSlice *field_map_slices;
|
||||
const TSFieldMapEntry *field_map_entries;
|
||||
const TSSymbolMetadata *symbol_metadata;
|
||||
const TSSymbol *public_symbol_map;
|
||||
const uint16_t *alias_map;
|
||||
const TSSymbol *alias_sequences;
|
||||
const TSLexMode *lex_modes;
|
||||
bool (*lex_fn)(TSLexer *, TSStateId);
|
||||
bool (*keyword_lex_fn)(TSLexer *, TSStateId);
|
||||
TSSymbol keyword_capture_token;
|
||||
struct {
|
||||
const bool *states;
|
||||
const TSSymbol *symbol_map;
|
||||
void *(*create)(void);
|
||||
void (*destroy)(void *);
|
||||
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
|
||||
unsigned (*serialize)(void *, char *);
|
||||
void (*deserialize)(void *, const char *, unsigned);
|
||||
} external_scanner;
|
||||
const TSStateId *primary_state_ids;
|
||||
};
|
||||
|
||||
static inline bool set_contains(TSCharacterRange *ranges, uint32_t len, int32_t lookahead) {
|
||||
uint32_t index = 0;
|
||||
uint32_t size = len - index;
|
||||
while (size > 1) {
|
||||
uint32_t half_size = size / 2;
|
||||
uint32_t mid_index = index + half_size;
|
||||
TSCharacterRange *range = &ranges[mid_index];
|
||||
if (lookahead >= range->start && lookahead <= range->end) {
|
||||
return true;
|
||||
} else if (lookahead > range->end) {
|
||||
index = mid_index;
|
||||
}
|
||||
size -= half_size;
|
||||
}
|
||||
TSCharacterRange *range = &ranges[index];
|
||||
return (lookahead >= range->start && lookahead <= range->end);
|
||||
}
|
||||
|
||||
/*
|
||||
* Lexer Macros
|
||||
*/
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define UNUSED __pragma(warning(suppress : 4101))
|
||||
#else
|
||||
#define UNUSED __attribute__((unused))
|
||||
#endif
|
||||
|
||||
#define START_LEXER() \
|
||||
bool result = false; \
|
||||
bool skip = false; \
|
||||
UNUSED \
|
||||
bool eof = false; \
|
||||
int32_t lookahead; \
|
||||
goto start; \
|
||||
next_state: \
|
||||
lexer->advance(lexer, skip); \
|
||||
start: \
|
||||
skip = false; \
|
||||
lookahead = lexer->lookahead;
|
||||
|
||||
#define ADVANCE(state_value) \
|
||||
{ \
|
||||
state = state_value; \
|
||||
goto next_state; \
|
||||
}
|
||||
|
||||
#define ADVANCE_MAP(...) \
|
||||
{ \
|
||||
static const uint16_t map[] = { __VA_ARGS__ }; \
|
||||
for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) { \
|
||||
if (map[i] == lookahead) { \
|
||||
state = map[i + 1]; \
|
||||
goto next_state; \
|
||||
} \
|
||||
} \
|
||||
}
|
||||
|
||||
#define SKIP(state_value) \
|
||||
{ \
|
||||
skip = true; \
|
||||
state = state_value; \
|
||||
goto next_state; \
|
||||
}
|
||||
|
||||
#define ACCEPT_TOKEN(symbol_value) \
|
||||
result = true; \
|
||||
lexer->result_symbol = symbol_value; \
|
||||
lexer->mark_end(lexer);
|
||||
|
||||
#define END_STATE() return result;
|
||||
|
||||
/*
|
||||
* Parse Table Macros
|
||||
*/
|
||||
|
||||
#define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT)
|
||||
|
||||
#define STATE(id) id
|
||||
|
||||
#define ACTIONS(id) id
|
||||
|
||||
#define SHIFT(state_value) \
|
||||
{{ \
|
||||
.shift = { \
|
||||
.type = TSParseActionTypeShift, \
|
||||
.state = (state_value) \
|
||||
} \
|
||||
}}
|
||||
|
||||
#define SHIFT_REPEAT(state_value) \
|
||||
{{ \
|
||||
.shift = { \
|
||||
.type = TSParseActionTypeShift, \
|
||||
.state = (state_value), \
|
||||
.repetition = true \
|
||||
} \
|
||||
}}
|
||||
|
||||
#define SHIFT_EXTRA() \
|
||||
{{ \
|
||||
.shift = { \
|
||||
.type = TSParseActionTypeShift, \
|
||||
.extra = true \
|
||||
} \
|
||||
}}
|
||||
|
||||
#define REDUCE(symbol_name, children, precedence, prod_id) \
|
||||
{{ \
|
||||
.reduce = { \
|
||||
.type = TSParseActionTypeReduce, \
|
||||
.symbol = symbol_name, \
|
||||
.child_count = children, \
|
||||
.dynamic_precedence = precedence, \
|
||||
.production_id = prod_id \
|
||||
}, \
|
||||
}}
|
||||
|
||||
#define RECOVER() \
|
||||
{{ \
|
||||
.type = TSParseActionTypeRecover \
|
||||
}}
|
||||
|
||||
#define ACCEPT_INPUT() \
|
||||
{{ \
|
||||
.type = TSParseActionTypeAccept \
|
||||
}}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // TREE_SITTER_PARSER_H_
|
||||
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"grammars": [
|
||||
{
|
||||
"name": "p8-cart",
|
||||
"camelcase": "P8Cart",
|
||||
"scope": "source.p8-cart",
|
||||
"path": ".",
|
||||
"file-types": [
|
||||
"p8"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"version": "0.0.1",
|
||||
"license": "0BSD",
|
||||
"description": "tree-sitter grammar for the PICO-8 .p8 cartridge text format",
|
||||
"links": {
|
||||
"repository": "https://github.com/tree-sitter/tree-sitter-p8-cart"
|
||||
}
|
||||
},
|
||||
"bindings": {
|
||||
"c": true,
|
||||
"go": true,
|
||||
"node": true,
|
||||
"python": true,
|
||||
"rust": true,
|
||||
"swift": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user