add errors

This commit is contained in:
Josh Thomas 2025-01-04 16:23:36 -06:00
parent 7bb9904f81
commit a4b6742c6a
15 changed files with 42 additions and 5 deletions

View file

@ -5,6 +5,7 @@ use thiserror::Error;
#[derive(Clone, Debug, Default, Serialize)]
pub struct Ast {
nodes: Vec<Node>,
errors: Vec<AstError>,
}
impl Ast {
@ -12,12 +13,20 @@ impl Ast {
&self.nodes
}
pub fn errors(&self) -> &Vec<AstError> {
&self.errors
}
pub fn add_node(&mut self, node: Node) {
self.nodes.push(node);
}
pub fn add_error(&mut self, error: AstError) {
self.errors.push(error);
}
pub fn finalize(&mut self) -> Result<Ast, AstError> {
if self.nodes.is_empty() {
if self.nodes.is_empty() && self.errors.is_empty() {
return Err(AstError::EmptyAst);
}
Ok(self.clone())
@ -123,10 +132,24 @@ pub enum AttributeValue {
pub type Attributes = BTreeMap<String, AttributeValue>;
#[derive(Error, Debug)]
#[derive(Clone, Debug, Error, Serialize)]
pub enum AstError {
#[error("error parsing django tag, recieved empty tag name")]
EmptyTag,
#[error("empty ast")]
#[error("Empty AST")]
EmptyAst,
#[error("Stream error: {0}")]
StreamError(String),
#[error("Unclosed tag: {0}")]
UnclosedTag(String),
#[error("Unexpected tag: {0}")]
UnexpectedTag(String),
#[error("Invalid tag: {0}")]
InvalidTag(String),
#[error("Block error: {0} in {1}")]
BlockError(String, String),
#[error("Argument error: {0} - {1}")]
ArgumentError(String, String),
#[error("Unexpected token")]
UnexpectedToken,
#[error("Unexpected end of file")]
UnexpectedEof,
}

View file

@ -7,3 +7,4 @@ nodes:
Comment: HTML comment
- Django:
Comment: Django comment
errors: []

View file

@ -36,3 +36,4 @@ nodes:
Closing:
name: endif
bits: []
errors: []

View file

@ -30,3 +30,4 @@ nodes:
Closing:
name: endfor
bits: []
errors: []

View file

@ -17,3 +17,4 @@ nodes:
Closing:
name: endif
bits: []
errors: []

View file

@ -11,3 +11,4 @@ nodes:
filters:
- name: title
arguments: []
errors: []

View file

@ -15,3 +15,4 @@ nodes:
arguments: []
- name: upper
arguments: []
errors: []

View file

@ -107,3 +107,4 @@ nodes:
name: endif
bits: []
- Text: "!"
errors: []

View file

@ -37,3 +37,4 @@ nodes:
Closing:
name: endfor
bits: []
errors: []

View file

@ -128,3 +128,4 @@ nodes:
Closing:
name: endif
bits: []
errors: []

View file

@ -5,3 +5,4 @@ expression: ast
nodes:
- Html:
Doctype: "!DOCTYPE"
errors: []

View file

@ -11,3 +11,4 @@ nodes:
Value: container
children:
- Text: Hello
errors: []

View file

@ -9,3 +9,4 @@ nodes:
attributes:
type:
Value: text
errors: []

View file

@ -19,3 +19,4 @@ nodes:
content: "Multi-line\n comment"
kind: MultiLine
- Text: console.log(x);
errors: []

View file

@ -16,3 +16,4 @@ nodes:
- Text: "{"
- Text: "color: blue;"
- Text: "}"
errors: []