From 9ecf749f909d267c9af3ab8e6ae0241c4cf57dff Mon Sep 17 00:00:00 2001 From: A4-Tacks Date: Tue, 16 Jul 2024 12:23:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E5=9D=97=E7=9B=B8=E5=85=B3=E7=9A=84=E8=AF=AD=E6=B3=95?= =?UTF-8?q?=E7=B3=96,=20=E4=BD=BF=E5=A4=9A=E5=8F=82=E6=95=B0=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E6=97=B6=E6=9B=B4=E6=96=B9=E4=BE=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 8 ++++---- Cargo.toml | 2 +- examples/match.mdtlbl | 23 +++++++++++++++++++++++ tools/parser/Cargo.toml | 2 +- tools/parser/src/parser.lalrpop | 23 ++++++++++++++++------- tools/parser/tests/Cargo.toml | 2 +- tools/parser/tests/src/lib.rs | 17 +++++++++++++++++ tools/syntax/Cargo.toml | 2 +- tools/syntax/src/lib.rs | 3 +++ 9 files changed, 67 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2452ab4..5803761 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -301,7 +301,7 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "mindustry_logic_bang_lang" -version = "0.16.17" +version = "0.16.18" dependencies = [ "display_source", "logic_lint", @@ -347,7 +347,7 @@ dependencies = [ [[package]] name = "parser" -version = "0.3.16" +version = "0.3.17" dependencies = [ "lalrpop", "lalrpop-util", @@ -358,7 +358,7 @@ dependencies = [ [[package]] name = "parser-tests" -version = "0.1.30" +version = "0.1.31" dependencies = [ "parser", "syntax", @@ -535,7 +535,7 @@ dependencies = [ [[package]] name = "syntax" -version = "0.2.36" +version = "0.2.37" dependencies = [ "either", "tag_code", diff --git a/Cargo.toml b/Cargo.toml index 7f82489..e956170 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mindustry_logic_bang_lang" -version = "0.16.17" +version = "0.16.18" edition = "2021" authors = ["A4-Tacks "] diff --git a/examples/match.mdtlbl b/examples/match.mdtlbl index d699a2c..95a49c7 100644 --- a/examples/match.mdtlbl +++ b/examples/match.mdtlbl @@ -357,3 +357,26 @@ print "b" print "empty" *# # 在这里, `Foo`和`Foo[]`的作用已经不同了 + + +# 在0.16.18版本给重复块增加了一个语法糖, 如下 +inline@ A B *C { + print A", "B", "C"\n"; +} +#* A >>> +inline 3@{ + const match @ { + A B *C { + { + `'print'` A; + `'print'` ", "; + `'print'` B; + `'print'` ", "; + `'print'` C; + `'print'` "\n"; + } + } + } +} +*# +# 由上述展开可以看到它的运作方式, 很容易理解 diff --git a/tools/parser/Cargo.toml b/tools/parser/Cargo.toml index c76f3c1..55d7993 100644 --- a/tools/parser/Cargo.toml +++ b/tools/parser/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "parser" -version = "0.3.16" +version = "0.3.17" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/tools/parser/src/parser.lalrpop b/tools/parser/src/parser.lalrpop index 277683c..bad6c48 100644 --- a/tools/parser/src/parser.lalrpop +++ b/tools/parser/src/parser.lalrpop @@ -459,13 +459,22 @@ Args: Args = { args.into() }, } -ArgsRepeatBlock: ArgsRepeat -= "@" > =>? { - if chunk == Some(0) { - return Err(Error::from((l, Errors::ArgsRepeatChunkByZero, r)).into()) - } - Ok(ArgsRepeat::new(chunk.unwrap_or(1), block.into())) -}; +ArgsRepeatBlock: ArgsRepeat = { + + "@" =>? { + if chunk == Some(0) { + return Err(Error::from((l, Errors::ArgsRepeatChunkByZero, r)).into()) + } + Ok(ArgsRepeat::new(chunk.unwrap_or(1), block)) + }, + "@" => { + ArgsRepeat::new(pats.len(), vec![ + ConstMatch::new(Args::GLOB_ONLY, vec![ + (pats.into(), block), + ]).into() + ].into()) + }, +} Print: LogicLine = "print" LEnd => { let Some(args) = <> else { diff --git a/tools/parser/tests/Cargo.toml b/tools/parser/tests/Cargo.toml index f0484f6..5754935 100644 --- a/tools/parser/tests/Cargo.toml +++ b/tools/parser/tests/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "parser-tests" -version = "0.1.30" +version = "0.1.31" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/tools/parser/tests/src/lib.rs b/tools/parser/tests/src/lib.rs index 20c0c1d..c2aef90 100644 --- a/tools/parser/tests/src/lib.rs +++ b/tools/parser/tests/src/lib.rs @@ -5052,6 +5052,23 @@ fn match_test() { "print nouse", ], ); + + assert_eq!( + parse!(parser, r#" + inline@ A B *C { + print A B C; + } + "#).unwrap(), + parse!(parser, r#" + inline 3@{ + const match @ { + A B *C { + print A B C; + } + } + } + "#).unwrap(), + ); } #[test] diff --git a/tools/syntax/Cargo.toml b/tools/syntax/Cargo.toml index 8808dfa..fe31302 100644 --- a/tools/syntax/Cargo.toml +++ b/tools/syntax/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "syntax" -version = "0.2.36" +version = "0.2.37" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/tools/syntax/src/lib.rs b/tools/syntax/src/lib.rs index 7df1b3b..5a6acbb 100644 --- a/tools/syntax/src/lib.rs +++ b/tools/syntax/src/lib.rs @@ -3172,6 +3172,9 @@ impl ConstMatchPat { } } } +impl_enum_froms!(impl From for ConstMatchPat { + Normal => Vec; +}); #[derive(Debug, PartialEq, Clone)] pub struct ConstMatchPatAtom {