Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
903 changes: 634 additions & 269 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ members = [
"compiler/span",
"docs/grammar",
"errors",
"formatter",
"interpreter",
"leo/package",
"test-framework",
Expand All @@ -61,6 +62,10 @@ version = "=3.3.1"
path = "./errors"
version = "=3.3.1"

[workspace.dependencies.leo-fmt]
path = "./formatter"
version = "=3.3.1"

[workspace.dependencies.leo-interpreter]
path = "./interpreter"
version = "=3.3.1"
Expand Down Expand Up @@ -192,6 +197,9 @@ workspace = true
[dependencies.leo-errors]
workspace = true

[dependencies.leo-fmt]
workspace = true

[dependencies.leo-interpreter]
workspace = true

Expand Down
2 changes: 1 addition & 1 deletion compiler/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use leo_span::{
sym,
};

mod conversions;
pub mod conversions;

#[cfg(test)]
mod test;
Expand Down
48 changes: 48 additions & 0 deletions formatter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[package]
name = "leo-fmt"
version = "3.3.1"
authors = [ "The Leo Team <[email protected]>" ]
description = "Formatter for Leo programming language"
homepage = "https://leo-lang.org"
repository = "https://github.com/ProvableHQ/leo"
keywords = [
"aleo",
"cryptography",
"leo",
"programming-language",
"zero-knowledge"
]
categories = [ "compilers", "cryptography", "web-programming" ]
include = [ "Cargo.toml", "src", "LICENSE.md" ]
license = "GPL-3.0"
edition = "2024"
rust-version = "1.88.0"

[dependencies.leo-errors]
workspace = true

[dependencies.leo-parser]
workspace = true

[dependencies.leo-parser-lossless]
workspace = true

[dependencies.leo-span]
workspace = true

[dependencies.anyhow]
workspace = true

[dependencies.biome_formatter]
git = "https://github.com/biomejs/biome.git"
package = "biome_formatter"
tag = "@biomejs/[email protected]"

[dependencies.colored]
workspace = true

[dependencies.similar]
version = "2.7.0"

[dependencies.walkdir]
workspace = true
596 changes: 596 additions & 0 deletions formatter/LICENSE.md

Large diffs are not rendered by default.

70 changes: 70 additions & 0 deletions formatter/src/composite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (C) 2019-2025 Provable Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use leo_parser_lossless::{SyntaxKind, SyntaxNode};

use crate::{Formatter, Output, impl_tests};

impl Formatter<'_, '_> {
pub(super) fn format_composite(&mut self, node: &SyntaxNode<'_>) -> Output {
assert_eq!(node.kind, SyntaxKind::StructDeclaration);

let [struct_or_record, i, maybe_const_list @ .., members] = &node.children[..] else {
panic!("Can't happen");
};

self.node_with_trivia(struct_or_record, 0)?;
self.space()?;
self.node_with_trivia(i, 0)?;

if let Some(const_list) = maybe_const_list.first() {
self.format_const_list(const_list)?;
}

let format_func = |slf: &mut Formatter<'_, '_>, member: &SyntaxNode<'_>| {
assert_eq!(member.kind, SyntaxKind::StructMemberDeclaration);
let [k @ .., i, c, t] = &member.children[..] else {
panic!("Can't happen");
};

if let Some(k) = k.first() {
slf.node_with_trivia(k, 0)?;
slf.space()?;
}

slf.node_with_trivia(i, 0)?;
slf.node_with_trivia(c, 0)?;
slf.space()?;
slf.format_type(t)?;

Ok(())
};

self.space()?;
self.format_collection(&members.children, true, true, format_func)?;

Ok(())
}
}

impl_tests!(
test_format_composite,
src = "struct A{a:u32}",
exp = "struct A {
a: u32,
}",
Kind::Module
);
94 changes: 94 additions & 0 deletions formatter/src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (C) 2019-2025 Provable Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use leo_parser_lossless::{StatementKind, SyntaxKind, SyntaxNode};

use crate::{Formatter, Output, impl_tests};

impl Formatter<'_, '_> {
pub(crate) fn format_const_general(&mut self, node: &SyntaxNode<'_>, trailing_empty_lines: bool) -> Output {
assert!(matches!(node.kind, SyntaxKind::GlobalConst | SyntaxKind::Statement(StatementKind::Const)));
let [const_, name, c, type_, a, rhs, s] = &node.children[..] else {
panic!("Can't happen");
};

self.node_with_trivia(const_, 1)?;

self.space()?;

self.node_with_trivia(name, 1)?;
self.node_with_trivia(c, 1)?;

self.space()?;

self.format_type(type_)?;

self.space()?;

self.node_with_trivia(a, 1)?;

self.group(|slf| {
slf.soft_indent_or_space(|slf| {
slf.format_expression(rhs)?;
Ok(())
})?;

slf.node_with_trivia(s, if trailing_empty_lines { 2 } else { 1 })?;

Ok(())
})?;

Ok(())
}
}

