summaryrefslogtreecommitdiff
path: root/src/syntax
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-04-08 15:08:26 +0200
committerLaurenz <laurmaedje@gmail.com>2022-04-08 15:45:14 +0200
commit712c00ecb72b67da2c0788e5d3eb4dcc6366b2a7 (patch)
treef5d7ef4341a4728c980d020cc173fa6bb70feaff /src/syntax
parent977ac77e6a3298be2644a8231e93acbef9f7f396 (diff)
Em units
Diffstat (limited to 'src/syntax')
-rw-r--r--src/syntax/ast.rs40
-rw-r--r--src/syntax/highlight.rs5
-rw-r--r--src/syntax/mod.rs26
3 files changed, 26 insertions, 45 deletions
diff --git a/src/syntax/ast.rs b/src/syntax/ast.rs
index cb0a99b9..97ab055f 100644
--- a/src/syntax/ast.rs
+++ b/src/syntax/ast.rs
@@ -5,7 +5,7 @@
use std::ops::Deref;
use super::{Green, GreenData, NodeKind, RedNode, RedRef, Span};
-use crate::geom::{AngularUnit, LengthUnit};
+use crate::geom::{AngleUnit, LengthUnit};
use crate::util::EcoString;
/// A typed AST node.
@@ -352,10 +352,7 @@ node! {
| NodeKind::Bool(_)
| NodeKind::Int(_)
| NodeKind::Float(_)
- | NodeKind::Length(_, _)
- | NodeKind::Angle(_, _)
- | NodeKind::Percentage(_)
- | NodeKind::Fraction(_)
+ | NodeKind::Numeric(_, _)
| NodeKind::Str(_)
}
@@ -368,10 +365,7 @@ impl Lit {
NodeKind::Bool(v) => LitKind::Bool(v),
NodeKind::Int(v) => LitKind::Int(v),
NodeKind::Float(v) => LitKind::Float(v),
- NodeKind::Length(v, unit) => LitKind::Length(v, unit),
- NodeKind::Angle(v, unit) => LitKind::Angle(v, unit),
- NodeKind::Percentage(v) => LitKind::Percent(v),
- NodeKind::Fraction(v) => LitKind::Fractional(v),
+ NodeKind::Numeric(v, unit) => LitKind::Numeric(v, unit),
NodeKind::Str(ref v) => LitKind::Str(v.clone()),
_ => panic!("literal is of wrong kind"),
}
@@ -391,21 +385,27 @@ pub enum LitKind {
Int(i64),
/// A floating-point literal: `1.2`, `10e-4`.
Float(f64),
- /// A length literal: `12pt`, `3cm`.
- Length(f64, LengthUnit),
- /// An angle literal: `1.5rad`, `90deg`.
- Angle(f64, AngularUnit),
- /// 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 fraction unit literal: `1fr`.
- Fractional(f64),
+ /// A numeric literal with a unit: `12pt`, `3cm`, `2em`, `90deg`, `50%`.
+ Numeric(f64, Unit),
/// A string literal: `"hello!"`.
Str(EcoString),
}
+/// Unit of a numeric value.
+#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
+pub enum Unit {
+ /// An absolute length unit.
+ Length(LengthUnit),
+ /// An angular unit.
+ Angle(AngleUnit),
+ /// Font-relative: `1em` is the same as the font size.
+ Em,
+ /// Fractions: `fr`.
+ Fr,
+ /// Percentage: `%`.
+ Percent,
+}
+
node! {
/// A code block: `{ let x = 1; x + 2 }`.
CodeBlock: CodeBlock
diff --git a/src/syntax/highlight.rs b/src/syntax/highlight.rs
index bad434b9..b0486c7b 100644
--- a/src/syntax/highlight.rs
+++ b/src/syntax/highlight.rs
@@ -187,10 +187,7 @@ impl Category {
NodeKind::Bool(_) => Some(Category::Bool),
NodeKind::Int(_) => Some(Category::Number),
NodeKind::Float(_) => Some(Category::Number),
- NodeKind::Length(_, _) => Some(Category::Number),
- NodeKind::Angle(_, _) => Some(Category::Number),
- NodeKind::Percentage(_) => Some(Category::Number),
- NodeKind::Fraction(_) => Some(Category::Number),
+ NodeKind::Numeric(_, _) => Some(Category::Number),
NodeKind::Str(_) => Some(Category::String),
NodeKind::Error(_, _) => Some(Category::Invalid),
NodeKind::Unknown(_) => Some(Category::Invalid),
diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs
index d0920d20..b4908ff2 100644
--- a/src/syntax/mod.rs
+++ b/src/syntax/mod.rs
@@ -12,9 +12,8 @@ use std::sync::Arc;
pub use highlight::*;
pub use span::*;
-use self::ast::{MathNode, RawNode, TypedNode};
+use self::ast::{MathNode, RawNode, TypedNode, Unit};
use crate::diag::Error;
-use crate::geom::{AngularUnit, LengthUnit};
use crate::parse::TokenMode;
use crate::source::SourceId;
use crate::util::EcoString;
@@ -629,17 +628,8 @@ pub enum NodeKind {
Int(i64),
/// A floating-point number: `1.2`, `10e-4`.
Float(f64),
- /// A length: `12pt`, `3cm`.
- Length(f64, LengthUnit),
- /// An angle: `90deg`.
- Angle(f64, AngularUnit),
- /// A percentage: `50%`.
- ///
- /// _Note_: `50%` is stored as `50.0` here, as in the corresponding
- /// [literal](ast::LitKind::Percent).
- Percentage(f64),
- /// A fraction unit: `3fr`.
- Fraction(f64),
+ /// A numeric value with a unit: `12pt`, `3cm`, `2em`, `90deg`, `50%`.
+ Numeric(f64, Unit),
/// A quoted string: `"..."`.
Str(EcoString),
/// A code block: `{ let x = 1; x + 2 }`.
@@ -886,10 +876,7 @@ impl NodeKind {
Self::Bool(_) => "boolean",
Self::Int(_) => "integer",
Self::Float(_) => "float",
- Self::Length(_, _) => "length",
- Self::Angle(_, _) => "angle",
- Self::Percentage(_) => "percentage",
- Self::Fraction(_) => "`fr` value",
+ Self::Numeric(_, _) => "numeric value",
Self::Str(_) => "string",
Self::CodeBlock => "code block",
Self::ContentBlock => "content block",
@@ -1010,10 +997,7 @@ impl Hash for NodeKind {
Self::Bool(v) => v.hash(state),
Self::Int(v) => v.hash(state),
Self::Float(v) => v.to_bits().hash(state),
- Self::Length(v, u) => (v.to_bits(), u).hash(state),
- Self::Angle(v, u) => (v.to_bits(), u).hash(state),
- Self::Percentage(v) => v.to_bits().hash(state),
- Self::Fraction(v) => v.to_bits().hash(state),
+ Self::Numeric(v, u) => (v.to_bits(), u).hash(state),
Self::Str(v) => v.hash(state),
Self::CodeBlock => {}
Self::ContentBlock => {}