diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-01-31 15:05:06 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2020-01-31 15:05:06 +0000 |
| commit | d6d294cabcd01ec7060953e692b3e8c119bd04ec (patch) | |
| tree | 5cc57f39d418bb965ad590d720649ca90a9be81f | |
| parent | comments, actual working things (diff) | |
| download | alive-d6d294cabcd01ec7060953e692b3e8c119bd04ec.tar.gz alive-d6d294cabcd01ec7060953e692b3e8c119bd04ec.zip | |
nested parens in comments
| -rw-r--r-- | parsing.moon | 7 | ||||
| -rw-r--r-- | spec/parsing_spec.moon | 15 |
2 files changed, 20 insertions, 2 deletions
diff --git a/parsing.moon b/parsing.moon index 5c3995f..1110b42 100644 --- a/parsing.moon +++ b/parsing.moon @@ -3,7 +3,11 @@ import R, S, P, V, C, Ct from require 'lpeg' -- whitespace wc = S ' \t\r\n' -comment = (P '#(') * (1 - P ')')^0 * (P ')') +comment = P { + 'comment', + expr: (P '(') * ((V 'expr') + (1 - P ')'))^0 * (P ')') + comment: (P '#(') * ((V 'expr') + (1 - P ')'))^0 * (P ')') +} space = (wc^1 * (comment * wc^1)^0) / 1 -- required whitespace mspace = (comment + wc)^0 / 1 -- optional whitespace @@ -40,6 +44,7 @@ sexpr = P { program = nexpr * -1 { + :comment :space :atom :expr diff --git a/spec/parsing_spec.moon b/spec/parsing_spec.moon index f1c85f2..5af12ba 100644 --- a/spec/parsing_spec.moon +++ b/spec/parsing_spec.moon @@ -1,4 +1,4 @@ -import space, atom, expr, explist, sexpr, nexpr, program from require 'parsing' +import space, atom, expr, explist, sexpr, nexpr, program, comment from require 'parsing' describe 'atom parsing', -> test 'symbols', -> @@ -104,6 +104,19 @@ describe 'sexpr', -> assert.is.equal 42, node.tag assert.is.equal str, node\stringify! +describe 'comments', -> + comment = comment / 1 + test 'simple parsing', -> + str = '#(this is a comment)' + assert.is.equal str, comment\match str + + test 'nested parsing', -> + str = '#(this is a comment (with nested parenthesis))' + assert.is.equal str, comment\match str + + str = '#(this is a comment #(with nested comments))' + assert.is.equal str, comment\match str + describe 'resynthesis', -> test 'mixed parsing', -> str = '( 3 ok-yes |
