diff options
| author | s-ol <s+removethis@s-ol.nu> | 2025-03-14 21:36:23 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2025-03-14 21:36:23 +0000 |
| commit | 491804dc42dcdc84b8eea52d435c6648f0fb274c (patch) | |
| tree | 37e63568d66bdd1146de4240cd5285e26c0e16c7 | |
| parent | support fractions as number literals (diff) | |
| download | alive-491804dc42dcdc84b8eea52d435c6648f0fb274c.tar.gz alive-491804dc42dcdc84b8eea52d435c6648f0fb274c.zip | |
somewhat clean up alv.parsing
| -rw-r--r-- | alv/ast.moon | 3 | ||||
| -rw-r--r-- | alv/cell.moon | 45 | ||||
| -rw-r--r-- | alv/parsing.moon | 12 | ||||
| -rw-r--r-- | spec/internal/cell_spec.moon | 6 |
4 files changed, 32 insertions, 34 deletions
diff --git a/alv/ast.moon b/alv/ast.moon index 9f0b819..04e6627 100644 --- a/alv/ast.moon +++ b/alv/ast.moon @@ -32,13 +32,14 @@ -- @function stringify -- @treturn string the exact string this Node was parsed from -import Cell from require 'alv.cell' +import Cell, RootCell from require 'alv.cell' import Constant from require 'alv.result.const' import Dummy from require 'alv.dummy' import Tag from require 'alv.tag' { :Cell + :RootCell :Constant :Dummy :Tag diff --git a/alv/cell.moon b/alv/cell.moon index 1b035f4..ced9032 100644 --- a/alv/cell.moon +++ b/alv/cell.moon @@ -11,7 +11,17 @@ import Error from require 'alv.error' import op_invoke, fn_invoke from require 'alv.invoke' import Tag from require 'alv.tag' -local RootCell +parse_args = (tag, parts) -> + if not parts + parts, tag = tag, nil + + children, white = {}, { [0]: parts[1] } + + for i = 2,#parts,2 + children[i/2] = parts[i] + white[i/2] = parts[i+1] + + tag, children, white class Cell --- members @@ -131,17 +141,6 @@ class Cell assert #@white == #@children, "mismatched whitespace length" - parse_args = (tag, parts) -> - if not parts - parts, tag = tag, nil - - children, white = {}, { [0]: parts[1] } - - for i = 2,#parts,2 - children[i/2] = parts[i] - white[i/2] = parts[i+1] - - tag, children, white --- parse a Cell (for parsing with Lpeg). -- -- @tparam[opt] Tag tag @@ -151,17 +150,6 @@ class Cell tag, children, white = parse_args ... Cell tag, children, white - --- parse a root Cell (for parsing with Lpeg). - -- - -- Root-Cells are at the root of an ALV document. - -- They have an implicit head of 'do' and a `[0]` tag. - -- - -- @tparam table parts - -- @treturn Cell - @parse_root: (...) -> - tag, children, white = parse_args (Tag.parse '0'), ... - RootCell tag, children, white - -- @type RootCell class RootCell extends Cell head: => Constant.sym 'do' @@ -182,6 +170,17 @@ class RootCell extends Cell buf + --- parse a root Cell (for parsing with Lpeg). + -- + -- Root-Cells are at the root of an ALV document. + -- They have an implicit head of 'do' and a `[0]` tag. + -- + -- @tparam table parts + -- @treturn Cell + @parse: (...) -> + tag, children, white = parse_args (Tag.parse '0'), ... + RootCell tag, children, white + { :Cell :RootCell diff --git a/alv/parsing.moon b/alv/parsing.moon index c44a1ab..7174c06 100644 --- a/alv/parsing.moon +++ b/alv/parsing.moon @@ -2,9 +2,7 @@ -- Lpeg Grammar for parsing `alive` code. -- -- @module parsing -import Constant from require 'alv.result' -import Cell from require 'alv.cell' -import Tag from require 'alv.tag' +import Cell, RootCell, Constant, Tag from require 'alv.ast' import R, S, P, V, C, Ct from require 'lpeg' -- whitespace @@ -37,16 +35,16 @@ expr = (V 'cell') + atom explist = Ct mspace * ((V 'expr') * (space * (V 'expr'))^0 * mspace)^-1 tag = (P '[') * (digit^1 / Tag.parse) * (P ']') -cell = (P '(') * tag^-1 * (V 'explist') * (P ')') / Cell.parse +cell = (P '(') * tag^-1 * explist * (P ')') / Cell.parse root = P { - (V 'explist') / Cell.parse_root - :expr, :explist, :cell + explist / RootCell.parse + :expr, :cell } cell = P { 'cell' - :expr, :explist, :cell + :expr, :cell } program = root * -1 diff --git a/spec/internal/cell_spec.moon b/spec/internal/cell_spec.moon index a8e79fe..bd97e63 100644 --- a/spec/internal/cell_spec.moon +++ b/spec/internal/cell_spec.moon @@ -39,18 +39,18 @@ describe 'Cell', -> describe 'RootCell', -> test 'tag is always [0]', -> - cell = Cell.parse_root {} + cell = RootCell.parse {} assert.is.equal '[0]', cell.tag\stringify! test 'head is always "do"', -> - cell = Cell.parse_root {} + cell = RootCell.parse {} assert.is.equal (Constant.sym 'do'), cell\head! cell = RootCell nil, { hello_world, two_plus_two } assert.is.equal (Constant.sym 'do'), cell\head! test 'tail is all children', -> - cell = Cell.parse_root {} + cell = RootCell.parse {} assert.is.same {}, cell\tail! cell = RootCell nil, { hello_world, two_plus_two } |