impl<'a, 'b> Formatter<'a, 'b>
where
'b: 'a,
{
pub(super) fn format_const_list(&mut self, node: &SyntaxNode<'_>) -> Output {
let [c, s_ps_r @ ..] = &node.children[..] else { panic!("Can't happen") };
self.node_with_trivia(c, 0)?;
let format_func = match node.kind {
SyntaxKind::ConstParameterList => |slf: &mut Formatter<'a, 'b>, node: &SyntaxNode<'_>| {
assert_eq!(node.kind, SyntaxKind::ConstParameter);
let [i, c, t] = node.children[..].as_ref() else { panic!("Can't happen") };
slf.node_with_trivia(i, 1)?;
slf.node_with_trivia(c, 1)?;
slf.space()?;
slf.format_type(t)?;
Ok(())
},
SyntaxKind::ConstArgumentList => Self::format_expression,
_ => panic!("Can't happen"),
};

self.format_collection(s_ps_r, false, false, format_func)?;
self.space()?;
Ok(())
}
}

impl_tests!(
test_format_const_param_list,
src = "struct Example::[M: u32, N: u32] {}",
exp = "struct Example::[M: u32, N: u32] {}",
Kind::Module,
test_format_const_arg_list,
src = "Example::[M, N] {}",
exp = "Example::[M, N] {}",
Kind::Expression
);
42 changes: 42 additions & 0 deletions formatter/src/expressions/array.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2019-2025 Provable Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use leo_parser_lossless::{ExpressionKind, SyntaxKind, SyntaxNode};

use crate::{Formatter, Output, impl_tests};

impl Formatter<'_, '_> {
pub(super) fn format_array(&mut self, node: &SyntaxNode<'_>) -> Output {
assert_eq!(node.kind, SyntaxKind::Expression(ExpressionKind::Array));
self.format_collection(&node.children, false, false, Self::format_expression)?;
Ok(())
}
}

impl_tests!(
test_format_array,
src = "[a, b, c, // ddd
d, e, f]",
exp = "[
a,
b,
c, // ddd
d,
e,
f,
]",
Kind::Expression
);
37 changes: 37 additions & 0 deletions formatter/src/expressions/array_access.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (C) 2019-2025 Provable Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use leo_parser_lossless::{ExpressionKind, SyntaxKind, SyntaxNode};

use crate::{Formatter, Output, impl_tests};

impl Formatter<'_, '_> {
pub(super) fn format_array_access(&mut self, node: &SyntaxNode) -> Output {
assert_eq!(node.kind, SyntaxKind::Expression(ExpressionKind::ArrayAccess));
let [array, left, index, right] = &node.children[..] else {
panic!("Can't happen");
};

self.format_expression(array)?;
self.node_with_trivia(left, 0)?;
self.format_expression(index)?;
self.node_with_trivia(right, 0)?;

Ok(())
}
}

impl_tests!(test_format_array_access, src = "a[ i ] [ i ]", exp = "a[i][i]", Kind::Expression);
29 changes: 29 additions & 0 deletions formatter/src/expressions/associated_constant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (C) 2019-2025 Provable Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use leo_parser_lossless::{ExpressionKind, SyntaxKind, SyntaxNode};

use crate::{Formatter, Output, impl_tests};

impl Formatter<'_, '_> {
pub(super) fn format_associated_constant(&mut self, node: &SyntaxNode<'_>) -> Output {
assert_eq!(node.kind, SyntaxKind::Expression(ExpressionKind::AssociatedConstant));
self.node_with_trivia(&node.children[0], 1)?;
Ok(())
}
}

impl_tests!(test_format_associated_constant, src = "group::GEN", exp = "group::GEN", Kind::Expression);
35 changes: 35 additions & 0 deletions formatter/src/expressions/associated_function_call.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (C) 2019-2025 Provable Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use leo_parser_lossless::{ExpressionKind, SyntaxKind, SyntaxNode};

use crate::{Formatter, Output, impl_tests};

impl Formatter<'_, '_> {
pub(super) fn format_associated_function_call(&mut self, node: &SyntaxNode<'_>) -> Output {
assert_eq!(node.kind, SyntaxKind::Expression(ExpressionKind::AssociatedFunctionCall));
self.node_with_trivia(&node.children[0], 1)?;
self.format_collection(&node.children[1..], false, false, Self::format_expression)?;
Ok(())
}
}

impl_tests!(
test_format_associated_function_call,
src = "Pedersen64::hash(a, b, 2)",
exp = "Pedersen64::hash(a, b, 2)",
Kind::Expression
);
Loading