feat: install moo and use in toy example (#2)

I failed to find a satisfactory way to compile and import moo.js with
  fixed specifiers, so finally I just gave up and patched the distributed
  moo code to be an es6 module. Very ugly but it works.

Resolves #1.

Reviewed-on: #2
Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Co-committed-by: Glen Whitney <glen@studioinfinity.org>
This commit is contained in:
Glen Whitney 2023-09-01 21:32:25 +00:00 committed by Glen Whitney
parent 67a07e2000
commit e0861ea157
11 changed files with 88 additions and 25 deletions

View file

@ -1,2 +1,2 @@
{convert} from ./index.js
console.log convert 'bar'
console.log convert 'while (10) cows\nmoo'

View file

@ -1,14 +1,37 @@
moo from ../deps/moo.js
type Tree = {[key:string]: string | Tree}
lexer := moo.compile
WS: /[ \t]+/
comment: /\/\/.*?$/
number: /0|[1-9][0-9]*/
string: /"(?:\\["\\]|[^\n"\\])*"/
lparen: '('
rparen: ')'
keyword: 'while if else moo cows'.split(' ')
NL:
match: /\n/
lineBreaks: true
export function tree97(vrml1: string): Tree
{converted: 'foo'}
tree: Tree := {}
for tok, index of lexer.reset vrml1
if tok.type then tree[`${index}`] = {tok.type, tok.value}
tree
function render(t: string | Tree): string
if typeof t is 'string'
return t
if result := t.converted
render result
else '<Conversion failed>'
result .= ''
for item of Object.values t
if typeof item is 'string' then result += item + ' '
else switch item.type
'WS' // ignore
'NL'
result += "\n"
else result += render(item.type) + ' '
result
export function convert(vrml1: string): string
render tree97 vrml1