Skip to content

Commit

Permalink
将is_ident_keyword使用的OnceLock改为LocalKey
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Oct 6, 2023
1 parent 07c8f52 commit 057ac5a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 28 deletions.
4 changes: 2 additions & 2 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.12.2"
version = "0.12.3"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
21 changes: 2 additions & 19 deletions src/syntax/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod def;
use std::{
ops::Deref,
num::ParseIntError,
collections::{HashMap, HashSet},
collections::HashMap,
iter::{
zip,
repeat_with,
Expand Down Expand Up @@ -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 {
/// 编译依赖并返回句柄
Expand Down Expand Up @@ -262,13 +251,7 @@ impl Value {

/// 判断是否是一个标识符(包括数字)关键字
pub fn is_ident_keyword(s: &str) -> bool {
use std::sync::OnceLock;
static VAR_KEYWORDS_SET: OnceLock<HashSet<&'static str>>
= 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)
}

/// 判断是否不应该由原始标识符包裹
Expand Down
10 changes: 5 additions & 5 deletions tools/var_utils/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 tools/var_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
28 changes: 28 additions & 0 deletions tools/var_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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> = regex!(
Expand All @@ -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;

0 comments on commit 057ac5a

Please sign in to comment.