rufus-lua-moon 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
- module("moonscript.data", package.seeall)
2
- local concat = table.concat
1
+ local concat, remove, insert = table.concat, table.remove, table.insert
2
+ local Set
3
3
  Set = function(items)
4
4
  local self = { }
5
5
  local _list_0 = items
@@ -9,6 +9,7 @@ Set = function(items)
9
9
  end
10
10
  return self
11
11
  end
12
+ local Stack
12
13
  do
13
14
  local _parent_0 = nil
14
15
  local _base_0 = {
@@ -16,10 +17,10 @@ do
16
17
  return "<Stack {" .. concat(self, ", ") .. "}>"
17
18
  end,
18
19
  pop = function(self)
19
- return table.remove(self)
20
+ return remove(self)
20
21
  end,
21
22
  push = function(self, value)
22
- table.insert(self, value)
23
+ insert(self, value)
23
24
  return value
24
25
  end,
25
26
  top = function(self)
@@ -65,7 +66,7 @@ do
65
66
  end
66
67
  Stack = _class_0
67
68
  end
68
- lua_keywords = Set({
69
+ local lua_keywords = Set({
69
70
  'and',
70
71
  'break',
71
72
  'do',
@@ -88,4 +89,8 @@ lua_keywords = Set({
88
89
  'until',
89
90
  'while'
90
91
  })
91
- return nil
92
+ return {
93
+ Set = Set,
94
+ Stack = Stack,
95
+ lua_keywords = lua_keywords
96
+ }
@@ -1,4 +1,3 @@
1
- module("moonscript.dump", package.seeall)
2
1
  local flat_value
3
2
  flat_value = function(op, depth)
4
3
  if depth == nil then
@@ -12,21 +11,23 @@ flat_value = function(op, depth)
12
11
  end
13
12
  local items = (function()
14
13
  local _accum_0 = { }
15
- local _len_0 = 0
14
+ local _len_0 = 1
16
15
  local _list_0 = op
17
16
  for _index_0 = 1, #_list_0 do
18
17
  local item = _list_0[_index_0]
19
- _len_0 = _len_0 + 1
20
18
  _accum_0[_len_0] = flat_value(item, depth + 1)
19
+ _len_0 = _len_0 + 1
21
20
  end
22
21
  return _accum_0
23
22
  end)()
24
23
  local pos = op[-1]
25
24
  return "{" .. (pos and "[" .. pos .. "] " or "") .. table.concat(items, ", ") .. "}"
26
25
  end
26
+ local value
27
27
  value = function(op)
28
28
  return flat_value(op)
29
29
  end
30
+ local tree
30
31
  tree = function(block)
31
32
  local _list_0 = block
32
33
  for _index_0 = 1, #_list_0 do
@@ -34,3 +35,7 @@ tree = function(block)
34
35
  print(flat_value(value))
35
36
  end
36
37
  end
38
+ return {
39
+ value = value,
40
+ tree = tree
41
+ }
@@ -1,9 +1,14 @@
1
- module("moonscript.errors", package.seeall)
2
- local moon = require("moonscript")
3
1
  local util = require("moonscript.util")
4
- require("lpeg")
2
+ local lpeg = require("lpeg")
5
3
  local concat, insert = table.concat, table.insert
6
4
  local split, pos_to_line = util.split, util.pos_to_line
5
+ local user_error
6
+ user_error = function(...)
7
+ return error({
8
+ "user-error",
9
+ ...
10
+ })
11
+ end
7
12
  local lookup_line
8
13
  lookup_line = function(fname, pos, cache)
9
14
  if not cache[fname] then
@@ -24,6 +29,7 @@ reverse_line_number = function(fname, line_table, line_num, cache)
24
29
  end
25
30
  return "unknown"
26
31
  end
32
+ local truncate_traceback
27
33
  truncate_traceback = function(traceback, chunk_func)
28
34
  if chunk_func == nil then
29
35
  chunk_func = "moonscript_chunk"
@@ -38,13 +44,13 @@ truncate_traceback = function(traceback, chunk_func)
38
44
  end
39
45
  traceback = (function()
40
46
  local _accum_0 = { }
41
- local _len_0 = 0
47
+ local _len_0 = 1
42
48
  local _list_0 = traceback
43
49
  local _max_0 = stop
44
50
  for _index_0 = 1, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do
45
51
  local t = _list_0[_index_0]
46
- _len_0 = _len_0 + 1
47
52
  _accum_0[_len_0] = t
53
+ _len_0 = _len_0 + 1
48
54
  end
49
55
  return _accum_0
50
56
  end)()
@@ -52,8 +58,9 @@ truncate_traceback = function(traceback, chunk_func)
52
58
  traceback[#traceback] = traceback[#traceback]:gsub(rep, "main chunk")
53
59
  return concat(traceback, "\n")
54
60
  end
61
+ local rewrite_traceback
55
62
  rewrite_traceback = function(text, err)
56
- local line_tables = moon.line_tables
63
+ local line_tables = require("moonscript.line_tables")
57
64
  local V, S, Ct, C = lpeg.V, lpeg.S, lpeg.Ct, lpeg.C
58
65
  local header_text = "stack traceback:"
59
66
  local Header, Line = V("Header"), V("Line")
@@ -97,3 +104,8 @@ rewrite_traceback = function(text, err)
97
104
  "\t" .. concat(match, "\n\t")
98
105
  }, "\n")
99
106
  end
107
+ return {
108
+ rewrite_traceback = rewrite_traceback,
109
+ truncate_traceback = truncate_traceback,
110
+ user_error = user_error
111
+ }
@@ -1,14 +1,16 @@
1
- module("moonscript", package.seeall)
2
- require("moonscript.compile")
3
- require("moonscript.parse")
4
- require("moonscript.util")
1
+ local compile = require("moonscript.compile")
2
+ local parse = require("moonscript.parse")
5
3
  local concat, insert = table.concat, table.insert
6
- local split, dump = util.split, util.dump
4
+ local split, dump, get_options, unpack
5
+ do
6
+ local _table_0 = require("moonscript.util")
7
+ split, dump, get_options, unpack = _table_0.split, _table_0.dump, _table_0.get_options, _table_0.unpack
8
+ end
7
9
  local lua = {
8
10
  loadstring = loadstring
9
11
  }
10
- dirsep = "/"
11
- line_tables = { }
12
+ local dirsep = "/"
13
+ local line_tables = require("moonscript.line_tables")
12
14
  local create_moonpath
13
15
  create_moonpath = function(package_path)
14
16
  local paths = split(package_path, ";")
@@ -20,6 +22,7 @@ create_moonpath = function(package_path)
20
22
  end
21
23
  return concat(paths, ";")
22
24
  end
25
+ local to_lua
23
26
  to_lua = function(text, options)
24
27
  if options == nil then
25
28
  options = { }
@@ -38,6 +41,7 @@ to_lua = function(text, options)
38
41
  end
39
42
  return code, ltable
40
43
  end
44
+ local moon_loader
41
45
  moon_loader = function(name)
42
46
  local name_path = name:gsub("%.", dirsep)
43
47
  local file, file_path = nil, nil
@@ -63,15 +67,15 @@ if not package.moonpath then
63
67
  end
64
68
  local init_loader
65
69
  init_loader = function()
66
- return insert(package.loaders, 2, moon_loader)
70
+ return insert(package.loaders or package.searchers, 2, moon_loader)
67
71
  end
68
- if not _G.moon_no_loader then
72
+ if not (_G.moon_no_loader) then
69
73
  init_loader()
70
74
  end
71
- loadstring = function(str, chunk_name, options)
72
- if options == nil then
73
- options = nil
74
- end
75
+ local loadstring
76
+ loadstring = function(...)
77
+ local options, str, chunk_name, mode, env = get_options(...)
78
+ chunk_name = chunk_name or "=(moonscript.loadstring)"
75
79
  local passed, code, ltable = pcall(function()
76
80
  return to_lua(str, options)
77
81
  end)
@@ -81,21 +85,33 @@ loadstring = function(str, chunk_name, options)
81
85
  if chunk_name then
82
86
  line_tables[chunk_name] = ltable
83
87
  end
84
- return lua.loadstring(code, chunk_name or "=(moonscript.loadstring)")
88
+ return (lua.loadstring or lua.load)(code, chunk_name, unpack({
89
+ mode,
90
+ env
91
+ }))
85
92
  end
86
- loadfile = function(fname, options)
87
- if options == nil then
88
- options = nil
89
- end
93
+ local loadfile
94
+ loadfile = function(fname, ...)
90
95
  local file, err = io.open(fname)
91
96
  if not file then
92
97
  return nil, err
93
98
  end
94
99
  local text = assert(file:read("*a"))
95
100
  file:close()
96
- return loadstring(text, fname, options)
101
+ return loadstring(text, fname, ...)
97
102
  end
98
- dofile = function(fname, options)
99
- local f = assert(loadfile(fname))
103
+ local dofile
104
+ dofile = function(...)
105
+ local f = assert(loadfile(...))
100
106
  return f()
101
107
  end
108
+ return {
109
+ _NAME = "moonscript",
110
+ to_lua = to_lua,
111
+ moon_chunk = moon_chunk,
112
+ moon_loader = moon_loader,
113
+ dirsep = dirsep,
114
+ dofile = dofile,
115
+ loadfile = loadfile,
116
+ loadstring = loadstring
117
+ }
@@ -0,0 +1 @@
1
+ return { }
@@ -1,8 +1,7 @@
1
- module("moonscript.parse", package.seeall)
2
1
 
3
2
  local util = require"moonscript.util"
4
3
 
5
- require"lpeg"
4
+ local lpeg = require"lpeg"
6
5
 
7
6
  local debug_grammar = false
8
7
 
@@ -14,6 +13,10 @@ local ntype = types.ntype
14
13
  local dump = util.dump
15
14
  local trim = util.trim
16
15
 
16
+ local getfenv = util.getfenv
17
+ local setfenv = util.setfenv
18
+ local unpack = util.unpack
19
+
17
20
  local Stack = data.Stack
18
21
 
19
22
  local function count_indent(str)
@@ -49,7 +52,10 @@ local _Name = C(R("az", "AZ", "__") * AlphaNum^0)
49
52
  local Name = Space * _Name
50
53
 
51
54
  local Num = P"0x" * R("09", "af", "AF")^1 +
52
- R"09"^1 * (P"." * R"09"^1)^-1 * (S"eE" * P"-"^-1 * R"09"^1)^-1
55
+ (
56
+ R"09"^1 * (P"." * R"09"^1)^-1 +
57
+ P"." * R"09"^1
58
+ ) * (S"eE" * P"-"^-1 * R"09"^1)^-1
53
59
 
54
60
  Num = Space * (Num / function(value) return {"number", value} end)
55
61
 
@@ -117,7 +123,7 @@ local function wrap_env(fn)
117
123
  }))
118
124
  end
119
125
 
120
- function extract_line(str, start_pos)
126
+ local function extract_line(str, start_pos)
121
127
  str = str:sub(start_pos)
122
128
  m = str:match"^(.-)\n"
123
129
  if m then return m end
@@ -170,7 +176,8 @@ local _chain_assignable = { index = true, dot = true, slice = true }
170
176
  local function is_assignable(node)
171
177
  local t = ntype(node)
172
178
  return t == "self" or t == "value" or t == "self_class" or
173
- t == "chain" and _chain_assignable[ntype(node[#node])]
179
+ t == "chain" and _chain_assignable[ntype(node[#node])] or
180
+ t == "table"
174
181
  end
175
182
 
176
183
  local function check_assignable(str, pos, value)
@@ -400,7 +407,7 @@ local build_grammar = wrap_env(function()
400
407
  PopIndent = Cmt("", pop_indent),
401
408
  InBlock = Advance * Block * PopIndent,
402
409
 
403
- Local = key"local" * Ct(NameList) / mark"declare_with_shadows",
410
+ Local = key"local" * ((op"*" + op"^") / mark"declare_glob" + Ct(NameList) / mark"declare_with_shadows"),
404
411
 
405
412
  Import = key"import" * Ct(ImportNameList) * key"from" * Exp / mark"import",
406
413
  ImportName = (sym"\\" * Ct(Cc"colon_stub" * Name) + Name),
@@ -418,7 +425,7 @@ local build_grammar = wrap_env(function()
418
425
  Switch = key"switch" * DisableDo * ensure(Exp, PopDo) * key"do"^-1 * Space^-1 * Break * SwitchBlock / mark"switch",
419
426
 
420
427
  SwitchBlock = EmptyLine^0 * Advance * Ct(SwitchCase * (Break^1 * SwitchCase)^0 * (Break^1 * SwitchElse)^-1) * PopIndent,
421
- SwitchCase = key"when" * Exp * key"then"^-1 * Body / mark"case",
428
+ SwitchCase = key"when" * Ct(ExpList) * key"then"^-1 * Body / mark"case",
422
429
  SwitchElse = key"else" * Body / mark"else",
423
430
 
424
431
  IfCond = Exp * Assign^-1 / format_single_assign,
@@ -435,7 +442,7 @@ local build_grammar = wrap_env(function()
435
442
  For = key"for" * DisableDo * ensure(Name * sym"=" * Ct(Exp * sym"," * Exp * (sym"," * Exp)^-1), PopDo) *
436
443
  key"do"^-1 * Body / mark"for",
437
444
 
438
- ForEach = key"for" * Ct(NameList) * key"in" * DisableDo * ensure(Ct(sym"*" * Exp / mark"unpack" + ExpList), PopDo) * key"do"^-1 * Body / mark"foreach",
445
+ ForEach = key"for" * Ct(AssignableNameList) * key"in" * DisableDo * ensure(Ct(sym"*" * Exp / mark"unpack" + ExpList), PopDo) * key"do"^-1 * Body / mark"foreach",
439
446
 
440
447
  Do = key"do" * Body / mark"do",
441
448
 
@@ -443,9 +450,10 @@ local build_grammar = wrap_env(function()
443
450
 
444
451
  TblComprehension = sym"{" * Ct(Exp * (sym"," * Exp)^-1) * CompInner * sym"}" / mark"tblcomprehension",
445
452
 
446
- CompInner = Ct(CompFor * CompClause^0),
447
- CompFor = key"for" * Ct(NameList) * key"in" * (sym"*" * Exp / mark"unpack" + Exp) / mark"for",
448
- CompClause = CompFor + key"when" * Exp / mark"when",
453
+ CompInner = Ct((CompForEach + CompFor) * CompClause^0),
454
+ CompForEach = key"for" * Ct(NameList) * key"in" * (sym"*" * Exp / mark"unpack" + Exp) / mark"foreach",
455
+ CompFor = key "for" * Name * sym"=" * Ct(Exp * sym"," * Exp * (sym"," * Exp)^-1) / mark"for",
456
+ CompClause = CompFor + CompForEach + key"when" * Exp / mark"when",
449
457
 
450
458
  Assign = sym"=" * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark"assign",
451
459
  Update = ((sym"..=" + sym"+=" + sym"-=" + sym"*=" + sym"/=" + sym"%=" + sym"or=" + sym"and=") / trim) * Exp / mark"update",
@@ -552,7 +560,7 @@ local build_grammar = wrap_env(function()
552
560
  TableBlockInner = Ct(KeyValueLine * (SpaceBreak^1 * KeyValueLine)^0),
553
561
  TableBlock = SpaceBreak^1 * Advance * ensure(TableBlockInner, PopIndent) / mark"table",
554
562
 
555
- ClassDecl = key"class" * (Assignable + Cc(nil)) * (key"extends" * PreventIndent * ensure(Exp, PopIndent) + C"")^-1 * (ClassBlock + Ct("")) / mark"class",
563
+ ClassDecl = key"class" * -P":" * (Assignable + Cc(nil)) * (key"extends" * PreventIndent * ensure(Exp, PopIndent) + C"")^-1 * (ClassBlock + Ct("")) / mark"class",
556
564
 
557
565
  ClassBlock = SpaceBreak^1 * Advance *
558
566
  Ct(ClassLine * (SpaceBreak^1 * ClassLine)^0) * PopIndent,
@@ -575,7 +583,7 @@ local build_grammar = wrap_env(function()
575
583
  (key"using" * Ct(NameList + Space * "nil") + Ct"") *
576
584
  sym")" + Ct"" * Ct"",
577
585
 
578
- FnArgDefList = FnArgDef * (sym"," * FnArgDef)^0,
586
+ FnArgDefList = FnArgDef * (sym"," * FnArgDef)^0,
579
587
  FnArgDef = Ct(Name * (sym"=" * Exp)^-1),
580
588
 
581
589
  FunLit = FnArgsDef *
@@ -583,6 +591,9 @@ local build_grammar = wrap_env(function()
583
591
  (Body + Ct"") / mark"fndef",
584
592
 
585
593
  NameList = Name * (sym"," * Name)^0,
594
+ NameOrDestructure = Name + TableLit,
595
+ AssignableNameList = NameOrDestructure * (sym"," * NameOrDestructure)^0,
596
+
586
597
  ExpList = Exp * (sym"," * Exp)^0,
587
598
  ExpListLow = Exp * ((sym"," + sym";") * Exp)^0,
588
599
 
@@ -632,13 +643,16 @@ local build_grammar = wrap_env(function()
632
643
  return tree
633
644
  end
634
645
  }
635
-
636
646
  end)
637
647
 
638
- -- parse a string
639
- -- returns tree, or nil and error message
640
- function string(str)
641
- local g = build_grammar()
642
- return g:match(str)
643
- end
648
+ return {
649
+ extract_line = extract_line,
650
+
651
+ -- parse a string
652
+ -- returns tree, or nil and error message
653
+ string = function (str)
654
+ local g = build_grammar()
655
+ return g:match(str)
656
+ end
657
+ }
644
658
 
@@ -0,0 +1,234 @@
1
+ local ntype, mtype, build
2
+ do
3
+ local _table_0 = require("moonscript.types")
4
+ ntype, mtype, build = _table_0.ntype, _table_0.mtype, _table_0.build
5
+ end
6
+ local NameProxy
7
+ do
8
+ local _table_0 = require("moonscript.transform.names")
9
+ NameProxy = _table_0.NameProxy
10
+ end
11
+ local insert = table.insert
12
+ local unpack
13
+ do
14
+ local _table_0 = require("moonscript.util")
15
+ unpack = _table_0.unpack
16
+ end
17
+ local user_error
18
+ do
19
+ local _table_0 = require("moonscript.errors")
20
+ user_error = _table_0.user_error
21
+ end
22
+ local join
23
+ join = function(...)
24
+ do
25
+ local _with_0 = { }
26
+ local out = _with_0
27
+ local i = 1
28
+ local _list_0 = {
29
+ ...
30
+ }
31
+ for _index_0 = 1, #_list_0 do
32
+ local tbl = _list_0[_index_0]
33
+ local _list_1 = tbl
34
+ for _index_1 = 1, #_list_1 do
35
+ local v = _list_1[_index_1]
36
+ out[i] = v
37
+ i = i + 1
38
+ end
39
+ end
40
+ return _with_0
41
+ end
42
+ end
43
+ local has_destructure
44
+ has_destructure = function(names)
45
+ local _list_0 = names
46
+ for _index_0 = 1, #_list_0 do
47
+ local n = _list_0[_index_0]
48
+ if ntype(n) == "table" then
49
+ return true
50
+ end
51
+ end
52
+ return false
53
+ end
54
+ local extract_assign_names
55
+ extract_assign_names = function(name, accum, prefix)
56
+ if accum == nil then
57
+ accum = { }
58
+ end
59
+ if prefix == nil then
60
+ prefix = { }
61
+ end
62
+ local i = 1
63
+ local _list_0 = name[2]
64
+ for _index_0 = 1, #_list_0 do
65
+ local tuple = _list_0[_index_0]
66
+ local value, suffix
67
+ if #tuple == 1 then
68
+ local s = {
69
+ "index",
70
+ {
71
+ "number",
72
+ i
73
+ }
74
+ }
75
+ i = i + 1
76
+ value, suffix = tuple[1], s
77
+ else
78
+ local key = tuple[1]
79
+ local s
80
+ if ntype(key) == "key_literal" then
81
+ s = {
82
+ "dot",
83
+ key[2]
84
+ }
85
+ else
86
+ s = {
87
+ "index",
88
+ key
89
+ }
90
+ end
91
+ value, suffix = tuple[2], s
92
+ end
93
+ suffix = join(prefix, {
94
+ suffix
95
+ })
96
+ local t = ntype(value)
97
+ if t == "value" or t == "chain" or t == "self" then
98
+ insert(accum, {
99
+ value,
100
+ suffix
101
+ })
102
+ elseif t == "table" then
103
+ extract_assign_names(value, accum, suffix)
104
+ else
105
+ user_error("Can't destructure value of type: " .. tostring(ntype(value)))
106
+ end
107
+ end
108
+ return accum
109
+ end
110
+ local build_assign
111
+ build_assign = function(destruct_literal, receiver)
112
+ local extracted_names = extract_assign_names(destruct_literal)
113
+ local names = { }
114
+ local values = { }
115
+ local inner = {
116
+ "assign",
117
+ names,
118
+ values
119
+ }
120
+ local obj
121
+ if mtype(receiver) == NameProxy then
122
+ obj = receiver
123
+ else
124
+ do
125
+ local _with_0 = NameProxy("obj")
126
+ obj = _with_0
127
+ inner = build["do"]({
128
+ build.assign_one(obj, receiver),
129
+ {
130
+ "assign",
131
+ names,
132
+ values
133
+ }
134
+ })
135
+ obj = _with_0
136
+ end
137
+ end
138
+ local _list_0 = extracted_names
139
+ for _index_0 = 1, #_list_0 do
140
+ local tuple = _list_0[_index_0]
141
+ insert(names, tuple[1])
142
+ insert(values, obj:chain(unpack(tuple[2])))
143
+ end
144
+ return build.group({
145
+ {
146
+ "declare",
147
+ names
148
+ },
149
+ inner
150
+ })
151
+ end
152
+ local split_assign
153
+ split_assign = function(assign)
154
+ local names, values = unpack(assign, 2)
155
+ local g = { }
156
+ local total_names = #names
157
+ local total_values = #values
158
+ local start = 1
159
+ for i, n in ipairs(names) do
160
+ if ntype(n) == "table" then
161
+ if i > start then
162
+ local stop = i - 1
163
+ insert(g, {
164
+ "assign",
165
+ (function()
166
+ local _accum_0 = { }
167
+ local _len_0 = 1
168
+ for i = start, stop do
169
+ _accum_0[_len_0] = names[i]
170
+ _len_0 = _len_0 + 1
171
+ end
172
+ return _accum_0
173
+ end)(),
174
+ (function()
175
+ local _accum_0 = { }
176
+ local _len_0 = 1
177
+ for i = start, stop do
178
+ _accum_0[_len_0] = values[i]
179
+ _len_0 = _len_0 + 1
180
+ end
181
+ return _accum_0
182
+ end)()
183
+ })
184
+ end
185
+ insert(g, build_assign(n, values[i]))
186
+ start = i + 1
187
+ end
188
+ end
189
+ if total_names >= start or total_values >= start then
190
+ local name_slice
191
+ if total_names < start then
192
+ name_slice = {
193
+ "_"
194
+ }
195
+ else
196
+ name_slice = (function()
197
+ local _accum_0 = { }
198
+ local _len_0 = 1
199
+ for i = start, total_names do
200
+ _accum_0[_len_0] = names[i]
201
+ _len_0 = _len_0 + 1
202
+ end
203
+ return _accum_0
204
+ end)()
205
+ end
206
+ local value_slice
207
+ if total_values < start then
208
+ value_slice = {
209
+ "nil"
210
+ }
211
+ else
212
+ value_slice = (function()
213
+ local _accum_0 = { }
214
+ local _len_0 = 1
215
+ for i = start, total_values do
216
+ _accum_0[_len_0] = values[i]
217
+ _len_0 = _len_0 + 1
218
+ end
219
+ return _accum_0
220
+ end)()
221
+ end
222
+ insert(g, {
223
+ "assign",
224
+ name_slice,
225
+ value_slice
226
+ })
227
+ end
228
+ return build.group(g)
229
+ end
230
+ return {
231
+ has_destructure = has_destructure,
232
+ split_assign = split_assign,
233
+ build_assign = build_assign
234
+ }