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 / 0.input.cstml
Last active March 14, 2024 03:56
BABLR two-VM parse log
<></>
@conartist6
conartist6 / index.md
Last active November 29, 2023 15:10
agAST

What is agAST?

agAST is a generalized AST format for javascript. It is the format created for and used by the BABLR VM. It is meant to specify basic aspects of how tools store programs, so that common tools can be used to work on programs written in any programming language.

agAST is meant to be the successor to the existing ESTree specification. Unlike ESTree, agAST has no language-specific opinions.

agAST trees are made up of nodes of the following shape:

let node = {
@conartist6
conartist6 / parse-trampoline.js
Created November 10, 2023 18:35
BABLR-VM minimal parse trampoline
import { buildExpression, effectsFor } from './utils/instruction.js';
import { getCooked } from './utils/token.js';
import { Match } from './match.js';
import { Context } from './context.js';
import { Source } from './source.js';
import { runSync } from './run.js';
import { dispatcher } from './dispatcher.js';
import { transformTokenMatcher } from './transforms.generated.js';
const defer = Symbol('defer');
@conartist6
conartist6 / isobench.js
Created November 3, 2023 16:24
Iterator benchmark
import { IsoBench } from 'iso-bench';
const range = max => ({
*[Symbol.iterator]() {
for (let i = 0; i < max; i++) yield i;
}
});
globalThis.i = 0;
@conartist6
conartist6 / index.js
Last active October 22, 2023 15:17
Private/readonly class properties with weak maps
// To keep the data truly private, just don't export these
const privateMaps = {
secret: new WeakMap(),
constant: new WeakMap(),
}
export class Test {
constructor(secret, constant) {
privateMaps.secret.set(this, secret);
privateMaps.constant.set(this, constant);
@conartist6
conartist6 / index.js
Last active October 13, 2023 16:01
Prettier agAST example
// Node objects are immutable
// Also immutable are: properties, attributes, children, terminals, and any arrays
// Immutable trees can be cached as valid with regard to a particular grammar!
let freeze = (node) => Object.freeze(Object.seal(node));
// Helpers for constructing agAST trees
let t = {
// Tokens are really just nodes with non-ref children
token: (type, str, attributes = {}) => t.node(type, [t.str([str])], {}, attributes),
node: (type, children = [], properties = {}, attributes = {}) =>
@conartist6
conartist6 / agAST.js
Last active October 8, 2023 13:11
Annotated agAST builder example
// Node objects are immutable
// Also immutable are: properties, attributes, children, terminals, and any arrays
// Immutable trees can be cached as valid with regard to a particular grammar!
const freeze = (node) => Object.freeze(Object.seal(node));
// Helpers to make the following code less verbose
let t = {
token: (type, str, attributes) => t.node(type, [t.str([str])], {}, attributes),
node: (type, children, properties, attributes) =>
freeze({
@conartist6
conartist6 / index.jsonc
Last active October 5, 2023 15:54
@bablr/boot sample instruction AST
// eat(<| Token 'foo' |>
{
"tagName": {
"language": "Instruction",
"type": "Call"
},
"children": [
{
"type": "Reference",
"value": "verb"
@conartist6
conartist6 / cstml.cstml
Last active September 9, 2023 13:53
Proposal: tagType.parser
<!docype cstml>
<!parsers>
<CSTML "https://url/to/grammar"/>
<Regex "https://url/to/re/grammar"/>
</>
<CSTML:TokenTag [Tag]>
<| Punctuator "<|" balanced="|>" startSpan="Tag" |>
<Identifier path="tagName">
<| Identifier "ID" |>
</>
@conartist6
conartist6 / cstml.js
Last active September 18, 2023 00:39
CSTML BABLR grammar (will run with https://github.com/bablr-lang/bablr-vm)
/*
This file contains a formal grammar defined by yielding instructions to a state machine.
The grammar is extensible, because you can always wrap it in a higher-order grammar!
Formally the system is a VM exectuting an https://en.wikipedia.org/wiki/Earley_parser
The VM is not yet sufficiently finished to execute this grammar, but it soon will be.
Usage will be:
```js
import { parse } from 'cst-tokens';