1
0

Pass format tests

This commit is contained in:
Jeff 2024-01-06 10:40:25 -05:00
parent 7d7b96d76f
commit f89e94cc33
6 changed files with 27 additions and 42 deletions

View File

@ -1,6 +0,0 @@
const fs = require('node:fs');
const data = fs.readFileSync("examples/assets/jq_data.json");
const items = JSON.parse(data);
const output = items.map((item) => item:commit:committer:name);
console.log(output)

View File

@ -4,7 +4,7 @@ use rayon::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{AbstractTree, Error, Expression, Format, Map, Result, Statement, Type, Value}; use crate::{AbstractTree, Error, Format, Map, Result, Statement, Type, Value};
/// Abstract representation of a block. /// Abstract representation of a block.
/// ///
@ -136,26 +136,12 @@ impl Format for Block {
output.push_str("{\n"); output.push_str("{\n");
} }
let mut previous: Option<&Statement> = None; for (index, statement) in self.statements.iter().enumerate() {
if index > 0 {
for next_statement in &self.statements { output.push('\n');
if let Some(previous_statement) = &previous {
match previous_statement {
Statement::While(_) | Statement::Expression(Expression::FunctionCall(_)) => {
output.push('\n');
}
_ => {}
}
} }
match next_statement { statement.format(output, indent_level + 1);
Statement::Block(_) | Statement::While(_) => output.push('\n'),
_ => {}
}
next_statement.format(output, indent_level + 1);
previous = Some(next_statement);
} }
output.push('\n'); output.push('\n');

View File

@ -124,7 +124,7 @@ impl AbstractTree for Root {
impl Format for Root { impl Format for Root {
fn format(&self, output: &mut String, indent_level: u8) { fn format(&self, output: &mut String, indent_level: u8) {
for (index, statement) in self.statements.iter().enumerate() { for (index, statement) in self.statements.iter().enumerate() {
if index > 1 { if index > 0 {
output.push('\n'); output.push('\n');
} }
statement.format(output, indent_level); statement.format(output, indent_level);

View File

@ -332,24 +332,27 @@ impl Format for ValueNode {
} }
} }
ValueNode::Map(nodes) => { ValueNode::Map(nodes) => {
output.push('{'); output.push_str("{\n");
for (key, (statement, type_option)) in nodes { for (key, (statement, type_option)) in nodes {
if let Some(r#type) = type_option { if let Some(r#type) = type_option {
output.push_str(" "); ValueNode::indent(output, indent_level + 1);
output.push_str(key); output.push_str(key);
output.push_str(" <"); output.push_str(" <");
r#type.format(output, indent_level); r#type.format(output, 0);
output.push_str("> = "); output.push_str("> = ");
statement.format(output, indent_level); statement.format(output, 0);
} else { } else {
output.push_str(" "); ValueNode::indent(output, indent_level + 1);
output.push_str(key); output.push_str(key);
output.push_str(" = "); output.push_str(" = ");
statement.format(output, indent_level); statement.format(output, 0);
} }
output.push('\n');
} }
ValueNode::indent(output, indent_level);
output.push('}'); output.push('}');
} }
ValueNode::BuiltInValue(built_in_value) => built_in_value.format(output, indent_level), ValueNode::BuiltInValue(built_in_value) => built_in_value.format(output, indent_level),

View File

@ -44,8 +44,6 @@ impl Format for Function {
Function::BuiltIn(built_in_function) => built_in_function.format(output, indent_level), Function::BuiltIn(built_in_function) => built_in_function.format(output, indent_level),
Function::ContextDefined(function_node) => function_node.format(output, indent_level), Function::ContextDefined(function_node) => function_node.format(output, indent_level),
} }
output.push('\n');
} }
} }

View File

@ -6,14 +6,15 @@ fn format_simple_program() {
interpreter.run("x=1").unwrap(); interpreter.run("x=1").unwrap();
assert_eq!(interpreter.format(), "x = 1"); assert_eq!(interpreter.format(), "x = 1\n");
} }
const FORMATTED_BLOCK: &str = "{ const FORMATTED_BLOCK: &str = "{
1 1
2 2
3 3
}"; }
";
#[test] #[test]
fn format_block() { fn format_block() {
@ -24,24 +25,27 @@ fn format_block() {
assert_eq!(FORMATTED_BLOCK, interpreter.format()); assert_eq!(FORMATTED_BLOCK, interpreter.format());
} }
const FORMATTED_NESTED_BLOCK: &str = "{ const FORMATTED_MAP: &str = "{
{ {
x = 1 x = 1
y <int> = 2
} }
}"; }
";
#[test] #[test]
fn format_nested_block() { fn format_map() {
let mut interpreter = Interpreter::new(Map::new()); let mut interpreter = Interpreter::new(Map::new());
interpreter.run("{{x=1}}").unwrap(); interpreter.run("{{x=1 y <int> = 2}}").unwrap();
assert_eq!(FORMATTED_NESTED_BLOCK, interpreter.format()); assert_eq!(FORMATTED_MAP, interpreter.format());
} }
const FORMATTED_FUNCTION: &str = "(x <int>) <num> { const FORMATTED_FUNCTION: &str = "(x <int>) <num> {
x / 2 x / 2
}"; }
";
#[test] #[test]
fn format_function() { fn format_function() {