From 4ba4c90e711c6204aa40e38347c5a5a076d9370e Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 23 May 2025 15:07:45 +0800 Subject: Added `-` for implicit object. --- doc/docs/doc/README.md | 63 ++++++++++++++++++++++++------------- doc/docs/zh/doc/README.md | 64 +++++++++++++++++++++++++------------- spec/inputs/tables.yue | 18 +++++++++++ spec/inputs/with.yue | 4 +++ spec/outputs/codes_from_doc.lua | 16 ++++++++++ spec/outputs/codes_from_doc_zh.lua | 16 ++++++++++ spec/outputs/tables.lua | 22 +++++++++++++ spec/outputs/with.lua | 30 +++++++++++++++--- src/yuescript/yue_parser.cpp | 4 +-- 9 files changed, 187 insertions(+), 50 deletions(-) diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md index 0853391..58b37c0 100755 --- a/doc/docs/doc/README.md +++ b/doc/docs/doc/README.md @@ -16,17 +16,17 @@ Yue (月) is the name of moon in Chinese and it's pronounced as [jyɛ]. ### An Overview of YueScript ```moonscript -- import syntax -import "yue" as :p, :to_lua +import p, to_lua from "yue" -- object literals inventory = equipment: - * "sword" - * "shield" + - "sword" + - "shield" items: - * name: "potion" + - name: "potion" count: 10 - * name: "bread" + - name: "bread" count: 3 -- list comprehension @@ -61,17 +61,17 @@ export 🌛 = "月之脚本"
 -- import syntax
-import "yue" as :p, :to_lua
+import p, to_lua from "yue"
 
 -- object literals
 inventory =
   equipment:
-    * "sword"
-    * "shield"
+    - "sword"
+    - "shield"
   items:
-    * name: "potion"
+    - name: "potion"
       count: 10
-    * name: "bread"
+    - name: "bread"
       count: 3
 
 -- list comprehension
@@ -752,34 +752,45 @@ a ??= false
 
 ### Implicit Object
 
-You can write a list of implicit structures that starts with the symbol **\*** inside a table block. If you are creating implicit object, the fields of the object must be with the same indent.
+You can write a list of implicit structures that starts with the symbol **\*** or **-** inside a table block. If you are creating implicit object, the fields of the object must be with the same indent.
+
 ```moonscript
+-- assignment with implicit object
 list =
   * 1
   * 2
   * 3
 
+-- function call with implicit object
 func
   * 1
   * 2
   * 3
 
+-- return with implicit object
+f = ->
+  return
+    * 1
+    * 2
+    * 3
+
+-- table with implicit object
 tb =
   name: "abc"
 
   values:
-    * "a"
-    * "b"
-    * "c"
+    - "a"
+    - "b"
+    - "c"
 
   objects:
-    * name: "a"
+    - name: "a"
       value: 1
       func: => @value + 1
       tb:
         fieldA: 1
 
-    * name: "b"
+    - name: "b"
       value: 2
       func: => @value + 2
       tb: { }
@@ -787,32 +798,42 @@ tb =
 ```
 
 
+-- assignment with implicit object
 list =
   * 1
   * 2
   * 3
 
+-- function call with implicit object
 func
   * 1
   * 2
   * 3
 
+-- return with implicit object
+f = ->
+  return
+    * 1
+    * 2
+    * 3
+
+-- table with implicit object
 tb =
   name: "abc"
 
   values:
-    * "a"
-    * "b"
-    * "c"
+    - "a"
+    - "b"
+    - "c"
 
   objects:
-    * name: "a"
+    - name: "a"
       value: 1
       func: => @value + 1
       tb:
         fieldA: 1
 
