aboutsummaryrefslogtreecommitdiffstats
path: root/core/pattern.moon
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-03-07 20:58:30 +0000
committers-ol <s-ol@users.noreply.github.com>2020-03-07 20:58:51 +0000
commit6a7a2ddaca798f3cccac394d1fb9f317cbe90de6 (patch)
tree4e49e53057b39f75813311ede13f87d238061fe6 /core/pattern.moon
parentspellcheck (diff)
downloadalive-6a7a2ddaca798f3cccac394d1fb9f317cbe90de6.tar.gz
alive-6a7a2ddaca798f3cccac394d1fb9f317cbe90de6.zip
add ldoc documentation
Diffstat (limited to 'core/pattern.moon')
-rw-r--r--core/pattern.moon65
1 files changed, 0 insertions, 65 deletions
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
-}