aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs
diff options
context:
space:
mode:
Diffstat (limited to 'spec/inputs')
-rw-r--r--spec/inputs/compile_doc.yue2
-rw-r--r--spec/inputs/luarocks_upload.yue64
-rw-r--r--spec/inputs/test/reserve_comments_spec.yue413
-rw-r--r--spec/inputs/whitespace.yue39
4 files changed, 513 insertions, 5 deletions
diff --git a/spec/inputs/compile_doc.yue b/spec/inputs/compile_doc.yue
index f6a8d49..34f810c 100644
--- a/spec/inputs/compile_doc.yue
+++ b/spec/inputs/compile_doc.yue
@@ -35,7 +35,7 @@ getFiles = (locale) ->
35 "doc/docs/#{locale}doc/language-basics/literals.md" 35 "doc/docs/#{locale}doc/language-basics/literals.md"
36 "doc/docs/#{locale}doc/language-basics/module.md" 36 "doc/docs/#{locale}doc/language-basics/module.md"
37 "doc/docs/#{locale}doc/reference/license-mit.md" 37 "doc/docs/#{locale}doc/reference/license-mit.md"
38 "doc/docs/#{locale}doc/reference/the-yuescript-library.md" 38 "doc/docs/#{locale}doc/advanced/the-yuescript-library.md"
39 ] 39 ]
40docs = [ ["codes_from_doc_#{locale}.lua", "yue-#{locale}.md", getFiles locale] for locale in *["en", "zh", "pt-br", "de", "id-id"]] 40docs = [ ["codes_from_doc_#{locale}.lua", "yue-#{locale}.md", getFiles locale] for locale in *["en", "zh", "pt-br", "de", "id-id"]]
41for [compiledFile, docFile, docFiles] in *docs 41for [compiledFile, docFile, docFiles] in *docs
diff --git a/spec/inputs/luarocks_upload.yue b/spec/inputs/luarocks_upload.yue
index 30e8434..8409484 100644
--- a/spec/inputs/luarocks_upload.yue
+++ b/spec/inputs/luarocks_upload.yue
@@ -1,4 +1,5 @@
1luarocksKey = ... 1luarocksKey = ...
2
2local version 3local version
3with? io.open "src/yuescript/yue_compiler.cpp" 4with? io.open "src/yuescript/yue_compiler.cpp"
4 codes = \read "*a" 5 codes = \read "*a"
@@ -8,17 +9,52 @@ unless version?
8 print "failed to get version!" 9 print "failed to get version!"
9 os.exit 1 10 os.exit 1
10 11
12-- ==============================
13-- 1. Prepare minimal source tree
14-- ==============================
15
16local tmpBase = os.getenv("RUNNER_TEMP") or os.getenv("TMPDIR") or "/tmp"
17local pkgRoot = "#{tmpBase}/yuescript_pack_#{version}"
18local srcDir = "#{pkgRoot}/yuescript-#{version}"
19local tarFile = "#{pkgRoot}/yuescript-#{version}.tar.gz"
20
21run = (cmd) ->
22 print cmd
23 ok = os.execute cmd
24 unless ok == true or ok == 0
25 print "Command failed!"
26 os.exit 1
27
28-- clean & create
29run "rm -rf '#{pkgRoot}'"
30run "mkdir -p '#{srcDir}'"
31
32-- copy whitelist files
33run "cp CMakeLists.txt '#{srcDir}/'"
34run "cp README.md '#{srcDir}/'"
35run "cp LICENSE '#{srcDir}/'"
36run "cp -R src '#{srcDir}/'"
37
38-- create tar.gz
39run "tar -C '#{pkgRoot}' -czf '#{tarFile}' 'yuescript-#{version}'"
40
41local sourceUrl = "file://#{tarFile}"
42
43-- =========================
44-- 2. Generate rockspec
45-- =========================
46
11rockspec = "rockspec_format = '3.0' 47rockspec = "rockspec_format = '3.0'
12package = 'Yuescript' 48package = 'Yuescript'
13version = '#{version}-1' 49version = '#{version}-1'
14source = { 50source = {
15 url = 'git+https://github.com/pigpigyyy/yuescript' 51 url = '#{sourceUrl}'
16} 52}
17description = { 53description = {
18 summary = 'Yuescript is a Moonscript dialect.', 54 summary = 'Yuescript is a Moonscript dialect.',
19 detailed = [[ 55 detailed = [[
20 Yuescript is a Moonscript dialect. It is derived from Moonscript language 0.5.0 and continuously adopting new features to be more up to date. ]], 56 Yuescript is a Moonscript dialect. It is derived from Moonscript language 0.5.0 and continuously adopting new features to be more up to date. ]],
21 homepage = 'https://github.com/pigpigyyy/yuescript', 57 homepage = 'https://github.com/IppClub/YueScript',
22 maintainer = 'Li Jin <dragon-fly@qq.com>', 58 maintainer = 'Li Jin <dragon-fly@qq.com>',
23 labels = {'yuescript','cpp','transpiler','moonscript'}, 59 labels = {'yuescript','cpp','transpiler','moonscript'},
24 license = 'MIT' 60 license = 'MIT'
@@ -29,8 +65,8 @@ dependencies = {
29build = { 65build = {
30 type = 'cmake', 66 type = 'cmake',
31 variables = { 67 variables = {
32 LUA='$(LUA)', 68 LUA = '$(LUA)',
33 LUA_INCDIR='$(LUA_INCDIR)', 69 LUA_INCDIR = '$(LUA_INCDIR)',
34 CMAKE_BUILD_TYPE='Release' 70 CMAKE_BUILD_TYPE='Release'
35 }, 71 },
36 install = { 72 install = {
@@ -40,14 +76,34 @@ build = {
40 bin = { 76 bin = {
41 'build.luarocks/yue' 77 'build.luarocks/yue'
42 } 78 }
79 },
80 platforms = {
81 windows = {
82 install = {
83 lib = {
84 'build.luarocks/Release/yue.dll'
85 },
86 bin = {
87 'build.luarocks/Release/yue.exe'
88 }
89 }
90 }
43 } 91 }
44}" 92}"
45 93
46specFile = "yuescript-#{version}-1.rockspec" 94specFile = "yuescript-#{version}-1.rockspec"
95
47with? io.open specFile, "w+" 96with? io.open specFile, "w+"
48 \write rockspec 97 \write rockspec
49 \close! 98 \close!
50 99
100print "Using source: #{sourceUrl}"
101print "Uploading rockspec: #{specFile}"
102
103-- =========================
104-- 3. Upload
105-- =========================
106
51result = io.popen("luarocks upload --api-key #{luarocksKey} #{specFile}")\read '*a' 107result = io.popen("luarocks upload --api-key #{luarocksKey} #{specFile}")\read '*a'
52unless result\match "Done:" 108unless result\match "Done:"
53 print result 109 print result
diff --git a/spec/inputs/test/reserve_comments_spec.yue b/spec/inputs/test/reserve_comments_spec.yue
new file mode 100644
index 0000000..3c0b824
--- /dev/null
+++ b/spec/inputs/test/reserve_comments_spec.yue
@@ -0,0 +1,413 @@
1describe "reserve_comments option", ->
2 import to_lua from require("yue")
3
4 it "should preserve top-level comments with reserve_comment option", ->
5 code = [[
6-- Top level comment
7x = 1
8-- Another comment
9y = 2
10]]
11 result = to_lua code, {reserve_comment: true}
12 assert.is_true result\match("Top level comment") ~= nil
13 assert.is_true result\match("Another comment") ~= nil
14
15 it "should NOT preserve comments without reserve_comment option", ->
16 code = [[
17-- Top level comment
18x = 1
19-- Another comment
20y = 2
21]]
22 result = to_lua code, {}
23 assert.is_true result\match("Top level comment") == nil
24 assert.is_true result\match("Another comment") == nil
25
26 it "should preserve comments in table literals", ->
27 code = [[
28t = {
29 -- First value comment
30 1,
31 -- Second value comment
32 2
33}
34]]
35 result = to_lua code, {reserve_comment: true}
36 assert.is_true result\match("First value comment") ~= nil
37 assert.is_true result\match("Second value comment") ~= nil
38
39 it "should preserve comments in if statement", ->
40 code = [[
41if true
42 -- Inside if block
43 print "test"
44]]
45 result = to_lua code, {reserve_comment: true}
46 assert.is_true result\match("Inside if block") ~= nil
47
48 it "should preserve comments in function body", ->
49 code = [[
50func = =>
51 -- Inside function
52 print "hello"
53]]
54 result = to_lua code, {reserve_comment: true}
55 assert.is_true result\match("Inside function") ~= nil
56
57 it "should preserve comments in while loop", ->
58 code = [[
59while true
60 -- Loop body comment
61 print "looping"
62 break
63]]
64 result = to_lua code, {reserve_comment: true}
65 assert.is_true result\match("Loop body comment") ~= nil
66
67 it "should preserve comments in for loop", ->
68 code = [[
69for i = 1, 3
70 -- For loop comment
71 print i
72]]
73 result = to_lua code, {reserve_comment: true}
74 assert.is_true result\match("For loop comment") ~= nil
75
76 it "should preserve comments in TableBlock syntax", ->
77 code = [[
78tbl = {
79 -- Key comment
80 key: "value"
81 -- Another key
82 another: 123
83}
84]]
85 result = to_lua code, {reserve_comment: true}
86 assert.is_true result\match("Key comment") ~= nil
87 assert.is_true result\match("Another key") ~= nil
88
89 it "should preserve multiple comments across statements", ->
90 code = [[
91-- Assign x
92x = 1
93-- Assign y
94y = 2
95-- Assign z
96z = 3
97]]
98 result = to_lua code, {reserve_comment: true}
99 assert.is_true result\match("Assign x") ~= nil
100 assert.is_true result\match("Assign y") ~= nil
101 assert.is_true result\match("Assign z") ~= nil
102
103 it "should handle table with mixed values and comments", ->
104 code = [[
105t = {
106 -- First item
107 1,
108 -- Second item
109 2,
110 -- Third item
111 3
112}
113]]
114 result = to_lua code, {reserve_comment: true}
115 assert.is_true result\match("First item") ~= nil
116 assert.is_true result\match("Second item") ~= nil
117 assert.is_true result\match("Third item") ~= nil
118
119 it "should preserve comments in nested structures", ->
120 code = [[
121outer = {
122 -- outer comment
123 inner: {
124 -- inner comment
125 value: 42
126 }
127}
128]]
129 result = to_lua code, {reserve_comment: true}
130 assert.is_true result\match("outer comment") ~= nil
131 assert.is_true result\match("inner comment") ~= nil
132
133 it "should preserve comments in else block", ->
134 code = [[
135if false
136 print "if"
137else
138 -- else comment
139 print "else"
140]]
141 result = to_lua code, {reserve_comment: true}
142 assert.is_true result\match("else comment") ~= nil
143
144 it "should preserve comments in elseif block", ->
145 code = [[
146if false
147 print "if"
148elseif true
149 -- elseif comment
150 print "elseif"
151]]
152 result = to_lua code, {reserve_comment: true}
153 assert.is_true result\match("elseif comment") ~= nil
154
155 it "should preserve comments before return statement", ->
156 code = [[
157func = =>
158 -- before return
159 return 42
160]]
161 result = to_lua code, {reserve_comment: true}
162 assert.is_true result\match("before return") ~= nil
163
164 it "should preserve comments in switch statement", ->
165 code = [[
166switch 2
167 when 1
168 -- case 1 comment
169 print "one"
170 when 2
171 -- case 2 comment
172 print "two"
173]]
174 result = to_lua code, {reserve_comment: true}
175 assert.is_true result\match("case 1 comment") ~= nil
176 assert.is_true result\match("case 2 comment") ~= nil
177
178 it "should preserve comments in with statement", ->
179 code = [[
180with t
181 -- with body comment
182 .value = 10
183]]
184 result = to_lua code, {reserve_comment: true}
185 assert.is_true result\match("with body comment") ~= nil
186
187 it "should handle empty lines with reserve_comment", ->
188 code = [[
189-- First comment
190x = 1
191-- Second comment
192]]
193 -- Just verify it compiles without error
194 result = to_lua code, {reserve_comment: true}
195 assert.is_true result ~= nil
196 assert.is_true type(result) == "string"
197
198 it "should preserve comments in class body", ->
199 code = [[
200class MyClass
201 -- property comment
202 value: 10
203 -- method comment
204 method: => print "hello"
205]]
206 result = to_lua code, {reserve_comment: true}
207 assert.is_true result\match("property comment") ~= nil
208 assert.is_true result\match("method comment") ~= nil
209
210 it "should preserve comments in class with inheritance", ->
211 code = [[
212class Child extends Parent
213 -- child property
214 value: 100
215]]
216 result = to_lua code, {reserve_comment: true}
217 assert.is_true result\match("child property") ~= nil
218
219 it "should preserve comments in export statement", ->
220 code = [[
221-- export value comment
222export x = 42
223]]
224 result = to_lua code, {reserve_comment: true}
225 assert.is_true result\match("export value comment") ~= nil
226
227 it "should preserve comments in import statement", ->
228 code = [[
229-- import comment
230import format from "string"
231]]
232 result = to_lua code, {reserve_comment: true}
233 assert.is_true result\match("import comment") ~= nil
234
235 -- Additional tests for TableBlock syntax with multiple empty lines
236 it "should preserve empty lines between comments in TableBlock", ->
237 code = "tb =\n\t-- line\n\n\n\n\t--[[ajdjd]]\n\ta: ->\n\n\t-- line 2\n\tb: 123\n"
238 result = to_lua code, {reserve_comment: true}
239 assert.is_true result\match("line") ~= nil
240 assert.is_true result\match("ajdjd") ~= nil
241 assert.is_true result\match("line 2") ~= nil
242
243 it "should preserve block comments in TableBlock", ->
244 code = "tb =\n\t--[[block comment]]\n\ta: 1\n\n\t--[[another block]]\n\tb: 2\n"
245 result = to_lua code, {reserve_comment: true}
246 assert.is_true result\match("block comment") ~= nil
247 assert.is_true result\match("another block") ~= nil
248
249 it "should preserve multiple empty lines in table literal", ->
250 code = "tb = {\n\t-- line\n\n\n\n\t--[[ajdjd]]\n\ta: ->\n\n\t-- line 2\n\tb: 123\n}\n"
251 result = to_lua code, {reserve_comment: true}
252 assert.is_true result\match("line") ~= nil
253 assert.is_true result\match("ajdjd") ~= nil
254 assert.is_true result\match("line 2") ~= nil
255
256 it "should preserve mixed single and block comments in TableBlock", ->
257 code = "tb =\n\t-- single line comment\n\ta: 1\n\n\t--[[multi\n\tline\n\tblock\n\tcomment]]\n\tb: 2\n\n\t-- another single\n\tc: 3\n"
258 result = to_lua code, {reserve_comment: true}
259 assert.is_true result\match("single line comment") ~= nil
260 assert.is_true result\match("multi") ~= nil
261 assert.is_true result\match("another single") ~= nil
262
263 it "should preserve comments and empty lines in table with colon syntax", ->
264 code = "tbl = {\n\t-- first key\n\tkey1: \"value1\"\n\n\n\t-- second key\n\tkey2: \"value2\"\n\n\t-- third key\n\tkey3: \"value3\"\n}\n"
265 result = to_lua code, {reserve_comment: true}
266 assert.is_true result\match("first key") ~= nil
267 assert.is_true result\match("second key") ~= nil
268 assert.is_true result\match("third key") ~= nil
269
270 it "should preserve comments in nested TableBlock structures", ->
271 code = "outer =\n\t-- outer item\n\ta: 1\n\n\t-- inner tableblock\n\tinner:\n\t\t-- inner item 1\n\t\tx: 10\n\t\t-- inner item 2\n\t\ty: 20\n"
272 result = to_lua code, {reserve_comment: true}
273 assert.is_true result\match("outer item") ~= nil
274 assert.is_true result\match("inner tableblock") ~= nil
275 assert.is_true result\match("inner item 1") ~= nil
276 assert.is_true result\match("inner item 2") ~= nil
277
278 it "should handle function values in TableBlock with comments", ->
279 code = "tb =\n\t-- comment before function\n\tfunc1: => print \"a\"\n\n\t-- another function\n\tfunc2: (x) => x * 2\n\n\t-- method\n\tmethod: =>\n\t\t-- inside method\n\t\tprint \"method\"\n"
280 result = to_lua code, {reserve_comment: true}
281 assert.is_true result\match("comment before function") ~= nil
282 assert.is_true result\match("another function") ~= nil
283 assert.is_true result\match("method") ~= nil
284 assert.is_true result\match("inside method") ~= nil
285
286 it "should preserve comments in TableBlock with various value types", ->
287 code = "tb =\n\t-- string value\n\tstr: \"hello\"\n\n\t-- number value\n\tnum: 42\n\n\t-- boolean value\n\tbool: true\n\n\t-- table value\n\ttbl: {1, 2, 3}\n"
288 result = to_lua code, {reserve_comment: true}
289 assert.is_true result\match("string value") ~= nil
290 assert.is_true result\match("number value") ~= nil
291 assert.is_true result\match("boolean value") ~= nil
292 assert.is_true result\match("table value") ~= nil
293
294 it "should preserve empty lines at end of TableBlock", ->
295 code = "tb =\n\t-- item 1\n\ta: 1\n\n\t-- item 2\n\tb: 2\n\n\n"
296 result = to_lua code, {reserve_comment: true}
297 assert.is_true result\match("item 1") ~= nil
298 assert.is_true result\match("item 2") ~= nil
299
300 -- Tests specifically for empty lines between comments
301 it "should preserve empty lines in TableBlock between comments", ->
302 code = "tb =\n\t-- a\n\t\n\t\n\t\n\tval: 1\n"
303 result = to_lua code, {reserve_comment: true}
304 -- Empty lines should produce line number comments in output
305 -- Check that there's a line with just a comment marker (line number)
306 assert.is_true result\match("-- %d+") ~= nil
307
308 it "should preserve empty lines in TableBlock with comments", ->
309 code = "tb =\n\t-- first\n\t\n\t\n\tval: 1\n\t\n\t-- second\n\tval2: 2\n"
310 result = to_lua code, {reserve_comment: true}
311 assert.is_true result\match("first") ~= nil
312 assert.is_true result\match("second") ~= nil
313 -- Should have empty line representations (lines with line number comments)
314 assert.is_true result\match("-- %d+") ~= nil
315
316 it "should preserve empty lines in table literal", ->
317 code = "t = {\n\t-- item1\n\t\n\t\n\t1,\n\t\n\t-- item2\n\t2\n}\n"
318 result = to_lua code, {reserve_comment: true}
319 assert.is_true result\match("item1") ~= nil
320 assert.is_true result\match("item2") ~= nil
321 -- Empty lines should produce line comments
322 assert.is_true result\match("-- %d+") ~= nil
323
324 it "should have more newlines with reserve_comment than without", ->
325 code = "-- comment1\nx = 1\n-- comment2\ny = 2\n"
326 result_with = to_lua code, {reserve_comment: true}
327 result_without = to_lua code, {}
328 -- Count newlines in both results using gmatch
329 newlines_with = 0
330 newlines_without = 0
331 for _ in result_with\gmatch("\n")
332 newlines_with += 1
333 for _ in result_without\gmatch("\n")
334 newlines_without += 1
335 -- With reserve_comment should have equal or more newlines
336 assert.is_true newlines_with >= newlines_without
337
338 it "should preserve empty lines in TableBlock between entries", ->
339 code = "tb =\n\t-- key1\n\tkey1: 1\n\t\n\t\n\t-- key2\n\tkey2: 2\n"
340 result = to_lua code, {reserve_comment: true}
341 assert.is_true result\match("key1") ~= nil
342 assert.is_true result\match("key2") ~= nil
343 -- Empty lines should produce lines with line number comments
344 assert.is_true result\match("\t-- %d+\n") ~= nil
345
346 it "should preserve empty lines in class body", ->
347 code = "class C\n\t-- prop1\n\tprop1: 1\n\t\n\t\n\t-- prop2\n\tprop2: 2\n"
348 result = to_lua code, {reserve_comment: true}
349 assert.is_true result\match("prop1") ~= nil
350 assert.is_true result\match("prop2") ~= nil
351
352 it "should preserve empty lines between comments in table", ->
353 code = "t = {\n\t-- first\n\t\n\t-- second\n\t\n\t-- third\n\tval: 1\n}\n"
354 result = to_lua code, {reserve_comment: true}
355 assert.is_true result\match("first") ~= nil
356 assert.is_true result\match("second") ~= nil
357 assert.is_true result\match("third") ~= nil
358 -- Empty lines should produce line comments
359 assert.is_true result\match("-- %d+") ~= nil
360
361 it "should preserve multiple consecutive empty lines in TableBlock", ->
362 code = "tb =\n\t-- start\n\tval1: 1\n\t\n\t\n\t\n\t-- middle\n\tval2: 2\n\t\n\t\n\t-- end\n\tval3: 3\n"
363 result = to_lua code, {reserve_comment: true}
364 assert.is_true result\match("start") ~= nil
365 assert.is_true result\match("middle") ~= nil
366 assert.is_true result\match("end") ~= nil
367 -- Should have line number comments for empty lines
368 assert.is_true result\match("-- %d+") ~= nil
369
370 -- Comparison tests: Table literal vs TableBlock vs Class
371 it "should preserve comments in table literal", ->
372 code = "t = {\n\t-- comment\n\tkey: 1\n}\n"
373 result = to_lua code, {reserve_comment: true}
374 assert.is_true result\match("comment") ~= nil
375
376 it "should preserve comments in TableBlock", ->
377 code = "t =\n\t-- comment\n\tkey: 1\n"
378 result = to_lua code, {reserve_comment: true}
379 assert.is_true result\match("comment") ~= nil
380
381 it "should preserve comments in class body", ->
382 code = "class C\n\t-- comment\n\tkey: 1\n"
383 result = to_lua code, {reserve_comment: true}
384 assert.is_true result\match("comment") ~= nil
385
386 it "should preserve multiple comments in class body", ->
387 code = "class C\n\t-- prop1\n\tprop1: 1\n\t-- prop2\n\tprop2: 2\n"
388 result = to_lua code, {reserve_comment: true}
389 assert.is_true result\match("prop1") ~= nil
390 assert.is_true result\match("prop2") ~= nil
391
392 it "should preserve empty lines in table literal", ->
393 code = "t = {\n\t-- a\n\t\n\t-- b\n\tkey: 1\n}\n"
394 result = to_lua code, {reserve_comment: true}
395 assert.is_true result\match("a") ~= nil
396 assert.is_true result\match("b") ~= nil
397 -- Empty lines produce line comments
398 assert.is_true result\match("-- %d+") ~= nil
399
400 it "should preserve empty lines in TableBlock", ->
401 code = "t =\n\t-- a\n\t\n\t-- b\n\tkey: 1\n"
402 result = to_lua code, {reserve_comment: true}
403 assert.is_true result\match("a") ~= nil
404 assert.is_true result\match("b") ~= nil
405 -- Empty lines produce line comments
406 assert.is_true result\match("-- %d+") ~= nil
407
408 it "should preserve empty lines in class body", ->
409 code = "class C\n\t-- a\n\ta: 1\n\t\n\t-- b\n\tb: 2\n"
410 result = to_lua code, {reserve_comment: true}
411 assert.is_true result\match("a") ~= nil
412 assert.is_true result\match("b") ~= nil
413 -- Empty lines in class should also be preserved
diff --git a/spec/inputs/whitespace.yue b/spec/inputs/whitespace.yue
index e501d3d..a999143 100644
--- a/spec/inputs/whitespace.yue
+++ b/spec/inputs/whitespace.yue
@@ -160,4 +160,43 @@ local a,\
160 b,\ 160 b,\
161 c 161 c
162 162
163do
164 tb =
165
166
167 -- one
168
169
170 -- a
171 a: 1 -- 1
172
173 -- two
174 b: -> -- 2
175
176 tb2 = {
177
178
179 -- one
180
181
182 -- a
183 a: 1 -- 1
184
185 -- two
186 b: -> -- 2
187 }
188
189 tb3 = class -- dsd
190
191
192
193 -- one
194
195 -- a
196 a: 1 -- 1
197
198 -- two
199 b: -> -- 2
200
201
163nil 202nil