From 264a7dedd42e27cd9e604037640cf0594b2ec46b Mon Sep 17 00:00:00 2001 From: Laurenz Date: Fri, 19 Mar 2021 17:57:31 +0100 Subject: =?UTF-8?q?Scheduled=20maintenance=20=F0=9F=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - New naming scheme - TextNode instead of NodeText - CallExpr instead of ExprCall - ... - Less glob imports - Removes Value::Args variant - Removes prelude - Renames Layouted to Fragment - Moves font into env - Moves shaping into layout - Moves frame into separate module --- src/syntax/expr.rs | 78 ++++++++++++++++++++++++++--------------------------- src/syntax/ident.rs | 2 +- src/syntax/mod.rs | 2 ++ src/syntax/node.rs | 10 +++---- src/syntax/span.rs | 2 +- src/syntax/token.rs | 24 ++++++++--------- src/syntax/visit.rs | 34 +++++++++++------------ 7 files changed, 77 insertions(+), 75 deletions(-) (limited to 'src/syntax') diff --git a/src/syntax/expr.rs b/src/syntax/expr.rs index 5d10349b..2c631991 100644 --- a/src/syntax/expr.rs +++ b/src/syntax/expr.rs @@ -7,36 +7,36 @@ use crate::geom::{AngularUnit, LengthUnit}; /// An expression. #[derive(Debug, Clone, PartialEq)] pub enum Expr { - /// A literal. + /// A literal, like `11pt` or `"hi"`. Lit(Lit), /// An identifier: `left`. Ident(Ident), /// An array expression: `(1, "hi", 12cm)`. - Array(ExprArray), + Array(ArrayExpr), /// A dictionary expression: `(color: #f79143, pattern: dashed)`. - Dict(ExprDict), + Dict(DictExpr), /// A template expression: `[*Hi* there!]`. - Template(ExprTemplate), + Template(TemplateExpr), /// A grouped expression: `(1 + 2)`. - Group(ExprGroup), + Group(GroupExpr), /// A block expression: `{ let x = 1; x + 2 }`. - Block(ExprBlock), + Block(BlockExpr), /// A unary operation: `-x`. - Unary(ExprUnary), + Unary(UnaryExpr), /// A binary operation: `a + b`. - Binary(ExprBinary), + Binary(BinaryExpr), /// An invocation of a function: `f(x, y)`. - Call(ExprCall), + Call(CallExpr), /// A closure expression: `(x, y) => z`. - Closure(ExprClosure), + Closure(ClosureExpr), /// A let expression: `let x = 1`. - Let(ExprLet), - /// An if expression: `if x { y } else { z }`. - If(ExprIf), - /// A while expression: `while x { y }`. - While(ExprWhile), - /// A for expression: `for x in y { z }`. - For(ExprFor), + Let(LetExpr), + /// An if-else expression: `if x { y } else { z }`. + If(IfExpr), + /// A while loop expression: `while x { y }`. + While(WhileExpr), + /// A for loop expression: `for x in y { z }`. + For(ForExpr), } impl Expr { @@ -74,7 +74,7 @@ impl Expr { } } -/// A literal. +/// A literal, like `11pt` or `"hi"`. #[derive(Debug, Clone, PartialEq)] pub struct Lit { /// The source code location. @@ -111,7 +111,7 @@ pub enum LitKind { /// An array expression: `(1, "hi", 12cm)`. #[derive(Debug, Clone, PartialEq)] -pub struct ExprArray { +pub struct ArrayExpr { /// The source code location. pub span: Span, /// The entries of the array. @@ -120,7 +120,7 @@ pub struct ExprArray { /// A dictionary expression: `(color: #f79143, pattern: dashed)`. #[derive(Debug, Clone, PartialEq)] -pub struct ExprDict { +pub struct DictExpr { /// The source code location. pub span: Span, /// The named dictionary entries. @@ -145,7 +145,7 @@ impl Named { /// A template expression: `[*Hi* there!]`. #[derive(Debug, Clone, PartialEq)] -pub struct ExprTemplate { +pub struct TemplateExpr { /// The source code location. pub span: Span, /// The contents of the template. @@ -154,7 +154,7 @@ pub struct ExprTemplate { /// A grouped expression: `(1 + 2)`. #[derive(Debug, Clone, PartialEq)] -pub struct ExprGroup { +pub struct GroupExpr { /// The source code location. pub span: Span, /// The wrapped expression. @@ -163,7 +163,7 @@ pub struct ExprGroup { /// A block expression: `{ let x = 1; x + 2 }`. #[derive(Debug, Clone, PartialEq)] -pub struct ExprBlock { +pub struct BlockExpr { /// The source code location. pub span: Span, /// The list of expressions contained in the block. @@ -174,7 +174,7 @@ pub struct ExprBlock { /// A unary operation: `-x`. #[derive(Debug, Clone, PartialEq)] -pub struct ExprUnary { +pub struct UnaryExpr { /// The source code location. pub span: Span, /// The operator: `-`. @@ -225,7 +225,7 @@ impl UnOp { /// A binary operation: `a + b`. #[derive(Debug, Clone, PartialEq)] -pub struct ExprBinary { +pub struct BinaryExpr { /// The source code location. pub span: Span, /// The left-hand side of the operation: `a`. @@ -374,13 +374,13 @@ pub enum Associativity { /// An invocation of a function: `foo(...)`. #[derive(Debug, Clone, PartialEq)] -pub struct ExprCall { +pub struct CallExpr { /// The source code location. pub span: Span, /// The callee of the function. pub callee: Box, /// The arguments to the function. - pub args: ExprArgs, + pub args: CallArgs, } /// The arguments to a function: `12, draw: false`. @@ -388,23 +388,23 @@ pub struct ExprCall { /// In case of a bracketed invocation with a body, the body is _not_ /// included in the span for the sake of clearer error messages. #[derive(Debug, Clone, PartialEq)] -pub struct ExprArgs { +pub struct CallArgs { /// The source code location. pub span: Span, /// The positional and named arguments. - pub items: Vec, + pub items: Vec, } /// An argument to a function call: `12` or `draw: false`. #[derive(Debug, Clone, PartialEq)] -pub enum ExprArg { +pub enum CallArg { /// A positional argument. Pos(Expr), /// A named argument. Named(Named), } -impl ExprArg { +impl CallArg { /// The source code location. pub fn span(&self) -> Span { match self { @@ -416,7 +416,7 @@ impl ExprArg { /// A closure expression: `(x, y) => z`. #[derive(Debug, Clone, PartialEq)] -pub struct ExprClosure { +pub struct ClosureExpr { /// The source code location. pub span: Span, /// The name of the closure. @@ -431,7 +431,7 @@ pub struct ExprClosure { /// A let expression: `let x = 1`. #[derive(Debug, Clone, PartialEq)] -pub struct ExprLet { +pub struct LetExpr { /// The source code location. pub span: Span, /// The binding to assign to. @@ -440,9 +440,9 @@ pub struct ExprLet { pub init: Option>, } -/// An if expression: `if x { y } else { z }`. +/// An if-else expression: `if x { y } else { z }`. #[derive(Debug, Clone, PartialEq)] -pub struct ExprIf { +pub struct IfExpr { /// The source code location. pub span: Span, /// The condition which selects the body to evaluate. @@ -453,9 +453,9 @@ pub struct ExprIf { pub else_body: Option>, } -/// A while expression: `while x { y }`. +/// A while loop expression: `while x { y }`. #[derive(Debug, Clone, PartialEq)] -pub struct ExprWhile { +pub struct WhileExpr { /// The source code location. pub span: Span, /// The condition which selects whether to evaluate the body. @@ -464,9 +464,9 @@ pub struct ExprWhile { pub body: Box, } -/// A for expression: `for x in y { z }`. +/// A for loop expression: `for x in y { z }`. #[derive(Debug, Clone, PartialEq)] -pub struct ExprFor { +pub struct ForExpr { /// The source code location. pub span: Span, /// The pattern to assign to. diff --git a/src/syntax/ident.rs b/src/syntax/ident.rs index 26c46b98..a46ad232 100644 --- a/src/syntax/ident.rs +++ b/src/syntax/ident.rs @@ -4,7 +4,7 @@ use unicode_xid::UnicodeXID; use super::Span; -/// An Unicode identifier with a few extra permissible characters. +/// An unicode identifier with a few extra permissible characters. /// /// In addition to what is specified in the [Unicode Standard][uax31], we allow: /// - `_` as a starting character, diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index 09c445b0..9426090c 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -14,4 +14,6 @@ pub use span::*; pub use token::*; /// The abstract syntax tree. +/// +/// This type can represent a full parsed document. pub type Tree = Vec; diff --git a/src/syntax/node.rs b/src/syntax/node.rs index 19fdfa50..c94ee5b0 100644 --- a/src/syntax/node.rs +++ b/src/syntax/node.rs @@ -16,23 +16,23 @@ pub enum Node { /// Plain text. Text(String), /// A section heading. - Heading(NodeHeading), + Heading(HeadingNode), /// An optionally syntax-highlighted raw block. - Raw(NodeRaw), + Raw(RawNode), /// An expression. Expr(Expr), } /// A section heading: `= Introduction`. #[derive(Debug, Clone, PartialEq)] -pub struct NodeHeading { +pub struct HeadingNode { /// The section depth (numer of equals signs minus 1). pub level: usize, /// The contents of the heading. pub contents: Tree, } -/// A raw block with optional syntax highlighting: `` `raw` ``. +/// A raw block with optional syntax highlighting: `` `...` ``. /// /// Raw blocks start with 1 or 3+ backticks and end with the same number of /// backticks. @@ -96,7 +96,7 @@ pub struct NodeHeading { /// Note that with these rules you can always force leading or trailing /// whitespace simply by adding more spaces. #[derive(Debug, Clone, PartialEq)] -pub struct NodeRaw { +pub struct RawNode { /// An optional identifier specifying the language to syntax-highlight in. pub lang: Option, /// The lines of raw text, determined as the raw string between the diff --git a/src/syntax/span.rs b/src/syntax/span.rs index d939ed28..d3683c1a 100644 --- a/src/syntax/span.rs +++ b/src/syntax/span.rs @@ -32,7 +32,7 @@ impl Spanned { Spanned { v: &self.v, span: self.span } } - /// Map the value using a function while keeping the span. + /// Map the value using a function keeping the span. pub fn map(self, f: F) -> Spanned where F: FnOnce(T) -> U, diff --git a/src/syntax/token.rs b/src/syntax/token.rs index e57620af..832c923d 100644 --- a/src/syntax/token.rs +++ b/src/syntax/token.rs @@ -99,13 +99,13 @@ pub enum Token<'s> { Text(&'s str), /// An arbitrary number of backticks followed by inner contents, terminated /// with the same number of backticks: `` `...` ``. - Raw(TokenRaw<'s>), + Raw(RawToken<'s>), /// One or two dollar signs followed by inner contents, terminated with the /// same number of dollar signs. - Math(TokenMath<'s>), + Math(MathToken<'s>), /// A slash and the letter "u" followed by a hexadecimal unicode entity /// enclosed in curly braces: `\u{1F5FA}`. - UnicodeEscape(TokenUnicodeEscape<'s>), + UnicodeEscape(UnicodeEscapeToken<'s>), /// An identifier: `center`. Ident(&'s str), /// A boolean: `true`, `false`. @@ -126,7 +126,7 @@ pub enum Token<'s> { /// A color value: `#20d82a`. Color(RgbaColor), /// A quoted string: `"..."`. - Str(TokenStr<'s>), + Str(StrToken<'s>), /// Two slashes followed by inner contents, terminated with a newline: /// `//\n`. LineComment(&'s str), @@ -139,9 +139,9 @@ pub enum Token<'s> { Invalid(&'s str), } -/// A quoted string: `"..."`. +/// A quoted string token: `"..."`. #[derive(Debug, Copy, Clone, PartialEq)] -pub struct TokenStr<'s> { +pub struct StrToken<'s> { /// The string inside the quotes. /// /// _Note_: If the string contains escape sequences these are not yet @@ -152,9 +152,9 @@ pub struct TokenStr<'s> { pub terminated: bool, } -/// A raw block: `` `...` ``. +/// A raw block token: `` `...` ``. #[derive(Debug, Copy, Clone, PartialEq)] -pub struct TokenRaw<'s> { +pub struct RawToken<'s> { /// The raw text between the backticks. pub text: &'s str, /// The number of opening backticks. @@ -163,9 +163,9 @@ pub struct TokenRaw<'s> { pub terminated: bool, } -/// A math formula: `$2pi + x$`, `$$f'(x) = x^2$$`. +/// A math formula token: `$2pi + x$` or `$[f'(x) = x^2]$`. #[derive(Debug, Copy, Clone, PartialEq)] -pub struct TokenMath<'s> { +pub struct MathToken<'s> { /// The formula between the dollars. pub formula: &'s str, /// Whether the formula is display-level, that is, it is surrounded by @@ -175,9 +175,9 @@ pub struct TokenMath<'s> { pub terminated: bool, } -/// A unicode escape sequence: `\u{1F5FA}`. +/// A unicode escape sequence token: `\u{1F5FA}`. #[derive(Debug, Copy, Clone, PartialEq)] -pub struct TokenUnicodeEscape<'s> { +pub struct UnicodeEscapeToken<'s> { /// The escape sequence between the braces. pub sequence: &'s str, /// Whether the closing brace was present. diff --git a/src/syntax/visit.rs b/src/syntax/visit.rs index 15613233..04546c5d 100644 --- a/src/syntax/visit.rs +++ b/src/syntax/visit.rs @@ -82,29 +82,29 @@ visit! { } } - fn visit_array(v, node: &ExprArray) { + fn visit_array(v, node: &ArrayExpr) { for expr in &node.items { v.visit_expr(&expr); } } - fn visit_dict(v, node: &ExprDict) { + fn visit_dict(v, node: &DictExpr) { for named in &node.items { v.visit_expr(&named.expr); } } - fn visit_template(v, node: &ExprTemplate) { + fn visit_template(v, node: &TemplateExpr) { v.visit_enter(); v.visit_tree(&node.tree); v.visit_exit(); } - fn visit_group(v, node: &ExprGroup) { + fn visit_group(v, node: &GroupExpr) { v.visit_expr(&node.expr); } - fn visit_block(v, node: &ExprBlock) { + fn visit_block(v, node: &BlockExpr) { if node.scoping { v.visit_enter(); } @@ -116,48 +116,48 @@ visit! { } } - fn visit_binary(v, node: &ExprBinary) { + fn visit_binary(v, node: &BinaryExpr) { v.visit_expr(&node.lhs); v.visit_expr(&node.rhs); } - fn visit_unary(v, node: &ExprUnary) { + fn visit_unary(v, node: &UnaryExpr) { v.visit_expr(&node.expr); } - fn visit_call(v, node: &ExprCall) { + fn visit_call(v, node: &CallExpr) { v.visit_expr(&node.callee); v.visit_args(&node.args); } - fn visit_closure(v, node: &ExprClosure) { + fn visit_closure(v, node: &ClosureExpr) { for param in node.params.iter() { v.visit_binding(param); } v.visit_expr(&node.body); } - fn visit_args(v, node: &ExprArgs) { + fn visit_args(v, node: &CallArgs) { for arg in &node.items { v.visit_arg(arg); } } - fn visit_arg(v, node: &ExprArg) { + fn visit_arg(v, node: &CallArg) { match node { - ExprArg::Pos(expr) => v.visit_expr(&expr), - ExprArg::Named(named) => v.visit_expr(&named.expr), + CallArg::Pos(expr) => v.visit_expr(&expr), + CallArg::Named(named) => v.visit_expr(&named.expr), } } - fn visit_let(v, node: &ExprLet) { + fn visit_let(v, node: &LetExpr) { v.visit_binding(&node.binding); if let Some(init) = &node.init { v.visit_expr(&init); } } - fn visit_if(v, node: &ExprIf) { + fn visit_if(v, node: &IfExpr) { v.visit_expr(&node.condition); v.visit_expr(&node.if_body); if let Some(body) = &node.else_body { @@ -165,12 +165,12 @@ visit! { } } - fn visit_while(v, node: &ExprWhile) { + fn visit_while(v, node: &WhileExpr) { v.visit_expr(&node.condition); v.visit_expr(&node.body); } - fn visit_for(v, node: &ExprFor) { + fn visit_for(v, node: &ForExpr) { match &node.pattern { ForPattern::Value(value) => v.visit_binding(value), ForPattern::KeyValue(key, value) => { -- cgit v1.2.3