Skip to content

Commit

Permalink
将数字的科学计数法形式添加至Var, 如1e9 5e-4
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Nov 16, 2023
1 parent fab9137 commit 9d4a1f4
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 6 deletions.
2 changes: 1 addition & 1 deletion 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.12.8"
version = "0.12.9"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
3 changes: 3 additions & 0 deletions examples/value.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@ set n_bin 0b-1001_0010;
set x 'abc"def'; # 其中的双引号会被替换为单引号
set y `y`; # 这里`y`会被编译为y
set z `"z"`; # 这里`"z"`会被编译为"z"
set pown1 2e8; # 科学计数法语法, 可惜mdt并没有解析其小数形式, bang也暂时没对其扩展
set pown2 1e+9;
set pown3 3e-4;
2 changes: 1 addition & 1 deletion src/syntax/def.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub TopLevel: Expand = CtrlStart <mut lines:Expand> <ctrl:CtrlStop> => {
pub String: Var = r#""[^"]*""# => <>.lines().collect::<Vec<_>>().join("\\n");
pub Ident: Var = r"[_\p{XID_Start}]\p{XID_Continue}*" => <>.into();
pub OIdent: Var = r"@[_\p{XID_Start}][\p{XID_Continue}\-]*" => <>.into(); // `@abc-def`这种
pub Number: Var = r"(?:0(?:x-?[\da-fA-F][_\da-fA-F]*|b-?[01][_01]*)|-?\d[_\d]*(?:\.\d[\d_]*)?)"
pub Number: Var = r"(?:0(?:x-?[\da-fA-F][_\da-fA-F]*|b-?[01][_01]*)|-?\d[_\d]*(?:\.\d[\d_]*|e[+\-]?\d[\d_]*)?)"
=> <>.chars().filter(|&c| c != '_').collect();
// 原始字面量, 如`'@abc-def'`, 其中双引号会被替换为单引号.
// 在一对单引号内可以写任意非空字符, 可避开关键字等
Expand Down
85 changes: 85 additions & 0 deletions src/syntax/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3291,3 +3291,88 @@ fn control_block_test() {
]
);
}

#[test]
fn number_test() {
let parser = NumberParser::new();
let mut meta = Meta::new();

let nums: &[[&'static str; 2]] = { &[
["0", "0"],
["1", "1"],
["10", "10"],
["123.456", "123.456"],
["-123.456", "-123.456"],
["-12_3__.4_5_6_", "-123.456"],
["-10", "-10"],
["0x1b", "0x1b"],
["0x-2c", "0x-2c"],
["0b1001", "0b1001"],
["0b-1101", "0b-1101"],
["1e9", "1e9"],
["1e10", "1e10"],
["1e+10", "1e+10"],
["1e-10", "1e-10"],
["19e9", "19e9"],
["19e10", "19e10"],
["19e+10", "19e+10"],
["19e-10", "19e-10"],
["0_", "0"],
["1_", "1"],
["1_0", "10"],
["-1_0__", "-10"],
["0x1_b", "0x1b"],
["0x-2_c", "0x-2c"],
["0b10_01", "0b1001"],
["0b-11_01", "0b-1101"],
["1e9_", "1e9"],
["1e1_0", "1e10"],
["1e+1_0", "1e+10"],
["1e-1_0", "1e-10"],
["19e9_", "19e9"],
["19e1_0", "19e10"],
["19e+1_0", "19e+10"],
["19e-1_0", "19e-10"],
["1_e9_", "1e9"],
["1_e1_0", "1e10"],
["1_e+1_0", "1e+10"],
["1_e-1_0", "1e-10"],
["1_9__e9_", "19e9"],
["1_9__e1_0", "19e10"],
["1_9__e+1_0", "19e+10"],
["1_9__e-1_0", "19e-10"],
] };
let fails: &[&'static str] = { &[
"0o25",
"1e\\3",
"0o-25",
"- 5",
"_23",
"1._5",
"_1.5",
"0b12",
"1.2e9",
"1e",
"0x1be+9",
"0x1be+0x9",
"2e8.5",
] };

for &[src, dst] in nums {
assert_eq!(
parser.parse(&mut meta, src),
Ok(dst.into()), // alloc, 懒得优化
"assert failed. ({:?} -> {:?})",
src,
dst,
);
}

for &src in fails {
assert!(
parser.parse(&mut meta, src).is_err(),
"assert failed. ({:?})",
src
);
}
}
2 changes: 1 addition & 1 deletion syntax/MT-Manager/MindustryLogicBangLang.mtsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
{
// number
match: /\b(?:0(?:x-?[\da-fA-F][_\da-fA-F]*|b-?[01][_01]*)/
+ /|-?\d[_\d]*(?:\.\d[\d_]*)?)\b/,
+ /|-?\d[_\d]*(?:\.\d[\d_]*|e[+\-]?\d[\d_]*)?)\b/,
0: "number"
},
{
Expand Down
2 changes: 1 addition & 1 deletion syntax/vim/mdtlbl.vim
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ hi link mdtlblOIdent Identifier
syn match mdtlblOtherVar /'[^' \t]\+'/
hi link mdtlblOtherVar Identifier

syn match mdtlblNumber /\v(<0(x\-?[0-9a-fA-F][0-9a-fA-F_]*|b\-?[01][_01]*)|\-?<[0-9][0-9_]*(\.[0-9][0-9_]*)?)>/
syn match mdtlblNumber /\v(<0(x\-?[0-9a-fA-F][0-9a-fA-F_]*|b\-?[01][_01]*)|\-?<\d[0-9_]*(\.\d[0-9_]*|e[+\-]?\d[0-9_]*)?)>/
hi link mdtlblNumber Number

syn match mdtlblBoolean /\v<true|false>/
Expand Down
3 changes: 2 additions & 1 deletion tools/var_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use std::{
thread_local,
};

/// 判断是否是一个标识符(包括数字)
pub fn is_ident(s: &str) -> bool {
static REGEX: &Lazy<Regex> = regex!(
r#"^(?:(?:[_\p{XID_Start}]\p{XID_Continue}*)|(?:@[_\p{XID_Start}][\p{XID_Continue}\-]*)|(?:0(?:x-?[\da-fA-F][_\da-fA-F]*|b-?[01][_01]*)|-?\d[_\d]*(?:\.\d[\d_]*)?))$"#
r#"^(?:(?:[_\p{XID_Start}]\p{XID_Continue}*)|(?:@[_\p{XID_Start}][\p{XID_Continue}\-]*)|(?:0(?:x-?[\da-fA-F][_\da-fA-F]*|b-?[01][_01]*)|-?\d[_\d]*(?:\.\d[\d_]*|e[+\-]?\d[\d_]*)?))$"#
);
REGEX.is_match(s)
}
Expand Down

0 comments on commit 9d4a1f4

Please sign in to comment.