use std::collections::{HashMap, HashSet};
use crate::ast::{Expr, ExprKind, Pattern, Stmt, StmtKind, UnaryOp};
use crate::lexer::Lexer;
use crate::token::{FStringPart, TokenKind};
use crate::MiruError;
pub use crate::token::RESERVED_WORDS as RESERVED_IN_2;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Rename {
pub from: String,
pub to: String,
pub lines: Vec<usize>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Note {
pub line: usize,
pub message: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Migration {
pub source: String,
pub renames: Vec<Rename>,
pub notes: Vec<Note>,
}
impl Migration {
pub fn is_empty(&self) -> bool {
self.renames.is_empty() && self.notes.is_empty()
}
}
pub fn migrate(source: &str) -> Result<Migration, MiruError> {
let (renamed, renames) = rename_reserved(source)?;
let (tokens, _) = Lexer::tokenize_1x_with_spans(source)?;
let program = crate::parser::Parser::parse(tokens).map_err(|errors| {
errors
.into_iter()
.next()
.expect("a failed parse has at least one error")
})?;
Ok(Migration {
source: renamed,
renames,
notes: notes_for(&program),
})
}
struct Occurrence {
start: usize,
len: usize,
word: String,
line: usize,
}
fn rename_reserved(source: &str) -> Result<(String, Vec<Rename>), MiruError> {
let (tokens, spans) = Lexer::tokenize_1x_with_spans(source)?;
let chars: Vec<char> = source.chars().collect();
let line_starts = line_starts(&chars);
let mut taken: HashSet<String> = HashSet::new();
let mut occurrences: Vec<Occurrence> = Vec::new();
for (index, token) in tokens.iter().enumerate() {
match &token.kind {
TokenKind::Ident(name) => {
taken.insert(name.clone());
if is_reserved(name) {
let Some((start, len)) = spans.tokens.get(index).copied() else {
continue;
};
occurrences.push(Occurrence {
start,
len,
word: name.clone(),
line: token.line,
});
}
}
TokenKind::FString(parts) => {
for part in parts {
let FStringPart::Name { name, line, column } = part else {
continue;
};
taken.insert(name.clone());
if is_reserved(name) {
occurrences.push(Occurrence {
start: line_starts[line - 1] + column - 1,
len: name.chars().count(),
word: name.clone(),
line: *line,
});
}
}
}
_ => {}
}
}
let replacements = choose_replacements(&occurrences, &mut taken);
let renamed = apply(chars, &occurrences, &replacements);
Ok((renamed, summarise(&occurrences, &replacements)))
}
fn is_reserved(name: &str) -> bool {
RESERVED_IN_2.contains(&name)
}
fn line_starts(chars: &[char]) -> Vec<usize> {
let mut starts = vec![0usize];
for (index, c) in chars.iter().enumerate() {
if *c == '\n' {
starts.push(index + 1);
}
}
starts
}
fn choose_replacements(
occurrences: &[Occurrence],
taken: &mut HashSet<String>,
) -> HashMap<String, String> {
let mut replacements: HashMap<String, String> = HashMap::new();
for occurrence in occurrences {
if replacements.contains_key(&occurrence.word) {
continue;
}
let mut candidate = format!("{}_", occurrence.word);
while taken.contains(&candidate) {
candidate.push('_');
}
taken.insert(candidate.clone());
replacements.insert(occurrence.word.clone(), candidate);
}
replacements
}
fn apply(
chars: Vec<char>,
occurrences: &[Occurrence],
replacements: &HashMap<String, String>,
) -> String {
let mut out = chars;
let mut order: Vec<&Occurrence> = occurrences.iter().collect();
order.sort_by_key(|occurrence| std::cmp::Reverse(occurrence.start));
for occurrence in order {
let Some(to) = replacements.get(&occurrence.word) else {
continue;
};
out.splice(
occurrence.start..occurrence.start + occurrence.len,
to.chars(),
);
}
out.into_iter().collect()
}
fn summarise(occurrences: &[Occurrence], replacements: &HashMap<String, String>) -> Vec<Rename> {
let mut lines: HashMap<&str, Vec<usize>> = HashMap::new();
for occurrence in occurrences {
let entry = lines.entry(occurrence.word.as_str()).or_default();
if !entry.contains(&occurrence.line) {
entry.push(occurrence.line);
}
}
let mut renames: Vec<Rename> = lines
.into_iter()
.map(|(word, mut lines)| {
lines.sort_unstable();
Rename {
from: word.to_string(),
to: replacements[word].clone(),
lines,
}
})
.collect();
renames.sort_by(|a, b| a.from.cmp(&b.from));
renames
}
fn notes_for(program: &[Stmt]) -> Vec<Note> {
let mut bound: HashSet<String> = HashSet::new();
for stmt in program {
bound_in_stmt(stmt, &mut bound);
}
let mut notes = Vec::new();
for stmt in program {
notes_in_stmt(stmt, &bound, &mut notes);
}
notes.sort_by_key(|note| note.line);
notes
}
fn bound_in_stmt(stmt: &Stmt, into: &mut HashSet<String>) {
match &stmt.kind {
StmtKind::Let { pattern, .. } => bound_in_pattern(pattern, into),
StmtKind::For {
name, value, body, ..
} => {
bound_in_pattern(name, into);
if let Some(value) = value {
bound_in_pattern(value, into);
}
for stmt in body {
bound_in_stmt(stmt, into);
}
}
StmtKind::Function { name, params, body } => {
into.insert(name.clone());
into.extend(params.names().map(str::to_string));
for stmt in body {
bound_in_stmt(stmt, into);
}
}
StmtKind::Import { alias, .. } => {
into.insert(alias.clone());
}
StmtKind::Match { arms, .. } => {
for arm in arms {
for stmt in &arm.body {
bound_in_stmt(stmt, into);
}
}
}
StmtKind::If {
then_branch,
else_branch,
..
} => {
for stmt in then_branch.iter().chain(else_branch.iter().flatten()) {
bound_in_stmt(stmt, into);
}
}
StmtKind::While { body, .. } => {
for stmt in body {
bound_in_stmt(stmt, into);
}
}
_ => {}
}
}
fn bound_in_pattern(pattern: &Pattern, into: &mut HashSet<String>) {
for name in pattern.names() {
into.insert(name.to_string());
}
}
fn notes_in_stmt(stmt: &Stmt, bound: &HashSet<String>, notes: &mut Vec<Note>) {
match &stmt.kind {
StmtKind::Let { value, .. } | StmtKind::Expr(value) => notes_in_expr(value, bound, notes),
StmtKind::Return(value) => {
if let Some(value) = value {
notes_in_expr(value, bound, notes);
}
}
StmtKind::Assign { target, value } | StmtKind::CompoundAssign { target, value, .. } => {
notes_in_expr(target, bound, notes);
notes_in_expr(value, bound, notes);
}
StmtKind::If {
condition,
then_branch,
else_branch,
} => {
notes_in_expr(condition, bound, notes);
for stmt in then_branch.iter().chain(else_branch.iter().flatten()) {
notes_in_stmt(stmt, bound, notes);
}
}
StmtKind::While { condition, body } => {
notes_in_expr(condition, bound, notes);
for stmt in body {
notes_in_stmt(stmt, bound, notes);
}
}
StmtKind::For { iterable, body, .. } => {
notes_in_expr(iterable, bound, notes);
for stmt in body {
notes_in_stmt(stmt, bound, notes);
}
}
StmtKind::Function { body, .. } => {
for stmt in body {
notes_in_stmt(stmt, bound, notes);
}
}
StmtKind::Match { subject, arms, .. } => {
notes_in_expr(subject, bound, notes);
for arm in arms {
for case in &arm.cases {
notes_in_expr(case, bound, notes);
}
if let Some(guard) = &arm.guard {
notes_in_expr(guard, bound, notes);
}
for stmt in &arm.body {
notes_in_stmt(stmt, bound, notes);
}
}
}
StmtKind::Import { .. } | StmtKind::Break | StmtKind::Continue => {}
}
}
fn notes_in_expr(expr: &Expr, bound: &HashSet<String>, notes: &mut Vec<Note>) {
match &expr.kind {
ExprKind::Call { callee, arguments } => {
if let ExprKind::Identifier(name) = &callee.kind {
if !bound.contains(name) {
if let Some(message) = call_note(name, arguments) {
notes.push(Note {
line: expr.line,
message,
});
}
}
}
notes_in_expr(callee, bound, notes);
for argument in arguments {
notes_in_expr(argument, bound, notes);
}
}
ExprKind::Array(items) => {
for item in items {
notes_in_expr(item, bound, notes);
}
}
ExprKind::Map(pairs) => {
for (key, value) in pairs {
notes_in_expr(key, bound, notes);
notes_in_expr(value, bound, notes);
}
}
ExprKind::Index { target, index } => {
notes_in_expr(target, bound, notes);
notes_in_expr(index, bound, notes);
}
ExprKind::Field { target, .. }
| ExprKind::Unary {
operand: target, ..
} => notes_in_expr(target, bound, notes),
ExprKind::Try(inner) => notes_in_expr(inner, bound, notes),
ExprKind::Binary { left, right, .. } | ExprKind::Logical { left, right, .. } => {
notes_in_expr(left, bound, notes);
notes_in_expr(right, bound, notes);
}
ExprKind::If {
condition,
then_value,
else_value,
} => {
notes_in_expr(condition, bound, notes);
notes_in_expr(then_value, bound, notes);
notes_in_expr(else_value, bound, notes);
}
ExprKind::Function { body, .. } => {
for stmt in body {
notes_in_stmt(stmt, bound, notes);
}
}
ExprKind::Match { subject, arms } => {
notes_in_expr(subject, bound, notes);
for arm in arms {
for case in &arm.cases {
notes_in_expr(case, bound, notes);
}
if let Some(guard) = &arm.guard {
notes_in_expr(guard, bound, notes);
}
notes_in_expr(&arm.body, bound, notes);
}
}
ExprKind::FString(_)
| ExprKind::Int(_)
| ExprKind::Float(_)
| ExprKind::Str(_)
| ExprKind::Bool(_)
| ExprKind::Nil
| ExprKind::Identifier(_) => {}
}
}
fn call_note(name: &str, arguments: &[Expr]) -> Option<String> {
match name {
"slice" => {
let bounds = arguments.iter().skip(1);
if bounds.clone().any(is_negative_literal) {
Some(
"slice has a negative bound here: today it clamps to 0, in 2.0 it counts \
from the end"
.to_string(),
)
} else if bounds.clone().any(|bound| !is_non_negative_literal(bound)) {
Some(
"slice: if a bound here can be negative, today it clamps to 0 and in 2.0 \
it counts from the end"
.to_string(),
)
} else {
None
}
}
"index_of" | "find" => Some(format!(
"{name} gives nil rather than -1 when it finds nothing in 2.0, so a comparison \
against -1 stops matching"
)),
_ => None,
}
}
fn is_negative_literal(expr: &Expr) -> bool {
matches!(
&expr.kind,
ExprKind::Unary {
op: UnaryOp::Negate,
operand,
} if matches!(operand.kind, ExprKind::Int(_) | ExprKind::Float(_))
)
}
fn is_non_negative_literal(expr: &Expr) -> bool {
matches!(expr.kind, ExprKind::Int(_) | ExprKind::Float(_))
}
#[cfg(test)]
mod tests {
use super::*;
fn run(source: &str) -> Migration {
migrate(source).expect("the program parses")
}
#[test]
fn a_program_that_needs_nothing_is_left_alone() {
let source = "let total = 0\nfor i in range(3) {\n total += i\n}\nprint(total)\n";
let migration = run(source);
assert!(migration.is_empty());
assert_eq!(migration.source, source);
}
#[test]
fn a_reserved_word_is_renamed_everywhere_it_is_written() {
let migration = run("let match = 1\nprint(match + match)\n");
assert_eq!(migration.source, "let match_ = 1\nprint(match_ + match_)\n");
assert_eq!(
migration.renames,
vec![Rename {
from: "match".to_string(),
to: "match_".to_string(),
lines: vec![1, 2],
}]
);
}
#[test]
fn a_rename_does_not_land_on_a_name_the_file_already_has() {
let migration = run("let match_ = 1\nlet match = 2\nprint(match_ + match)\n");
assert_eq!(
migration.source,
"let match_ = 1\nlet match__ = 2\nprint(match_ + match__)\n"
);
}
#[test]
fn only_the_names_change_and_nothing_is_reformatted() {
let source = "// a note\nlet loop = 1\n\n\n print( loop )\n";
let migration = run(source);
assert_eq!(
migration.source,
"// a note\nlet loop_ = 1\n\n\n print( loop_ )\n"
);
}
#[test]
fn a_name_inside_an_f_string_is_renamed_too() {
let migration = run("let case = 2\nprint(f\"it is {case} now\")\n");
assert_eq!(
migration.source,
"let case_ = 2\nprint(f\"it is {case_} now\")\n"
);
}
#[test]
fn a_reserved_word_inside_a_string_is_not_a_name() {
let source = "let m = {\"match\": 1}\nprint(m[\"match\"])\n";
let migration = run(source);
assert_eq!(migration.source, source);
assert!(migration.renames.is_empty());
}
#[test]
fn every_reserved_word_is_recognised() {
for word in RESERVED_IN_2 {
let migration = run(&format!("let {word} = 1\n"));
assert_eq!(
migration.source,
format!("let {word}_ = 1\n"),
"{word} was not renamed"
);
}
}
#[test]
fn type_is_left_alone_because_it_is_still_a_builtin() {
let source = "print(type(1))\n";
assert_eq!(run(source).source, source);
}
#[test]
fn a_negative_slice_bound_is_reported_rather_than_rewritten() {
let migration = run("let a = [1, 2, 3]\nprint(slice(a, -2, 3))\n");
assert_eq!(
migration.source,
"let a = [1, 2, 3]\nprint(slice(a, -2, 3))\n"
);
assert_eq!(migration.notes.len(), 1);
assert_eq!(migration.notes[0].line, 2);
assert!(migration.notes[0].message.contains("counts from the end"));
}
#[test]
fn a_slice_bound_that_might_be_negative_is_reported_as_a_maybe() {
let migration = run("fn f(a, n) {\n return slice(a, n, 3)\n}\n");
assert_eq!(migration.notes.len(), 1);
assert!(migration.notes[0].message.contains("can be negative"));
}
#[test]
fn a_slice_with_literal_bounds_is_not_reported() {
let migration = run("let a = [1, 2, 3]\nprint(slice(a, 0, 2))\n");
assert!(migration.notes.is_empty());
}
#[test]
fn index_of_and_find_are_reported_wherever_they_are_called() {
let migration =
run("let a = [1]\nif index_of(a, 9) == -1 {\n print(find(\"ab\", \"z\"))\n}\n");
assert_eq!(migration.notes.len(), 2);
assert_eq!(migration.notes[0].line, 2);
assert!(migration.notes[0].message.starts_with("index_of gives nil"));
assert_eq!(migration.notes[1].line, 3);
assert!(migration.notes[1].message.starts_with("find gives nil"));
}
#[test]
fn a_file_that_defines_its_own_slice_is_not_told_about_the_builtin() {
let migration = run("fn slice(a, i) {\n return a\n}\nprint(slice([1], -1))\n");
assert!(
migration.notes.is_empty(),
"reported the builtin for a call that does not reach it: {:?}",
migration.notes
);
}
#[test]
fn a_program_that_does_not_parse_says_so() {
assert!(migrate("let = 1\n").is_err());
}
}