Skip to content

Commit

Permalink
增加一个重复块相关的语法糖, 使多参数重复时更方便
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Jul 16, 2024
1 parent 4d7024d commit 9ecf749
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 15 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mindustry_logic_bang_lang"
version = "0.16.17"
version = "0.16.18"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
23 changes: 23 additions & 0 deletions examples/match.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
}
}
*#
# 由上述展开可以看到它的运作方式, 很容易理解
2 changes: 1 addition & 1 deletion tools/parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
23 changes: 16 additions & 7 deletions tools/parser/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -459,13 +459,22 @@ Args: Args = {
args.into()
},
}
ArgsRepeatBlock: ArgsRepeat
= <l:@L><chunk:LiteralUInt?><r:@R> "@" <block:MBlock<LogicLine*>> =>? {
if chunk == Some(0) {
return Err(Error::from((l, Errors::ArgsRepeatChunkByZero, r)).into())
}
Ok(ArgsRepeat::new(chunk.unwrap_or(1), block.into()))
};
ArgsRepeatBlock: ArgsRepeat = {
<l:@L><chunk:LiteralUInt?><r:@R>
"@" <block:NoPrefixInlineBlock> =>? {
if chunk == Some(0) {
return Err(Error::from((l, Errors::ArgsRepeatChunkByZero, r)).into())
}
Ok(ArgsRepeat::new(chunk.unwrap_or(1), block))
},
"@" <pats:ConstMatchPatAtom+> <block:NoPrefixInlineBlock> => {
ArgsRepeat::new(pats.len(), vec![
ConstMatch::new(Args::GLOB_ONLY, vec![
(pats.into(), block),
]).into()
].into())
},
}

Print: LogicLine = "print" <Args?> LEnd => {
let Some(args) = <> else {
Expand Down
2 changes: 1 addition & 1 deletion tools/parser/tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
17 changes: 17 additions & 0 deletions tools/parser/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion tools/syntax/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions tools/syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3172,6 +3172,9 @@ impl ConstMatchPat {
}
}
}
impl_enum_froms!(impl From for ConstMatchPat {
Normal => Vec<ConstMatchPatAtom>;
});

#[derive(Debug, PartialEq, Clone)]
pub struct ConstMatchPatAtom {
Expand Down

0 comments on commit 9ecf749

Please sign in to comment.