From 6a7a2ddaca798f3cccac394d1fb9f317cbe90de6 Mon Sep 17 00:00:00 2001 From: s-ol Date: Sat, 7 Mar 2020 21:58:30 +0100 Subject: add ldoc documentation --- core/pattern.moon | 65 ------------------------------------------------------- 1 file changed, 65 deletions(-) delete mode 100644 core/pattern.moon (limited to 'core/pattern.moon') diff --git a/core/pattern.moon b/core/pattern.moon deleted file mode 100644 index 13497af..0000000 --- a/core/pattern.moon +++ /dev/null @@ -1,65 +0,0 @@ -unpack or= table.unpack - -class Pattern - new: (opts) => - if 'string' == type opts - splat, const, type, opt = opts\match '^(%*?)(=?)([%w%-%_%/]+)(%??)$' - assert type, "couldn't parse type pattern '#{opts}'" - opts = { - :type - splat: splat == '*' - const: const == '=' - opt: opt == '?' - } - - @type = opts.type - @const = opts.const - @opt = opts.opt - @splat = opts.splat - - matches: (result) => - return false unless result - - if @const - return false unless result\is_const! - - if not result.value - return @type == 'nil' - - return true if @type == 'any' - - result.value.type == @type - - match: (results) => - if @splat - matched = while @matches results[1] - table.remove results, 1 - - assert @opt or #matched > 0, "expected at least one argument for spread" - matched - else - matches = @matches results[1] - assert @opt or matches, "couldn't match argument #{results[1]} as #{@}" - if matches then table.remove results, 1 - - __tostring: => - str = @type - str = '*' .. str if @splat - str = '=' .. str if @const - str = str .. '?' if @opt - str - -match = (pattern, results) -> - patterns = while pattern - pat, rest = pattern\match '^([^ ]+) (.*)$' - pat = pattern unless pat - pattern = rest - Pattern pat - values = [p\match results for p in *patterns] - assert #results == 0, "#{#results} extra arguments given!" - values - -{ - :Pattern - :match -} -- cgit v1.2.3