Cart grammar: tolerate leading blank lines before the magic header

`extras: $ => []` in the cart grammar made the parser fail at byte 0
on any whitespace-only or empty line before `pico-8 cartridge //...`.
Real PICO-8 carts always start with the header at byte 0 so this
rarely surfaced in production, but it ( a ) broke the `tree-sitter
test` corpus harness, which prepends a newline to each fixture, and
( b ) would mis-flag a hand-edited cart that gained an accidental
blank line up top.

Fix: prefix the `cartridge` rule with `repeat($._blank_line)` and add
a hidden `_blank_line` token matching `[ \t]*\n`. Junk content before
the header ( a non-blank, non-magic line ) still produces an ERROR.

Restores the test corpus that was dropped in v0.1 ( previously failing
on this same edge case ) and adds a fixture for the unknown_section
fallback while the corpus is being rebuilt.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-01 12:56:59 -07:00
parent 445aac4e30
commit 7557a34c89
5 changed files with 1190 additions and 894 deletions
+9 -4
View File
@@ -149,10 +149,15 @@ dev extension to pick up changes.
### Tests
Sample carts live under `examples/`. Use `tree-sitter parse` directly for
verification — corpus-style `tree-sitter test` is not currently set up
because the cart grammar's strict `extras: []` doesn't tolerate the
leading newline that the test harness prepends to each fixture.
Sample carts live under `examples/`; parse them directly with
`tree-sitter parse <file>` for ad-hoc checks.
The cart grammar has a corpus under `grammars/p8-cart/test/corpus/` —
run `( cd grammars/p8-cart && npx tree-sitter test )`. The corpus
covers the empty-section skeleton, normal Lua content, the case where
a Lua identifier resembles a section marker ( e.g. `local __foo__ = 1`
must remain a `line`, not be re-tokenized as a marker ), and the
fallback `unknown_section` rule.
## Roadmap