summaryrefslogtreecommitdiff
path: root/src/syntax
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-12-31 17:05:00 +0100
committerLaurenz <laurmaedje@gmail.com>2020-12-31 17:48:56 +0100
commit4069f0744dc24c05d5a6fd6d0530984c4c7ff881 (patch)
tree64fb7211e638462779b03b4ae5b2ea0cc25d23d7 /src/syntax
parentba3d43f7b2a18984be27f3d472884a19f3adce4c (diff)
Parsing improvements 🧽
- Simplified scanner code - Peek eagerly - Skip whitespace and comments automatically in header mode - Parse simple block expressions - Move literal definitions into expression module - Raw resolving tests
Diffstat (limited to 'src/syntax')
-rw-r--r--src/syntax/expr.rs57
-rw-r--r--src/syntax/lit.rs54
-rw-r--r--src/syntax/mod.rs2
3 files changed, 54 insertions, 59 deletions
diff --git a/src/syntax/expr.rs b/src/syntax/expr.rs
index 91f4053c..4c6ce872 100644
--- a/src/syntax/expr.rs
+++ b/src/syntax/expr.rs
@@ -1,6 +1,9 @@
//! Expressions.
use super::*;
+use crate::color::RgbaColor;
+use crate::eval::DictKey;
+use crate::geom::Unit;
/// An expression.
#[derive(Debug, Clone, PartialEq)]
@@ -33,7 +36,7 @@ pub struct ExprUnary {
/// The operator: `-`.
pub op: Spanned<UnOp>,
/// The expression to operator on: `x`.
- pub expr: Spanned<Box<Expr>>,
+ pub expr: Box<Spanned<Expr>>,
}
/// A unary operator.
@@ -47,11 +50,11 @@ pub enum UnOp {
#[derive(Debug, Clone, PartialEq)]
pub struct ExprBinary {
/// The left-hand side of the operation: `a`.
- pub lhs: Spanned<Box<Expr>>,
+ pub lhs: Box<Spanned<Expr>>,
/// The operator: `+`.
pub op: Spanned<BinOp>,
/// The right-hand side of the operation: `b`.
- pub rhs: Spanned<Box<Expr>>,
+ pub rhs: Box<Spanned<Expr>>,
}
/// A binary operator.
@@ -66,3 +69,51 @@ pub enum BinOp {
/// The division operator: `/`.
Div,
}
+
+/// A literal.
+#[derive(Debug, Clone, PartialEq)]
+pub enum Lit {
+ /// A identifier literal: `left`.
+ Ident(Ident),
+ /// A boolean literal: `true`, `false`.
+ Bool(bool),
+ /// An integer literal: `120`.
+ Int(i64),
+ /// A floating-point literal: `1.2`, `10e-4`.
+ Float(f64),
+ /// A length literal: `12pt`, `3cm`.
+ Length(f64, Unit),
+ /// A percent literal: `50%`.
+ ///
+ /// _Note_: `50%` is stored as `50.0` here, but as `0.5` in the
+ /// corresponding [value](crate::geom::Relative).
+ Percent(f64),
+ /// A color literal: `#ffccee`.
+ Color(RgbaColor),
+ /// A string literal: `"hello!"`.
+ Str(String),
+ /// A dictionary literal: `(false, 12cm, greeting: "hi")`.
+ Dict(LitDict),
+ /// A content literal: `{*Hello* there!}`.
+ Content(SynTree),
+}
+
+/// A dictionary literal: `(false, 12cm, greeting: "hi")`.
+#[derive(Debug, Default, Clone, PartialEq)]
+pub struct LitDict(pub Vec<LitDictEntry>);
+
+/// An entry in a dictionary literal: `false` or `greeting: "hi"`.
+#[derive(Debug, Clone, PartialEq)]
+pub struct LitDictEntry {
+ /// The key of the entry if there was one: `greeting`.
+ pub key: Option<Spanned<DictKey>>,
+ /// The value of the entry: `"hi"`.
+ pub expr: Spanned<Expr>,
+}
+
+impl LitDict {
+ /// Create an empty dict literal.
+ pub fn new() -> Self {
+ Self::default()
+ }
+}
diff --git a/src/syntax/lit.rs b/src/syntax/lit.rs
deleted file mode 100644
index 84d5c6f4..00000000
--- a/src/syntax/lit.rs
+++ /dev/null
@@ -1,54 +0,0 @@
-//! Literals.
-
-use super::*;
-use crate::color::RgbaColor;
-use crate::eval::DictKey;
-use crate::geom::Unit;
-
-/// A literal.
-#[derive(Debug, Clone, PartialEq)]
-pub enum Lit {
- /// A identifier literal: `left`.
- Ident(Ident),
- /// A boolean literal: `true`, `false`.
- Bool(bool),
- /// An integer literal: `120`.
- Int(i64),
- /// A floating-point literal: `1.2`, `10e-4`.
- Float(f64),
- /// A length literal: `12pt`, `3cm`.
- Length(f64, Unit),
- /// A percent literal: `50%`.
- ///
- /// _Note_: `50%` is stored as `50.0` here, but as `0.5` in the
- /// corresponding [value](crate::geom::Relative).
- Percent(f64),
- /// A color literal: `#ffccee`.
- Color(RgbaColor),
- /// A string literal: `"hello!"`.
- Str(String),
- /// A dictionary literal: `(false, 12cm, greeting: "hi")`.
- Dict(LitDict),
- /// A content literal: `{*Hello* there!}`.
- Content(SynTree),
-}
-
-/// A dictionary literal: `(false, 12cm, greeting: "hi")`.
-#[derive(Debug, Default, Clone, PartialEq)]
-pub struct LitDict(pub Vec<LitDictEntry>);
-
-/// An entry in a dictionary literal: `false` or `greeting: "hi"`.
-#[derive(Debug, Clone, PartialEq)]
-pub struct LitDictEntry {
- /// The key of the entry if there was one: `greeting`.
- pub key: Option<Spanned<DictKey>>,
- /// The value of the entry: `"hi"`.
- pub expr: Spanned<Expr>,
-}
-
-impl LitDict {
- /// Create an empty dict literal.
- pub fn new() -> Self {
- Self::default()
- }
-}
diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs
index 81702201..970cd283 100644
--- a/src/syntax/mod.rs
+++ b/src/syntax/mod.rs
@@ -2,14 +2,12 @@
mod expr;
mod ident;
-mod lit;
mod node;
mod span;
mod token;
pub use expr::*;
pub use ident::*;
-pub use lit::*;
pub use node::*;
pub use span::*;
pub use token::*;