Separate is_valid_function_name.

This commit is contained in:
Stephen Chung 2021-08-30 15:42:47 +08:00
parent cb90ce96d2
commit 7b2b26aa0d
2 changed files with 13 additions and 5 deletions

View File

@ -11,7 +11,8 @@ use crate::fn_hash::get_hasher;
use crate::module::NamespaceRef; use crate::module::NamespaceRef;
use crate::optimize::{optimize_into_ast, OptimizationLevel}; use crate::optimize::{optimize_into_ast, OptimizationLevel};
use crate::token::{ use crate::token::{
is_keyword_function, is_valid_identifier, Token, TokenStream, TokenizerControl, is_keyword_function, is_valid_function_name, is_valid_identifier, Token, TokenStream,
TokenizerControl,
}; };
use crate::{ use crate::{
calc_fn_hash, calc_qualified_fn_hash, calc_qualified_var_hash, Engine, Identifier, calc_fn_hash, calc_qualified_fn_hash, calc_qualified_var_hash, Engine, Identifier,
@ -478,7 +479,7 @@ fn parse_fn_call(
}, },
); );
let hashes = if is_valid_identifier(id.chars()) { let hashes = if is_valid_function_name(&id) {
FnCallHashes::from_script(hash) FnCallHashes::from_script(hash)
} else { } else {
FnCallHashes::from_native(hash) FnCallHashes::from_native(hash)
@ -528,7 +529,7 @@ fn parse_fn_call(
}, },
); );
let hashes = if is_valid_identifier(id.chars()) { let hashes = if is_valid_function_name(&id) {
FnCallHashes::from_script(hash) FnCallHashes::from_script(hash)
} else { } else {
FnCallHashes::from_native(hash) FnCallHashes::from_native(hash)
@ -1949,7 +1950,7 @@ fn parse_binary_op(
let hash = calc_fn_hash(&s, 2); let hash = calc_fn_hash(&s, 2);
FnCallExpr { FnCallExpr {
hashes: if is_valid_identifier(s.chars()) { hashes: if is_valid_function_name(&s) {
FnCallHashes::from_script(hash) FnCallHashes::from_script(hash)
} else { } else {
FnCallHashes::from_native(hash) FnCallHashes::from_native(hash)

View File

@ -960,7 +960,7 @@ impl Token {
#[inline] #[inline]
pub(crate) fn into_function_name_for_override(self) -> Result<String, Self> { pub(crate) fn into_function_name_for_override(self) -> Result<String, Self> {
match self { match self {
Self::Custom(s) | Self::Identifier(s) if is_valid_identifier(s.chars()) => Ok(s), Self::Custom(s) | Self::Identifier(s) if is_valid_function_name(&s) => Ok(s),
_ => Err(self), _ => Err(self),
} }
} }
@ -2015,6 +2015,13 @@ pub fn is_valid_identifier(name: impl Iterator<Item = char>) -> bool {
first_alphabetic first_alphabetic
} }
/// Is a text string a valid scripted function name?
#[inline(always)]
#[must_use]
pub fn is_valid_function_name(name: &str) -> bool {
is_valid_identifier(name.chars())
}
/// Is a character valid to start an identifier? /// Is a character valid to start an identifier?
#[cfg(feature = "unicode-xid-ident")] #[cfg(feature = "unicode-xid-ident")]
#[inline(always)] #[inline(always)]