Simplify errors; Make another pretty error type
This commit is contained in:
parent
ee4f37080e
commit
bda217135e
@ -15,7 +15,7 @@ pub struct As {
|
|||||||
|
|
||||||
impl AbstractTree for As {
|
impl AbstractTree for As {
|
||||||
fn from_syntax(node: Node, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: Node, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "as", node)?;
|
SyntaxError::expect_syntax_node("as", node)?;
|
||||||
|
|
||||||
let expression_node = node.child(0).unwrap();
|
let expression_node = node.child(0).unwrap();
|
||||||
let expression = Expression::from_syntax(expression_node, source, context)?;
|
let expression = Expression::from_syntax(expression_node, source, context)?;
|
||||||
|
@ -23,7 +23,7 @@ impl AbstractTree for Assignment {
|
|||||||
source: &str,
|
source: &str,
|
||||||
context: &Context,
|
context: &Context,
|
||||||
) -> Result<Self, SyntaxError> {
|
) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "assignment", syntax_node)?;
|
SyntaxError::expect_syntax_node("assignment", syntax_node)?;
|
||||||
|
|
||||||
let child_count = syntax_node.child_count();
|
let child_count = syntax_node.child_count();
|
||||||
|
|
||||||
|
@ -16,10 +16,10 @@ pub enum AssignmentOperator {
|
|||||||
impl AbstractTree for AssignmentOperator {
|
impl AbstractTree for AssignmentOperator {
|
||||||
fn from_syntax(
|
fn from_syntax(
|
||||||
node: SyntaxNode,
|
node: SyntaxNode,
|
||||||
source: &str,
|
_source: &str,
|
||||||
_context: &Context,
|
_context: &Context,
|
||||||
) -> Result<Self, SyntaxError> {
|
) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "assignment_operator", node)?;
|
SyntaxError::expect_syntax_node("assignment_operator", node)?;
|
||||||
|
|
||||||
let operator_node = node.child(0).unwrap();
|
let operator_node = node.child(0).unwrap();
|
||||||
let operator = match operator_node.kind() {
|
let operator = match operator_node.kind() {
|
||||||
@ -30,8 +30,7 @@ impl AbstractTree for AssignmentOperator {
|
|||||||
return Err(SyntaxError::UnexpectedSyntaxNode {
|
return Err(SyntaxError::UnexpectedSyntaxNode {
|
||||||
expected: "=, += or -=".to_string(),
|
expected: "=, += or -=".to_string(),
|
||||||
actual: operator_node.kind().to_string(),
|
actual: operator_node.kind().to_string(),
|
||||||
location: operator_node.start_position(),
|
position: node.range().into(),
|
||||||
relevant_source: source[operator_node.byte_range()].to_string(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -34,7 +34,7 @@ impl Block {
|
|||||||
|
|
||||||
impl AbstractTree for Block {
|
impl AbstractTree for Block {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "block", node)?;
|
SyntaxError::expect_syntax_node("block", node)?;
|
||||||
|
|
||||||
let first_child = node.child(0).unwrap();
|
let first_child = node.child(0).unwrap();
|
||||||
let is_async = first_child.kind() == "async";
|
let is_async = first_child.kind() == "async";
|
||||||
|
@ -21,7 +21,7 @@ impl AbstractTree for Command {
|
|||||||
source: &str,
|
source: &str,
|
||||||
_context: &Context,
|
_context: &Context,
|
||||||
) -> Result<Self, SyntaxError> {
|
) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "command", node)?;
|
SyntaxError::expect_syntax_node("command", node)?;
|
||||||
|
|
||||||
let command_text_node = node.child(1).unwrap();
|
let command_text_node = node.child(1).unwrap();
|
||||||
let command_text = source[command_text_node.byte_range()].to_string();
|
let command_text = source[command_text_node.byte_range()].to_string();
|
||||||
|
@ -31,7 +31,7 @@ impl EnumDefinition {
|
|||||||
|
|
||||||
impl AbstractTree for EnumDefinition {
|
impl AbstractTree for EnumDefinition {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "enum_definition", node)?;
|
SyntaxError::expect_syntax_node("enum_definition", node)?;
|
||||||
|
|
||||||
let identifier_node = node.child(1).unwrap();
|
let identifier_node = node.child(1).unwrap();
|
||||||
let identifier = Identifier::from_syntax(identifier_node, source, context)?;
|
let identifier = Identifier::from_syntax(identifier_node, source, context)?;
|
||||||
|
@ -29,7 +29,7 @@ impl EnumPattern {
|
|||||||
|
|
||||||
impl AbstractTree for EnumPattern {
|
impl AbstractTree for EnumPattern {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "enum_pattern", node)?;
|
SyntaxError::expect_syntax_node("enum_pattern", node)?;
|
||||||
|
|
||||||
let enum_name_node = node.child(0).unwrap();
|
let enum_name_node = node.child(0).unwrap();
|
||||||
let name = Identifier::from_syntax(enum_name_node, source, context)?;
|
let name = Identifier::from_syntax(enum_name_node, source, context)?;
|
||||||
|
@ -30,7 +30,7 @@ impl AbstractTree for Expression {
|
|||||||
source: &str,
|
source: &str,
|
||||||
_context: &Context,
|
_context: &Context,
|
||||||
) -> Result<Self, SyntaxError> {
|
) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "expression", node)?;
|
SyntaxError::expect_syntax_node("expression", node)?;
|
||||||
|
|
||||||
let child = if node.child(0).unwrap().is_named() {
|
let child = if node.child(0).unwrap().is_named() {
|
||||||
node.child(0).unwrap()
|
node.child(0).unwrap()
|
||||||
@ -56,8 +56,7 @@ impl AbstractTree for Expression {
|
|||||||
expected: "value, identifier, index, math, logic, function call, as or command"
|
expected: "value, identifier, index, math, logic, function call, as or command"
|
||||||
.to_string(),
|
.to_string(),
|
||||||
actual: child.kind().to_string(),
|
actual: child.kind().to_string(),
|
||||||
location: child.start_position(),
|
position: node.range().into(),
|
||||||
relevant_source: source[child.byte_range()].to_string(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -22,7 +22,7 @@ pub struct For {
|
|||||||
|
|
||||||
impl AbstractTree for For {
|
impl AbstractTree for For {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "for", node)?;
|
SyntaxError::expect_syntax_node("for", node)?;
|
||||||
|
|
||||||
let for_node = node.child(0).unwrap();
|
let for_node = node.child(0).unwrap();
|
||||||
let is_async = match for_node.kind() {
|
let is_async = match for_node.kind() {
|
||||||
@ -32,8 +32,7 @@ impl AbstractTree for For {
|
|||||||
return Err(SyntaxError::UnexpectedSyntaxNode {
|
return Err(SyntaxError::UnexpectedSyntaxNode {
|
||||||
expected: "for or async for".to_string(),
|
expected: "for or async for".to_string(),
|
||||||
actual: for_node.kind().to_string(),
|
actual: for_node.kind().to_string(),
|
||||||
location: for_node.start_position(),
|
position: node.range().into(),
|
||||||
relevant_source: source[for_node.byte_range()].to_string(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -32,7 +32,7 @@ impl FunctionCall {
|
|||||||
|
|
||||||
impl AbstractTree for FunctionCall {
|
impl AbstractTree for FunctionCall {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "function_call", node)?;
|
SyntaxError::expect_syntax_node("function_call", node)?;
|
||||||
|
|
||||||
let function_node = node.child(0).unwrap();
|
let function_node = node.child(0).unwrap();
|
||||||
let function_expression = FunctionExpression::from_syntax(function_node, source, context)?;
|
let function_expression = FunctionExpression::from_syntax(function_node, source, context)?;
|
||||||
|
@ -16,7 +16,7 @@ pub enum FunctionExpression {
|
|||||||
|
|
||||||
impl AbstractTree for FunctionExpression {
|
impl AbstractTree for FunctionExpression {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "function_expression", node)?;
|
SyntaxError::expect_syntax_node("function_expression", node)?;
|
||||||
|
|
||||||
let first_child = node.child(0).unwrap();
|
let first_child = node.child(0).unwrap();
|
||||||
let child = if first_child.is_named() {
|
let child = if first_child.is_named() {
|
||||||
@ -39,8 +39,7 @@ impl AbstractTree for FunctionExpression {
|
|||||||
return Err(SyntaxError::UnexpectedSyntaxNode {
|
return Err(SyntaxError::UnexpectedSyntaxNode {
|
||||||
expected: "identifier, function call, value or index".to_string(),
|
expected: "identifier, function call, value or index".to_string(),
|
||||||
actual: child.kind().to_string(),
|
actual: child.kind().to_string(),
|
||||||
location: child.start_position(),
|
position: node.range().into(),
|
||||||
relevant_source: source[child.byte_range()].to_string(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -53,7 +53,7 @@ impl FunctionNode {
|
|||||||
|
|
||||||
impl AbstractTree for FunctionNode {
|
impl AbstractTree for FunctionNode {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "function", node)?;
|
SyntaxError::expect_syntax_node("function", node)?;
|
||||||
|
|
||||||
let child_count = node.child_count();
|
let child_count = node.child_count();
|
||||||
let mut parameters = Vec::new();
|
let mut parameters = Vec::new();
|
||||||
|
@ -50,7 +50,7 @@ impl AbstractTree for Identifier {
|
|||||||
source: &str,
|
source: &str,
|
||||||
_context: &Context,
|
_context: &Context,
|
||||||
) -> Result<Self, SyntaxError> {
|
) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "identifier", node)?;
|
SyntaxError::expect_syntax_node("identifier", node)?;
|
||||||
|
|
||||||
let text = &source[node.byte_range()];
|
let text = &source[node.byte_range()];
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ pub struct Index {
|
|||||||
|
|
||||||
impl AbstractTree for Index {
|
impl AbstractTree for Index {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "index", node)?;
|
SyntaxError::expect_syntax_node("index", node)?;
|
||||||
|
|
||||||
let collection_node = node.child(0).unwrap();
|
let collection_node = node.child(0).unwrap();
|
||||||
let collection = IndexExpression::from_syntax(collection_node, source, context)?;
|
let collection = IndexExpression::from_syntax(collection_node, source, context)?;
|
||||||
|
@ -15,7 +15,7 @@ pub struct IndexAssignment {
|
|||||||
|
|
||||||
impl AbstractTree for IndexAssignment {
|
impl AbstractTree for IndexAssignment {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "index_assignment", node)?;
|
SyntaxError::expect_syntax_node("index_assignment", node)?;
|
||||||
|
|
||||||
let index_node = node.child(0).unwrap();
|
let index_node = node.child(0).unwrap();
|
||||||
let index = Index::from_syntax(index_node, source, context)?;
|
let index = Index::from_syntax(index_node, source, context)?;
|
||||||
|
@ -16,7 +16,7 @@ pub enum IndexExpression {
|
|||||||
|
|
||||||
impl AbstractTree for IndexExpression {
|
impl AbstractTree for IndexExpression {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "index_expression", node)?;
|
SyntaxError::expect_syntax_node("index_expression", node)?;
|
||||||
|
|
||||||
let first_child = node.child(0).unwrap();
|
let first_child = node.child(0).unwrap();
|
||||||
let child = if first_child.is_named() {
|
let child = if first_child.is_named() {
|
||||||
@ -40,8 +40,7 @@ impl AbstractTree for IndexExpression {
|
|||||||
return Err(SyntaxError::UnexpectedSyntaxNode {
|
return Err(SyntaxError::UnexpectedSyntaxNode {
|
||||||
expected: "value, identifier, index or function call".to_string(),
|
expected: "value, identifier, index or function call".to_string(),
|
||||||
actual: child.kind().to_string(),
|
actual: child.kind().to_string(),
|
||||||
location: child.start_position(),
|
position: node.range().into(),
|
||||||
relevant_source: source[child.byte_range()].to_string(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -15,7 +15,7 @@ pub struct Logic {
|
|||||||
|
|
||||||
impl AbstractTree for Logic {
|
impl AbstractTree for Logic {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "logic", node)?;
|
SyntaxError::expect_syntax_node("logic", node)?;
|
||||||
|
|
||||||
let first_node = node.child(0).unwrap();
|
let first_node = node.child(0).unwrap();
|
||||||
let (left_node, operator_node, right_node) = {
|
let (left_node, operator_node, right_node) = {
|
||||||
|
@ -20,10 +20,10 @@ pub enum LogicOperator {
|
|||||||
impl AbstractTree for LogicOperator {
|
impl AbstractTree for LogicOperator {
|
||||||
fn from_syntax(
|
fn from_syntax(
|
||||||
node: SyntaxNode,
|
node: SyntaxNode,
|
||||||
source: &str,
|
_source: &str,
|
||||||
_context: &Context,
|
_context: &Context,
|
||||||
) -> Result<Self, SyntaxError> {
|
) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "logic_operator", node)?;
|
SyntaxError::expect_syntax_node("logic_operator", node)?;
|
||||||
|
|
||||||
let operator_node = node.child(0).unwrap();
|
let operator_node = node.child(0).unwrap();
|
||||||
let operator = match operator_node.kind() {
|
let operator = match operator_node.kind() {
|
||||||
@ -39,8 +39,7 @@ impl AbstractTree for LogicOperator {
|
|||||||
return Err(SyntaxError::UnexpectedSyntaxNode {
|
return Err(SyntaxError::UnexpectedSyntaxNode {
|
||||||
expected: "==, !=, &&, ||, >, <, >= or <=".to_string(),
|
expected: "==, !=, &&, ||, >, <, >= or <=".to_string(),
|
||||||
actual: operator_node.kind().to_string(),
|
actual: operator_node.kind().to_string(),
|
||||||
location: operator_node.start_position(),
|
position: node.range().into(),
|
||||||
relevant_source: source[operator_node.byte_range()].to_string(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -23,7 +23,7 @@ impl MapNode {
|
|||||||
|
|
||||||
impl AbstractTree for MapNode {
|
impl AbstractTree for MapNode {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "map", node)?;
|
SyntaxError::expect_syntax_node("map", node)?;
|
||||||
|
|
||||||
let mut properties = BTreeMap::new();
|
let mut properties = BTreeMap::new();
|
||||||
let mut current_identifier = None;
|
let mut current_identifier = None;
|
||||||
|
@ -20,7 +20,7 @@ pub struct Match {
|
|||||||
|
|
||||||
impl AbstractTree for Match {
|
impl AbstractTree for Match {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "match", node)?;
|
SyntaxError::expect_syntax_node("match", node)?;
|
||||||
|
|
||||||
let matcher_node = node.child(1).unwrap();
|
let matcher_node = node.child(1).unwrap();
|
||||||
let matcher = Expression::from_syntax(matcher_node, source, context)?;
|
let matcher = Expression::from_syntax(matcher_node, source, context)?;
|
||||||
|
@ -15,7 +15,7 @@ pub enum MatchPattern {
|
|||||||
|
|
||||||
impl AbstractTree for MatchPattern {
|
impl AbstractTree for MatchPattern {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "match_pattern", node)?;
|
SyntaxError::expect_syntax_node("match_pattern", node)?;
|
||||||
|
|
||||||
let child = node.child(0).unwrap();
|
let child = node.child(0).unwrap();
|
||||||
let pattern = match child.kind() {
|
let pattern = match child.kind() {
|
||||||
@ -28,8 +28,7 @@ impl AbstractTree for MatchPattern {
|
|||||||
return Err(SyntaxError::UnexpectedSyntaxNode {
|
return Err(SyntaxError::UnexpectedSyntaxNode {
|
||||||
expected: "enum pattern or value".to_string(),
|
expected: "enum pattern or value".to_string(),
|
||||||
actual: child.kind().to_string(),
|
actual: child.kind().to_string(),
|
||||||
location: child.start_position(),
|
position: node.range().into(),
|
||||||
relevant_source: source[child.byte_range()].to_string(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -18,7 +18,7 @@ pub struct Math {
|
|||||||
|
|
||||||
impl AbstractTree for Math {
|
impl AbstractTree for Math {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "math", node)?;
|
SyntaxError::expect_syntax_node("math", node)?;
|
||||||
|
|
||||||
let left_node = node.child(0).unwrap();
|
let left_node = node.child(0).unwrap();
|
||||||
let left = Expression::from_syntax(left_node, source, context)?;
|
let left = Expression::from_syntax(left_node, source, context)?;
|
||||||
|
@ -17,7 +17,7 @@ pub enum MathOperator {
|
|||||||
impl AbstractTree for MathOperator {
|
impl AbstractTree for MathOperator {
|
||||||
fn from_syntax(
|
fn from_syntax(
|
||||||
node: SyntaxNode,
|
node: SyntaxNode,
|
||||||
source: &str,
|
_source: &str,
|
||||||
_context: &Context,
|
_context: &Context,
|
||||||
) -> Result<Self, SyntaxError> {
|
) -> Result<Self, SyntaxError> {
|
||||||
let operator_node = node.child(0).unwrap();
|
let operator_node = node.child(0).unwrap();
|
||||||
@ -31,8 +31,7 @@ impl AbstractTree for MathOperator {
|
|||||||
return Err(SyntaxError::UnexpectedSyntaxNode {
|
return Err(SyntaxError::UnexpectedSyntaxNode {
|
||||||
expected: "+, -, *, / or %".to_string(),
|
expected: "+, -, *, / or %".to_string(),
|
||||||
actual: operator_node.kind().to_string(),
|
actual: operator_node.kind().to_string(),
|
||||||
location: operator_node.start_position(),
|
position: node.range().into(),
|
||||||
relevant_source: source[operator_node.byte_range()].to_string(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -85,7 +85,7 @@ pub struct Root {
|
|||||||
// top-level statements in the tree.
|
// top-level statements in the tree.
|
||||||
impl AbstractTree for Root {
|
impl AbstractTree for Root {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "root", node)?;
|
SyntaxError::expect_syntax_node("root", node)?;
|
||||||
|
|
||||||
let statement_count = node.child_count();
|
let statement_count = node.child_count();
|
||||||
let mut statements = Vec::with_capacity(statement_count);
|
let mut statements = Vec::with_capacity(statement_count);
|
||||||
|
@ -25,7 +25,7 @@ impl AbstractTree for Statement {
|
|||||||
source: &str,
|
source: &str,
|
||||||
_context: &Context,
|
_context: &Context,
|
||||||
) -> Result<Self, SyntaxError> {
|
) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "statement", node)?;
|
SyntaxError::expect_syntax_node("statement", node)?;
|
||||||
|
|
||||||
let first_child = node.child(0).unwrap();
|
let first_child = node.child(0).unwrap();
|
||||||
let mut is_return = first_child.kind() == "return";
|
let mut is_return = first_child.kind() == "return";
|
||||||
@ -83,7 +83,7 @@ enum StatementKind {
|
|||||||
|
|
||||||
impl AbstractTree for StatementKind {
|
impl AbstractTree for StatementKind {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "statement_kind", node)?;
|
SyntaxError::expect_syntax_node("statement_kind", node)?;
|
||||||
|
|
||||||
let child = node.child(0).unwrap();
|
let child = node.child(0).unwrap();
|
||||||
|
|
||||||
@ -119,8 +119,7 @@ impl AbstractTree for StatementKind {
|
|||||||
expected:
|
expected:
|
||||||
"assignment, index assignment, expression, type_definition, block, return, if...else, while, for or match".to_string(),
|
"assignment, index assignment, expression, type_definition, block, return, if...else, while, for or match".to_string(),
|
||||||
actual: child.kind().to_string(),
|
actual: child.kind().to_string(),
|
||||||
location: child.start_position(),
|
position: node.range().into(),
|
||||||
relevant_source: source[child.byte_range()].to_string(),
|
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ impl StructDefinition {
|
|||||||
|
|
||||||
impl AbstractTree for StructDefinition {
|
impl AbstractTree for StructDefinition {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "struct_definition", node)?;
|
SyntaxError::expect_syntax_node("struct_definition", node)?;
|
||||||
|
|
||||||
let name_node = node.child(1).unwrap();
|
let name_node = node.child(1).unwrap();
|
||||||
let name = Identifier::from_syntax(name_node, source, context)?;
|
let name = Identifier::from_syntax(name_node, source, context)?;
|
||||||
|
@ -147,7 +147,7 @@ impl AbstractTree for Type {
|
|||||||
_source: &str,
|
_source: &str,
|
||||||
context: &Context,
|
context: &Context,
|
||||||
) -> Result<Self, SyntaxError> {
|
) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(_source, "type", node)?;
|
SyntaxError::expect_syntax_node("type", node)?;
|
||||||
|
|
||||||
let type_node = node.child(0).unwrap();
|
let type_node = node.child(0).unwrap();
|
||||||
|
|
||||||
@ -207,8 +207,7 @@ impl AbstractTree for Type {
|
|||||||
return Err(SyntaxError::UnexpectedSyntaxNode {
|
return Err(SyntaxError::UnexpectedSyntaxNode {
|
||||||
expected: "any, bool, float, int, num, str, custom type, (, [ or {".to_string(),
|
expected: "any, bool, float, int, num, str, custom type, (, [ or {".to_string(),
|
||||||
actual: type_node.kind().to_string(),
|
actual: type_node.kind().to_string(),
|
||||||
location: type_node.start_position(),
|
position: node.range().into(),
|
||||||
relevant_source: _source[type_node.byte_range()].to_string(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -23,7 +23,7 @@ impl TypeDefinition {
|
|||||||
|
|
||||||
impl AbstractTree for TypeDefinition {
|
impl AbstractTree for TypeDefinition {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "type_definition", node)?;
|
SyntaxError::expect_syntax_node("type_definition", node)?;
|
||||||
|
|
||||||
let child = node.child(0).unwrap();
|
let child = node.child(0).unwrap();
|
||||||
|
|
||||||
@ -37,8 +37,7 @@ impl AbstractTree for TypeDefinition {
|
|||||||
_ => Err(SyntaxError::UnexpectedSyntaxNode {
|
_ => Err(SyntaxError::UnexpectedSyntaxNode {
|
||||||
expected: "enum or struct definition".to_string(),
|
expected: "enum or struct definition".to_string(),
|
||||||
actual: child.kind().to_string(),
|
actual: child.kind().to_string(),
|
||||||
location: child.start_position(),
|
position: node.range().into(),
|
||||||
relevant_source: source[child.byte_range()].to_string(),
|
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ impl TypeSpecification {
|
|||||||
|
|
||||||
impl AbstractTree for TypeSpecification {
|
impl AbstractTree for TypeSpecification {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "type_specification", node)?;
|
SyntaxError::expect_syntax_node("type_specification", node)?;
|
||||||
|
|
||||||
let type_node = node.child(1).unwrap();
|
let type_node = node.child(1).unwrap();
|
||||||
let r#type = Type::from_syntax(type_node, source, context)?;
|
let r#type = Type::from_syntax(type_node, source, context)?;
|
||||||
|
@ -32,7 +32,7 @@ pub enum ValueNode {
|
|||||||
|
|
||||||
impl AbstractTree for ValueNode {
|
impl AbstractTree for ValueNode {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "value", node)?;
|
SyntaxError::expect_syntax_node("value", node)?;
|
||||||
|
|
||||||
let child = node.child(0).unwrap();
|
let child = node.child(0).unwrap();
|
||||||
let value_node = match child.kind() {
|
let value_node = match child.kind() {
|
||||||
@ -108,8 +108,7 @@ impl AbstractTree for ValueNode {
|
|||||||
"string, integer, float, boolean, range, list, map, option, function, struct or enum"
|
"string, integer, float, boolean, range, list, map, option, function, struct or enum"
|
||||||
.to_string(),
|
.to_string(),
|
||||||
actual: child.kind().to_string(),
|
actual: child.kind().to_string(),
|
||||||
location: child.start_position(),
|
position: node.range().into(),
|
||||||
relevant_source: source[child.byte_range()].to_string(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -16,7 +16,7 @@ pub struct While {
|
|||||||
|
|
||||||
impl AbstractTree for While {
|
impl AbstractTree for While {
|
||||||
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
|
||||||
SyntaxError::expect_syntax_node(source, "while", node)?;
|
SyntaxError::expect_syntax_node("while", node)?;
|
||||||
|
|
||||||
let expression_node = node.child(1).unwrap();
|
let expression_node = node.child(1).unwrap();
|
||||||
let expression = Expression::from_syntax(expression_node, source, context)?;
|
let expression = Expression::from_syntax(expression_node, source, context)?;
|
||||||
|
@ -7,6 +7,7 @@ pub(crate) mod rw_lock_error;
|
|||||||
mod syntax_error;
|
mod syntax_error;
|
||||||
mod validation_error;
|
mod validation_error;
|
||||||
|
|
||||||
|
use colored::Colorize;
|
||||||
use lyneate::Report;
|
use lyneate::Report;
|
||||||
pub use runtime_error::RuntimeError;
|
pub use runtime_error::RuntimeError;
|
||||||
pub use syntax_error::SyntaxError;
|
pub use syntax_error::SyntaxError;
|
||||||
@ -35,15 +36,33 @@ impl Error {
|
|||||||
/// The `source` argument should be the full source code document that was
|
/// The `source` argument should be the full source code document that was
|
||||||
/// used to create this error.
|
/// used to create this error.
|
||||||
pub fn create_report(&self, source: &str) -> String {
|
pub fn create_report(&self, source: &str) -> String {
|
||||||
let markers = if let Error::Syntax(SyntaxError::InvalidSource { source, position }) = self {
|
let markers = if let Error::Syntax(SyntaxError::InvalidSource { position }) = self {
|
||||||
vec![(
|
vec![(
|
||||||
position.start_byte..position.end_byte,
|
position.start_byte..position.end_byte,
|
||||||
format!(
|
format!(
|
||||||
"Invalid syntax from ({}, {}) to ({}, {}).",
|
"Invalid syntax from ({}, {}) to ({}, {}).",
|
||||||
position.start_row,
|
position.start_row,
|
||||||
position.start_column,
|
position.start_column,
|
||||||
|
position.end_row,
|
||||||
position.end_column,
|
position.end_column,
|
||||||
position.end_row
|
),
|
||||||
|
(255, 200, 100),
|
||||||
|
)]
|
||||||
|
} else if let Error::Syntax(SyntaxError::UnexpectedSyntaxNode {
|
||||||
|
expected,
|
||||||
|
actual,
|
||||||
|
position,
|
||||||
|
}) = self
|
||||||
|
{
|
||||||
|
vec![(
|
||||||
|
position.start_byte..position.end_byte,
|
||||||
|
format!(
|
||||||
|
"Unexpected syntax from ({}, {}) to ({}, {}). {}",
|
||||||
|
position.start_row,
|
||||||
|
position.start_column,
|
||||||
|
position.end_row,
|
||||||
|
position.end_column,
|
||||||
|
format!("Expected {} but got {}.", expected, actual).dimmed(),
|
||||||
),
|
),
|
||||||
(255, 100, 100),
|
(255, 100, 100),
|
||||||
)]
|
)]
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::fmt::{self, Display, Formatter};
|
use std::fmt::{self, Display, Formatter};
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tree_sitter::{Node as SyntaxNode, Point};
|
use tree_sitter::Node as SyntaxNode;
|
||||||
|
|
||||||
use crate::SourcePosition;
|
use crate::SourcePosition;
|
||||||
|
|
||||||
@ -11,7 +11,6 @@ use super::rw_lock_error::RwLockError;
|
|||||||
pub enum SyntaxError {
|
pub enum SyntaxError {
|
||||||
/// Invalid user input.
|
/// Invalid user input.
|
||||||
InvalidSource {
|
InvalidSource {
|
||||||
source: String,
|
|
||||||
position: SourcePosition,
|
position: SourcePosition,
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -20,35 +19,25 @@ pub enum SyntaxError {
|
|||||||
UnexpectedSyntaxNode {
|
UnexpectedSyntaxNode {
|
||||||
expected: String,
|
expected: String,
|
||||||
actual: String,
|
actual: String,
|
||||||
|
position: SourcePosition,
|
||||||
#[serde(skip)]
|
|
||||||
location: Point,
|
|
||||||
|
|
||||||
relevant_source: String,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SyntaxError {
|
impl SyntaxError {
|
||||||
pub fn expect_syntax_node(
|
pub fn expect_syntax_node(expected: &str, actual: SyntaxNode) -> Result<(), SyntaxError> {
|
||||||
source: &str,
|
|
||||||
expected: &str,
|
|
||||||
actual: SyntaxNode,
|
|
||||||
) -> Result<(), SyntaxError> {
|
|
||||||
log::info!("Converting {} to abstract node", actual.kind());
|
log::info!("Converting {} to abstract node", actual.kind());
|
||||||
|
|
||||||
if expected == actual.kind() {
|
if expected == actual.kind() {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else if actual.is_error() {
|
} else if actual.is_error() {
|
||||||
Err(SyntaxError::InvalidSource {
|
Err(SyntaxError::InvalidSource {
|
||||||
source: source[actual.byte_range()].to_string(),
|
|
||||||
position: SourcePosition::from(actual.range()),
|
position: SourcePosition::from(actual.range()),
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
Err(SyntaxError::UnexpectedSyntaxNode {
|
Err(SyntaxError::UnexpectedSyntaxNode {
|
||||||
expected: expected.to_string(),
|
expected: expected.to_string(),
|
||||||
actual: actual.kind().to_string(),
|
actual: actual.kind().to_string(),
|
||||||
location: actual.start_position(),
|
position: SourcePosition::from(actual.range()),
|
||||||
relevant_source: source[actual.byte_range()].to_string(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -61,7 +50,7 @@ impl From<RwLockError> for SyntaxError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Display for SyntaxError {
|
impl Display for SyntaxError {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, _f: &mut Formatter<'_>) -> fmt::Result {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,11 +35,9 @@
|
|||||||
//! Ok(Value::Integer(10))
|
//! Ok(Value::Integer(10))
|
||||||
//! );
|
//! );
|
||||||
//! ```
|
//! ```
|
||||||
use tree_sitter::{Node as SyntaxNode, Parser, Tree as SyntaxTree, TreeCursor};
|
use tree_sitter::{Parser, Tree as SyntaxTree};
|
||||||
|
|
||||||
use crate::{
|
use crate::{language, AbstractTree, Context, Error, Format, Root, Value};
|
||||||
error::SyntaxError, language, AbstractTree, Context, Error, Format, Root, SourcePosition, Value,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Interpret the given source code. Returns the value of last statement or the
|
/// Interpret the given source code. Returns the value of last statement or the
|
||||||
/// first error encountered.
|
/// first error encountered.
|
||||||
|
Loading…
Reference in New Issue
Block a user