Skip to content

Commit

Permalink
Parser improvements (#147)
Browse files Browse the repository at this point in the history
- Add pretty printing for parameters and for loops
- Rename initiliser
- Replace .lines with split on '\n' for more consistent pretty comment behavior (hopefully fixes fuzzing)
- Fix some things around type annotations
- Fix output in perf action
- Fix `case` not being expression prefix
- Fix as printing
  • Loading branch information
kaleidawave authored May 29, 2024
1 parent e1a7b5e commit 3760f36
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 115 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/performance-and-size.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
<summary>Diagnostics</summary>
\`\`\`
$(./target/release/ezno check demo.ts --timings || true)
$(./target/release/ezno check demo.ts --timings 2>&1 || true)
\`\`\`
</details>
" >> $GITHUB_STEP_SUMMARY
Expand Down
1 change: 1 addition & 0 deletions checker/src/features/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ pub fn evaluate_mathematical_operation(
let lhs = cast_as_number(c1, strict_casts).unwrap_or(f64::NAN);
let rhs = cast_as_number(c2, strict_casts).unwrap_or(f64::NAN);
// TODO hopefully Rust implementation is the same as JS
#[allow(clippy::cast_possible_truncation)]
let value = match operator {
MathematicalAndBitwise::Add => unreachable!(),
MathematicalAndBitwise::Subtract => lhs - rhs,
Expand Down
9 changes: 1 addition & 8 deletions checker/src/synthesis/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ pub(super) fn synthesise_signatures<T: crate::ReadFromFS, B: SynthesiseInterface
),
InterfaceMember::Rule {
parameter,
rule,
matching_type,
as_type: _,
optionality: _,
Expand All @@ -280,13 +279,7 @@ pub(super) fn synthesise_signatures<T: crate::ReadFromFS, B: SynthesiseInterface
position,
} => {
// TODO WIP
let to = match rule {
parser::types::interface::TypeRule::In => {
crate::utilities::notify!("TODO");
TypeId::ANY_TYPE
}
parser::types::interface::TypeRule::InKeyOf => todo!(),
};
let to = TypeId::ANY_TYPE;

// TODO
let _parameter = checking_data.types.register_type(Type::RootPolyType(
Expand Down
8 changes: 4 additions & 4 deletions checker/src/synthesis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl crate::ASTImplementation for EznoParser {

type VariableField<'_a> = parser::VariableField;

type ForStatementInitiliser<'_a> = parser::statements::ForLoopStatementInitializer;
type ForStatementInitiliser<'_a> = parser::statements::ForLoopStatementinitialiser;

fn module_from_string(
// TODO remove
Expand Down Expand Up @@ -156,13 +156,13 @@ impl crate::ASTImplementation for EznoParser {
checking_data: &mut crate::CheckingData<T, Self>,
) {
match for_loop_initialiser {
parser::statements::ForLoopStatementInitializer::VariableDeclaration(declaration) => {
parser::statements::ForLoopStatementinitialiser::VariableDeclaration(declaration) => {
// TODO is this correct & the best
hoist_variable_declaration(declaration, environment, checking_data);
synthesise_variable_declaration(declaration, environment, checking_data, false);
}
parser::statements::ForLoopStatementInitializer::VarStatement(_) => todo!(),
parser::statements::ForLoopStatementInitializer::Expression(_) => todo!(),
parser::statements::ForLoopStatementinitialiser::VarStatement(_) => todo!(),
parser::statements::ForLoopStatementinitialiser::Expression(_) => todo!(),
}
}

Expand Down
7 changes: 7 additions & 0 deletions checker/src/synthesis/type_annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,13 @@ pub(super) fn synthesise_type_annotation<T: crate::ReadFromFS>(
);
TypeId::ERROR_TYPE
}
TypeAnnotation::This(position) => {
checking_data.raise_unimplemented_error(
"`this` annotation",
position.with_source(environment.get_source()),
);
TypeId::ERROR_TYPE
}
};

if checking_data.options.store_expression_type_mappings {
Expand Down
19 changes: 13 additions & 6 deletions parser/examples/pretty_printing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ fn main() {
const y = "hello world something x", z = "another thing quite long, lolmao. another thing quite long, lolmao";
function x(a: { something: string, another: number, third: "yes" }, b: Array<{ everything: any }>) {
something({ here: 2 }).property.get_lines_by_length(2323, 2323).then(console.log).afterwards({ do_something: ["with", 2] })
const normal = "simple".length;
for (let i = 0; i < something.anotherThing("large string here"); i += calculateFromFunction()) {
console.log("here 1")
}
for (let i = 0; i < 5; i += 1) {
console.log("here 2")
}
}
something({ here: 2 }).property.get_lines_by_length(2323, 2323).then(console.log).afterwards({ do_something: ["with", 2] })
const normal = "simple".length;
"#;
"#;

let module = Module::from_string(
input.to_owned(),
Expand Down
12 changes: 6 additions & 6 deletions parser/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,15 @@ impl<T: ASTNode> ASTNode for WithComment<T> {
buf.push_str("/*");
if options.pretty {
// Perform indent correction
for (idx, line) in comment.lines().enumerate() {
// Have to use '\n' as `.lines` with it's handling of '\r'
for (idx, line) in comment.split('\n').enumerate() {
if idx > 0 {
buf.push_new_line();
}
options.add_indent(local.depth, buf);
buf.push_str(line.trim_start());
buf.push_str(line.trim());
}
buf.push_new_line();
// buf.push_new_line();
} else {
buf.push_str_contains_new_line(comment.as_str());
}
Expand All @@ -163,14 +164,13 @@ impl<T: ASTNode> ASTNode for WithComment<T> {
buf.push_str("/*");
if options.pretty {
// Perform indent correction
for (idx, line) in comment.lines().enumerate() {
for (idx, line) in comment.split('\n').enumerate() {
if idx > 0 {
buf.push_new_line();
}
options.add_indent(local.depth, buf);
buf.push_str(line.trim_start());
buf.push_str(line.trim());
}
buf.push_new_line();
} else {
buf.push_str_contains_new_line(comment.as_str());
}
Expand Down
65 changes: 51 additions & 14 deletions parser/src/functions/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,46 +211,83 @@ where
local: crate::LocalToStringInformation,
) {
let FunctionParameters { parameters, rest_parameter, .. } = self;
// TODO parameters don't implement ASTNode
// let large = are_nodes_over_length(
// parameters.iter(),
// options,
// local,
// Some(MAX_INLINE_OBJECT_LITERAL),
// true,
// );
let mut large = false;
if options.enforce_limit_length_limit() && local.should_try_pretty_print {
let room = options.max_line_length as usize;
let mut buf = source_map::StringWithOptionalSourceMap {
source: String::new(),
source_map: None,
quit_after: Some(room),
since_new_line: 0,
};
// Not particularly accurate but does sort of work
for parameter in parameters {
parameter.name.to_string_from_buffer(&mut buf, options, local);
let type_annotation = parameter.type_annotation.as_ref();
type_annotation.inspect(|v| v.to_string_from_buffer(&mut buf, options, local));
if let Some(ParameterData::WithDefaultValue(ref value)) = parameter.additionally {
value.to_string_from_buffer(&mut buf, options, local);
}
large = buf.source.len() > room;
if large {
break;
}
}
if let Some(rest_parameter) = rest_parameter {
rest_parameter.name.to_string_from_buffer(&mut buf, options, local);
let type_annotation = rest_parameter.type_annotation.as_ref();
type_annotation.inspect(|v| v.to_string_from_buffer(&mut buf, options, local));
large = buf.source.len() > room;
}
}

let inner_local = if large { local.next_level() } else { local };

buf.push('(');
// let local = if large { local.next_level() } else { local };
for (at_end, Parameter { name, type_annotation, additionally, .. }) in
parameters.iter().endiate()
{
// decorators_to_string_from_buffer(decorators, buf, options, local);
name.to_string_from_buffer(buf, options, local);
if large {
buf.push_new_line();
options.add_indent(inner_local.depth, buf);
}
// decorators_to_string_from_buffer(decorators, buf, options, inner_local);
name.to_string_from_buffer(buf, options, inner_local);
if let (true, Some(ref type_annotation)) =
(options.include_type_annotations, type_annotation)
{
if let Some(ParameterData::Optional) = additionally {
buf.push('?');
}
buf.push_str(": ");
type_annotation.to_string_from_buffer(buf, options, local);
type_annotation.to_string_from_buffer(buf, options, inner_local);
}
if let Some(ParameterData::WithDefaultValue(value)) = additionally {
buf.push_str(if options.pretty { " = " } else { "=" });
value.to_string_from_buffer(buf, options, local);
value.to_string_from_buffer(buf, options, inner_local);
}
if !at_end || rest_parameter.is_some() {
buf.push(',');
options.push_gap_optionally(buf);
}
}
if let Some(rest_parameter) = rest_parameter {
if large {
buf.push_new_line();
options.add_indent(inner_local.depth, buf);
}
buf.push_str("...");
rest_parameter.name.to_string_from_buffer(buf, options, local);
rest_parameter.name.to_string_from_buffer(buf, options, inner_local);
if let Some(ref type_annotation) = rest_parameter.type_annotation {
buf.push_str(": ");
type_annotation.to_string_from_buffer(buf, options, local);
type_annotation.to_string_from_buffer(buf, options, inner_local);
}
}
if large {
buf.push_new_line();
options.add_indent(local.depth, buf);
}
buf.push(')');
}
}
Expand Down
9 changes: 5 additions & 4 deletions parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ pub(crate) fn to_string_bracketed<T: source_map::ToString, U: ASTNode>(
are_nodes_over_length(nodes.iter(), options, local, Some(MAX_INLINE_OBJECT_LITERAL), true);

buf.push(left_bracket);
let local = if large {
let inner_local = if large {
local.next_level()
} else {
if left_bracket == '{' {
Expand All @@ -1090,16 +1090,17 @@ pub(crate) fn to_string_bracketed<T: source_map::ToString, U: ASTNode>(
for (at_end, node) in nodes.iter().endiate() {
if large {
buf.push_new_line();
options.add_indent(local.depth, buf);
options.add_indent(inner_local.depth, buf);
}
node.to_string_from_buffer(buf, options, local);
node.to_string_from_buffer(buf, options, inner_local);
if !at_end {
buf.push(',');
options.push_gap_optionally(buf);
}
}
if large {
buf.push_new_line();
options.add_indent(local.depth, buf);
} else if left_bracket == '{' {
options.push_gap_optionally(buf);
}
Expand Down Expand Up @@ -1209,7 +1210,7 @@ pub fn are_nodes_over_length<'a, T: ASTNode>(
// Whether just to consider the amount on the line or the entire object
total: bool,
) -> bool {
if options.enforce_limit_length_limit() {
if options.enforce_limit_length_limit() && local.should_try_pretty_print {
let room = available_space.map_or(options.max_line_length as usize, |s| s as usize);
let mut buf = source_map::StringWithOptionalSourceMap {
source: String::new(),
Expand Down
Loading

0 comments on commit 3760f36

Please sign in to comment.