diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2024-04-25 19:25:49 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2024-04-25 21:13:45 +0200 |
commit | ac9d36ac937b80705d47fa10803ff905b4ed90a6 (patch) | |
tree | f921f068ceab796d2dba13cb9d49bc80ece5de24 | |
parent | 74b4ecf29d389728ed43692a308b42d4d282c6e5 (diff) | |
download | rebel-ac9d36ac937b80705d47fa10803ff905b4ed90a6.tar rebel-ac9d36ac937b80705d47fa10803ff905b4ed90a6.zip |
rebel-parse: ast: represent strings as a Vec of pieces
Pieces can be characters, escapes, or interpolated subexpressions.
-rw-r--r-- | crates/rebel-parse/src/ast.rs | 9 | ||||
-rw-r--r-- | 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<StringPiece<'a>>), Tuple(Vec<Expr<'a>>), Array(Vec<Expr<'a>>), Map(Vec<MapEntry<'a>>), @@ -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(')') { |