Skip to content

Commit d63aa16

Browse files
rename Ast to NodeList to reflect flat strucutre (#217)
1 parent d60b597 commit d63aa16

File tree

5 files changed

+62
-62
lines changed

5 files changed

+62
-62
lines changed

crates/djls-templates/src/ast.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use thiserror::Error;
44
use crate::tokens::Token;
55

66
#[salsa::tracked(debug)]
7-
pub struct Ast<'db> {
7+
pub struct NodeList<'db> {
88
#[tracked]
99
#[returns(ref)]
1010
pub nodelist: Vec<Node<'db>>,
@@ -144,9 +144,9 @@ impl Span {
144144
}
145145

146146
#[derive(Clone, Debug, Error, PartialEq, Eq, Serialize)]
147-
pub enum AstError {
148-
#[error("Empty AST")]
149-
EmptyAst,
147+
pub enum NodeListError {
148+
#[error("Empty NodeList")]
149+
EmptyNodeList,
150150
#[error("Invalid tag '{tag}' structure: {reason}")]
151151
InvalidTagStructure {
152152
tag: String,
@@ -182,38 +182,38 @@ pub enum AstError {
182182
TooManyArguments { tag: String, max: usize, span: Span },
183183
}
184184

185-
impl AstError {
185+
impl NodeListError {
186186
/// Get the span start and length of this error, if available
187187
#[must_use]
188188
pub fn span(&self) -> Option<(u32, u32)> {
189189
match self {
190-
AstError::UnbalancedStructure { opening_span, .. } => {
190+
NodeListError::UnbalancedStructure { opening_span, .. } => {
191191
Some((opening_span.start, opening_span.length))
192192
}
193-
AstError::InvalidTagStructure { span, .. }
194-
| AstError::InvalidNode { span, .. }
195-
| AstError::UnclosedTag { span, .. }
196-
| AstError::OrphanedTag { span, .. }
197-
| AstError::UnmatchedBlockName { span, .. }
198-
| AstError::MissingRequiredArguments { span, .. }
199-
| AstError::TooManyArguments { span, .. } => Some((span.start, span.length)),
200-
AstError::EmptyAst => None,
193+
NodeListError::InvalidTagStructure { span, .. }
194+
| NodeListError::InvalidNode { span, .. }
195+
| NodeListError::UnclosedTag { span, .. }
196+
| NodeListError::OrphanedTag { span, .. }
197+
| NodeListError::UnmatchedBlockName { span, .. }
198+
| NodeListError::MissingRequiredArguments { span, .. }
199+
| NodeListError::TooManyArguments { span, .. } => Some((span.start, span.length)),
200+
NodeListError::EmptyNodeList => None,
201201
}
202202
}
203203

204204
/// Get a diagnostic code string for this error type
205205
#[must_use]
206206
pub fn diagnostic_code(&self) -> &'static str {
207207
match self {
208-
AstError::EmptyAst => "T001",
209-
AstError::InvalidTagStructure { .. } => "T002",
210-
AstError::UnbalancedStructure { .. } => "T003",
211-
AstError::InvalidNode { .. } => "T004",
212-
AstError::UnclosedTag { .. } => "T005",
213-
AstError::OrphanedTag { .. } => "T006",
214-
AstError::UnmatchedBlockName { .. } => "T007",
215-
AstError::MissingRequiredArguments { .. } => "T008",
216-
AstError::TooManyArguments { .. } => "T009",
208+
NodeListError::EmptyNodeList => "T001",
209+
NodeListError::InvalidTagStructure { .. } => "T002",
210+
NodeListError::UnbalancedStructure { .. } => "T003",
211+
NodeListError::InvalidNode { .. } => "T004",
212+
NodeListError::UnclosedTag { .. } => "T005",
213+
NodeListError::OrphanedTag { .. } => "T006",
214+
NodeListError::UnmatchedBlockName { .. } => "T007",
215+
NodeListError::MissingRequiredArguments { .. } => "T008",
216+
NodeListError::TooManyArguments { .. } => "T009",
217217
}
218218
}
219219
}

crates/djls-templates/src/error.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use serde::Serialize;
22
use thiserror::Error;
33

4-
use crate::ast::AstError;
4+
use crate::ast::NodeListError;
55
use crate::lexer::LexerError;
66
use crate::parser::ParserError;
77

@@ -14,7 +14,7 @@ pub enum TemplateError {
1414
Parser(String),
1515

1616
#[error("{0}")]
17-
Validation(#[from] AstError),
17+
Validation(#[from] NodeListError),
1818

1919
#[error("IO error: {0}")]
2020
Io(String),
@@ -45,7 +45,7 @@ impl TemplateError {
4545
#[must_use]
4646
pub fn span(&self) -> Option<(u32, u32)> {
4747
match self {
48-
TemplateError::Validation(ast_error) => ast_error.span(),
48+
TemplateError::Validation(nodelist_error) => nodelist_error.span(),
4949
_ => None,
5050
}
5151
}
@@ -55,7 +55,7 @@ impl TemplateError {
5555
match self {
5656
TemplateError::Lexer(_) => "T200",
5757
TemplateError::Parser(_) => "T100",
58-
TemplateError::Validation(ast_error) => ast_error.diagnostic_code(),
58+
TemplateError::Validation(nodelist_error) => nodelist_error.diagnostic_code(),
5959
TemplateError::Io(_) => "T900",
6060
TemplateError::Config(_) => "T901",
6161
}

crates/djls-templates/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ pub mod templatetags;
5454
mod tokens;
5555
pub mod validation;
5656

57-
pub use ast::Ast;
5857
use ast::LineOffsets;
58+
pub use ast::NodeList;
5959
pub use db::Db;
6060
pub use db::TemplateDiagnostic;
6161
use djls_workspace::db::SourceFile;
@@ -100,15 +100,15 @@ fn lex_template(db: &dyn Db, file: SourceFile) -> TokenStream<'_> {
100100
/// This is the second phase of template processing. It takes the token stream
101101
/// from lexing and builds an Abstract Syntax Tree.
102102
#[salsa::tracked]
103-
fn parse_template(db: &dyn Db, file: SourceFile) -> Ast<'_> {
103+
fn parse_template(db: &dyn Db, file: SourceFile) -> NodeList<'_> {
104104
let token_stream = lex_template(db, file);
105105

106106
// Check if lexing produced no tokens (likely due to an error)
107107
if token_stream.stream(db).is_empty() {
108108
// Return empty AST for error recovery
109109
let empty_nodelist = Vec::new();
110110
let empty_offsets = LineOffsets::default();
111-
return Ast::new(db, empty_nodelist, empty_offsets);
111+
return NodeList::new(db, empty_nodelist, empty_offsets);
112112
}
113113

114114
// Parser needs the TokenStream<'db>
@@ -130,7 +130,7 @@ fn parse_template(db: &dyn Db, file: SourceFile) -> Ast<'_> {
130130
// Return empty AST
131131
let empty_nodelist = Vec::new();
132132
let empty_offsets = LineOffsets::default();
133-
Ast::new(db, empty_nodelist, empty_offsets)
133+
NodeList::new(db, empty_nodelist, empty_offsets)
134134
}
135135
}
136136
}
@@ -207,7 +207,7 @@ fn accumulate_error(db: &dyn Db, error: &TemplateError, line_offsets: &LineOffse
207207
/// analyze_template::accumulated::<TemplateDiagnostic>(db, file);
208208
/// ```
209209
#[salsa::tracked]
210-
pub fn analyze_template(db: &dyn Db, file: SourceFile) -> Option<Ast<'_>> {
210+
pub fn analyze_template(db: &dyn Db, file: SourceFile) -> Option<NodeList<'_>> {
211211
if file.kind(db) != FileKind::Template {
212212
return None;
213213
}

crates/djls-templates/src/parser.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use thiserror::Error;
22

3-
use crate::ast::Ast;
4-
use crate::ast::AstError;
53
use crate::ast::CommentNode;
64
use crate::ast::FilterName;
75
use crate::ast::Node;
6+
use crate::ast::NodeList;
7+
use crate::ast::NodeListError;
88
use crate::ast::Span;
99
use crate::ast::TagName;
1010
use crate::ast::TagNode;
@@ -35,7 +35,7 @@ impl<'db> Parser<'db> {
3535
}
3636
}
3737

38-
pub fn parse(&mut self) -> Result<(Ast<'db>, Vec<ParserError>), ParserError> {
38+
pub fn parse(&mut self) -> Result<(NodeList<'db>, Vec<ParserError>), ParserError> {
3939
let mut nodelist = Vec::new();
4040
let mut line_offsets = crate::ast::LineOffsets::default();
4141

@@ -64,8 +64,8 @@ impl<'db> Parser<'db> {
6464
}
6565
}
6666

67-
// Create the tracked Ast struct
68-
let ast = Ast::new(self.db, nodelist, line_offsets);
67+
// Create the tracked NodeList struct
68+
let ast = NodeList::new(self.db, nodelist, line_offsets);
6969

7070
Ok((ast, std::mem::take(&mut self.errors)))
7171
}
@@ -297,7 +297,7 @@ pub enum ParserError {
297297
#[error("Stream error: {kind:?}")]
298298
StreamError { kind: StreamError },
299299
#[error("AST error: {0}")]
300-
Ast(#[from] AstError),
300+
NodeList(#[from] NodeListError),
301301
}
302302

303303
impl ParserError {
@@ -360,7 +360,7 @@ mod tests {
360360
}
361361

362362
#[salsa::tracked]
363-
fn parse_test_template(db: &dyn TemplateDb, template: TestTemplate) -> Ast<'_> {
363+
fn parse_test_template(db: &dyn TemplateDb, template: TestTemplate) -> NodeList<'_> {
364364
let source = template.source(db);
365365
let tokens = Lexer::new(source).tokenize().unwrap();
366366
let token_stream = TokenStream::new(db, tokens);
@@ -423,7 +423,7 @@ mod tests {
423423
}
424424
}
425425

426-
fn convert_ast_for_testing(ast: Ast<'_>, db: &dyn crate::db::Db) -> TestAst {
426+
fn convert_ast_for_testing(ast: NodeList<'_>, db: &dyn crate::db::Db) -> TestAst {
427427
TestAst {
428428
nodelist: convert_nodelist_for_testing(ast.nodelist(db), db),
429429
line_offsets: ast.line_offsets(db).0.clone(),

0 commit comments

Comments
 (0)