summaryrefslogtreecommitdiff
path: root/src/syntax
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-08-13 17:44:21 +0200
committerLaurenz <laurmaedje@gmail.com>2021-08-13 18:17:24 +0200
commitf6814b7732931ab0f1bfc4028122522f53d6af0c (patch)
tree7be18b17ebb8e61bd2590d544fa8deb8e5315274 /src/syntax
parent6a3385e4e77ce7672bcc80941bf02c8218f344a2 (diff)
Optimize memory sizes
Diffstat (limited to 'src/syntax')
-rw-r--r--src/syntax/expr.rs180
-rw-r--r--src/syntax/node.rs12
-rw-r--r--src/syntax/token.rs2
-rw-r--r--src/syntax/visit.rs13
4 files changed, 107 insertions, 100 deletions
diff --git a/src/syntax/expr.rs b/src/syntax/expr.rs
index 5b751280..3a71bedd 100644
--- a/src/syntax/expr.rs
+++ b/src/syntax/expr.rs
@@ -6,6 +6,87 @@ use crate::geom::{AngularUnit, LengthUnit};
/// An expression.
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
+ /// An identifier: `left`.
+ Ident(Box<Ident>),
+ /// A literal: `1`, `true`, ...
+ Lit(Box<Lit>),
+ /// An array expression: `(1, "hi", 12cm)`.
+ Array(Box<ArrayExpr>),
+ /// A dictionary expression: `(thickness: 3pt, pattern: dashed)`.
+ Dict(Box<DictExpr>),
+ /// A template expression: `[*Hi* there!]`.
+ Template(Box<TemplateExpr>),
+ /// A grouped expression: `(1 + 2)`.
+ Group(Box<GroupExpr>),
+ /// A block expression: `{ let x = 1; x + 2 }`.
+ Block(Box<BlockExpr>),
+ /// A unary operation: `-x`.
+ Unary(Box<UnaryExpr>),
+ /// A binary operation: `a + b`.
+ Binary(Box<BinaryExpr>),
+ /// An invocation of a function: `f(x, y)`.
+ Call(Box<CallExpr>),
+ /// A closure expression: `(x, y) => z`.
+ Closure(Box<ClosureExpr>),
+ /// A with expression: `f with (x, y: 1)`.
+ With(Box<WithExpr>),
+ /// A let expression: `let x = 1`.
+ Let(Box<LetExpr>),
+ /// An if-else expression: `if x { y } else { z }`.
+ If(Box<IfExpr>),
+ /// A while loop expression: `while x { y }`.
+ While(Box<WhileExpr>),
+ /// A for loop expression: `for x in y { z }`.
+ For(Box<ForExpr>),
+ /// An import expression: `import a, b, c from "utils.typ"`.
+ Import(Box<ImportExpr>),
+ /// An include expression: `include "chapter1.typ"`.
+ Include(Box<IncludeExpr>),
+}
+
+impl Expr {
+ /// The source code location.
+ pub fn span(&self) -> Span {
+ match self {
+ Self::Ident(v) => v.span,
+ Self::Lit(v) => v.span(),
+ Self::Array(v) => v.span,
+ Self::Dict(v) => v.span,
+ Self::Template(v) => v.span,
+ Self::Group(v) => v.span,
+ Self::Block(v) => v.span,
+ Self::Unary(v) => v.span,
+ Self::Binary(v) => v.span,
+ Self::Call(v) => v.span,
+ Self::Closure(v) => v.span,
+ Self::With(v) => v.span,
+ Self::Let(v) => v.span,
+ Self::If(v) => v.span,
+ Self::While(v) => v.span,
+ Self::For(v) => v.span,
+ Self::Import(v) => v.span,
+ Self::Include(v) => v.span,
+ }
+ }
+
+ /// Whether the expression can be shortened in markup with a hashtag.
+ pub fn has_short_form(&self) -> bool {
+ matches!(self,
+ Self::Ident(_)
+ | Self::Call(_)
+ | Self::Let(_)
+ | Self::If(_)
+ | Self::While(_)
+ | Self::For(_)
+ | Self::Import(_)
+ | Self::Include(_)
+ )
+ }
+}
+
+/// A literal: `1`, `true`, ...
+#[derive(Debug, Clone, PartialEq)]
+pub enum Lit {
/// The none literal: `none`.
None(Span),
/// The auto literal: `auto`.
@@ -29,43 +110,9 @@ pub enum Expr {
Fractional(Span, f64),
/// A string literal: `"hello!"`.
Str(Span, EcoString),
- /// An identifier: `left`.
- Ident(Ident),
- /// An array expression: `(1, "hi", 12cm)`.
- Array(ArrayExpr),
- /// A dictionary expression: `(thickness: 3pt, pattern: dashed)`.
- Dict(DictExpr),
- /// A template expression: `[*Hi* there!]`.
- Template(TemplateExpr),
- /// A grouped expression: `(1 + 2)`.
- Group(GroupExpr),
- /// A block expression: `{ let x = 1; x + 2 }`.
- Block(BlockExpr),
- /// A unary operation: `-x`.
- Unary(UnaryExpr),
- /// A binary operation: `a + b`.
- Binary(BinaryExpr),
- /// An invocation of a function: `f(x, y)`.
- Call(CallExpr),
- /// A closure expression: `(x, y) => z`.
- Closure(ClosureExpr),
- /// A with expression: `f with (x, y: 1)`.
- With(WithExpr),
- /// A let expression: `let x = 1`.
- 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),
- /// An import expression: `import a, b, c from "utils.typ"`.
- Import(ImportExpr),
- /// An include expression: `include "chapter1.typ"`.
- Include(IncludeExpr),
}
-impl Expr {
+impl Lit {
/// The source code location.
pub fn span(&self) -> Span {
match *self {
@@ -79,39 +126,8 @@ impl Expr {
Self::Percent(span, _) => span,
Self::Fractional(span, _) => span,
Self::Str(span, _) => span,
- Self::Ident(ref v) => v.span,
- Self::Array(ref v) => v.span,
- Self::Dict(ref v) => v.span,
- Self::Template(ref v) => v.span,
- Self::Group(ref v) => v.span,
- Self::Block(ref v) => v.span,
- Self::Unary(ref v) => v.span,
- Self::Binary(ref v) => v.span,
- Self::Call(ref v) => v.span,
- Self::Closure(ref v) => v.span,
- Self::With(ref v) => v.span,
- Self::Let(ref v) => v.span,
- Self::If(ref v) => v.span,
- Self::While(ref v) => v.span,
- Self::For(ref v) => v.span,
- Self::Import(ref v) => v.span,
- Self::Include(ref v) => v.span,
}
}
-
- /// Whether the expression can be shortened in markup with a hashtag.
- pub fn has_short_form(&self) -> bool {
- matches!(self,
- Self::Ident(_)
- | Self::Call(_)
- | Self::Let(_)
- | Self::If(_)
- | Self::While(_)
- | Self::For(_)
- | Self::Import(_)
- | Self::Include(_)
- )
- }
}
/// An array expression: `(1, "hi", 12cm)`.
@@ -163,7 +179,7 @@ pub struct GroupExpr {
/// The source code location.
pub span: Span,
/// The wrapped expression.
- pub expr: Box<Expr>,
+ pub expr: Expr,
}
/// A block expression: `{ let x = 1; x + 2 }`.
@@ -185,7 +201,7 @@ pub struct UnaryExpr {
/// The operator: `-`.
pub op: UnOp,
/// The expression to operator on: `x`.
- pub expr: Box<Expr>,
+ pub expr: Expr,
}
/// A unary operator.
@@ -234,11 +250,11 @@ pub struct BinaryExpr {
/// The source code location.
pub span: Span,
/// The left-hand side of the operation: `a`.
- pub lhs: Box<Expr>,
+ pub lhs: Expr,
/// The operator: `+`.
pub op: BinOp,
/// The right-hand side of the operation: `b`.
- pub rhs: Box<Expr>,
+ pub rhs: Expr,
}
/// A binary operator.
@@ -389,7 +405,7 @@ pub struct CallExpr {
/// The source code location.
pub span: Span,
/// The function to call.
- pub callee: Box<Expr>,
+ pub callee: Expr,
/// Whether the call is wide, that is, capturing the template behind it.
pub wide: bool,
/// The arguments to the function.
@@ -475,7 +491,7 @@ pub struct WithExpr {
/// The source code location.
pub span: Span,
/// The function to apply the arguments to.
- pub callee: Box<Expr>,
+ pub callee: Expr,
/// The arguments to apply to the function.
pub args: CallArgs,
}
@@ -488,7 +504,7 @@ pub struct LetExpr {
/// The binding to assign to.
pub binding: Ident,
/// The expression the binding is initialized with.
- pub init: Option<Box<Expr>>,
+ pub init: Option<Expr>,
}
/// An import expression: `import a, b, c from "utils.typ"`.
@@ -499,7 +515,7 @@ pub struct ImportExpr {
/// The items to be imported.
pub imports: Imports,
/// The location of the importable file.
- pub path: Box<Expr>,
+ pub path: Expr,
}
/// The items that ought to be imported from a file.
@@ -517,7 +533,7 @@ pub struct IncludeExpr {
/// The source code location.
pub span: Span,
/// The location of the file to be included.
- pub path: Box<Expr>,
+ pub path: Expr,
}
/// An if-else expression: `if x { y } else { z }`.
@@ -526,11 +542,11 @@ pub struct IfExpr {
/// The source code location.
pub span: Span,
/// The condition which selects the body to evaluate.
- pub condition: Box<Expr>,
+ pub condition: Expr,
/// The expression to evaluate if the condition is true.
- pub if_body: Box<Expr>,
+ pub if_body: Expr,
/// The expression to evaluate if the condition is false.
- pub else_body: Option<Box<Expr>>,
+ pub else_body: Option<Expr>,
}
/// A while loop expression: `while x { y }`.
@@ -539,9 +555,9 @@ pub struct WhileExpr {
/// The source code location.
pub span: Span,
/// The condition which selects whether to evaluate the body.
- pub condition: Box<Expr>,
+ pub condition: Expr,
/// The expression to evaluate while the condition is true.
- pub body: Box<Expr>,
+ pub body: Expr,
}
/// A for loop expression: `for x in y { z }`.
@@ -552,9 +568,9 @@ pub struct ForExpr {
/// The pattern to assign to.
pub pattern: ForPattern,
/// The expression to iterate over.
- pub iter: Box<Expr>,
+ pub iter: Expr,
/// The expression to evaluate for each iteration.
- pub body: Box<Expr>,
+ pub body: Expr,
}
/// A pattern in a for loop.
diff --git a/src/syntax/node.rs b/src/syntax/node.rs
index 881752a6..2ca861dc 100644
--- a/src/syntax/node.rs
+++ b/src/syntax/node.rs
@@ -3,10 +3,10 @@ use super::*;
/// A syntax node, encompassing a single logical entity of parsed source code.
#[derive(Debug, Clone, PartialEq)]
pub enum SyntaxNode {
- /// Plain text.
- Text(EcoString),
/// Whitespace containing less than two newlines.
Space,
+ /// Plain text.
+ Text(EcoString),
/// A forced line break: `\`.
Linebreak(Span),
/// A paragraph break: Two or more newlines.
@@ -16,13 +16,13 @@ pub enum SyntaxNode {
/// Emphasized text was enabled / disabled: `_`.
Emph(Span),
/// A raw block with optional syntax highlighting: `` `...` ``.
- Raw(RawNode),
+ Raw(Box<RawNode>),
/// A section heading: `= Introduction`.
- Heading(HeadingNode),
+ Heading(Box<HeadingNode>),
/// An item in an unordered list: `- ...`.
- List(ListItem),
+ List(Box<ListItem>),
/// An item in an enumeration (ordered list): `1. ...`.
- Enum(EnumItem),
+ Enum(Box<EnumItem>),
/// An expression.
Expr(Expr),
}
diff --git a/src/syntax/token.rs b/src/syntax/token.rs
index 567782e7..a4d1867a 100644
--- a/src/syntax/token.rs
+++ b/src/syntax/token.rs
@@ -138,7 +138,7 @@ pub enum Token<'s> {
/// A percentage: `50%`.
///
/// _Note_: `50%` is stored as `50.0` here, as in the corresponding
- /// [literal](super::Expr::Percent).
+ /// [literal](super::Lit::Percent).
Percent(f64),
/// A fraction unit: `3fr`.
Fraction(f64),
diff --git a/src/syntax/visit.rs b/src/syntax/visit.rs
index e39d1f23..0a18271d 100644
--- a/src/syntax/visit.rs
+++ b/src/syntax/visit.rs
@@ -86,8 +86,8 @@ impl_visitors! {
visit_node(v, node: SyntaxNode) {
match node {
- SyntaxNode::Text(_) => {}
SyntaxNode::Space => {}
+ SyntaxNode::Text(_) => {}
SyntaxNode::Linebreak(_) => {}
SyntaxNode::Parbreak(_) => {}
SyntaxNode::Strong(_) => {}
@@ -114,17 +114,8 @@ impl_visitors! {
visit_expr(v, expr: Expr) {
match expr {
- Expr::None(_) => {}
- Expr::Auto(_) => {}
- Expr::Bool(_, _) => {}
- Expr::Int(_, _) => {}
- Expr::Float(_, _) => {}
- Expr::Length(_, _, _) => {}
- Expr::Angle(_, _, _) => {}
- Expr::Percent(_, _) => {}
- Expr::Fractional(_, _) => {}
- Expr::Str(_, _) => {}
Expr::Ident(_) => {}
+ Expr::Lit(_) => {},
Expr::Array(e) => v.visit_array(e),
Expr::Dict(e) => v.visit_dict(e),
Expr::Template(e) => v.visit_template(e),