summaryrefslogtreecommitdiff
path: root/src/syntax/tree.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-09-03 19:56:37 +0200
committerGitHub <noreply@github.com>2020-09-03 19:56:37 +0200
commitcc5a74d5a99a26f46deef4f6ea20391e5327f0e6 (patch)
treeeb0084ea898a6e2e7c9034778654b02944048cbb /src/syntax/tree.rs
parent7f575dc09870848a1a4e5ba1f17a47cf83b60046 (diff)
parent7f8f225cb3cb44367d731c544f7ce1eebdb97dd5 (diff)
Merge pull request #16 from typst/split-up-parsing
Split up parser into multiple files 🧱
Diffstat (limited to 'src/syntax/tree.rs')
-rw-r--r--src/syntax/tree.rs28
1 files changed, 27 insertions, 1 deletions
diff --git a/src/syntax/tree.rs b/src/syntax/tree.rs
index 94dfc124..715db109 100644
--- a/src/syntax/tree.rs
+++ b/src/syntax/tree.rs
@@ -4,7 +4,7 @@ use std::fmt::{self, Debug, Formatter};
use super::decoration::Decoration;
use super::span::{SpanVec, Spanned};
-use super::Ident;
+use super::tokens::is_identifier;
use crate::color::RgbaColor;
use crate::compute::table::{SpannedEntry, Table};
use crate::compute::value::{TableValue, Value};
@@ -157,6 +157,32 @@ impl Debug for Expr {
}
}
+/// An identifier as defined by unicode with a few extra permissible characters.
+#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
+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()) {
+ Some(Self(ident.into()))
+ } else {
+ None
+ }
+ }
+
+ /// Return a reference to the underlying string.
+ pub fn as_str(&self) -> &str {
+ self.0.as_str()
+ }
+}
+
+impl Debug for Ident {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ write!(f, "`{}`", self.0)
+ }
+}
+
/// A table of expressions.
///
/// # Example