This commit is contained in:
Josh Thomas 2025-01-04 23:26:19 -06:00
parent 0b8ca4061c
commit 660fc64b78
11 changed files with 326 additions and 313 deletions

View file

@ -36,7 +36,12 @@ impl Ast {
pub enum Node {
Text(String),
Comment(String),
Block(BlockNode),
Block {
block_type: BlockType,
name: String,
bits: Vec<String>,
children: Option<Vec<Node>>,
},
Variable {
bits: Vec<String>,
filters: Vec<DjangoFilter>,
@ -44,21 +49,10 @@ pub enum Node {
}
#[derive(Clone, Debug, Serialize)]
pub enum BlockNode {
Standard {
name: String,
bits: Vec<String>,
children: Vec<Node>,
},
Branch {
name: String,
bits: Vec<String>,
children: Vec<Node>,
},
Closing {
name: String,
bits: Vec<String>,
},
pub enum BlockType {
Standard,
Branch,
Closing,
}
#[derive(Clone, Debug, Serialize)]

View file

@ -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(

View file

@ -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: []

View file

@ -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: []

View file

@ -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: []

View file

@ -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: []

View file

@ -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: []

View file

@ -5,42 +5,43 @@ expression: ast
nodes:
- Text: "<div class=\"container\">\n <h1>Header</h1>\n "
- Block:
Standard:
name: if
bits:
- if
- user.is_authenticated
children:
- Text: "\n "
- Comment: This if is unclosed which does matter
- Text: "\n <p>Welcome "
- Variable:
bits:
- user
- name
filters: []
- Text: "</p>\n <div>\n "
- Comment: "This div is unclosed which doesn't matter"
- Text: "\n "
- Block:
Standard:
name: for
bits:
- for
- item
- in
- items
children:
- Text: "\n <span>"
- Variable:
bits:
- item
filters: []
- Text: "</span>\n "
- Block:
Closing:
name: endfor
bits: []
- Text: "\n <footer>Page Footer</footer>\n</div>"
block_type: Standard
name: if
bits:
- if
- user.is_authenticated
children:
- Text: "\n "
- Comment: This if is unclosed which does matter
- Text: "\n <p>Welcome "
- Variable:
bits:
- user
- name
filters: []
- Text: "</p>\n <div>\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 <span>"
- Variable:
bits:
- item
filters: []
- Text: "</span>\n "
- Block:
block_type: Closing
name: endfor
bits: []
children: ~
- Text: "\n <footer>Page Footer</footer>\n</div>"
errors:
- UnclosedTag: if

View file

@ -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

View file

@ -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

View file

@ -5,48 +5,50 @@ expression: ast
nodes:
- Text: "<!DOCTYPE html>\n<html>\n <head>\n <style type=\"text/css\">\n /* Style header */\n .header { color: blue; }\n </style>\n <script type=\"text/javascript\">\n // Init app\n const app = {\n /* Config */\n debug: true\n };\n </script>\n </head>\n <body>\n <!-- Header section -->\n <div class=\"header\" id=\"main\" data-value=\"123\" disabled>\n "
- Block:
Standard:
name: if
bits:
- if
- user.is_authenticated
children:
- Text: "\n "
- Comment: Welcome message
- Text: "\n <h1>Welcome, "
- Variable:
bits:
- user
- name
filters:
- name: title
arguments: []
- name: default
arguments:
- "'Guest'"
- Text: "!</h1>\n "
- Block:
Standard:
name: if
bits:
- if
- user.is_staff
children:
- Text: "\n <span>Admin</span>\n "
- Block:
Branch:
name: else
bits: []
children:
- Text: "\n <span>User</span>\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 <h1>Welcome, "
- Variable:
bits:
- user
- name
filters:
- name: title
arguments: []
- name: default
arguments:
- "'Guest'"
- Text: "!</h1>\n "
- Block:
block_type: Standard
name: if
bits:
- if
- user.is_staff
children:
- Text: "\n <span>Admin</span>\n "
- Block:
block_type: Branch
name: else
bits: []
children:
- Text: "\n <span>User</span>\n "
- Block:
block_type: Closing
name: endif
bits: []
children: ~
- Text: "\n "
- Block:
block_type: Closing
name: endif
bits: []
children: ~
- Text: "\n </div>\n </body>\n</html>"
errors: []