diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-11-02 12:13:45 +0100 |
|---|---|---|
| committer | Martin Haug <mhaug@live.de> | 2021-11-05 13:46:41 +0100 |
| commit | 65fac0e57c9852eb2131aa06c0bac43b70bfbfbc (patch) | |
| tree | 8ed11d7cefd4e64f523b975f077e4b10f67a7cb9 /src/syntax/ident.rs | |
| parent | 42afb27cef5540535420fb6d8d9d2fcda7300a47 (diff) | |
Refactoring
Co-Authored-By: Martin <mhaug@live.de>
Diffstat (limited to 'src/syntax/ident.rs')
| -rw-r--r-- | src/syntax/ident.rs | 94 |
1 files changed, 0 insertions, 94 deletions
diff --git a/src/syntax/ident.rs b/src/syntax/ident.rs deleted file mode 100644 index f5cc6330..00000000 --- a/src/syntax/ident.rs +++ /dev/null @@ -1,94 +0,0 @@ -use std::borrow::Borrow; -use std::ops::Deref; - -use unicode_xid::UnicodeXID; - -use super::{NodeKind, RedRef, Span, TypedNode}; -use crate::util::EcoString; - -/// An unicode identifier with a few extra permissible characters. -/// -/// In addition to what is specified in the [Unicode Standard][uax31], we allow: -/// - `_` as a starting character, -/// - `_` and `-` as continuing characters. -/// -/// [uax31]: http://www.unicode.org/reports/tr31/ -#[derive(Debug, Clone, PartialEq)] -pub struct Ident { - /// The source code location. - pub span: Span, - /// The identifier string. - pub string: EcoString, -} - -impl Ident { - /// Create a new identifier from a string checking that it is a valid. - pub fn new( - string: impl AsRef<str> + Into<EcoString>, - span: impl Into<Span>, - ) -> Option<Self> { - if is_ident(string.as_ref()) { - Some(Self { span: span.into(), string: string.into() }) - } else { - None - } - } - - /// Return a reference to the underlying string. - pub fn as_str(&self) -> &str { - self - } -} - -impl Deref for Ident { - type Target = str; - - fn deref(&self) -> &Self::Target { - self.string.as_str() - } -} - -impl AsRef<str> for Ident { - fn as_ref(&self) -> &str { - self - } -} - -impl Borrow<str> for Ident { - fn borrow(&self) -> &str { - self - } -} - -impl From<&Ident> for EcoString { - fn from(ident: &Ident) -> Self { - ident.string.clone() - } -} - -impl TypedNode for Ident { - fn cast_from(node: RedRef) -> Option<Self> { - match node.kind() { - NodeKind::Ident(i) => Some(Ident::new(i, node.span()).unwrap()), - _ => None, - } - } -} - -/// Whether a string is a valid identifier. -pub fn is_ident(string: &str) -> bool { - let mut chars = string.chars(); - chars - .next() - .map_or(false, |c| is_id_start(c) && chars.all(is_id_continue)) -} - -/// Whether a character can start an identifier. -pub fn is_id_start(c: char) -> bool { - c.is_xid_start() || c == '_' -} - -/// Whether a character can continue an identifier. -pub fn is_id_continue(c: char) -> bool { - c.is_xid_continue() || c == '_' || c == '-' -} |
