summaryrefslogtreecommitdiff
path: root/src/syntax
diff options
context:
space:
mode:
Diffstat (limited to 'src/syntax')
-rw-r--r--src/syntax/expr.rs53
-rw-r--r--src/syntax/ident.rs8
-rw-r--r--src/syntax/mod.rs4
-rw-r--r--src/syntax/node.rs9
-rw-r--r--src/syntax/span.rs14
-rw-r--r--src/syntax/token.rs2
6 files changed, 44 insertions, 46 deletions
diff --git a/src/syntax/expr.rs b/src/syntax/expr.rs
index 905ade04..94d07b07 100644
--- a/src/syntax/expr.rs
+++ b/src/syntax/expr.rs
@@ -1,5 +1,3 @@
-//! Expressions.
-
use super::*;
use crate::color::RgbaColor;
use crate::geom::Unit;
@@ -7,7 +5,7 @@ use crate::geom::Unit;
/// An expression.
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
- /// A literal: `true`, `1cm`, `"hi"`, `{_Hey!_}`.
+ /// A literal: `true`, `1cm`, `"hi"`.
Lit(Lit),
/// An invocation of a function: `[foo ...]`, `foo(...)`.
Call(ExprCall),
@@ -15,6 +13,12 @@ pub enum Expr {
Unary(ExprUnary),
/// A binary operation: `a + b`, `a / b`.
Binary(ExprBinary),
+ /// An array expression: `(1, "hi", 12cm)`.
+ Array(ExprArray),
+ /// A dictionary expression: `(color: #f79143, pattern: dashed)`.
+ Dict(ExprDict),
+ /// A content expression: `{*Hello* there!}`.
+ Content(ExprContent),
}
/// An invocation of a function: `[foo ...]`, `foo(...)`.
@@ -23,14 +27,14 @@ pub struct ExprCall {
/// The name of the function.
pub name: Spanned<Ident>,
/// The arguments to the function.
- pub args: Spanned<Arguments>,
+ pub args: Spanned<ExprArgs>,
}
/// The arguments to a function: `12, draw: false`.
///
/// In case of a bracketed invocation with a body, the body is _not_
/// included in the span for the sake of clearer error messages.
-pub type Arguments = Vec<Argument>;
+pub type ExprArgs = Vec<Argument>;
/// An argument to a function call: `12` or `draw: false`.
#[derive(Debug, Clone, PartialEq)]
@@ -41,6 +45,15 @@ pub enum Argument {
Named(Named),
}
+/// A pair of a name and an expression: `pattern: dashed`.
+#[derive(Debug, Clone, PartialEq)]
+pub struct Named {
+ /// The name: `pattern`.
+ pub name: Spanned<Ident>,
+ /// The right-hand side of the pair: `dashed`.
+ pub expr: Spanned<Expr>,
+}
+
/// A unary operation: `-x`.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprUnary {
@@ -81,6 +94,15 @@ pub enum BinOp {
Div,
}
+/// An array expression: `(1, "hi", 12cm)`.
+pub type ExprArray = SpanVec<Expr>;
+
+/// A dictionary expression: `(color: #f79143, pattern: dashed)`.
+pub type ExprDict = Vec<Named>;
+
+/// A content expression: `{*Hello* there!}`.
+pub type ExprContent = Tree;
+
/// A literal.
#[derive(Debug, Clone, PartialEq)]
pub enum Lit {
@@ -103,25 +125,4 @@ pub enum Lit {
Color(RgbaColor),
/// A string literal: `"hello!"`.
Str(String),
- /// An array literal: `(1, "hi", 12cm)`.
- Array(Array),
- /// A dictionary literal: `(color: #f79143, pattern: dashed)`.
- Dict(Dict),
- /// A content literal: `{*Hello* there!}`.
- Content(SynTree),
-}
-
-/// An array literal: `(1, "hi", 12cm)`.
-pub type Array = SpanVec<Expr>;
-
-/// A dictionary literal: `(color: #f79143, pattern: dashed)`.
-pub type Dict = Vec<Named>;
-
-/// A pair of a name and an expression: `pattern: dashed`.
-#[derive(Debug, Clone, PartialEq)]
-pub struct Named {
- /// The name: `pattern`.
- pub name: Spanned<Ident>,
- /// The right-hand side of the pair: `dashed`.
- pub expr: Spanned<Expr>,
}
diff --git a/src/syntax/ident.rs b/src/syntax/ident.rs
index 4f3668c0..3cb47c47 100644
--- a/src/syntax/ident.rs
+++ b/src/syntax/ident.rs
@@ -1,5 +1,3 @@
-//! Unicode identifiers.
-
use std::ops::Deref;
use unicode_xid::UnicodeXID;
@@ -44,7 +42,7 @@ impl Deref for Ident {
}
}
-/// Whether the string is a valid identifier.
+/// Whether a string is a valid identifier.
pub fn is_ident(string: &str) -> bool {
let mut chars = string.chars();
chars
@@ -52,12 +50,12 @@ pub fn is_ident(string: &str) -> bool {
.map_or(false, |c| is_id_start(c) && chars.all(is_id_continue))
}
-/// Whether the character can start an identifier.
+/// Whether a character can start an identifier.
pub fn is_id_start(c: char) -> bool {
c.is_xid_start() || c == '_'
}
-/// Whether the character can continue an identifier.
+/// Whether a character can continue an identifier.
pub fn is_id_continue(c: char) -> bool {
c.is_xid_continue() || c == '_' || c == '-'
}
diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs
index 970cd283..9c78fbc3 100644
--- a/src/syntax/mod.rs
+++ b/src/syntax/mod.rs
@@ -12,5 +12,5 @@ pub use node::*;
pub use span::*;
pub use token::*;
-/// A collection of nodes which form a tree together with the nodes' children.
-pub type SynTree = SpanVec<SynNode>;
+/// A collection of nodes which form a tree together with their children.
+pub type Tree = SpanVec<Node>;
diff --git a/src/syntax/node.rs b/src/syntax/node.rs
index cee810a2..d64d4fed 100644
--- a/src/syntax/node.rs
+++ b/src/syntax/node.rs
@@ -1,11 +1,8 @@
-//! Syntax tree nodes.
-
use super::*;
-/// A syntax node, which encompasses a single logical entity of parsed source
-/// code.
+/// A syntax node, encompassing a single logical entity of parsed source code.
#[derive(Debug, Clone, PartialEq)]
-pub enum SynNode {
+pub enum Node {
/// Plain text.
Text(String),
@@ -36,7 +33,7 @@ pub struct NodeHeading {
/// The section depth (numer of hashtags minus 1).
pub level: Spanned<u8>,
/// The contents of the heading.
- pub contents: SynTree,
+ pub contents: Tree,
}
/// A raw block with optional syntax highlighting: `` `raw` ``.
diff --git a/src/syntax/span.rs b/src/syntax/span.rs
index 20008354..3be770b8 100644
--- a/src/syntax/span.rs
+++ b/src/syntax/span.rs
@@ -1,5 +1,3 @@
-//! Mapping of values to the locations they originate from in source code.
-
use std::fmt::{self, Debug, Display, Formatter};
use std::ops::Range;
@@ -66,12 +64,18 @@ impl<T> Spanned<T> {
}
/// Map the value using a function while keeping the span.
- pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Spanned<U> {
+ pub fn map<F, U>(self, f: F) -> Spanned<U>
+ where
+ F: FnOnce(T) -> U,
+ {
Spanned { v: f(self.v), span: self.span }
}
/// Maps the span while keeping the value.
- pub fn map_span(mut self, f: impl FnOnce(Span) -> Span) -> Self {
+ pub fn map_span<F>(mut self, f: F) -> Self
+ where
+ F: FnOnce(Span) -> Span,
+ {
self.span = f(self.span);
self
}
@@ -102,7 +106,7 @@ impl<T: Debug> Debug for Spanned<T> {
}
}
-/// Locates a slice of source code.
+/// Bounds of a slice of source code.
#[derive(Copy, Clone, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Span {
diff --git a/src/syntax/token.rs b/src/syntax/token.rs
index b365d8d3..ef17fac2 100644
--- a/src/syntax/token.rs
+++ b/src/syntax/token.rs
@@ -1,5 +1,3 @@
-//! Token definition.
-
use crate::geom::Unit;
/// A minimal semantic entity of source code.