From 4031b59b19152bb3e9e2d323caa7faffb7557c82 Mon Sep 17 00:00:00 2001 From: s-ol Date: Wed, 29 Jan 2020 23:41:05 +0100 Subject: support tagging --- spec/ast_spec.moon | 15 ++++++++ spec/parsing_spec.moon | 100 +++++++++++++++++++++++++++++++++++++++++++++++++ spec/peg_spec.moon | 71 ----------------------------------- 3 files changed, 115 insertions(+), 71 deletions(-) create mode 100644 spec/ast_spec.moon create mode 100644 spec/parsing_spec.moon delete mode 100644 spec/peg_spec.moon (limited to 'spec') diff --git a/spec/ast_spec.moon b/spec/ast_spec.moon new file mode 100644 index 0000000..402ce03 --- /dev/null +++ b/spec/ast_spec.moon @@ -0,0 +1,15 @@ +import Atom, Xpr from require 'ast' + +describe 'Xpr', -> + it 'can be walked', -> + aa = Xpr { '', (Atom '1', '', 1), '' } + ab = Xpr { '', (Atom '2', '', 2), '' } + ac = Xpr { '', (Atom '3', '', 3), '' } + b = Xpr { '', aa, ' ', ab, '' } + + root = Xpr { '', ac, ' ', b, '' }, 'naked' + + iter = root\walk_sexpr! + for res in *{ ac, b, aa, ab } + assert.is.equal res, iter! + assert.is.nil iter! diff --git a/spec/parsing_spec.moon b/spec/parsing_spec.moon new file mode 100644 index 0000000..5e6e1fc --- /dev/null +++ b/spec/parsing_spec.moon @@ -0,0 +1,100 @@ +import space, atom, expr, explist, sexpr, nexpr, program from require 'parsing' + +describe 'atom parsing', -> + test 'symbols', -> + sym = atom\match 'some-toast help' + assert.is.equal 'some-toast', sym.raw + assert.is.equal 'some-toast', sym.value + assert.is.equal 'some-toast', sym\stringify! + + test 'numbers', -> + num = atom\match '1234 nope' + assert.is.equal '1234', num.raw + assert.is.equal 1234, num.value + assert.is.equal '1234', num\stringify! + + test 'strings', -> + str = atom\match '"help some stuff!" nope' + assert.is.equal 'help some stuff!', str.raw + assert.is.equal 'help some stuff!', str.value + assert.is.equal '"help some stuff!"', str\stringify! + +test 'whitespace parsing', -> + assert.is.equal ' ', space\match ' ' + assert.is.equal '\n\t ', space\match '\n\t ' + +describe 'nexpr parsing', -> + it 'handles leading whitespace', -> + node = nexpr\match ' 3\tok-yes' + + assert.is.equal 2, #node + assert.is.equal 3, node[1].value + assert.is.equal 'ok-yes', node[2].value + + assert.is.equal ' 3\tok-yes', node\stringify! + + it 'handles trailing whitespace', -> + node = nexpr\match '3\tok-yes\n' + + assert.is.equal 2, #node + assert.is.equal 3, node[1].value + assert.is.equal 'ok-yes', node[2].value + + assert.is.equal '3\tok-yes\n', node\stringify! + + it 'handles whitespace everywhere', -> + node = nexpr\match ' 3\tok-yes\n' + + assert.is.equal 2, #node + assert.is.equal 3, node[1].value + assert.is.equal 'ok-yes', node[2].value + + assert.is.equal ' 3\tok-yes\n', node\stringify! + +describe 'sexpr', -> + test 'basic parsing', -> + str = '( 3 ok-yes + "friend" )' + node = sexpr\match str + + assert.is.equal '(', node.style + assert.is.equal 3, #node + assert.is.equal 3, node[1].value + assert.is.equal 'ok-yes', node[2].value + assert.is.equal 'friend', node[3].value + + assert.is.equal str, node\stringify! + + test 'tag parsing', -> + str = '([42]tagged 2)' + node = sexpr\match str + + assert.is.equal '(', node.style + assert.is.equal 2, #node + assert.is.equal 'tagged', node[1].value + assert.is.equal 2, node[2].value + + assert.is.equal 42, node.tag + assert.is.equal str, node\stringify! + +describe 'resynthesis', -> + test 'mixed parsing', -> + str = '( 3 ok-yes + "friend" )' + node = program\match str + assert.is.equal str, node\stringify! + + test 'complex', -> + str = ' + (osc "/radius" (lfo (cc 14))) + + (osc rot + (step + (note "kick") + (random-rot) + (random-rot) + (random-rot) + (random-rot) + ) + ) ' + assert.is.equal str, (program\match str)\stringify! diff --git a/spec/peg_spec.moon b/spec/peg_spec.moon deleted file mode 100644 index 1b25406..0000000 --- a/spec/peg_spec.moon +++ /dev/null @@ -1,71 +0,0 @@ -import space, atom, expr, explist, sexpr, nexpr, program from require 'parsing' - -describe 'atom parsing', -> - test 'symbols', -> - sym = atom\match 'some-toast help' - assert.is.equal 'some-toast', sym.raw - assert.is.equal 'some-toast', sym.value - assert.is.equal 'some-toast', sym\stringify! - - test 'numbers', -> - num = atom\match '1234 nope' - assert.is.equal '1234', num.raw - assert.is.equal 1234, num.value - assert.is.equal '1234', num\stringify! - - test 'strings', -> - str = atom\match '"help some stuff!" nope' - assert.is.equal 'help some stuff!', str.raw - assert.is.equal 'help some stuff!', str.value - assert.is.equal '"help some stuff!"', str\stringify! - -test 'whitespace parsing', -> - assert.is.equal ' ', space\match ' ' - assert.is.equal '\n\t ', space\match '\n\t ' - -describe 'nexpr parsing', -> - it 'handles leading whitespace', -> - node = nexpr\match ' 3\tok-yes' - - assert.is.equal 2, #node - assert.is.equal 3, node[1].value - assert.is.equal 'ok-yes', node[2].value - - assert.is.equal ' 3\tok-yes', node\stringify! - - it 'handles trailing whitespace', -> - node = nexpr\match '3\tok-yes\n' - - assert.is.equal 2, #node - assert.is.equal 3, node[1].value - assert.is.equal 'ok-yes', node[2].value - - assert.is.equal '3\tok-yes\n', node\stringify! - - it 'handles whitespace everywhere', -> - node = nexpr\match ' 3\tok-yes\n' - - assert.is.equal 2, #node - assert.is.equal 3, node[1].value - assert.is.equal 'ok-yes', node[2].value - - assert.is.equal ' 3\tok-yes\n', node\stringify! - -test 'sexpr parsing', -> - str = '( 3 ok-yes - "friend" )' - node = sexpr\match str - - assert.is.equal '(', node.style - assert.is.equal 3, #node - assert.is.equal 3, node[1].value - assert.is.equal 'ok-yes', node[2].value - assert.is.equal 'friend', node[3].value - - assert.is.equal str, node\stringify! - -test 'mixed parsing', -> - str = '( 3 ok-yes - "friend" )' - node = program\match str - assert.is.equal str, node\stringify! -- cgit v1.2.3