mirror of
https://github.com/gorules/zen.git
synced 2026-10-05 00:02:26 +00:00
fix tests
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use crate::lexer::Operator;
|
||||
use crate::parser::builtin::BuiltInFunction;
|
||||
use rust_decimal::Decimal;
|
||||
use std::cell::Cell;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
@@ -55,9 +56,11 @@ pub enum Node<'a> {
|
||||
impl<'a> Node<'a> {
|
||||
pub fn walk<F>(&self, mut func: F)
|
||||
where
|
||||
F: FnMut(&Self),
|
||||
F: FnMut(&Self) + Clone,
|
||||
{
|
||||
func(self);
|
||||
{
|
||||
func(self);
|
||||
};
|
||||
|
||||
match self {
|
||||
Node::Error(_) => {}
|
||||
@@ -68,51 +71,51 @@ impl<'a> Node<'a> {
|
||||
Node::Pointer => {}
|
||||
Node::Identifier(_) => {}
|
||||
Node::Root => {}
|
||||
Node::TemplateString(parts) => parts.iter().for_each(|n| func(n)),
|
||||
Node::Array(parts) => parts.iter().for_each(|n| func(n)),
|
||||
Node::TemplateString(parts) => parts.iter().for_each(|n| n.walk(func.clone())),
|
||||
Node::Array(parts) => parts.iter().for_each(|n| n.walk(func.clone())),
|
||||
Node::Object(obj) => obj.iter().for_each(|(k, v)| {
|
||||
func(k);
|
||||
func(v);
|
||||
k.walk(func.clone());
|
||||
v.walk(func.clone());
|
||||
}),
|
||||
Node::Closure(closure) => func(closure),
|
||||
Node::Closure(closure) => closure.walk(func.clone()),
|
||||
Node::Member { node, property } => {
|
||||
func(node);
|
||||
func(property);
|
||||
node.walk(func.clone());
|
||||
property.walk(func.clone());
|
||||
}
|
||||
Node::Slice { node, to, from } => {
|
||||
func(node);
|
||||
node.walk(func.clone());
|
||||
if let Some(to) = to {
|
||||
func(to);
|
||||
to.walk(func.clone());
|
||||
}
|
||||
|
||||
if let Some(from) = from {
|
||||
func(from);
|
||||
from.walk(func.clone());
|
||||
}
|
||||
}
|
||||
Node::Interval { right, left, .. } => {
|
||||
func(right);
|
||||
func(left);
|
||||
Node::Interval { left, right, .. } => {
|
||||
left.walk(func.clone());
|
||||
right.walk(func.clone());
|
||||
}
|
||||
Node::Unary { node, .. } => {
|
||||
node.walk(func);
|
||||
}
|
||||
Node::Binary { left, right, .. } => {
|
||||
left.walk(func.clone());
|
||||
right.walk(func.clone());
|
||||
}
|
||||
Node::BuiltIn { arguments, .. } => {
|
||||
arguments.iter().for_each(|n| n.walk(func.clone()));
|
||||
}
|
||||
Node::Conditional {
|
||||
on_true,
|
||||
condition,
|
||||
on_false,
|
||||
} => {
|
||||
func(condition);
|
||||
func(on_true);
|
||||
func(on_false);
|
||||
condition.walk(func.clone());
|
||||
on_true.walk(func.clone());
|
||||
on_false.walk(func.clone());
|
||||
}
|
||||
Node::Unary { node, .. } => {
|
||||
func(node);
|
||||
}
|
||||
Node::Binary { left, right, .. } => {
|
||||
func(left);
|
||||
func(right);
|
||||
}
|
||||
Node::BuiltIn { arguments, .. } => {
|
||||
arguments.iter().for_each(|n| func(n));
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub fn is_error(&self) -> bool {
|
||||
@@ -121,16 +124,16 @@ impl<'a> Node<'a> {
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn has_error(&self) -> bool {
|
||||
let mut has_error = false;
|
||||
let has_error = Cell::new(false);
|
||||
self.walk(|n| {
|
||||
if n.is_error() {
|
||||
has_error = true
|
||||
has_error.set(true);
|
||||
}
|
||||
});
|
||||
|
||||
has_error
|
||||
|
||||
has_error.get()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -80,6 +80,10 @@ impl<'arena, 'token_ref, Flavor> Parser<'arena, 'token_ref, Flavor> {
|
||||
pub(crate) fn current(&self) -> Option<&Token<'arena>> {
|
||||
self.current.get()
|
||||
}
|
||||
|
||||
pub(crate) fn current_kind(&self) -> Option<&TokenKind> {
|
||||
self.current.get().map(|token| &token.kind)
|
||||
}
|
||||
|
||||
fn position(&self) -> usize {
|
||||
self.position.get()
|
||||
|
||||
@@ -14,7 +14,6 @@ const ROOT_NODE: Node<'static> = Node::Identifier("$");
|
||||
impl<'arena, 'token_ref> Parser<'arena, 'token_ref, Unary> {
|
||||
pub fn parse(&self) -> ParserResult<&'arena Node<'arena>> {
|
||||
let result = self.root_expression();
|
||||
println!("{:?}", result);
|
||||
if !self.is_done() {
|
||||
return match self.current() {
|
||||
None => Err(ParserError::TokenOutOfBounds),
|
||||
@@ -59,22 +58,15 @@ impl<'arena, 'token_ref> Parser<'arena, 'token_ref, Unary> {
|
||||
|
||||
fn expression_pair(&self) -> &'arena Node<'arena> {
|
||||
let mut left_node = &ROOT_NODE;
|
||||
let Some(initial_token) = self.current() else {
|
||||
return left_node;
|
||||
};
|
||||
|
||||
if let TokenKind::Operator(Operator::Comparison(_)) = &initial_token.kind {
|
||||
if let Some(TokenKind::Operator(Operator::Comparison(_))) = self.current_kind() {
|
||||
// Skips
|
||||
} else {
|
||||
left_node = self.binary_expression(0);
|
||||
}
|
||||
|
||||
let Some(current_token) = self.current() else {
|
||||
return left_node;
|
||||
};
|
||||
|
||||
match ¤t_token.kind {
|
||||
TokenKind::Operator(Operator::Comparison(comparison)) => {
|
||||
match self.current_kind() {
|
||||
Some(TokenKind::Operator(Operator::Comparison(comparison))) => {
|
||||
self.next();
|
||||
let right_node = self.binary_expression(0);
|
||||
left_node = self.node(Node::Binary {
|
||||
|
||||
@@ -324,7 +324,8 @@ fn failure_tests() {
|
||||
let tokens = lexer.tokenize(test).unwrap();
|
||||
let parser = Parser::try_new(tokens, &bump).unwrap().standard();
|
||||
let ast = parser.parse();
|
||||
assert!(ast.is_err());
|
||||
|
||||
assert!(ast.is_err() || ast.unwrap().has_error());
|
||||
|
||||
bump.reset();
|
||||
}
|
||||
|
||||
@@ -163,7 +163,7 @@ fn failure_tests() {
|
||||
let tokens = lexer.tokenize(test).unwrap();
|
||||
let unary_parser = Parser::try_new(tokens, &bump).unwrap().standard();
|
||||
let ast = unary_parser.parse();
|
||||
assert!(ast.is_err());
|
||||
assert!(ast.is_err() || ast.unwrap().has_error());
|
||||
|
||||
bump.reset();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user