summaryrefslogtreecommitdiff
path: root/src/syntax/ident.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-11-08 13:08:15 +0100
committerGitHub <noreply@github.com>2021-11-08 13:08:15 +0100
commitc6f8ad35f45248f1fd36ee00195966f1629c6ca7 (patch)
tree51faa3f6bbc56f75636823adeea135ed76e1b33b /src/syntax/ident.rs
parentea6ee3f667e922ed2f21b08719a45d2395787932 (diff)
parent38c5c362419c5eee7a4fdc0b43d3a9dfb339a6d2 (diff)
Merge pull request #46 from typst/parser-ng
Next Generation Parser
Diffstat (limited to 'src/syntax/ident.rs')
-rw-r--r--src/syntax/ident.rs85
1 files changed, 0 insertions, 85 deletions
diff --git a/src/syntax/ident.rs b/src/syntax/ident.rs
deleted file mode 100644
index 398e2ff9..00000000
--- a/src/syntax/ident.rs
+++ /dev/null
@@ -1,85 +0,0 @@
-use std::borrow::Borrow;
-use std::ops::Deref;
-
-use unicode_xid::UnicodeXID;
-
-use super::Span;
-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()
- }
-}
-
-/// 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 == '-'
-}