diff --git a/crates/djls-template-ast/src/ast.rs b/crates/djls-template-ast/src/ast.rs index 6187022..5fd2a74 100644 --- a/crates/djls-template-ast/src/ast.rs +++ b/crates/djls-template-ast/src/ast.rs @@ -36,7 +36,12 @@ impl Ast { pub enum Node { Text(String), Comment(String), - Block(BlockNode), + Block { + block_type: BlockType, + name: String, + bits: Vec, + children: Option>, + }, Variable { bits: Vec, filters: Vec, @@ -44,21 +49,10 @@ pub enum Node { } #[derive(Clone, Debug, Serialize)] -pub enum BlockNode { - Standard { - name: String, - bits: Vec, - children: Vec, - }, - Branch { - name: String, - bits: Vec, - children: Vec, - }, - Closing { - name: String, - bits: Vec, - }, +pub enum BlockType { + Standard, + Branch, + Closing, } #[derive(Clone, Debug, Serialize)] diff --git a/crates/djls-template-ast/src/parser.rs b/crates/djls-template-ast/src/parser.rs index 6232f17..384071e 100644 --- a/crates/djls-template-ast/src/parser.rs +++ b/crates/djls-template-ast/src/parser.rs @@ -1,4 +1,4 @@ -use crate::ast::{Ast, AstError, BlockNode, DjangoFilter, Node}; +use crate::ast::{Ast, AstError, BlockType, DjangoFilter, Node}; use crate::tagspecs::TagSpec; use crate::tokens::{Token, TokenStream, TokenType}; use thiserror::Error; @@ -151,16 +151,19 @@ impl Parser { if spec.closing.as_deref() == Some(&tag) { // If we have a current branch, add it to children if let Some((name, bits, branch_children)) = current_branch { - children.push(Node::Block(BlockNode::Branch { + children.push(Node::Block { + block_type: BlockType::Branch, name, bits, - children: branch_children, - })); + children: Some(branch_children), + }); } - children.push(Node::Block(BlockNode::Closing { + children.push(Node::Block { + block_type: BlockType::Closing, name: tag, bits: vec![], - })); + children: None, + }); found_closing_tag = true; break; } @@ -169,11 +172,12 @@ impl Parser { if let Some(branch) = branches.iter().find(|i| i.name == tag) { // If we have a current branch, add it to children if let Some((name, bits, branch_children)) = current_branch { - children.push(Node::Block(BlockNode::Branch { + children.push(Node::Block { + block_type: BlockType::Branch, name, bits, - children: branch_children, - })); + children: Some(branch_children), + }); } // Create new branch node let branch_bits = if branch.args { @@ -194,11 +198,12 @@ impl Parser { } } // If we get here, it's an unexpected tag - let node = Node::Block(BlockNode::Standard { + let node = Node::Block { + block_type: BlockType::Standard, name: tag_name.clone(), bits: bits.clone(), - children: children.clone(), - }); + children: Some(children.clone()), + }; return Err(ParserError::Ast(AstError::UnexpectedTag(tag), Some(node))); } Err(ParserError::Ast(AstError::StreamError(kind), _)) if kind == "AtEnd" => { @@ -208,11 +213,12 @@ impl Parser { } } - let node = Node::Block(BlockNode::Standard { + let node = Node::Block { + block_type: BlockType::Standard, name: tag_name.clone(), bits, - children, - }); + children: Some(children), + }; if !found_closing_tag { return Err(ParserError::Ast( diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_complex_if_elif.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_complex_if_elif.snap index 112361d..5bb029f 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_complex_if_elif.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_complex_if_elif.snap @@ -4,32 +4,33 @@ expression: ast --- nodes: - Block: - Standard: - name: if - bits: - - if - - x - - ">" - - "0" - children: - - Text: Positive - - Block: - Branch: - name: elif - bits: - - x - - "<" - - "0" - children: - - Text: Negative - - Block: - Branch: - name: else - bits: [] - children: - - Text: Zero - - Block: - Closing: - name: endif - bits: [] + block_type: Standard + name: if + bits: + - if + - x + - ">" + - "0" + children: + - Text: Positive + - Block: + block_type: Branch + name: elif + bits: + - x + - "<" + - "0" + children: + - Text: Negative + - Block: + block_type: Branch + name: else + bits: [] + children: + - Text: Zero + - Block: + block_type: Closing + name: endif + bits: [] + children: ~ errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_for_block.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_for_block.snap index 7571c2c..bacec67 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_for_block.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_for_block.snap @@ -4,26 +4,27 @@ expression: ast --- nodes: - Block: - Standard: - name: for - bits: - - for - - item - - in - - items - children: - - Variable: - bits: - - item - filters: [] - - Block: - Branch: - name: empty - bits: [] - children: - - Text: No items - - Block: - Closing: - name: endfor - bits: [] + block_type: Standard + name: for + bits: + - for + - item + - in + - items + children: + - Variable: + bits: + - item + filters: [] + - Block: + block_type: Branch + name: empty + bits: [] + children: + - Text: No items + - Block: + block_type: Closing + name: endfor + bits: [] + children: ~ errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_if_block.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_if_block.snap index 39973ad..9f663bd 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_if_block.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_django_if_block.snap @@ -4,15 +4,16 @@ expression: ast --- nodes: - Block: - Standard: - name: if - bits: - - if - - user.is_authenticated - children: - - Text: Welcome - - Block: - Closing: - name: endif - bits: [] + block_type: Standard + name: if + bits: + - if + - user.is_authenticated + children: + - Text: Welcome + - Block: + block_type: Closing + name: endif + bits: [] + children: ~ errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap index f0827f9..c728b26 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_mixed_content.snap @@ -5,100 +5,105 @@ expression: ast nodes: - Text: "Welcome, " - Block: - Standard: - name: if - bits: - - if - - user.is_authenticated - children: - - Text: "\n " - - Variable: - bits: - - user - - name - filters: - - name: title - arguments: [] - - name: default - arguments: - - "'Guest'" - - Text: "\n " - - Block: - Standard: - name: for - bits: - - for - - group - - in - - user.groups - children: - - Text: "\n " - - Block: - Standard: - name: if - bits: - - if - - forloop.first - children: - - Text: ( - - Block: - Closing: - name: endif - bits: [] - - Text: "\n " - - Variable: - bits: - - group - - name - filters: [] - - Text: "\n " - - Block: - Standard: - name: if - bits: - - if - - not - - forloop.last - children: - - Text: ", " - - Block: - Closing: - name: endif - bits: [] - - Text: "\n " - - Block: - Standard: - name: if - bits: - - if - - forloop.last - children: - - Text: ) - - Block: - Closing: - name: endif - bits: [] - - Text: "\n " - - Block: - Branch: - name: empty + block_type: Standard + name: if + bits: + - if + - user.is_authenticated + children: + - Text: "\n " + - Variable: + bits: + - user + - name + filters: + - name: title + arguments: [] + - name: default + arguments: + - "'Guest'" + - Text: "\n " + - Block: + block_type: Standard + name: for + bits: + - for + - group + - in + - user.groups + children: + - Text: "\n " + - Block: + block_type: Standard + name: if + bits: + - if + - forloop.first + children: + - Text: ( + - Block: + block_type: Closing + name: endif bits: [] - children: - - Text: "\n (no groups)\n " - - Block: - Closing: - name: endfor + children: ~ + - Text: "\n " + - Variable: + bits: + - group + - name + filters: [] + - Text: "\n " + - Block: + block_type: Standard + name: if + bits: + - if + - not + - forloop.last + children: + - Text: ", " + - Block: + block_type: Closing + name: endif bits: [] - - Text: "\n" - - Block: - Branch: - name: else - bits: [] - children: - - Text: "\n Guest\n" - - Block: - Closing: - name: endif - bits: [] + children: ~ + - Text: "\n " + - Block: + block_type: Standard + name: if + bits: + - if + - forloop.last + children: + - Text: ) + - Block: + block_type: Closing + name: endif + bits: [] + children: ~ + - Text: "\n " + - Block: + block_type: Branch + name: empty + bits: [] + children: + - Text: "\n (no groups)\n " + - Block: + block_type: Closing + name: endfor + bits: [] + children: ~ + - Text: "\n" + - Block: + block_type: Branch + name: else + bits: [] + children: + - Text: "\n Guest\n" + - Block: + block_type: Closing + name: endif + bits: [] + children: ~ - Text: "!" errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_nested_for_if.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_nested_for_if.snap index d93a949..8f920db 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_nested_for_if.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__django__parse_nested_for_if.snap @@ -4,32 +4,34 @@ expression: ast --- nodes: - Block: - Standard: - name: for - bits: - - for - - item - - in - - items - children: - - Block: - Standard: - name: if - bits: - - if - - item.active - children: - - Variable: - bits: - - item - - name - filters: [] - - Block: - Closing: - name: endif - bits: [] - - Block: - Closing: - name: endfor - bits: [] + block_type: Standard + name: for + bits: + - for + - item + - in + - items + children: + - Block: + block_type: Standard + name: if + bits: + - if + - item.active + children: + - Variable: + bits: + - item + - name + filters: [] + - Block: + block_type: Closing + name: endif + bits: [] + children: ~ + - Block: + block_type: Closing + name: endfor + bits: [] + children: ~ errors: [] diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_error_recovery.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_error_recovery.snap index e1427d0..7b7b1a3 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_error_recovery.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_error_recovery.snap @@ -5,42 +5,43 @@ expression: ast nodes: - Text: "
\n

Header

\n " - Block: - Standard: - name: if - bits: - - if - - user.is_authenticated - children: - - Text: "\n " - - Comment: This if is unclosed which does matter - - Text: "\n

Welcome " - - Variable: - bits: - - user - - name - filters: [] - - Text: "

\n
\n " - - Comment: "This div is unclosed which doesn't matter" - - Text: "\n " - - Block: - Standard: - name: for - bits: - - for - - item - - in - - items - children: - - Text: "\n " - - Variable: - bits: - - item - filters: [] - - Text: "\n " - - Block: - Closing: - name: endfor - bits: [] - - Text: "\n
Page Footer
\n
" + block_type: Standard + name: if + bits: + - if + - user.is_authenticated + children: + - Text: "\n " + - Comment: This if is unclosed which does matter + - Text: "\n

Welcome " + - Variable: + bits: + - user + - name + filters: [] + - Text: "

\n
\n " + - Comment: "This div is unclosed which doesn't matter" + - Text: "\n " + - Block: + block_type: Standard + name: for + bits: + - for + - item + - in + - items + children: + - Text: "\n " + - Variable: + bits: + - item + filters: [] + - Text: "\n " + - Block: + block_type: Closing + name: endfor + bits: [] + children: ~ + - Text: "\n
Page Footer
\n
" errors: - UnclosedTag: if diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_django_for.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_django_for.snap index 35a4608..20e02f8 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_django_for.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_django_for.snap @@ -4,18 +4,18 @@ expression: ast --- nodes: - Block: - Standard: - name: for - bits: - - for - - item - - in - - items - children: - - Variable: - bits: - - item - - name - filters: [] + block_type: Standard + name: for + bits: + - for + - item + - in + - items + children: + - Variable: + bits: + - item + - name + filters: [] errors: - UnclosedTag: for diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_django_if.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_django_if.snap index d4ed42d..01bfcfe 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_django_if.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__errors__parse_unclosed_django_if.snap @@ -4,12 +4,12 @@ expression: ast --- nodes: - Block: - Standard: - name: if - bits: - - if - - user.is_authenticated - children: - - Text: Welcome + block_type: Standard + name: if + bits: + - if + - user.is_authenticated + children: + - Text: Welcome errors: - UnclosedTag: if diff --git a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__full_templates__parse_full.snap b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__full_templates__parse_full.snap index f1b9571..2679dfd 100644 --- a/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__full_templates__parse_full.snap +++ b/crates/djls-template-ast/src/snapshots/djls_template_ast__parser__tests__full_templates__parse_full.snap @@ -5,48 +5,50 @@ expression: ast nodes: - Text: "\n\n \n \n \n \n \n \n
\n " - Block: - Standard: - name: if - bits: - - if - - user.is_authenticated - children: - - Text: "\n " - - Comment: Welcome message - - Text: "\n

Welcome, " - - Variable: - bits: - - user - - name - filters: - - name: title - arguments: [] - - name: default - arguments: - - "'Guest'" - - Text: "!

\n " - - Block: - Standard: - name: if - bits: - - if - - user.is_staff - children: - - Text: "\n Admin\n " - - Block: - Branch: - name: else - bits: [] - children: - - Text: "\n User\n " - - Block: - Closing: - name: endif - bits: [] - - Text: "\n " - - Block: - Closing: - name: endif - bits: [] + block_type: Standard + name: if + bits: + - if + - user.is_authenticated + children: + - Text: "\n " + - Comment: Welcome message + - Text: "\n

Welcome, " + - Variable: + bits: + - user + - name + filters: + - name: title + arguments: [] + - name: default + arguments: + - "'Guest'" + - Text: "!

\n " + - Block: + block_type: Standard + name: if + bits: + - if + - user.is_staff + children: + - Text: "\n Admin\n " + - Block: + block_type: Branch + name: else + bits: [] + children: + - Text: "\n User\n " + - Block: + block_type: Closing + name: endif + bits: [] + children: ~ + - Text: "\n " + - Block: + block_type: Closing + name: endif + bits: [] + children: ~ - Text: "\n
\n \n" errors: []