diff options
| author | s-ol <s+removethis@s-ol.nu> | 2025-04-05 17:48:08 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2025-04-07 10:33:17 +0000 |
| commit | 6d05263540b4a50f8d2ccb70b69ed6760e2ab691 (patch) | |
| tree | 36c130068e18712181a6d7db095ed8092d12b694 /alv/template_string.moon | |
| parent | propagate dynamic scope in require/import (diff) | |
| download | alive-6d05263540b4a50f8d2ccb70b69ed6760e2ab691.tar.gz alive-6d05263540b4a50f8d2ccb70b69ed6760e2ab691.zip | |
template strings as syntax sugar for Ops
Diffstat (limited to 'alv/template_string.moon')
| -rw-r--r-- | alv/template_string.moon | 83 |
1 files changed, 0 insertions, 83 deletions
diff --git a/alv/template_string.moon b/alv/template_string.moon deleted file mode 100644 index bd6ae0f..0000000 --- a/alv/template_string.moon +++ /dev/null @@ -1,83 +0,0 @@ ----- --- A string with substitutions, partially implements the `AST` interface. --- --- Is automatically "splatted" into multiple expressions when the containing --- `Cell` is evaluated: the original string (str=), but with with substitution --- markers like `#{1}`, `#{2}` and so on and one expression for each substitution. --- --- `#` symbols in the original string are escaped to `##`. --- --- @classmod TemplateString -import Constant from require 'alv.result' - --- @type TemplateString -class TemplateString ---- AST interface --- --- `TemplateString` partially implements the `AST` interface. --- @section ast - - --- create a clone with its own identity. - -- - -- creates a clone of this TemplateString by cloning all child expressions - -- recursively. - -- - -- @tparam Tag parent - -- @treturn TemplateString - clone: (parent) => - children = [child\clone parent for child in *@children] - @@ @string, children - - --- stringify this TemplateString. - -- - -- if `depth` is passed, does not faithfully recreate the original string but - -- rather create useful debug output. - -- - -- @tparam[opt] int depth the maximum depth, defaults to infinite - -- @treturn string the exact string this TemplateString was parsed from - stringify: (depth=-1) => - children = ['#' .. child\stringify depth for child in *@children] - str = @@.subst @.string!, (i) -> '#' .. @children[i]\stringify depth - '#"' .. str .. '"' - ---- static functions --- @section static - - new: (@string, @children) => - - --- apply substitutions to a template string. - -- - -- This also reverses the escaping that the parser applied. - -- - -- @classmethod - -- @tparam string str the evaluated `TemplateString.string` - -- @tparam function fn function called with index i to obtain string to substitute - -- @treturn string - @subst: (str, fn) -> - str = str\gsub '##', '#' - str\gsub '#{(%d+)}', (i) -> fn tonumber i - - --- parse a TemplateString (for parsing with Lpeg). - -- - -- @classmethod - -- @tparam {string|AST,...} pieces - -- @treturn TemplateString - @parse: (...) => - string = '' - children = {} - - i = 1 - for elem in *{...} - string ..= if elem == '##' then elem - elseif 'string' == type elem then elem\gsub '#', '##' - else - table.insert children, elem - '#{' .. #children .. '}' - - string = Constant.str string - - @@ string, children - -{ - :TemplateString -} |
