From 057ac5a68dedd917f4e0cef79fc637282ec1ca72 Mon Sep 17 00:00:00 2001 From: A4-Tacks Date: Fri, 6 Oct 2023 17:27:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86is=5Fident=5Fkeyword=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E7=9A=84OnceLock=E6=94=B9=E4=B8=BALocalKey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- src/syntax/mod.rs | 21 ++------------------- tools/var_utils/Cargo.lock | 10 +++++----- tools/var_utils/Cargo.toml | 2 +- tools/var_utils/src/lib.rs | 28 ++++++++++++++++++++++++++++ 6 files changed, 39 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b90c49d..1ebe0de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -288,7 +288,7 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "mindustry_logic_bang_lang" -version = "0.12.2" +version = "0.12.3" dependencies = [ "display_source", "lalrpop", @@ -558,7 +558,7 @@ checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" [[package]] name = "var_utils" -version = "0.1.0" +version = "0.2.0" dependencies = [ "lazy-regex", ] diff --git a/Cargo.toml b/Cargo.toml index 265bd8a..6aacd8d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mindustry_logic_bang_lang" -version = "0.12.2" +version = "0.12.3" edition = "2021" authors = ["A4-Tacks "] diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index 7f72d2d..6ed2178 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -3,7 +3,7 @@ pub mod def; use std::{ ops::Deref, num::ParseIntError, - collections::{HashMap, HashSet}, + collections::HashMap, iter::{ zip, repeat_with, @@ -123,17 +123,6 @@ pub const COUNTER: &str = "@counter"; pub const FALSE_VAR: &str = "false"; pub const ZERO_VAR: &str = "0"; pub const UNUSED_VAR: &str = "0"; -pub const VAR_KEYWORDS: &[&str] = {&[ - "_", "abs", "acos", "add", "always", "and", "angle", - "asin", "atan", "break", "case", "ceil", "const", "continue", - "cos", "div", "do", "elif", "else", "equal", "floor", - "goto", "greaterThan", "greaterThanEq", "gwhile", "idiv", "if", "inline", - "land", "len", "lessThan", "lessThanEq", "lnot", "log", "max", - "min", "mod", "mul", "noise", "noop", "not", "notEqual", - "op", "or", "pow", "print", "rand", "select", "set", - "setres", "shl", "shr", "sin", "skip", "sqrt", "strictEqual", - "strictNotEqual", "sub", "switch", "take", "tan", "while", "xor", -]}; pub trait TakeHandle { /// 编译依赖并返回句柄 @@ -262,13 +251,7 @@ impl Value { /// 判断是否是一个标识符(包括数字)关键字 pub fn is_ident_keyword(s: &str) -> bool { - use std::sync::OnceLock; - static VAR_KEYWORDS_SET: OnceLock> - = OnceLock::new(); - let var_keywords = VAR_KEYWORDS_SET.get_or_init(|| { - HashSet::from_iter(VAR_KEYWORDS.into_iter().copied()) - }); - var_keywords.get(s).is_some() + var_utils::is_ident_keyword(s) } /// 判断是否不应该由原始标识符包裹 diff --git a/tools/var_utils/Cargo.lock b/tools/var_utils/Cargo.lock index 638a4df..ac40d8d 100644 --- a/tools/var_utils/Cargo.lock +++ b/tools/var_utils/Cargo.lock @@ -48,9 +48,9 @@ checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" [[package]] name = "proc-macro2" -version = "1.0.67" +version = "1.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328" +checksum = "5b1106fec09662ec6dd98ccac0f81cef56984d0b49f75c92d8cbad76e20c005c" dependencies = [ "unicode-ident", ] @@ -95,9 +95,9 @@ checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" [[package]] name = "syn" -version = "2.0.37" +version = "2.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7303ef2c05cd654186cb250d29049a24840ca25d2747c25c0381c8d9e2f582e8" +checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b" dependencies = [ "proc-macro2", "quote", @@ -112,7 +112,7 @@ checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "var_utils" -version = "0.1.0" +version = "0.2.0" dependencies = [ "lazy-regex", ] diff --git a/tools/var_utils/Cargo.toml b/tools/var_utils/Cargo.toml index 4281e9e..ca7a0a4 100644 --- a/tools/var_utils/Cargo.toml +++ b/tools/var_utils/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "var_utils" -version = "0.1.0" +version = "0.2.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/tools/var_utils/src/lib.rs b/tools/var_utils/src/lib.rs index 0c25743..7a8d2fa 100644 --- a/tools/var_utils/src/lib.rs +++ b/tools/var_utils/src/lib.rs @@ -1,4 +1,8 @@ use lazy_regex::{regex,Lazy,Regex}; +use std::{ + collections::HashSet, + thread_local, +}; pub fn is_ident(s: &str) -> bool { static REGEX: &Lazy = regex!( @@ -7,5 +11,29 @@ pub fn is_ident(s: &str) -> bool { REGEX.is_match(s) } +pub const VAR_KEYWORDS: &[&str] = {&[ + "_", "abs", "acos", "add", "always", "and", "angle", + "asin", "atan", "break", "case", "ceil", "const", "continue", + "cos", "div", "do", "elif", "else", "equal", "floor", + "goto", "greaterThan", "greaterThanEq", "gwhile", "idiv", "if", "inline", + "land", "len", "lessThan", "lessThanEq", "lnot", "log", "max", + "min", "mod", "mul", "noise", "noop", "not", "notEqual", + "op", "or", "pow", "print", "rand", "select", "set", + "setres", "shl", "shr", "sin", "skip", "sqrt", "strictEqual", + "strictNotEqual", "sub", "switch", "take", "tan", "while", "xor", +]}; + +/// 判断是否是一个标识符(包括数字)关键字 +pub fn is_ident_keyword(s: &str) -> bool { + thread_local! { + static VAR_KEYWORDS_SET: HashSet<&'static str> + = HashSet::from_iter(VAR_KEYWORDS.into_iter().copied()); + } + VAR_KEYWORDS_SET.with(|var_keywords| { + var_keywords.get(s).is_some() + }) +} + + #[cfg(test)] mod tests;