-    * name: "b"
+    - name: "b"
       value: 2
       func: => @value + 2
       tb: { }
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md
index 2968f6e..1dd59a7 100755
--- a/doc/docs/zh/doc/README.md
+++ b/doc/docs/zh/doc/README.md
@@ -16,17 +16,17 @@ Yue(月)是中文中“月亮”的名称。
 ### 月之脚本概览
 ```moonscript
 -- 导入语法
-import "yue" as :p, :to_lua
+import p, to_lua from "yue"
 
 -- 隐式对象
 inventory =
   equipment:
-    * "sword"
-    * "shield"
+    - "sword"
+    - "shield"
   items:
-    * name: "potion"
+    - name: "potion"
       count: 10
-    * name: "bread"
+    - name: "bread"
       count: 3
 
 -- 列表推导
@@ -61,17 +61,17 @@ export 🌛 = "月之脚本"
 
 
 -- 导入语法
-import "yue" as :p, :to_lua
+import p, to_lua from "yue"
 
 -- 隐式对象
 inventory =
   equipment:
-    * "sword"
-    * "shield"
+    - "sword"
+    - "shield"
   items:
-    * name: "potion"
+    - name: "potion"
       count: 10
-    * name: "bread"
+    - name: "bread"
       count: 3
 
 -- 列表推导
@@ -751,67 +751,87 @@ a ??= false
 
 ### 隐式对象
 
-你可以在表格块内使用符号 **\*** 开始编写一系列隐式结构。如果你正在创建隐式对象,对象的字段必须具有相同的缩进。
+你可以在表格块内使用符号 **\*** 或是 **-** 开始编写一系列隐式结构。如果你正在创建隐式对象,对象的字段必须具有相同的缩进。
+
 ```moonscript
+-- 赋值时使用隐式对象
 list =
   * 1
   * 2
   * 3
 
+-- 函数调用时使用隐式对象
 func
   * 1
   * 2
   * 3
 
+-- 返回时使用隐式对象
+f = ->
+  return
+    * 1
+    * 2
+    * 3
+
+-- 表格时使用隐式对象
 tb =
   name: "abc"
 
   values:
-    * "a"
-    * "b"
-    * "c"
+    - "a"
+    - "b"
+    - "c"
 
   objects:
-    * name: "a"
+    - name: "a"
       value: 1
       func: => @value + 1
       tb:
         fieldA: 1
 
-    * name: "b"
+    - name: "b"
       value: 2
       func: => @value + 2
       tb: { }
