Thursday, 3 August 2023

Codemirror

 Quite recently, CodeMirror 6 was born. It is high quality.

So lets read the guide until you get to Extension

Extensions can do all kinds of things

"from merely configuring some option"

/docs/ref/ is a little terse since it's the index of everything (except lezer). In /examples/config/ Compartments is how it might work.

 I cobble together:

All our languages in Codemirror.svelte:

import {stho} from "../lang/stho.ts";
import {javascript} from "@codemirror/lang-javascript"
$langs = [stho,javascript]

In stho, which this sort of is, you buy variables instead of declaring them, with a dollar sign in front, which compiles to 'let '. VScode doesn't know that, so syntax breaks all over.

Svelteily select lang:

<p>lang:
<select bind:value={lang} on:change={() => (setlang(lang))}>
{#each langs as lang}
<option value={lang}> {lang.name} </option>
{/each}
</select></p>

Which goes here:

$language = new Compartment
$setlang = &lang,{
view.dispatch({effects: language.reconfigure(lang())})
}

Which swaps it into place in here:

let startState = EditorState.create({
doc: value,
extensions: [
language.of(lang()),

And the result is:

@codemirror/lang-javascript provides also completion: 

If you are following along, give yourself the "use a Compartment around an extension so language is changeable" badge or commit.






stho lang

Is stho.grammar and stho.ts.

Lets get it parsing comments:


@tokens {
IOness { "i" | "o" }
Name { @asciiLetter+ }
Number { @digit+ }
nonnl { ![\n]+ }
nl { "\n" }
Sigil { "@" | "#" }
Space { @whitespace }
@precedence {IOness, Name}
@precedence {nl, Space}
}

@top Program { line* }
line { inline nl }

inline { Comment | expression* | Space* }

Comment { "#" nonnl }

expression { Name | Number | IOing }

IOing { IOness ' ' IOpath }

IOpath { Leg ("/" Leg)* }
Leg { Sigil? Name }

The @precedence {nl, Space} decides \n should complete line instead of just being Space at the end of inline. Else this error will erupt:

Overlapping tokens Space and nl used in same context (example: "\n")

You may have to reload the page to get that error to come and go. There's probably something to the way we import it in the 

But what about indent?

Perhaps I should start with lang-javascript, so we can have a solid understanding of typescript and sensible indenting|hints|etc, and add stho syntax to it.

At around letz.git / 83ca4da0159eaa

  lib/lang/lang-javascript, aka sthovascript
   in Codemirror.svelte, we:
   import {javascript as sthovascript} from "$lib/lang/lang-javascript"

   so far bad: sthovascript.name still says javascript
   I then change the name from 'javascript' in sthovascript.ts
   it has dist/index* that are still serving the old name!
    lets avoid dist/ (used when import is a package directory?)
   I then make the import from "$lib/lang/lang-javascript/src/index"
    and sthovascript.ts import grammar from '../lezer-javascript/src/sthovascript.grammar?raw'
     since we are now in /src/

   error: this.b.options.externalTokenizer is not a function
   in ExternalTokenSet.create
   not far inside our buildParser(grammar)

   I then try and `npm run prepare` for $lib/lang/lang-javascript
    but the output has not compiled the grammar yet either

  lib/lang/istho
   mergeforking just src/* from lang-javascript and lezer-javascript
   I lezer-generator to (BUILD.sh) the grammar (10s)
   on engaging this lang via the dropdown:
    Uncaught Error: Unrecognized extension value in extension set ([object Object]). This sometimes happens because multiple instances of @codemirror/state are loaded, breaking instanceof checks.
    https://github.com/codemirror/dev/issues/859

So on the way to try drop and reinstall everything I remember bun is so much faster at that, and recently got Svelte support, and bun 1.0 is coming out in a few days!

And it works!

To Bun

So I set about redoing the Containerfile for bun:

FROM debian:bookworm-slim
...
# bun
RUN apt-get install -y curl unzip
curl -fsSL https://bun.sh/install | bash

This runs out of disk space... In the qemu vm where I keep things that pipe from the web into bash.

There's nothing in podman ps -a, yet podman image rm -a (and subsets of) fail with image is in use by a container

The more powerful podman ps --all --storage and buildah rm --all are the answer.

But it only cleared a gig! Maybe I should kick this whole vm into the river.

I waste that gig installing baobab to further investigate where all this bloat is, yet it fails to start. This is too far off the track now.

...Minutes (of work) later I have deleted most qemu vms, replaced gox, am now running from my own notes:

and `curl ... > ~s/.ssh...`
`adduser s sudo`
generate sshd host keys, which the cloud image couldnt come with:
`dpkg-reconfigure openssh-server`

The latter says:
Creating SSH2 RSA key; this may take some time ...
3072 SHA256:GQi3LWeTLoImDqjiExFZOZ0QDZg/faHODLnxdj/o/Vw root@localhost (RSA)
Creating SSH2 ECDSA key; this may take some time ...
256 SHA256:xtVu3mGNMp5z63fSYLD5eH+mnDF7HQeZbR3R6jgcyCo root@localhost (ECDSA)
Creating SSH2 ED25519 key; this may take some time ...
256 SHA256:P1EWq5IxabTdCQIplJhA9nmOW8uenRbdO2s4agDoa1Q root@localhost (ED25519)
rescue-ssh.target is a disabled or a static unit not running, not starting it.
ssh.socket is a disabled or a static unit not running, not starting it.
s@localhost:~$ 
Which gets it ssh-to-able!

$ sudo hostname gox

Is not enough, you must echo 'gox' > /etc/hostname

$ sudo apt install podman rsync sshfs

Then I zap init to podman build, yet we're out of space! The backing image is 1.9G, and hasn't expanded for the 10G gox.qcow2 image. You just drop and recreate the first partition with fdisk, then resize2fs /dev/vda

Then it hums along...

Resize again. Etc.

Somehow gox:/ is 20G after building the py and cos containers, so I had to move gox to slower storage, but everything starts! As long as:

To podman-run cos, we specify the command: bash -ci 'bun run...

-c gives bash a command, -i (interactive) loads /root/.bashrc (and more?) for that bash session, which includes our bun installation. Otherwise bun is not found in $PATH.

There's something I didn't create expected in /app/.svelte-kit, so I add that.

Then, drama!

root@593e25ee7e35:/app# bun run vite dev -- --port 8000 --host 0.0.0.0
Loaded stylehouse_lite
error when starting dev server:
TypeError: Attempting to change configurable attribute of unconfigurable property.
at defineProperty (native)
at installPolyfills (/app/node_modules/@sveltejs/kit/src/exports/node/polyfills.js:21:15)
at <anonymous> (/app/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:31:18)
at dev (/app/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:28:6)
at <anonymous> (/app/node_modules/@sveltejs/kit/src/exports/vite/index.js:444:52)
at configureServer (/app/node_modules/@sveltejs/kit/src/exports/vite/index.js:444:26)
at <anonymous> (/app/node_modules/vite/dist/node/chunks/dep-df561101.js:65158:20)
error: "vite" exited with code 1 (SIGHUP)

This looks terrible - like Svelte isn't supported. Perhaps bun is okay with node/polyfills. Trying to understand what is going on here would be a mistake. You should always look for a high-level forward-escape instead of making sense of unexpected noise like this, or you'll lose days.

Turns out another something I didn't create was expected at /app/.vite! They both seem to be compiled stuff.

Then I delete them both and it works too! Works as in gets a different error message, which is a good thing most of the time:

   the stho-lang prototype transform|compiler
    vite <-> src/lib/lang/stho_lite_compiler.js
   is apparently not working, errors about that syntax.

   Im thinking of forward-escaping to svelte 4.2.0
    ya, `bun x create-svelte letz` makes a new project
     I copy things around...

And that works! Yay. letz.git 541b7dfb



No comments:

Post a Comment