I'm trying to write a small major-mode for editing pico-8 files in emacs and I had a quick question regarding best practices and matching a somewhat arbitrary form in the pico-8 cartridge files for the syntax table of the mode.
A pico-8 cartridge, in plain-text looks like this:
pico-8 cartridge // http://www.pico-8.com
version 4
__lua__
-- pico-8 api test // by zep
-- demonstrates all pico-8
-- functions and some lua
cls() -- clear screen
rect(0,0,127,127,1)
music(0)
sfx(0, 3)
-- draw palette
x=3
rectfill(1,1,7,7,5)
for i=0,15 do
print(i,x,2,i)
x = x + 6 + flr(i/10)*4
end
__gfx__
0000000000ff0ff044444444bbbbbbbb000000004444444400000000000000000000000000000000000000000000000000000000000000000000000000000000
0700007000ff0ff04444444433333333000000004444444400000000000000000000000000000000000000000000000000000000000000000000000000000000
0070070000fffff042444444b3b3b3b3000000004444444400000000000000000000000000000000000000000000000000000000000000000000000000000000
0007700000f1ff104444444433333333000000004444424400000e00000000000000000000000000000000000000000000000000000000000000000000000000
...
Basically, it's some metadata, a bunch of lua code, and then some more raw data of the cart's graphics/music/sound effects.
I wrote the following regex to match lines up to and including __lua__:
"\\(.\\|\n\\)*?__lua__$"
This works to present the first three lines in a comment face.
The following regex was an attempt to match every line after __gfx__:
"__gfx__\\(.\\|\n\\)*"
I am pretty sure there's a better way to write this regex, but even so, it actually only puts __gfx__ and the following few lines into a comment face, rather than the rest of the buffer like I had hoped.
Is using regexes the best way to match arbitrary syntaxes like this though? I tried looking at some other major-modes to see how they handled it (particularly things like code blocks in markdown-mode), and it seems they also use regex for these kinds of expressions.
My full major-mode code is here: https://gist.github.com/taylskid/a81ca9343711ada4ba85c7451803185a
I also attached an image showing the comment face not correctly applying here:
Sorry if this is trivial or can easily be found somewhere else. My searches didn't get very far. Thanks in advance.
__gfx__
had an\r
in it? Otherwise Emacs' behavior seems kind of random. Though, in general regular expressions in Emacs / POSIX don't work well with very long strings that don't have a definitive end marker. So, maybe there's a hardcoded restriction in Emacs implementation of regex preventing it from matching very long strings (this is very inefficient). In general, regular expressions can only approximate the grammar of the language you work with, so you need to take this into account up-front.__gfx__
and down to the end of buffer.