From 13dd2ad7d0643278e79cc6f60515c83263c1a763 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 26 Apr 2024 21:39:11 +0200 Subject: rebel-parse: shorten type names Make the names match the in-language identifiers. Also, avoid having a String type conflicting with the std one. --- crates/rebel-parse/src/ast.rs | 26 ++++++++++++------------ crates/rebel-parse/src/grammar/recipe.rs | 8 ++++---- crates/rebel-parse/src/grammar/tokenize.rs | 32 +++++++++++++++--------------- crates/rebel-parse/src/token.rs | 18 ++++++++--------- 4 files changed, 42 insertions(+), 42 deletions(-) (limited to 'crates/rebel-parse') diff --git a/crates/rebel-parse/src/ast.rs b/crates/rebel-parse/src/ast.rs index 389523e..ef27ecf 100644 --- a/crates/rebel-parse/src/ast.rs +++ b/crates/rebel-parse/src/ast.rs @@ -132,16 +132,16 @@ pub struct FuncParam<'a> { pub typ: Expr<'a>, } -pub use token::StringKind; +pub use token::StrKind; #[derive(Debug, Clone, PartialEq, Eq)] pub enum Literal<'a> { Unit, - Boolean(bool), - Integer(u64), - String { - pieces: Vec>, - kind: StringKind, + Bool(bool), + Int(u64), + Str { + pieces: Vec>, + kind: StrKind, }, Tuple(Vec>), Array(Vec>), @@ -161,27 +161,27 @@ impl<'a> Literal<'a> { }; let digits = rest.replace('_', ""); let value = u64::from_str_radix(&digits, radix).or(Err("number"))?; - Ok(Literal::Integer(value)) + Ok(Literal::Int(value)) } } #[derive(Clone, Debug, PartialEq, Eq)] -pub enum StringPiece<'a> { +pub enum StrPiece<'a> { Chars(&'a str), Escape(char), Interp(Expr<'a>), } -impl<'a> TryFrom<&token::StringPiece<'a>> for StringPiece<'a> { +impl<'a> TryFrom<&token::StrPiece<'a>> for StrPiece<'a> { type Error = &'static str; - fn try_from(value: &token::StringPiece<'a>) -> Result { + fn try_from(value: &token::StrPiece<'a>) -> Result { use crate::recipe; Ok(match value { - token::StringPiece::Chars(chars) => StringPiece::Chars(chars), - token::StringPiece::Escape(c) => StringPiece::Escape(*c), - token::StringPiece::Interp(tokens) => StringPiece::Interp( + token::StrPiece::Chars(chars) => StrPiece::Chars(chars), + token::StrPiece::Escape(c) => StrPiece::Escape(*c), + token::StrPiece::Interp(tokens) => StrPiece::Interp( recipe::expr(tokens).or(Err("Invalid expression in string interpolation"))?, ), }) diff --git a/crates/rebel-parse/src/grammar/recipe.rs b/crates/rebel-parse/src/grammar/recipe.rs index 9b79d41..6c52367 100644 --- a/crates/rebel-parse/src/grammar/recipe.rs +++ b/crates/rebel-parse/src/grammar/recipe.rs @@ -92,17 +92,17 @@ peg::parser! { = name:ident() p(':') typ:expr() { ast::FuncParam { name, typ } } rule literal() -> ast::Literal<'a> - = keyword_true() { ast::Literal::Boolean(true) } - / keyword_false() { ast::Literal::Boolean(false) } + = keyword_true() { ast::Literal::Bool(true) } + / keyword_false() { ast::Literal::Bool(false) } / [Token::Number(content)] { ? ast::Literal::number(content) } - / [Token::String(String { pieces, kind })] { ? + / [Token::Str(Str { pieces, kind })] { ? let pieces = pieces .iter() .map(|piece| piece.try_into()) .collect::>()?; - Ok(ast::Literal::String{ pieces, kind: *kind }) + Ok(ast::Literal::Str{ pieces, kind: *kind }) } / p('(') p(')') { ast::Literal::Unit } / p('(') elements:(expr() ** p(',')) p(',')? p(')') { diff --git a/crates/rebel-parse/src/grammar/tokenize.rs b/crates/rebel-parse/src/grammar/tokenize.rs index 7eddebc..a68d7c6 100644 --- a/crates/rebel-parse/src/grammar/tokenize.rs +++ b/crates/rebel-parse/src/grammar/tokenize.rs @@ -9,7 +9,7 @@ peg::parser! { pub rule token() -> Token<'input> = number:number() { Token::Number(number) } - / string:string() { Token::String(string) } + / string:string() { Token::Str(string) } / ident:ident() { Token::Ident(ident) } / punct:punct() { Token::Punct(punct) } @@ -32,29 +32,29 @@ peg::parser! { rule number() -> &'input str = $(['0'..='9'] ['0'..='9' | 'a'..='z' | 'A'..='Z' | '_']*) - rule string() -> String<'input> + rule string() -> Str<'input> = "\"" pieces:string_piece()* "\"" { - String { + Str { pieces, - kind: StringKind::String, + kind: StrKind::Regular, } } / "r\"" chars:$([^'"']*) "\"" { - String { - pieces: vec![StringPiece::Chars(chars)], - kind: StringKind::RawString, + Str { + pieces: vec![StrPiece::Chars(chars)], + kind: StrKind::Raw, } } / "```" newline() pieces:script_string_piece()* "```" { - String { + Str { pieces, - kind: StringKind::ScriptString, + kind: StrKind::Script, } } - rule string_piece() -> StringPiece<'input> - = chars:$((!"{{" [^'"' | '\\'])+) { StringPiece::Chars(chars) } - / "\\" escape:string_escape() { StringPiece::Escape(escape) } + rule string_piece() -> StrPiece<'input> + = chars:$((!"{{" [^'"' | '\\'])+) { StrPiece::Chars(chars) } + / "\\" escape:string_escape() { StrPiece::Escape(escape) } / string_interp() rule string_escape() -> char @@ -72,13 +72,13 @@ peg::parser! { u32::from_str_radix(digits, 16).unwrap().try_into().or(Err("Invalid unicode escape")) } - rule script_string_piece() -> StringPiece<'input> - = chars:$((!"{{" !"```" [_])+) { StringPiece::Chars(chars) } + rule script_string_piece() -> StrPiece<'input> + = chars:$((!"{{" !"```" [_])+) { StrPiece::Chars(chars) } / string_interp() - rule string_interp() -> StringPiece<'input> + rule string_interp() -> StrPiece<'input> = "{{" _ tokens:(subtoken() ++ _) _ "}}" { - StringPiece::Interp(TokenStream(tokens)) + StrPiece::Interp(TokenStream(tokens)) } rule subtoken() -> Token<'input> diff --git a/crates/rebel-parse/src/token.rs b/crates/rebel-parse/src/token.rs index 2f2f849..80d758d 100644 --- a/crates/rebel-parse/src/token.rs +++ b/crates/rebel-parse/src/token.rs @@ -2,7 +2,7 @@ pub enum Token<'a> { Ident(&'a str), Punct(Punct), - String(String<'a>), + Str(Str<'a>), Number(&'a str), } @@ -16,23 +16,23 @@ pub enum Spacing { } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct String<'a> { - pub pieces: Vec>, - pub kind: StringKind, +pub struct Str<'a> { + pub pieces: Vec>, + pub kind: StrKind, } #[derive(Clone, Debug, PartialEq, Eq)] -pub enum StringPiece<'a> { +pub enum StrPiece<'a> { Chars(&'a str), Escape(char), Interp(TokenStream<'a>), } #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum StringKind { - String, - RawString, - ScriptString, +pub enum StrKind { + Regular, + Raw, + Script, } #[derive(Clone, Debug, PartialEq, Eq)] -- cgit v1.2.3