38 lines
916 B
Plaintext
38 lines
916 B
Plaintext
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
|
|
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
|
|
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
|