summaryrefslogtreecommitdiff
path: root/src/syntax
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-10-03 15:07:57 +0200
committerLaurenz <laurmaedje@gmail.com>2020-10-03 15:07:57 +0200
commit95bae5725cf6495644e2593f8492f1cd0e5bd3c1 (patch)
tree919dd90cac7623bcbbc09d9c92399eaa65e537f2 /src/syntax
parent0fc25d732d7cbc37cf801645849d1060f2cec4a3 (diff)
Int, Float, Relative and Linear values 🍉
Diffstat (limited to 'src/syntax')
-rw-r--r--src/syntax/ast/lit.rs17
-rw-r--r--src/syntax/token.rs89
2 files changed, 63 insertions, 43 deletions
diff --git a/src/syntax/ast/lit.rs b/src/syntax/ast/lit.rs
index bbdd0c81..ba7e7e4f 100644
--- a/src/syntax/ast/lit.rs
+++ b/src/syntax/ast/lit.rs
@@ -18,10 +18,15 @@ pub enum Lit {
Int(i64),
/// A floating-point literal: `1.2`, `10e-4`.
Float(f64),
- /// A percent literal: `50%`.
- Percent(f64),
/// A length literal: `12pt`, `3cm`.
Length(Length),
+ /// A percent literal: `50%`.
+ ///
+ /// Note: `50%` is represented as `50.0` here, but as `0.5` in the
+ /// corresponding [value].
+ ///
+ /// [value]: ../../eval/enum.Value.html#variant.Relative
+ Percent(f64),
/// A color literal: `#ffccee`.
Color(RgbaColor),
/// A string literal: `"hello!"`.
@@ -42,10 +47,10 @@ impl Lit {
match *self {
Lit::Ident(ref i) => Value::Ident(i.clone()),
Lit::Bool(b) => Value::Bool(b),
- Lit::Int(i) => Value::Number(i as f64),
- Lit::Float(f) => Value::Number(f as f64),
- Lit::Percent(p) => Value::Number(p as f64 / 100.0),
- Lit::Length(l) => Value::Length(l),
+ Lit::Int(i) => Value::Int(i),
+ Lit::Float(f) => Value::Float(f),
+ Lit::Length(l) => Value::Length(l.as_raw()),
+ Lit::Percent(p) => Value::Relative(p / 100.0),
Lit::Color(c) => Value::Color(c),
Lit::Str(ref s) => Value::Str(s.clone()),
Lit::Dict(ref d) => Value::Dict(d.eval(ctx, f).await),
diff --git a/src/syntax/token.rs b/src/syntax/token.rs
index 5c159bbd..e3fada43 100644
--- a/src/syntax/token.rs
+++ b/src/syntax/token.rs
@@ -5,79 +5,91 @@ use crate::length::Length;
/// A minimal semantic entity of source code.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Token<'s> {
- /// One or more whitespace characters. The contained `usize` denotes the
- /// number of newlines that were contained in the whitespace.
+ /// 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),
- /// A block comment with inner string contents `/*<str>*/`. The comment
- /// can contain nested block comments.
+ /// A block comment with inner string contents `/*<str>*/`.
+ ///
+ /// The comment can contain nested block comments.
BlockComment(&'s str),
- /// A star. It can appear in a function header where it signifies the
- /// multiplication of expressions or the body where it modifies the styling.
+ /// A star: `*`.
Star,
- /// An underscore in body-text.
+ /// An underscore: `_`.
Underscore,
- /// A backslash followed by whitespace in text.
+ /// A backslash followed by whitespace: `\`.
Backslash,
- /// A hashtag indicating a section heading.
+ /// A hashtag indicating a section heading: `#`.
Hashtag,
- /// A raw block.
+ /// A non-breaking space: `~`.
+ NonBreakingSpace,
+ /// A raw block: `` `...` ``.
Raw(TokenRaw<'s>),
- /// A unicode escape sequence.
+ /// A unicode escape sequence: `\u{1F5FA}`.
UnicodeEscape(TokenUnicodeEscape<'s>),
- /// A left bracket starting a function invocation or body: `[`.
+ /// A left bracket: `[`.
LeftBracket,
- /// A right bracket ending a function invocation or body: `]`.
+ /// A right bracket: `]`.
RightBracket,
- /// A left brace indicating the start of content: `{`.
+ /// A left brace: `{`.
LeftBrace,
- /// A right brace indicating the end of content: `}`.
+ /// A right brace: `}`.
RightBrace,
- /// A left parenthesis in a function header: `(`.
+ /// A left parenthesis: `(`.
LeftParen,
- /// A right parenthesis in a function header: `)`.
+ /// A right parenthesis: `)`.
RightParen,
- /// A colon in a function header: `:`.
+ /// A colon: `:`.
Colon,
- /// A comma in a function header: `,`.
+ /// A comma: `,`.
Comma,
- /// An equals sign in a function header: `=`.
+ /// An equals sign: `=`.
Equals,
- /// A double forward chevron in a function header: `>>`.
+ /// A double forward chevron: `>>`.
Chain,
- /// A plus in a function header, signifying the addition of expressions.
+ /// A plus: `+`.
Plus,
- /// A hyphen in a function header, signifying the subtraction of
- /// expressions.
+ /// A hyphen: `-`.
Hyphen,
- /// A slash in a function header, signifying the division of expressions.
+ /// A slash: `/`.
Slash,
- /// An identifier in a function header: `center`.
+ /// An identifier: `center`.
Ident(&'s str),
- /// A boolean in a function header: `true | false`.
+ /// A boolean: `true`, `false`.
Bool(bool),
- /// A number in a function header: `3.14`.
- Number(f64),
- /// A length in a function header: `12pt`.
+ /// An integer: `120`.
+ Int(i64),
+ /// A floating-point number: `1.2`, `10e-4`.
+ Float(f64),
+ /// A length: `12pt`, `3cm`.
Length(Length),
- /// A hex value in a function header: `#20d82a`.
+ /// A percentage: `50%`.
+ ///
+ /// Note: `50%` is represented as `50.0` here, as in the corresponding
+ /// [literal].
+ ///
+ /// [literal]: ../ast/enum.Lit.html#variant.Percent
+ Percent(f64),
+ /// A hex value: `#20d82a`.
Hex(&'s str),
- /// A quoted string in a function header: `"..."`.
+ /// A quoted string: `"..."`.
Str(TokenStr<'s>),
/// Things that are not valid in the context they appeared in.
Invalid(&'s str),
}
-/// A quoted string in a function header: `"..."`.
+/// A quoted string: `"..."`.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct TokenStr<'s> {
/// The string inside the quotes.
@@ -90,7 +102,7 @@ pub struct TokenStr<'s> {
pub terminated: bool,
}
-/// A unicode escape sequence.
+/// A unicode escape sequence: `\u{1F5FA}`.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct TokenUnicodeEscape<'s> {
/// The escape sequence between two braces.
@@ -99,7 +111,7 @@ pub struct TokenUnicodeEscape<'s> {
pub terminated: bool,
}
-/// A raw block.
+/// A raw block: `` `...` ``.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct TokenRaw<'s> {
/// The raw text between the backticks.
@@ -111,7 +123,7 @@ pub struct TokenRaw<'s> {
}
impl<'s> Token<'s> {
- /// The natural-language name for this token for use in error messages.
+ /// The natural-language name of this token for use in error messages.
pub fn name(self) -> &'static str {
match self {
Self::Space(_) => "space",
@@ -124,6 +136,7 @@ impl<'s> Token<'s> {
Self::Underscore => "underscore",
Self::Backslash => "backslash",
Self::Hashtag => "hashtag",
+ Self::NonBreakingSpace => "non-breaking space",
Self::Raw { .. } => "raw block",
Self::UnicodeEscape { .. } => "unicode escape sequence",
@@ -144,8 +157,10 @@ impl<'s> Token<'s> {
Self::Ident(_) => "identifier",
Self::Bool(_) => "bool",
- Self::Number(_) => "number",
+ Self::Int(_) => "integer",
+ Self::Float(_) => "float",
Self::Length(_) => "length",
+ Self::Percent(_) => "percentage",
Self::Hex(_) => "hex value",
Self::Str { .. } => "string",