summaryrefslogtreecommitdiff
path: root/src/syntax/tree.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-10-01 01:38:18 +0200
committerLaurenz <laurmaedje@gmail.com>2020-10-01 01:38:18 +0200
commit4b9bc660281b2740c310bd9439493064017c9814 (patch)
tree609a44b34871c8582dffaf27cbb6636f1a869313 /src/syntax/tree.rs
parent38607b8bea1ede7a124c8fe384d7efca76f9f011 (diff)
Implement low-level char parser 🥜
Diffstat (limited to 'src/syntax/tree.rs')
-rw-r--r--src/syntax/tree.rs19
1 files changed, 17 insertions, 2 deletions
diff --git a/src/syntax/tree.rs b/src/syntax/tree.rs
index 51a7937a..bfbb3706 100644
--- a/src/syntax/tree.rs
+++ b/src/syntax/tree.rs
@@ -2,6 +2,8 @@
use std::fmt::{self, Debug, Formatter};
+use unicode_xid::UnicodeXID;
+
use super::span::{SpanVec, SpanWith, Spanned};
use super::Decoration;
use crate::color::RgbaColor;
@@ -9,7 +11,6 @@ use crate::compute::table::{SpannedEntry, Table};
use crate::compute::value::{TableValue, Value};
use crate::layout::LayoutContext;
use crate::length::Length;
-use crate::parse::is_identifier;
use crate::{DynFuture, Feedback};
/// A collection of nodes which form a tree together with the nodes' children.
@@ -233,7 +234,7 @@ pub struct Ident(pub String);
impl Ident {
/// Create a new identifier from a string checking that it is a valid.
pub fn new(ident: impl AsRef<str> + Into<String>) -> Option<Self> {
- if is_identifier(ident.as_ref()) {
+ if Self::is_ident(ident.as_ref()) {
Some(Self(ident.into()))
} else {
None
@@ -244,6 +245,20 @@ impl Ident {
pub fn as_str(&self) -> &str {
self.0.as_str()
}
+
+ /// Whether the string is a valid identifier.
+ pub fn is_ident(string: &str) -> bool {
+ fn is_ok(c: char) -> bool {
+ c == '-' || c == '_'
+ }
+
+ let mut chars = string.chars();
+ if matches!(chars.next(), Some(c) if c.is_xid_start() || is_ok(c)) {
+ chars.all(|c| c.is_xid_continue() || is_ok(c))
+ } else {
+ false
+ }
+ }
}
impl Debug for Ident {