Skip to content

Commit

Permalink
feat: Allow trans / endtrans tags (#138)
Browse files Browse the repository at this point in the history
* Allow trans / endtrans tags

* Tests: add parse_twig_trans test method

* refactor parse_twig_trans method

* Update crates/ludtwig-parser/src/grammar/twig/tags.rs

Co-authored-by: Malte Janz <service.malte.j@protonmail.com>

* Update crates/ludtwig-parser/src/grammar/twig/tags.rs

Co-authored-by: Malte Janz <service.malte.j@protonmail.com>

* Update crates/ludtwig-parser/src/grammar/twig/tags.rs

Co-authored-by: Malte Janz <service.malte.j@protonmail.com>

* Implement AST for trans / endtrans

* Update trans test

* chore: run cargo fmt

---------

Co-authored-by: Malte Janz <service.malte.j@protonmail.com>
  • Loading branch information
freezernick and MalteJanz authored Feb 17, 2025
1 parent 65b8854 commit 9bb41d0
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
65 changes: 65 additions & 0 deletions crates/ludtwig-parser/src/grammar/twig/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub(crate) fn at_twig_termination_tag(p: &mut Parser) -> bool {
|| p.at_following(&[T!["{%"], T!["endwith"]])
|| p.at_following(&[T!["{%"], T!["endcache"]])
|| p.at_following(&[T!["{%"], T!["endsw_silent_feature_call"]])
|| p.at_following(&[T!["{%"], T!["endtrans"]]) // Drupal Trans / Endtrans
}

pub(crate) fn parse_twig_block_statement(
Expand Down Expand Up @@ -83,6 +84,8 @@ pub(crate) fn parse_twig_block_statement(
Some(parse_twig_with(parser, m, child_parser))
} else if parser.at(T!["cache"]) {
Some(parse_twig_cache(parser, m, child_parser))
} else if parser.at(T!["trans"]) {
Some(parse_twig_trans(parser, m, child_parser))
} else {
match parse_shopware_twig_block_statement(parser, m, child_parser) {
BlockParseResult::NothingFound(m) => {
Expand Down Expand Up @@ -1048,6 +1051,39 @@ fn parse_twig_if(
parser.complete(wrapper_m, SyntaxKind::TWIG_IF)
}

fn parse_twig_trans(
parser: &mut Parser,
outer: Marker,
child_parser: ParseFunction,
) -> CompletedMarker {
debug_assert!(parser.at(T!["trans"]));
parser.bump();

parser.expect(T!["%}"], &[T!["endtrans"], T!["%}"], T!["</"]]);

let wrapper_m = parser.complete(outer, SyntaxKind::TWIG_TRANS_STARTING_BLOCK);
let wrapper_m = parser.precede(wrapper_m);

// parse all the children except endtrans
let body_m = parser.start();
parse_many(
parser,
|p| p.at_following(&[T!["{%"], T!["endtrans"]]),
|p| {
child_parser(p);
},
);
parser.complete(body_m, SyntaxKind::BODY);

let end_block_m = parser.start();
parser.expect(T!["{%"], &[T!["endtrans"], T!["%}"], T!["</"]]);
parser.expect(T!["endtrans"], &[T!["%}"], T!["</"]]);
parser.expect(T!["%}"], &[T!["</"]]);
parser.complete(end_block_m, SyntaxKind::TWIG_TRANS_ENDING_BLOCK);

parser.complete(wrapper_m, SyntaxKind::TWIG_TRANS)
}

#[cfg(test)]
mod tests {
use crate::parser::check_parse;
Expand Down Expand Up @@ -5167,4 +5203,33 @@ mod tests {
error at 9..11: expected twig expression as cache key but found %}"#]],
);
}

#[test]
fn parse_twig_trans() {
check_parse(
"{% trans %} hello world {% endtrans %}",
expect![[r#"
ROOT@0..38
TWIG_TRANS@0..38
TWIG_TRANS_STARTING_BLOCK@0..11
TK_CURLY_PERCENT@0..2 "{%"
TK_WHITESPACE@2..3 " "
TK_TRANS@3..8 "trans"
TK_WHITESPACE@8..9 " "
TK_PERCENT_CURLY@9..11 "%}"
BODY@11..23
HTML_TEXT@11..23
TK_WHITESPACE@11..12 " "
TK_WORD@12..17 "hello"
TK_WHITESPACE@17..18 " "
TK_WORD@18..23 "world"
TWIG_TRANS_ENDING_BLOCK@23..38
TK_WHITESPACE@23..24 " "
TK_CURLY_PERCENT@24..26 "{%"
TK_WHITESPACE@26..27 " "
TK_ENDTRANS@27..35 "endtrans"
TK_WHITESPACE@35..36 " "
TK_PERCENT_CURLY@36..38 "%}""#]],
);
}
}
2 changes: 2 additions & 0 deletions crates/ludtwig-parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ mod tests {
add("ludtwig-ignore-file", T!["ludtwig-ignore-file"]);
add("ludtwig-ignore", T!["ludtwig-ignore"]);
add("€", T![unknown]);
add("trans", T!["trans"]);
add("endtrans", T!["endtrans"]);

// lex and compare
let results = lex(&source);
Expand Down
6 changes: 6 additions & 0 deletions crates/ludtwig-parser/src/syntax/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,12 @@ ast_node!(HtmlRawText, SyntaxKind::HTML_RAW_TEXT);
ast_node!(HtmlComment, SyntaxKind::HTML_COMMENT);
ast_node!(Error, SyntaxKind::ERROR);
ast_node!(Root, SyntaxKind::ROOT);
ast_node!(TwigTrans, SyntaxKind::TWIG_TRANS);
ast_node!(
TwigTransStartingBlock,
SyntaxKind::TWIG_TRANS_STARTING_BLOCK
);
ast_node!(TwigTransEndingBlock, SyntaxKind::TWIG_TRANS_ENDING_BLOCK);

#[cfg(test)]
mod tests {
Expand Down
15 changes: 15 additions & 0 deletions crates/ludtwig-parser/src/syntax/untyped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ pub enum SyntaxKind {
#[token("source")]
TK_SOURCE,

/* Drupal Trans */
#[token("trans")]
TK_TRANS,
#[token("endtrans")]
TK_ENDTRANS,

/* shopware specific */
#[token("sw_extends")]
TK_SW_EXTENDS,
Expand Down Expand Up @@ -449,6 +455,11 @@ pub enum SyntaxKind {
TWIG_CACHE_STARTING_BLOCK,
TWIG_CACHE_ENDING_BLOCK,

// Drupal Trans
TWIG_TRANS,
TWIG_TRANS_STARTING_BLOCK,
TWIG_TRANS_ENDING_BLOCK,

// shopware specific
SHOPWARE_TWIG_SW_EXTENDS,
SHOPWARE_TWIG_SW_INCLUDE,
Expand Down Expand Up @@ -617,6 +628,8 @@ macro_rules! T {
["date"] => { $crate::syntax::untyped::SyntaxKind::TK_DATE };
["include"] => { $crate::syntax::untyped::SyntaxKind::TK_INCLUDE };
["source"] => { $crate::syntax::untyped::SyntaxKind::TK_SOURCE };
["trans"] => { $crate::syntax::untyped::SyntaxKind::TK_TRANS };
["endtrans"] => { $crate::syntax::untyped::SyntaxKind::TK_ENDTRANS };
["sw_extends"] => { $crate::syntax::untyped::SyntaxKind::TK_SW_EXTENDS };
["sw_silent_feature_call"] => { $crate::syntax::untyped::SyntaxKind::TK_SW_SILENT_FEATURE_CALL };
["endsw_silent_feature_call"] => { $crate::syntax::untyped::SyntaxKind::TK_ENDSW_SILENT_FEATURE_CALL };
Expand Down Expand Up @@ -769,6 +782,8 @@ impl fmt::Display for SyntaxKind {
SyntaxKind::TK_DATE => "date",
SyntaxKind::TK_INCLUDE => "include",
SyntaxKind::TK_SOURCE => "source",
SyntaxKind::TK_TRANS => "trans",
SyntaxKind::TK_ENDTRANS => "endtrans",
SyntaxKind::TK_SW_EXTENDS => "sw_extends",
SyntaxKind::TK_SW_SILENT_FEATURE_CALL => "sw_silent_feature_call",
SyntaxKind::TK_ENDSW_SILENT_FEATURE_CALL => "endsw_silent_feature_call",
Expand Down

0 comments on commit 9bb41d0

Please sign in to comment.