From 77c06ebc24ab3a43dc2268763ff8f10963f875b4 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 4 Jan 2021 17:19:49 +0100 Subject: [PATCH] =?UTF-8?q?None=20literal=20=F0=9F=95=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/eval/mod.rs | 1 + src/parse/mod.rs | 1 + src/parse/tests.rs | 1 + src/parse/tokens.rs | 8 +++++++- src/syntax/expr.rs | 2 ++ src/syntax/token.rs | 3 +++ 6 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/eval/mod.rs b/src/eval/mod.rs index 20d32e846..42a5555d5 100644 --- a/src/eval/mod.rs +++ b/src/eval/mod.rs @@ -176,6 +176,7 @@ impl Eval for Spanned<&Lit> { Value::Error } }, + Lit::None => Value::None, Lit::Bool(v) => Value::Bool(v), Lit::Int(v) => Value::Int(v), Lit::Float(v) => Value::Float(v), diff --git a/src/parse/mod.rs b/src/parse/mod.rs index e6cac17f1..ef4ce46fc 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -326,6 +326,7 @@ fn value(p: &mut Parser) -> Option { } // Basic values. + Some(Token::None) => Expr::Lit(Lit::None), Some(Token::Bool(b)) => Expr::Lit(Lit::Bool(b)), Some(Token::Int(i)) => Expr::Lit(Lit::Int(i)), Some(Token::Float(f)) => Expr::Lit(Lit::Float(f)), diff --git a/src/parse/tests.rs b/src/parse/tests.rs index d01d09a52..8de03aff5 100644 --- a/src/parse/tests.rs +++ b/src/parse/tests.rs @@ -614,6 +614,7 @@ fn test_parse_values() { t!("{name}" Block(Id("name"))); t!("{ke-bab}" Block(Id("ke-bab"))); t!("{α}" Block(Id("α"))); + t!("{none}" Block(Expr::Lit(Lit::None))); t!("{true}" Block(Bool(true))); t!("{false}" Block(Bool(false))); t!("{1.0e-4}" Block(Float(1e-4))); diff --git a/src/parse/tokens.rs b/src/parse/tokens.rs index ff7f11bd0..74ec47e9f 100644 --- a/src/parse/tokens.rs +++ b/src/parse/tokens.rs @@ -294,6 +294,7 @@ impl<'s> Tokens<'s> { self.s.eat_while(is_id_continue); let string = self.s.eaten_from(start); match string { + "none" => Token::None, "true" => Token::Bool(true), "false" => Token::Bool(false), _ => Token::Ident(string), @@ -377,6 +378,7 @@ mod tests { use super::*; use crate::parse::tests::check; + use Option::None; use Token::{ BlockComment as BC, Ident as Id, LeftBrace as LB, LeftBracket as L, LeftParen as LP, LineComment as LC, RightBrace as RB, RightBracket as R, @@ -679,7 +681,11 @@ mod tests { } #[test] - fn test_tokenize_bools() { + fn test_tokenize_keywords() { + // Test none. + t!(Header[" /"]: "none" => Token::None); + t!(Header[" /"]: "None" => Id("None")); + // Test valid bools. t!(Header[" /"]: "false" => Bool(false)); t!(Header[" /"]: "true" => Bool(true)); diff --git a/src/syntax/expr.rs b/src/syntax/expr.rs index 94d07b075..cf6611f93 100644 --- a/src/syntax/expr.rs +++ b/src/syntax/expr.rs @@ -108,6 +108,8 @@ pub type ExprContent = Tree; 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`. diff --git a/src/syntax/token.rs b/src/syntax/token.rs index ef17fac23..a28c35b6a 100644 --- a/src/syntax/token.rs +++ b/src/syntax/token.rs @@ -63,6 +63,8 @@ pub enum Token<'s> { /// An identifier: `center`. Ident(&'s str), + /// A none: `none`. + None, /// A boolean: `true`, `false`. Bool(bool), /// An integer: `120`. @@ -152,6 +154,7 @@ impl<'s> Token<'s> { Self::Slash => "slash", Self::Ident(_) => "identifier", + Self::None => "none", Self::Bool(_) => "bool", Self::Int(_) => "integer", Self::Float(_) => "float",