Skip to content
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable milestones for the nera project are documented in this file.

## Formatter keeps named fun names unquoted

- `nera fmt` now prints a named fun's variable name as written, so
`fun Loop(0) -> ...` stays valid Erlang. Previously it printed
`fun 'Loop'(0) -> ...`, which `erlc` rejects with a syntax error.

## Binary comprehension bodies written as segments are rejected

- A binary comprehension body with a size, a type or several segments, such as
Expand Down
13 changes: 12 additions & 1 deletion crates/nera-fmt/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1387,7 +1387,7 @@ impl<'src> Formatter<'src> {
fn format_fun_clause(&mut self, clause: &FunctionClause, name: &Option<AtomId>) -> Doc {
let mut parts = Vec::new();
if let Some(name_id) = name {
parts.push(self.format_atom(*name_id));
parts.push(self.format_fun_name(*name_id));
}
let params: Vec<Doc> = clause
.patterns
Expand All @@ -1402,6 +1402,17 @@ impl<'src> Formatter<'src> {
concat_all(parts)
}

/// Print a named fun's name. Erlang requires a variable here, so a name
/// shaped like one is never quoted; the parser also accepts atoms.
fn format_fun_name(&self, id: AtomId) -> Doc {
let name = self.atom_str(id);
if nera_syntax::token::is_variable_name(name) {
text(name.to_string())
} else {
self.format_atom(id)
}
}

fn format_fun_ref(&mut self, target: &FunRefTarget) -> Doc {
match target {
FunRefTarget::Local(name, arity) => {
Expand Down
44 changes: 44 additions & 0 deletions crates/nera-fmt/tests/named_fun_names.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use nera_fmt::{config::FormatConfig, format_source};

fn format(source: &str) -> String {
format_source(source, &FormatConfig::default()).unwrap()
}

// erlc accepts only a variable as a named fun's name, so quoting one makes the
// formatted file stop compiling.
#[test]
fn variable_names_stay_unquoted_in_every_clause() {
let source = "count(N) ->
Loop = fun Loop(0) -> done;
Loop(M) when M > 0 -> Loop(M - 1)
end,
Loop(N).
";
assert_eq!(format(source), source);
}

#[test]
fn underscore_names_stay_unquoted() {
let source = "add() -> fun _(N) -> N + 1
end.
unused() -> fun _BlackAdder(N) -> N + 42
end.
stack() -> fun _stack@2(N) -> N
end.
";
assert_eq!(format(source), source);
}

// Nera also reads an atom as the name. Only a name that is not a valid
// variable still needs its quotes.
#[test]
fn atom_names_keep_quotes_only_when_they_need_them() {
let formats_to = |name: &str, expected: &str| {
let source = format!("a() -> fun {name}(N) -> N\n end.\n");
let wanted = format!("a() -> fun {expected}(N) -> N\n end.\n");
assert_eq!(format(&source), wanted);
};
formats_to("'two words'", "'two words'");
formats_to("plain", "plain");
formats_to("'Loop'", "Loop");
}
6 changes: 6 additions & 0 deletions crates/nera-syntax/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,12 @@ pub fn atom_needs_quoting(text: &str) -> bool {
!identifier || reserved
}

/// Whether `text` is exactly one variable token, such as `Loop`, `_` or `_F1`.
pub fn is_variable_name(text: &str) -> bool {
let mut lexer = Token::lexer(text);
matches!(lexer.next(), Some(Ok(Token::Variable))) && lexer.next().is_none()
}

fn scan_triple_string(lex: &mut logos::Lexer<'_, Token>) -> Result<(), ()> {
let end = lex.remainder().find("\"\"\"").ok_or(())?;
lex.bump(end + 3);
Expand Down
Loading