diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-08-16 22:14:27 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-08-16 22:39:21 +0200 |
| commit | 30f16bbf6431ca0c174ca0a1abaa6a13ef50ab06 (patch) | |
| tree | f5a5c0adad15840ebe24b39e77ff467862067c91 /src/syntax/mod.rs | |
| parent | 9f6137d8a829fe8f34554623495fa620252a0184 (diff) | |
Add Value type and replace dyn-nodes with call-exprs 🏗
- In addition to syntax trees there are now `Value`s, which syntax trees can be evaluated into (e.g. the tree is `5+5` and the value is `10`)
- Parsing is completely pure, function calls are not parsed into nodes, but into simple call expressions, which are resolved later
- Functions aren't dynamic nodes anymore, but simply functions which receive their arguments as a table and the layouting context
- Functions may return any `Value`
- Layouting is powered by functions which return the new `Commands` value, which informs the layouting engine what to do
- When a function returns a non-`Commands` value, the layouter simply dumps the value into the document in monospace
Diffstat (limited to 'src/syntax/mod.rs')
| -rw-r--r-- | src/syntax/mod.rs | 47 |
1 files changed, 30 insertions, 17 deletions
diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index fdc105a3..596291a5 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -1,17 +1,44 @@ //! Syntax trees, parsing and tokenization. pub mod decoration; -pub mod expr; pub mod parsing; -pub mod scope; pub mod span; pub mod tokens; pub mod tree; +use std::fmt::{self, Debug, Formatter}; +use tokens::is_identifier; + +/// 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) + } +} + #[cfg(test)] mod tests { use std::fmt::Debug; - use crate::func::prelude::*; + use crate::prelude::*; use super::span; /// Assert that expected and found are equal, printing both and panicking @@ -49,18 +76,4 @@ mod tests { Spanned::zero(t) } } - - pub fn debug_func(call: FuncCall, _: &ParseState) -> Pass<SyntaxNode> { - Pass::node(DebugNode(call), Feedback::new()) - } - - #[derive(Debug, Clone, PartialEq)] - pub struct DebugNode(pub FuncCall); - - #[async_trait(?Send)] - impl Layout for DebugNode { - async fn layout<'a>(&'a self, _: LayoutContext<'_>) -> Pass<Commands<'a>> { - unimplemented!() - } - } } |