-
 ```
 
 
+-- 赋值时使用隐式对象
 list =
   * 1
   * 2
   * 3
 
+-- 函数调用时使用隐式对象
 func
   * 1
   * 2
   * 3
 
+-- 返回时使用隐式对象
+f = ->
+  return
+    * 1
+    * 2
+    * 3
+
+-- 表格时使用隐式对象
 tb =
   name: "abc"
 
   values:
-    * "a"
-    * "b"
-    * "c"
+    - "a"
+    - "b"
+    - "c"
 
   objects:
-    * name: "a"
+    - name: "a"
       value: 1
       func: => @value + 1
       tb:
         fieldA: 1
 
-    * name: "b"
+    - name: "b"
       value: 2
       func: => @value + 2
       tb: { }
diff --git a/spec/inputs/tables.yue b/spec/inputs/tables.yue
index 0de8a8c..702e04a 100644
--- a/spec/inputs/tables.yue
+++ b/spec/inputs/tables.yue
@@ -245,6 +245,24 @@ menus =
 				click: ->
 			}
 
+_ =
+	boolean:
+		- true
+		- false
+	float:
+		- 3.14
+		- -6.8523015e+5
+	int:
+		- 123
+		- -0b1010_0111_0100_1010_1110
+	null:
+		nodeName: 'node'
+		parent: nil
+	string:
+		- 'Hello world'
+		- "newline
+newline2"
+
 tb = {...other}
 
 tbMix = {
diff --git a/spec/inputs/with.yue b/spec/inputs/with.yue
index 3fee48e..dcd4053 100644
--- a/spec/inputs/with.yue
+++ b/spec/inputs/with.yue
@@ -161,4 +161,8 @@ do
 		if .v
 			break .a
 
+	a = while true
+		break with? tb
+			break 1
+
 nil
diff --git a/spec/outputs/codes_from_doc.lua b/spec/outputs/codes_from_doc.lua
index 3ee20bb..c7a2d50 100644
--- a/spec/outputs/codes_from_doc.lua
+++ b/spec/outputs/codes_from_doc.lua
@@ -339,6 +339,14 @@ func({
 	2,
 	3
 })
+local f
+f = function()
+	return {
+		1,
+		2,
+		3
+	}
+end
 local tb = {
 	name = "abc",
 	values = {
@@ -2548,6 +2556,14 @@ func({
 	2,
 	3
 })
+local f
+f = function()
+	return {
+		1,
+		2,
+		3
+	}
+end
 local tb = {
 	name = "abc",
 	values = {
diff --git a/spec/outputs/codes_from_doc_zh.lua b/spec/outputs/codes_from_doc_zh.lua
index 52204b7..fcde41f 100644
--- a/spec/outputs/codes_from_doc_zh.lua
+++ b/spec/outputs/codes_from_doc_zh.lua
@@ -339,6 +339,14 @@ func({
 	2,
 	3
 })
+local f
+f = function()
+	return {
+		1,
+		2,
+		3
+	}
+end
 local tb = {
 	name = "abc",
 	values = {
@@ -2542,6 +2550,14 @@ func({
 	2,
 	3
 })
+local f
+f = function()
+	return {
+		1,
+		2,
+		3
+	}
+end
 local tb = {
 	name = "abc",
 	values = {
diff --git a/spec/outputs/tables.lua b/spec/outputs/tables.lua
index f358811..3f851de 100644
--- a/spec/outputs/tables.lua
+++ b/spec/outputs/tables.lua
@@ -366,6 +366,28 @@ local menus = {
 		}
 	}
 }
+_ = {
+	boolean = {
+		true,
+		false
+	},
+	float = {
+		3.14,
+		-6.8523015e+5
+	},
+	int = {
+		123,
+		-685230
+	},
+	null = {
+		nodeName = 'node',
+		parent = nil
+	},
+	string = {
+		'Hello world',
+		"newline\nnewline2"
+	}
+}
 local tb
 do
 	local _tab_0 = { }
diff --git a/spec/outputs/with.lua b/spec/outputs/with.lua
index 20c5d44..867d1b5 100644
--- a/spec/outputs/with.lua
+++ b/spec/outputs/with.lua
@@ -203,15 +203,35 @@ do
 		return _with_0
 	end)())
 	local a
-	local _with_0 = tb
+	do
+		local _with_0 = tb
+		do
+			local _accum_0
+			while true do
+				if _with_0.v then
+					_accum_0 = _with_0.a
+					break
+				end
+			end
+			_with_0 = _accum_0
+		end
+		a = _with_0
+	end
 	local _accum_0
 	while true do
-		if _with_0.v then
-			_accum_0 = _with_0.a
+		local _with_0 = tb
+		local _accum_1
+		while true do
+			if _with_0 ~= nil then
+				_accum_1 = 1
+				break
+			end
 			break
 		end
+		_with_0 = _accum_1
+		_accum_0 = _with_0
+		break
 	end
-	_with_0 = _accum_0
-	a = _with_0
+	a = _accum_0
 end
 return nil
diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp
index cd1fd48..078509c 100644
--- a/src/yuescript/yue_parser.cpp
+++ b/src/yuescript/yue_parser.cpp
@@ -740,7 +740,7 @@ YueParser::YueParser() {
 
 	table_block_inner = Seperator >> key_value_line >> *(+space_break >> key_value_line);
 	TableBlock = +space_break >> advance_match >> ensure(table_block_inner, pop_indent);
-	TableBlockIndent = '*' >> Seperator >> disable_arg_table_block_rule(
+	TableBlockIndent = ('*' | '-' >> space_one) >> Seperator >> disable_arg_table_block_rule(
 		space >> key_value_list >> -(space >> ',') >>
 		-(+space_break >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *(+space_break >> key_value_line), pop_indent)));
 
@@ -843,7 +843,7 @@ YueParser::YueParser() {
 	key_value_line = check_indent_match >> space >> (
 		key_value_list >> -(space >> ',') |
 		TableBlockIndent |
-		'*' >> space >> (SpreadExp | Exp | TableBlock)
+		('*' | '-' >> space_one) >> space >> (SpreadExp | Exp | TableBlock)
 	);
 
 	fn_arg_def_list = FnArgDef >> *(space >> ',' >> space >> FnArgDef);
-- 
cgit v1.2.3-55-g6feb