Skip to content

Commit

Permalink
leading whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuadavidthomas committed Jan 6, 2025
1 parent ad9d6e5 commit 55b2b92
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 123 deletions.
80 changes: 44 additions & 36 deletions crates/djls-template-ast/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ impl Parser {
fn next_node(&mut self) -> Result<Node, ParserError> {
let token = self.peek()?;
match token.token_type() {
TokenType::Comment(content, start, end) => {
self.consume()?;
self.parse_comment(content, start, end.as_deref())
}
TokenType::Eof => Err(ParserError::Ast(AstError::StreamError("AtEnd".to_string()))),
TokenType::DjangoBlock(content) => {
self.consume()?;
self.parse_django_block(content)
Expand All @@ -62,24 +67,39 @@ impl Parser {
self.consume()?;
self.parse_django_variable(content)
}
TokenType::Comment(content, start, end) => {
self.consume()?;
self.parse_comment(content, start, end.as_deref())
}
TokenType::Text(_)
| TokenType::Whitespace(_)
| TokenType::Newline
TokenType::HtmlTagClose(_)
| TokenType::HtmlTagOpen(_)
| TokenType::HtmlTagClose(_)
| TokenType::HtmlTagVoid(_)
| TokenType::ScriptTagOpen(_)
| TokenType::Newline
| TokenType::ScriptTagClose(_)
| TokenType::ScriptTagOpen(_)
| TokenType::StyleTagClose(_)
| TokenType::StyleTagOpen(_)
| TokenType::StyleTagClose(_) => {
| TokenType::Text(_)
| TokenType::Whitespace(_) => {
self.consume()?;
self.parse_text()
}
TokenType::Eof => Err(ParserError::Ast(AstError::StreamError("AtEnd".to_string()))),
}
}

fn parse_comment(
&mut self,
content: &str,
start: &str,
end: Option<&str>,
) -> Result<Node, ParserError> {
let token = self.peek_previous()?;
let start_pos = token.start().unwrap_or(0);

// Only treat Django comments as Comment nodes
if start == "{#" && end == Some("#}") {
Ok(Node::Comment {
content: content.to_string(),
span: Span::new(start_pos, token.token_type().len().unwrap_or(0) as u32),
})
} else {
self.parse_text()
}
}

Expand Down Expand Up @@ -209,7 +229,7 @@ impl Parser {
..
} = &block
{
if let Some(expected_closing) = &spec.closing {
if let Some(_expected_closing) = &spec.closing {
self.errors.push(ParserError::Ast(AstError::UnclosedTag(
tag_ref.name.clone(),
)));
Expand Down Expand Up @@ -245,24 +265,32 @@ impl Parser {
filters.push(DjangoFilter {
name: filter_name.to_string(),
args: filter_args,
span: Span::new(start + 4, content.len() as u32), // Account for {{ and space
span: Span::new(start + 4, content.len() as u32),
});
}
}

Ok(Node::Variable {
bits,
filters,
span: Span::new(start + 3, content.len() as u32), // Account for {{ and space
span: Span::new(start + 3, content.len() as u32),
})
}

fn parse_text(&mut self) -> Result<Node, ParserError> {
let start_token = self.peek_previous()?;
let start_pos = start_token.start().unwrap_or(0);

if matches!(start_token.token_type(), TokenType::Newline) {
return self.next_node();
match start_token.token_type() {
TokenType::Newline => return self.next_node(),
TokenType::Whitespace(_)
if self
.peek_at(-2)
.map_or(false, |t| matches!(t.token_type(), TokenType::Newline)) =>
{
return self.next_node()
}
_ => {}
}

let mut text = start_token.token_type().to_string();
Expand Down Expand Up @@ -294,26 +322,6 @@ impl Parser {
}
}

fn parse_comment(
&mut self,
content: &str,
start: &str,
end: Option<&str>,
) -> Result<Node, ParserError> {
let token = self.peek_previous()?;
let start_pos = token.start().unwrap_or(0);

// Only treat Django comments as Comment nodes
if start == "{#" && end == Some("#}") {
Ok(Node::Comment {
content: content.to_string(),
span: Span::new(start_pos, token.token_type().len().unwrap_or(0) as u32),
})
} else {
self.parse_text()
}
}

fn peek(&self) -> Result<Token, ParserError> {
self.peek_at(0)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ nodes:
assignment: ~
nodes:
- Text:
content: " (no groups)"
content: (no groups)
span:
start: 290
length: 19
start: 298
length: 11
closing:
Closing:
tag:
Expand Down Expand Up @@ -218,10 +218,10 @@ nodes:
assignment: ~
nodes:
- Text:
content: " Guest"
content: Guest
span:
start: 338
length: 9
start: 342
length: 5
closing:
Closing:
tag:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ nodes:
start: 0
length: 23
- Text:
content: " <h1>Header</h1>"
content: "<h1>Header</h1>"
span:
start: 24
length: 19
start: 28
length: 15
- Block:
Block:
tag:
Expand All @@ -34,10 +34,10 @@ nodes:
start: 87
length: 43
- Text:
content: " <p>Welcome "
content: "<p>Welcome "
span:
start: 131
length: 19
start: 139
length: 11
- Variable:
bits:
- user
Expand All @@ -52,10 +52,10 @@ nodes:
start: 165
length: 4
- Text:
content: " <div>"
content: "<div>"
span:
start: 170
length: 13
start: 178
length: 5
- Comment:
content: "This div is unclosed which doesn't matter"
span:
Expand All @@ -79,10 +79,10 @@ nodes:
assignment: ~
nodes:
- Text:
content: " <span>"
content: "<span>"
span:
start: 276
length: 18
start: 288
length: 6
- Variable:
bits:
- item
Expand Down Expand Up @@ -110,10 +110,10 @@ nodes:
assignment: ~
assignments: ~
- Text:
content: " <footer>Page Footer</footer>"
content: "<footer>Page Footer</footer>"
span:
start: 333
length: 32
start: 337
length: 28
- Text:
content: "</div>"
span:
Expand Down
Loading

0 comments on commit 55b2b92

Please sign in to comment.