From ac9d36ac937b80705d47fa10803ff905b4ed90a6 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 25 Apr 2024 19:25:49 +0200 Subject: rebel-parse: ast: represent strings as a Vec of pieces Pieces can be characters, escapes, or interpolated subexpressions. --- crates/rebel-parse/src/ast.rs | 9 ++++++++- crates/rebel-parse/src/grammar/recipe.rs | 3 ++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/crates/rebel-parse/src/ast.rs b/crates/rebel-parse/src/ast.rs index c7ddb7a..f9943d4 100644 --- a/crates/rebel-parse/src/ast.rs +++ b/crates/rebel-parse/src/ast.rs @@ -135,7 +135,7 @@ pub enum Literal<'a> { Unit, Boolean(bool), Integer(u64), - String(&'a str), + String(Vec>), Tuple(Vec>), Array(Vec>), Map(Vec>), @@ -158,6 +158,13 @@ impl<'a> Literal<'a> { } } +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum StringPiece<'a> { + Chars(&'a str), + Escape(char), + Interp(Expr<'a>), +} + #[derive(Debug, Clone, PartialEq, Eq)] pub struct MapEntry<'a> { pub key: &'a str, diff --git a/crates/rebel-parse/src/grammar/recipe.rs b/crates/rebel-parse/src/grammar/recipe.rs index 633b021..6bd96bb 100644 --- a/crates/rebel-parse/src/grammar/recipe.rs +++ b/crates/rebel-parse/src/grammar/recipe.rs @@ -98,7 +98,8 @@ peg::parser! { ast::Literal::number(content) } / [Token::String(String { content, .. })] { - ast::Literal::String(content) + let ast_pieces = vec![ast::StringPiece::Chars(content)]; + ast::Literal::String(ast_pieces) } / p('(') p(')') { ast::Literal::Unit } / p('(') elements:(expr() ** p(',')) p(',')? p(')') { -- cgit v1.2.3