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)] #[derive(Clone, Debug, Default, Serialize)]
pub struct Ast { pub struct Ast {
nodes: Vec<Node>, nodes: Vec<Node>,
errors: Vec<AstError>,
} }
impl Ast { impl Ast {
@ -12,12 +13,20 @@ impl Ast {
&self.nodes &self.nodes
} }
pub fn errors(&self) -> &Vec<AstError> {
&self.errors
}
pub fn add_node(&mut self, node: Node) { pub fn add_node(&mut self, node: Node) {
self.nodes.push(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> { 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); return Err(AstError::EmptyAst);
} }
Ok(self.clone()) Ok(self.clone())
@ -123,10 +132,24 @@ pub enum AttributeValue {
pub type Attributes = BTreeMap<String, AttributeValue>; pub type Attributes = BTreeMap<String, AttributeValue>;
#[derive(Error, Debug)] #[derive(Clone, Debug, Error, Serialize)]
pub enum AstError { pub enum AstError {
#[error("error parsing django tag, recieved empty tag name")] #[error("Empty AST")]
EmptyTag,
#[error("empty ast")]
EmptyAst, 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 Comment: HTML comment
- Django: - Django:
Comment: Django comment Comment: Django comment
errors: []

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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