summaryrefslogtreecommitdiff
path: root/src/syntax
diff options
context:
space:
mode:
Diffstat (limited to 'src/syntax')
-rw-r--r--src/syntax/expr.rs75
-rw-r--r--src/syntax/node.rs4
-rw-r--r--src/syntax/token.rs50
3 files changed, 55 insertions, 74 deletions
diff --git a/src/syntax/expr.rs b/src/syntax/expr.rs
index 4916f34f..ae876209 100644
--- a/src/syntax/expr.rs
+++ b/src/syntax/expr.rs
@@ -5,8 +5,27 @@ use crate::geom::Unit;
/// An expression.
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
- /// A literal: `true`, `1cm`, `"hi"`.
- Lit(Lit),
+ /// The none literal: `none`.
+ None,
+ /// 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),
/// An invocation of a function: `[foo ...]`, `foo(...)`.
Call(ExprCall),
/// A unary operation: `-x`.
@@ -24,7 +43,15 @@ pub enum Expr {
impl Pretty for Expr {
fn pretty(&self, p: &mut Printer) {
match self {
- Self::Lit(lit) => lit.pretty(p),
+ Self::None => p.push_str("none"),
+ Self::Ident(v) => p.push_str(&v),
+ Self::Bool(v) => write!(p, "{}", v).unwrap(),
+ Self::Int(v) => write!(p, "{}", v).unwrap(),
+ Self::Float(v) => write!(p, "{}", v).unwrap(),
+ Self::Length(v, u) => write!(p, "{}{}", v, u).unwrap(),
+ Self::Percent(v) => write!(p, "{}%", v).unwrap(),
+ Self::Color(v) => write!(p, "{}", v).unwrap(),
+ Self::Str(s) => write!(p, "{:?}", &s).unwrap(),
Self::Call(call) => call.pretty(p),
Self::Unary(unary) => unary.pretty(p),
Self::Binary(binary) => binary.pretty(p),
@@ -264,48 +291,6 @@ impl Pretty for ExprDict {
/// A content expression: `{*Hello* there!}`.
pub type ExprContent = Tree;
-/// A literal.
-#[derive(Debug, Clone, PartialEq)]
-pub enum Lit {
- /// A identifier literal: `left`.
- Ident(Ident),
- /// The none literal: `none`.
- None,
- /// 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),
-}
-
-impl Pretty for Lit {
- fn pretty(&self, p: &mut Printer) {
- match self {
- Self::Ident(v) => p.push_str(&v),
- Self::None => p.push_str("none"),
- Self::Bool(v) => write!(p, "{}", v).unwrap(),
- Self::Int(v) => write!(p, "{}", v).unwrap(),
- Self::Float(v) => write!(p, "{}", v).unwrap(),
- Self::Length(v, u) => write!(p, "{}{}", v, u).unwrap(),
- Self::Percent(v) => write!(p, "{}%", v).unwrap(),
- Self::Color(v) => write!(p, "{}", v).unwrap(),
- Self::Str(s) => write!(p, "{:?}", &s).unwrap(),
- }
- }
-}
-
#[cfg(test)]
mod tests {
use super::super::tests::test_pretty;
diff --git a/src/syntax/node.rs b/src/syntax/node.rs
index 91fa72d7..01b4ee42 100644
--- a/src/syntax/node.rs
+++ b/src/syntax/node.rs
@@ -5,24 +5,20 @@ use super::*;
pub enum Node {
/// Plain text.
Text(String),
-
/// Whitespace containing less than two newlines.
Space,
/// A forced line break.
Linebreak,
/// A paragraph break.
Parbreak,
-
/// Strong text was enabled / disabled.
Strong,
/// Emphasized text was enabled / disabled.
Emph,
-
/// A section heading.
Heading(NodeHeading),
/// An optionally syntax-highlighted raw block.
Raw(NodeRaw),
-
/// An expression.
Expr(Expr),
}
diff --git a/src/syntax/token.rs b/src/syntax/token.rs
index a28c35b6..9a7379ca 100644
--- a/src/syntax/token.rs
+++ b/src/syntax/token.rs
@@ -3,13 +3,13 @@ use crate::geom::Unit;
/// A minimal semantic entity of source code.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Token<'s> {
+ /// A consecutive non-markup string.
+ Text(&'s str),
/// One or more whitespace characters.
///
/// The contained `usize` denotes the number of newlines that were contained
/// in the whitespace.
Space(usize),
- /// A consecutive non-markup string.
- Text(&'s str),
/// A line comment with inner string contents `//<str>\n`.
LineComment(&'s str),
@@ -20,6 +20,19 @@ pub enum Token<'s> {
/// An end of a block comment that was not started.
StarSlash,
+ /// A left bracket: `[`.
+ LeftBracket,
+ /// A right bracket: `]`.
+ RightBracket,
+ /// A left brace: `{`.
+ LeftBrace,
+ /// A right brace: `}`.
+ RightBrace,
+ /// A left parenthesis: `(`.
+ LeftParen,
+ /// A right parenthesis: `)`.
+ RightParen,
+
/// A star: `*`.
Star,
/// An underscore: `_`.
@@ -35,19 +48,6 @@ pub enum Token<'s> {
/// A unicode escape sequence: `\u{1F5FA}`.
UnicodeEscape(TokenUnicodeEscape<'s>),
- /// A left bracket: `[`.
- LeftBracket,
- /// A right bracket: `]`.
- RightBracket,
- /// A left brace: `{`.
- LeftBrace,
- /// A right brace: `}`.
- RightBrace,
- /// A left parenthesis: `(`.
- LeftParen,
- /// A right parenthesis: `)`.
- RightParen,
-
/// A colon: `:`.
Colon,
/// A comma: `,`.
@@ -76,7 +76,7 @@ pub enum Token<'s> {
/// A percentage: `50%`.
///
/// _Note_: `50%` is stored as `50.0` here, as in the corresponding
- /// [literal](super::Lit::Percent).
+ /// [literal](super::Expr::Percent).
Percent(f64),
/// A hex value: `#20d82a`.
Hex(&'s str),
@@ -124,13 +124,20 @@ impl<'s> Token<'s> {
/// The natural-language name of this token for use in error messages.
pub fn name(self) -> &'static str {
match self {
- Self::Space(_) => "space",
Self::Text(_) => "text",
+ Self::Space(_) => "space",
Self::LineComment(_) => "line comment",
Self::BlockComment(_) => "block comment",
Self::StarSlash => "end of block comment",
+ Self::LeftBracket => "opening bracket",
+ Self::RightBracket => "closing bracket",
+ Self::LeftBrace => "opening brace",
+ Self::RightBrace => "closing brace",
+ Self::LeftParen => "opening paren",
+ Self::RightParen => "closing paren",
+
Self::Star => "star",
Self::Underscore => "underscore",
Self::Backslash => "backslash",
@@ -139,13 +146,6 @@ impl<'s> Token<'s> {
Self::Raw { .. } => "raw block",
Self::UnicodeEscape { .. } => "unicode escape sequence",
- Self::LeftBracket => "opening bracket",
- Self::RightBracket => "closing bracket",
- Self::LeftBrace => "opening brace",
- Self::RightBrace => "closing brace",
- Self::LeftParen => "opening paren",
- Self::RightParen => "closing paren",
-
Self::Colon => "colon",
Self::Comma => "comma",
Self::Pipe => "pipe",
@@ -153,8 +153,8 @@ impl<'s> Token<'s> {
Self::Hyphen => "minus sign",
Self::Slash => "slash",
- Self::Ident(_) => "identifier",
Self::None => "none",
+ Self::Ident(_) => "identifier",
Self::Bool(_) => "bool",
Self::Int(_) => "integer",
Self::Float(_) => "float",