Skip to content
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
49 changes: 27 additions & 22 deletions crates/djls-templates/src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
use crate::tokens::Token;
use crate::tokens::{Token, TokenStream, TokenType};
use serde::Serialize;
use thiserror::Error;

#[derive(Clone, Default, Debug, Serialize)]
pub struct NodeList {
nodes: Vec<Node>,
#[derive(Clone, Debug, Default, Serialize)]
pub struct Ast {
nodelist: Vec<Node>,
line_offsets: LineOffsets,
}

impl NodeList {
pub fn nodes(&self) -> &Vec<Node> {
&self.nodes
impl Ast {
pub fn nodelist(&self) -> &Vec<Node> {
&self.nodelist
}

pub fn line_offsets(&self) -> &LineOffsets {
&self.line_offsets
}

pub fn add_node(&mut self, node: Node) {
self.nodes.push(node);
self.nodelist.push(node);
}

pub fn set_line_offsets(&mut self, line_offsets: LineOffsets) {
self.line_offsets = line_offsets
}

pub fn finalize(&mut self) -> NodeList {
self.clone()
pub fn set_line_offsets(&mut self, tokens: &TokenStream) {
for token in tokens.tokens() {
if let TokenType::Newline = token.token_type() {
if let Some(start) = token.start() {
// Add offset for next line
self.line_offsets.add_line(start + 1);
}
}
}
}
}

#[derive(Clone, Default, Debug, Serialize)]
#[derive(Clone, Debug, Serialize)]
pub struct LineOffsets(pub Vec<u32>);

impl LineOffsets {
pub fn new() -> Self {
Self(vec![0])
}

pub fn add_line(&mut self, offset: u32) {
self.0.push(offset);
}
Expand Down Expand Up @@ -67,6 +66,12 @@ impl LineOffsets {
}
}

impl Default for LineOffsets {
fn default() -> Self {
Self(vec![0])
}
}

#[derive(Clone, Debug, Serialize)]
pub enum Node {
Tag {
Expand Down Expand Up @@ -155,13 +160,13 @@ mod tests {

#[test]
fn test_new_starts_at_zero() {
let offsets = LineOffsets::new();
let offsets = LineOffsets::default();
assert_eq!(offsets.position_to_line_col(0), (1, 0)); // Line 1, column 0
}

#[test]
fn test_start_of_lines() {
let mut offsets = LineOffsets::new();
let mut offsets = LineOffsets::default();
offsets.add_line(10); // Line 2 starts at offset 10
offsets.add_line(25); // Line 3 starts at offset 25

Expand All @@ -183,7 +188,7 @@ mod tests {
assert!(errors.is_empty());

// Find the variable node
let nodes = nodelist.nodes();
let nodes = nodelist.nodelist();
let var_node = nodes
.iter()
.find(|n| matches!(n, Node::Variable { .. }))
Expand Down
4 changes: 2 additions & 2 deletions crates/djls-templates/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod parser;
mod tagspecs;
mod tokens;

use ast::NodeList;
use ast::Ast;
pub use error::{to_lsp_diagnostic, QuickFix, TemplateError};

use lexer::Lexer;
Expand All @@ -18,7 +18,7 @@ pub use parser::{Parser, ParserError};
///
/// Returns a `Result` containing a tuple of `(Ast, Vec<ParserError>)` on success,
/// or a `ParserError` on failure.
pub fn parse_template(source: &str) -> Result<(NodeList, Vec<TemplateError>), TemplateError> {
pub fn parse_template(source: &str) -> Result<(Ast, Vec<TemplateError>), TemplateError> {
let tokens = Lexer::new(source)
.tokenize()
.map_err(|e| TemplateError::Lexer(e.to_string()))?;
Expand Down
24 changes: 6 additions & 18 deletions crates/djls-templates/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ast::{AstError, LineOffsets, Node, NodeList, Span};
use crate::ast::{Ast, AstError, Node, Span};
use crate::lexer::LexerError;
use crate::tokens::{Token, TokenStream, TokenType};
use thiserror::Error;
Expand All @@ -18,25 +18,14 @@ impl Parser {
}
}

pub fn parse(&mut self) -> Result<(NodeList, Vec<ParserError>), ParserError> {
let mut nodelist = NodeList::default();
let mut line_offsets = LineOffsets::new();

for token in self.tokens.tokens() {
if let TokenType::Newline = token.token_type() {
if let Some(start) = token.start() {
// Add offset for next line
line_offsets.add_line(start + 1);
}
}
}

self.current = 0;
pub fn parse(&mut self) -> Result<(Ast, Vec<ParserError>), ParserError> {
let mut ast = Ast::default();
ast.set_line_offsets(&self.tokens);

while !self.is_at_end() {
match self.next_node() {
Ok(node) => {
nodelist.add_node(node);
ast.add_node(node);
}
Err(err) => {
if !self.is_at_end() {
Expand All @@ -47,8 +36,7 @@ impl Parser {
}
}

nodelist.set_line_offsets(line_offsets);
Ok((nodelist.finalize(), std::mem::take(&mut self.errors)))
Ok((ast.clone(), std::mem::take(&mut self.errors)))
}

fn next_node(&mut self) -> Result<Node, ParserError> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Text:
content: "<!-- HTML comment -->"
span:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Tag:
name: if
bits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Tag:
name: for
bits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Tag:
name: if
bits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Tag:
name: url
bits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Variable:
var: user.name
filters: []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Variable:
var: user.name
filters:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Variable:
var: value
filters:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Text:
content: "Welcome,"
span:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Tag:
name: for
bits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Text:
content: "<div class=\"container\">"
span:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Tag:
name: for
bits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Tag:
name: if
bits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Text:
content: "<div>"
span:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Text:
content: "<script>console.log('test');"
span:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Text:
content: "<style>body { color: blue;"
span:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Text:
content: "<!DOCTYPE html>"
span:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Text:
content: "<!DOCTYPE html>"
span:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Text:
content: "<div class=\"container\">Hello</div>"
span:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Text:
content: "<input type=\"text\" />"
span:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Text:
content: "<script type=\"text/javascript\">"
span:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Text:
content: "<style type=\"text/css\">"
span:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Text:
content: hello
span:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Text:
content: hello
span:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Text:
content: hello
span:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodes:
nodelist:
- Text:
content: hello
span:
Expand Down