From 5de791d9e6a1006dc6a017ec8e20a1c70a91a780 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 15 Sep 2021 12:43:57 +0200 Subject: Rename `SyntaxTree` to `Markup` Also `SyntaxNode` -> `MarkupNode`. --- src/syntax/expr.rs | 5 ++-- src/syntax/markup.rs | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/syntax/mod.rs | 11 ++------ src/syntax/node.rs | 74 ------------------------------------------------- src/syntax/pretty.rs | 6 ++-- src/syntax/visit.rs | 32 ++++++++++----------- 6 files changed, 102 insertions(+), 104 deletions(-) create mode 100644 src/syntax/markup.rs delete mode 100644 src/syntax/node.rs (limited to 'src/syntax') diff --git a/src/syntax/expr.rs b/src/syntax/expr.rs index aac23a6f..11a85404 100644 --- a/src/syntax/expr.rs +++ b/src/syntax/expr.rs @@ -1,7 +1,8 @@ use std::rc::Rc; -use super::*; +use super::{Ident, Markup, Span, Token}; use crate::geom::{AngularUnit, LengthUnit}; +use crate::util::EcoString; /// An expression. #[derive(Debug, Clone, PartialEq)] @@ -170,7 +171,7 @@ pub struct TemplateExpr { /// The source code location. pub span: Span, /// The contents of the template. - pub tree: SyntaxTree, + pub body: Markup, } /// A grouped expression: `(1 + 2)`. diff --git a/src/syntax/markup.rs b/src/syntax/markup.rs new file mode 100644 index 00000000..09a37116 --- /dev/null +++ b/src/syntax/markup.rs @@ -0,0 +1,78 @@ +use super::{Expr, Ident, Span}; +use crate::util::EcoString; + +/// The syntactical root capable of representing a full parsed document. +pub type Markup = Vec; + +/// A single piece of markup. +#[derive(Debug, Clone, PartialEq)] +pub enum MarkupNode { + /// Whitespace containing less than two newlines. + Space, + /// A forced line break: `\`. + Linebreak(Span), + /// A paragraph break: Two or more newlines. + Parbreak(Span), + /// Strong text was enabled / disabled: `*`. + Strong(Span), + /// Emphasized text was enabled / disabled: `_`. + Emph(Span), + /// Plain text. + Text(EcoString), + /// A raw block with optional syntax highlighting: `` `...` ``. + Raw(Box), + /// A section heading: `= Introduction`. + Heading(Box), + /// An item in an unordered list: `- ...`. + List(Box), + /// An item in an enumeration (ordered list): `1. ...`. + Enum(Box), + /// An expression. + Expr(Expr), +} + +/// A raw block with optional syntax highlighting: `` `...` ``. +#[derive(Debug, Clone, PartialEq)] +pub struct RawNode { + /// The source code location. + pub span: Span, + /// An optional identifier specifying the language to syntax-highlight in. + pub lang: Option, + /// The raw text, determined as the raw string between the backticks trimmed + /// according to the above rules. + pub text: EcoString, + /// Whether the element is block-level, that is, it has 3+ backticks + /// and contains at least one newline. + pub block: bool, +} + +/// A section heading: `= Introduction`. +#[derive(Debug, Clone, PartialEq)] +pub struct HeadingNode { + /// The source code location. + pub span: Span, + /// The section depth (numer of equals signs). + pub level: usize, + /// The contents of the heading. + pub body: Markup, +} + +/// An item in an unordered list: `- ...`. +#[derive(Debug, Clone, PartialEq)] +pub struct ListNode { + /// The source code location. + pub span: Span, + /// The contents of the list item. + pub body: Markup, +} + +/// An item in an enumeration (ordered list): `1. ...`. +#[derive(Debug, Clone, PartialEq)] +pub struct EnumNode { + /// The source code location. + pub span: Span, + /// The number, if any. + pub number: Option, + /// The contents of the list item. + pub body: Markup, +} diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index f07e3554..8dbb108d 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -2,7 +2,7 @@ mod expr; mod ident; -mod node; +mod markup; mod pretty; mod span; mod token; @@ -10,14 +10,7 @@ pub mod visit; pub use expr::*; pub use ident::*; -pub use node::*; +pub use markup::*; pub use pretty::*; pub use span::*; pub use token::*; - -use crate::util::EcoString; - -/// The abstract syntax tree. -/// -/// This type can represent a full parsed document. -pub type SyntaxTree = Vec; diff --git a/src/syntax/node.rs b/src/syntax/node.rs deleted file mode 100644 index 875d32f5..00000000 --- a/src/syntax/node.rs +++ /dev/null @@ -1,74 +0,0 @@ -use super::*; - -/// A syntax node, encompassing a single logical entity of parsed source code. -#[derive(Debug, Clone, PartialEq)] -pub enum SyntaxNode { - /// Whitespace containing less than two newlines. - Space, - /// A forced line break: `\`. - Linebreak(Span), - /// A paragraph break: Two or more newlines. - Parbreak(Span), - /// Strong text was enabled / disabled: `*`. - Strong(Span), - /// Emphasized text was enabled / disabled: `_`. - Emph(Span), - /// Plain text. - Text(EcoString), - /// A raw block with optional syntax highlighting: `` `...` ``. - Raw(Box), - /// A section heading: `= Introduction`. - Heading(Box), - /// An item in an unordered list: `- ...`. - List(Box), - /// An item in an enumeration (ordered list): `1. ...`. - Enum(Box), - /// An expression. - Expr(Expr), -} - -/// A raw block with optional syntax highlighting: `` `...` ``. -#[derive(Debug, Clone, PartialEq)] -pub struct RawNode { - /// The source code location. - pub span: Span, - /// An optional identifier specifying the language to syntax-highlight in. - pub lang: Option, - /// The raw text, determined as the raw string between the backticks trimmed - /// according to the above rules. - pub text: EcoString, - /// Whether the element is block-level, that is, it has 3+ backticks - /// and contains at least one newline. - pub block: bool, -} - -/// A section heading: `= Introduction`. -#[derive(Debug, Clone, PartialEq)] -pub struct HeadingNode { - /// The source code location. - pub span: Span, - /// The section depth (numer of equals signs). - pub level: usize, - /// The contents of the heading. - pub body: SyntaxTree, -} - -/// An item in an unordered list: `- ...`. -#[derive(Debug, Clone, PartialEq)] -pub struct ListNode { - /// The source code location. - pub span: Span, - /// The contents of the list item. - pub body: SyntaxTree, -} - -/// An item in an enumeration (ordered list): `1. ...`. -#[derive(Debug, Clone, PartialEq)] -pub struct EnumNode { - /// The source code location. - pub span: Span, - /// The number, if any. - pub number: Option, - /// The contents of the list item. - pub body: SyntaxTree, -} diff --git a/src/syntax/pretty.rs b/src/syntax/pretty.rs index 39f0676b..5e4d3ad2 100644 --- a/src/syntax/pretty.rs +++ b/src/syntax/pretty.rs @@ -75,7 +75,7 @@ impl Write for Printer { } } -impl Pretty for SyntaxTree { +impl Pretty for Markup { fn pretty(&self, p: &mut Printer) { for node in self { node.pretty(p); @@ -83,7 +83,7 @@ impl Pretty for SyntaxTree { } } -impl Pretty for SyntaxNode { +impl Pretty for MarkupNode { fn pretty(&self, p: &mut Printer) { match self { // TODO: Handle escaping. @@ -266,7 +266,7 @@ impl Pretty for Named { impl Pretty for TemplateExpr { fn pretty(&self, p: &mut Printer) { p.push('['); - self.tree.pretty(p); + self.body.pretty(p); p.push(']'); } } diff --git a/src/syntax/visit.rs b/src/syntax/visit.rs index fe270ac5..d689209b 100644 --- a/src/syntax/visit.rs +++ b/src/syntax/visit.rs @@ -1,6 +1,6 @@ //! Mutable and immutable syntax tree traversal. -use crate::syntax::*; +use super::*; /// Implement the immutable and the mutable visitor version. macro_rules! impl_visitors { @@ -78,25 +78,25 @@ macro_rules! impl_visitor { } impl_visitors! { - visit_tree(v, tree: SyntaxTree) { - for node in tree { + visit_tree(v, markup: Markup) { + for node in markup { v.visit_node(node); } } - visit_node(v, node: SyntaxNode) { + visit_node(v, node: MarkupNode) { match node { - SyntaxNode::Space => {} - SyntaxNode::Linebreak(_) => {} - SyntaxNode::Parbreak(_) => {} - SyntaxNode::Strong(_) => {} - SyntaxNode::Emph(_) => {} - SyntaxNode::Text(_) => {} - SyntaxNode::Raw(_) => {} - SyntaxNode::Heading(n) => v.visit_heading(n), - SyntaxNode::List(n) => v.visit_list(n), - SyntaxNode::Enum(n) => v.visit_enum(n), - SyntaxNode::Expr(n) => v.visit_expr(n), + MarkupNode::Space => {} + MarkupNode::Linebreak(_) => {} + MarkupNode::Parbreak(_) => {} + MarkupNode::Strong(_) => {} + MarkupNode::Emph(_) => {} + MarkupNode::Text(_) => {} + MarkupNode::Raw(_) => {} + MarkupNode::Heading(n) => v.visit_heading(n), + MarkupNode::List(n) => v.visit_list(n), + MarkupNode::Enum(n) => v.visit_enum(n), + MarkupNode::Expr(n) => v.visit_expr(n), } } @@ -149,7 +149,7 @@ impl_visitors! { visit_template(v, template: TemplateExpr) { v.visit_enter(); - v.visit_tree(r!(template.tree)); + v.visit_tree(r!(template.body)); v.visit_exit(); } -- cgit v1.2.3