Skip to content

Instantly share code, notes, and snippets.

View conartist6's full-sized avatar

Conrad Buck conartist6

  • Boulder, CO
View GitHub Profile
@conartist6
conartist6 / json-parser.js
Last active January 17, 2023 17:33
A human-friendly json parser with parserate
import parserate from '@iter-tools/parserate';
const t = {
token: (value) => ({ type: 'token', value }),
literal: (value) => ({ type: 'literal', value }),
};
const escapes = {
'"': '"',
'\\': '\\',
@conartist6
conartist6 / json-tokenizer.js
Last active March 20, 2022 21:29
No-lib json tokenizer
const t = {
token: (value) => ({ type: 'token', value }),
literal: (value) => ({ type: 'literal', value }),
};
const escapes = {
'"': '"',
'\\': '\\',
b: '\b',
f: '\f',
n: '\n',
@conartist6
conartist6 / json-tokenizer.js
Created March 20, 2022 21:29
Peekerate json tokenizer
import peekerate from '@iter-tools/peekerate';
export function* tokenize(input) {
const peekr = peekerate(input);
while (!peekr.done) {
const char = peekr.value;
if (char === '"') {
// yields three tokens: " content "
yield* tokenizeString(peekr);
@conartist6
conartist6 / data-structure
Last active March 22, 2022 13:04
@iter-tools/regex data structure
┌───────────────┐
│ Expression │
│ root: true │
│ globalIdx: 1 │
└───────┬───────┘
parentSeq │ ▲
│ │ expr
┌──────────────┐ ┌───────┴─┴─────┐
│ State │ │ State │
│ type: cont │ │ type: success │
@conartist6
conartist6 / diagrams.md
Last active March 28, 2022 18:15
Regex engine refactor diagrams

Before

                                                            ┌───────────────┐
                                                            │  Expression   │
                                                            │  root: true   │
                                                            │ globalIdx: 1  │
                                                            └───────┬───────┘
                                                          parentSeq │ ▲
                                                                    │ │ expr
                                  ┌──────────────┐          ┌───────┴─┴─────┐
@conartist6
conartist6 / index.md
Last active May 25, 2022 17:26
Node package scripts

Node packages usually ship with scripts that are useful to developers. Common scripts might be build, test, or lint, but you will need to look inside the package.json file to see what scripts are actually available for a particular package. Here is what a common package with a build script might look like:

{
  "name": "my-package",
  "version": "0.1.0",
  "scripts": {
    "build": "transpile -f lib/index.ts -o /lib/index.js"
  },
 "devDependencies": {
@conartist6
conartist6 / builders.js
Last active May 6, 2022 00:12
CST traversal prototype
class Builder {
*ensure(...tokens) {
for (const token of tokens) {
yield* token.type === 'Thunk' ? token.ensure(this) : this.advance(token);
}
}
*allow(...tokens) {
for (const token of tokens) {
yield* token.type === 'Thunk' ? token.allow(this) : this.advance(token, true);
function sum(arr) {
let acc = 0;
for (let i = 0; i < arr.length; i++) {
acc += arr[i].val;
}
return acc;
}
const array1 = [];
const array2 = [];
@conartist6
conartist6 / lamp.js
Last active June 1, 2023 15:53
State machine with generators
/*
Lamps often have on-off switches that twist clockwise, clicking as they do.
Some such switches have four states, even though the lamp has only two (on, and off).
On these lamps clicking the switch will only cause an on/off transition every other click.
Just looking at the lamp you'll know if it's on, but not if the next click will turn it off.
To reliably interact with such a lamp you will use an algorithm, even in real life.
I use the term strategy to refer to an algorithm expressed with generators like this --
the strategy pattern is useful to abstract an algorithm from the representation of the data.
For more about lamps: https://temperaturemaster.com/why-does-it-take-two-clicks-to-turn-on-a-lamp/
@conartist6
conartist6 / output.md
Last active September 15, 2022 22:21
cst-tokens alpha output

Input:

if (foo) {
  // comment
}

Traversal log