From 4069f0744dc24c05d5a6fd6d0530984c4c7ff881 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 31 Dec 2020 17:05:00 +0100 Subject: =?UTF-8?q?Parsing=20improvements=20=F0=9F=A7=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- src/syntax/expr.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++--- src/syntax/lit.rs | 54 --------------------------------------------------- src/syntax/mod.rs | 2 -- 3 files changed, 54 insertions(+), 59 deletions(-) delete mode 100644 src/syntax/lit.rs (limited to 'src/syntax') 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, /// The expression to operator on: `x`. - pub expr: Spanned>, + pub expr: Box>, } /// 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>, + pub lhs: Box>, /// The operator: `+`. pub op: Spanned, /// The right-hand side of the operation: `b`. - pub rhs: Spanned>, + pub rhs: Box>, } /// 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); + +/// 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>, + /// The value of the entry: `"hi"`. + pub expr: Spanned, +} + +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); - -/// 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>, - /// The value of the entry: `"hi"`. - pub expr: Spanned, -} - -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::*; -- cgit v1.2.3