summaryrefslogtreecommitdiff
path: root/src/syntax/tree.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-10-01 11:32:48 +0200
committerLaurenz <laurmaedje@gmail.com>2020-10-01 11:32:48 +0200
commit885bfec5d7524845b41e180fadc9cf5626157eec (patch)
treef798e03d101d568a110a5c56f4a9bfa2be892928 /src/syntax/tree.rs
parent16f0bd430e0864a3bbd0139803e476be413cb3cb (diff)
Make syntax not depend on parse 📩
This would make it possible to split them into two separate crates.
Diffstat (limited to 'src/syntax/tree.rs')
-rw-r--r--src/syntax/tree.rs43
1 files changed, 29 insertions, 14 deletions
diff --git a/src/syntax/tree.rs b/src/syntax/tree.rs
index bfbb3706..ee53d476 100644
--- a/src/syntax/tree.rs
+++ b/src/syntax/tree.rs
@@ -1,6 +1,7 @@
//! The syntax tree.
use std::fmt::{self, Debug, Formatter};
+use std::ops::Deref;
use unicode_xid::UnicodeXID;
@@ -234,7 +235,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 Self::is_ident(ident.as_ref()) {
+ if is_ident(ident.as_ref()) {
Some(Self(ident.into()))
} else {
None
@@ -243,21 +244,21 @@ impl Ident {
/// Return a reference to the underlying string.
pub fn as_str(&self) -> &str {
- self.0.as_str()
+ self
}
+}
- /// Whether the string is a valid identifier.
- pub fn is_ident(string: &str) -> bool {
- fn is_ok(c: char) -> bool {
- c == '-' || c == '_'
- }
+impl AsRef<str> for Ident {
+ fn as_ref(&self) -> &str {
+ self
+ }
+}
- 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 Deref for Ident {
+ type Target = str;
+
+ fn deref(&self) -> &Self::Target {
+ self.0.as_str()
}
}
@@ -267,6 +268,20 @@ impl Debug for Ident {
}
}
+/// 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
+ }
+}
+
/// A table of expressions.
///
/// # Example
@@ -307,7 +322,7 @@ pub struct CallExpr {
impl CallExpr {
/// Evaluate the call expression to a value.
pub async fn eval(&self, ctx: &LayoutContext<'_>, f: &mut Feedback) -> Value {
- let name = self.name.v.as_str();
+ let name = &self.name.v;
let span = self.name.span;
let args = self.args.eval(ctx, f).await;