Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement missing twig component syntax #141

Merged
merged 3 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/ludtwig-parser/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [#128](https://github.com/MalteJanz/ludtwig/pull/128) Fixed parsing of non self closing twig component HTML tags
- [#138](https://github.com/MalteJanz/ludtwig/pull/138) Added parsing support for `{% trans %}...{% endtrans %}` syntax,
commonly found in Drupal projects
- [#141](https://github.com/MalteJanz/ludtwig/pull/141) Added remaining syntax support for symfony twig components

# v0.6.0

Expand Down
2 changes: 1 addition & 1 deletion crates/ludtwig-parser/src/grammar/twig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ mod tests {
}

#[test]
fn parse_twig_component_call() {
fn parse_twig_function_call() {
check_parse(
"{{ component('Alert', { message: 'Hello Twig Components!' }) }}",
expect![[r#"
Expand Down
2 changes: 1 addition & 1 deletion crates/ludtwig-parser/src/grammar/twig/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ fn parse_twig_boolean(parser: &mut Parser) -> CompletedMarker {
parser.complete(m, SyntaxKind::TWIG_LITERAL_BOOLEAN)
}

fn parse_twig_hash(parser: &mut Parser) -> CompletedMarker {
pub(crate) fn parse_twig_hash(parser: &mut Parser) -> CompletedMarker {
debug_assert!(parser.at(T!["{"]));
let m = parser.start();
parser.bump();
Expand Down
544 changes: 543 additions & 1 deletion crates/ludtwig-parser/src/grammar/twig/tags.rs

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions crates/ludtwig-parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ mod tests {
add("endwith", T!["endwith"]);
add("ttl", T!["ttl"]);
add("tags", T!["tags"]);
add("props", T!["props"]);
add("component", T!["component"]);
add("endcomponent", T!["endcomponent"]);
add("not", T!["not"]);
add("or", T!["or"]);
add("and", T!["and"]);
Expand Down Expand Up @@ -837,6 +840,21 @@ mod tests {
check_token("tags", T!["tags"]);
}

#[test]
fn lex_props() {
check_token("props", T!["props"]);
}

#[test]
fn lex_component() {
check_token("component", T!["component"]);
}

#[test]
fn lex_endcomponent() {
check_token("endcomponent", T!["endcomponent"]);
}

#[test]
fn lex_not() {
check_token("not", T!["not"]);
Expand Down
11 changes: 11 additions & 0 deletions crates/ludtwig-parser/src/syntax/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,17 @@ ast_node!(
SyntaxKind::TWIG_CACHE_STARTING_BLOCK
);
ast_node!(TwigCacheEndingBlock, SyntaxKind::TWIG_CACHE_ENDING_BLOCK);
ast_node!(TwigProps, SyntaxKind::TWIG_PROPS);
ast_node!(TwigPropDeclaration, SyntaxKind::TWIG_PROP_DECLARATION);
ast_node!(TwigComponent, SyntaxKind::TWIG_COMPONENT);
ast_node!(
TwigComponentStartingBlock,
SyntaxKind::TWIG_COMPONENT_STARTING_BLOCK
);
ast_node!(
TwigComponentEndingBlock,
SyntaxKind::TWIG_COMPONENT_ENDING_BLOCK
);
ast_node!(ShopwareTwigExtends, SyntaxKind::SHOPWARE_TWIG_SW_EXTENDS);
ast_node!(ShopwareTwigInclude, SyntaxKind::SHOPWARE_TWIG_SW_INCLUDE);
ast_node!(
Expand Down
19 changes: 19 additions & 0 deletions crates/ludtwig-parser/src/syntax/untyped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ pub enum SyntaxKind {
TK_TTL,
#[token("tags")]
TK_TAGS,
#[token("props")]
TK_PROPS,
#[token("component")]
TK_COMPONENT,
#[token("endcomponent")]
TK_ENDCOMPONENT,
/* twig operators */
#[token("not")]
TK_NOT,
Expand Down Expand Up @@ -454,6 +460,13 @@ pub enum SyntaxKind {
TWIG_CACHE_TAGS,
TWIG_CACHE_STARTING_BLOCK,
TWIG_CACHE_ENDING_BLOCK,
// twig props
TWIG_PROPS,
TWIG_PROP_DECLARATION,
// twig component
TWIG_COMPONENT,
TWIG_COMPONENT_STARTING_BLOCK,
TWIG_COMPONENT_ENDING_BLOCK,

// Drupal Trans
TWIG_TRANS,
Expand Down Expand Up @@ -598,6 +611,9 @@ macro_rules! T {
["endwith"] => { $crate::syntax::untyped::SyntaxKind::TK_ENDWITH };
["ttl"] => { $crate::syntax::untyped::SyntaxKind::TK_TTL };
["tags"] => { $crate::syntax::untyped::SyntaxKind::TK_TAGS };
["props"] => { $crate::syntax::untyped::SyntaxKind::TK_PROPS };
["component"] => { $crate::syntax::untyped::SyntaxKind::TK_COMPONENT };
["endcomponent"] => { $crate::syntax::untyped::SyntaxKind::TK_ENDCOMPONENT };
["not"] => { $crate::syntax::untyped::SyntaxKind::TK_NOT };
["or"] => { $crate::syntax::untyped::SyntaxKind::TK_OR };
["and"] => { $crate::syntax::untyped::SyntaxKind::TK_AND };
Expand Down Expand Up @@ -752,6 +768,9 @@ impl fmt::Display for SyntaxKind {
SyntaxKind::TK_ENDWITH => "endwith",
SyntaxKind::TK_TTL => "ttl",
SyntaxKind::TK_TAGS => "tags",
SyntaxKind::TK_PROPS => "props",
SyntaxKind::TK_COMPONENT => "component",
SyntaxKind::TK_ENDCOMPONENT => "endcomponent",
SyntaxKind::TK_NOT => "not",
SyntaxKind::TK_OR => "or",
SyntaxKind::TK_AND => "and",
Expand Down