Skip to content

Commit

Permalink
Refactor Rust identifier constants
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingsilverfin committed Feb 14, 2024
1 parent 7c8eb18 commit 7237e05
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
55 changes: 24 additions & 31 deletions rust/common/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ use std::sync::OnceLock;

use regex::{Regex, RegexBuilder};

pub fn is_valid_label_identifier(identifier: &str) -> bool {
static REGEX: OnceLock<Regex> = OnceLock::new();
let regex = REGEX.get_or_init(|| {
let identifier_start = "A-Za-z\
const IDENTIFIER_CHAR: &str = "A-Za-z\
\\u00C0-\\u00D6\
\\u00D8-\\u00F6\
\\u00F8-\\u02FF\
Expand All @@ -38,17 +35,23 @@ pub fn is_valid_label_identifier(identifier: &str) -> bool {
\\u3001-\\uD7FF\
\\uF900-\\uFDCF\
\\uFDF0-\\uFFFD";
let identifier_tail = format!(
"{}\
0-9\
_\
const IDENTIFIER_CONNECTOR: &str = "_\
\\-\
\\u00B7\
\\u0300-\\u036F\
\\u203F-\\u2040",
identifier_start
\\u203F-\\u2040";
const IDENTIFIER_DIGIT: &str = "0-9";

pub fn is_valid_label_identifier(identifier: &str) -> bool {
static REGEX: OnceLock<Regex> = OnceLock::new();
let regex = REGEX.get_or_init(|| {
let identifier_tail = format!(
"{}{}{}",
IDENTIFIER_CHAR,
IDENTIFIER_CONNECTOR,
IDENTIFIER_DIGIT
);
let identifier_pattern = format!("^[{}][{}]*$", identifier_start, identifier_tail);
let identifier_pattern = format!("^[{}][{}]*$", IDENTIFIER_CHAR, identifier_tail);
RegexBuilder::new(&identifier_pattern).build().unwrap()
});
regex.is_match(identifier)
Expand All @@ -58,29 +61,19 @@ pub fn is_valid_label_identifier(identifier: &str) -> bool {
pub fn is_valid_var_identifier(identifier: &str) -> bool {
static REGEX: OnceLock<Regex> = OnceLock::new();
let regex = REGEX.get_or_init(|| {
let identifier_start = "A-Za-z0-9\
\\u00C0-\\u00D6\
\\u00D8-\\u00F6\
\\u00F8-\\u02FF\
\\u0370-\\u037D\
\\u037F-\\u1FFF\
\\u200C-\\u200D\
\\u2070-\\u218F\
\\u2C00-\\u2FEF\
\\u3001-\\uD7FF\
\\uF900-\\uFDCF\
\\uFDF0-\\uFFFD";
let identifier_start = format!(
"{}{}",
IDENTIFIER_CHAR,
IDENTIFIER_DIGIT
);
let identifier_tail = format!(
"{}\
_\
\\-\
\\u00B7\
\\u0300-\\u036F\
\\u203F-\\u2040",
identifier_start
"{}{}{}",
IDENTIFIER_CHAR,
IDENTIFIER_DIGIT,
IDENTIFIER_CONNECTOR
);
let identifier_pattern = format!("^[{}][{}]*$", identifier_start, identifier_tail);
RegexBuilder::new(&identifier_pattern).build().unwrap()
});
regex.is_match(identifier)
}
}
2 changes: 1 addition & 1 deletion rust/parser/typeql.pest
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ VAR_CONCEPT_ANONYMOUS_ = @{ "$_" ~ WB }
VAR_CONCEPT_NAMED_ = @{ "$" ~ IDENTIFIER_VAR_H_ ~ IDENTIFIER_VAR_T_* ~ WB }
VAR_VALUE_ = @{ "?" ~ (IDENTIFIER_LABEL_H_ ~ IDENTIFIER_LABEL_T_* ~ WB) }
IID_ = @{ "0x" ~ ASCII_HEX_DIGIT+ ~ WB }
LABEL_SCOPED_ = @{ (IDENTIFIER_LABEL_H_ ~ IDENTIFIER_LABEL_T_* ) ~ ":" ~ (IDENTIFIER_LABEL_H_ ~ IDENTIFIER_LABEL_T_* ~ WB) }
LABEL_ = @{ (IDENTIFIER_LABEL_H_ ~ IDENTIFIER_LABEL_T_* ~ WB) }
LABEL_SCOPED_ = @{ (IDENTIFIER_LABEL_H_ ~ IDENTIFIER_LABEL_T_* ) ~ ":" ~ (IDENTIFIER_LABEL_H_ ~ IDENTIFIER_LABEL_T_* ~ WB) }


// FRAGMENTS OF KEYWORDS =======================================================
Expand Down

0 comments on commit 7237e05

Please sign in to comment.