Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for undefined names #12

Merged
merged 7 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
36 changes: 24 additions & 12 deletions src/analysis/typepal/Solver.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ module analysis::typepal::Solver
/*
Implementation of the ISolver interface; this is the API of TypePal's constraint solver
*/
import Set;
import Node;
import Map;
extend analysis::typepal::Collector;
extend analysis::typepal::Messenger;

import Exception;
import IO;
import List;
import Location;
import Map;
import Message;
import Node;
import ParseTree;
import Type;
import Set;
import String;
import Message;
import Exception;
//import util::Benchmark;

extend analysis::typepal::Collector;
extend analysis::typepal::Messenger;
import Type;
import analysis::typepal::StringSimilarity;
import util::IDEServices;

void checkAllTypesAvailable(TModel tm){
for(tup: <loc _, str _, str _, IdRole _, loc _, DefInfo defInfo> <- tm.defines){
Expand Down Expand Up @@ -1517,13 +1518,13 @@ Solver newSolver(map[str,Tree] namedTrees, TModel tm){
foundDefs = scopeGraph.lookup(u);
} catch NoBinding(): {
roles = size(u.idRoles) > 5 ? "" : intercalateOr([prettyRole(idRole) | idRole <- u.idRoles]);
messages += error("Undefined <roles> `<getOrgId(u)>`", u.occ);
messages += error("Undefined <roles> `<getOrgId(u)>`", u.occ, fixes=undefinedNameProposals(u, tm));
}
}

for(u <- notYetDefinedUses){
roles = size(u.idRoles) > 5 ? "" : intercalateOr([prettyRole(idRole) | idRole <- u.idRoles]);
messages += error("Undefined <roles> `<getOrgId(u)>`", u.occ);
messages += error("Undefined <roles> `<getOrgId(u)>`", u.occ, fixes=undefinedNameProposals(u, tm));
}

error_locations = { src | error(_,loc src) <- messages };
Expand Down Expand Up @@ -1704,3 +1705,14 @@ Solver newSolver(map[str,Tree] namedTrees, TModel tm){

return thisSolver;
}

// CodeActions for errors generated by Solver

list[CodeAction] undefinedNameProposals(Use u, TModel tm)
=
[ action(
title="Replace undefined `<getOrgId(u)>`",
edits=[changed([replace(u.occ, prop)])]
)
| str prop <- similarNames(getOrgId(u), u.idRoles, tm, 3)
];
59 changes: 59 additions & 0 deletions src/analysis/typepal/StringSimilarity.rsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module analysis::typepal::StringSimilarity

import List;
import String;
import analysis::typepal::TModel;

@synopsis{Tryadic minimum function on integers}
int min(int a, int b, int c)
= a < b ? (a < c ? a : c) : (b < c ? b : c);

@synopsis{Calculate the Levenshtein distance of 2 strings}
int lev(str a, str b){
int sizea = size(a);
int sizeb = size(b);

@memo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we configure the memo to timeout after a few minutes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes will do

int lev(int ia, int ib){
if(ib == sizeb) return sizea - ia;
if(ia == sizea) return sizeb - ib;
if(a[ia] == b[ib]) return lev(ia+1, ib+1);

return 1 + min(lev(ia+1, ib),
lev(ia, ib+1),
lev(ia+1, ib+1));
}

return lev(0, 0);
}

// Tests for `lev`

test bool levCommutative(str a, str b) = lev(a, b) == lev(b, a);

test bool levLeftAdditive(str a, str b, str c) = lev(a, b) == lev(c + a, c + b);

test bool lev1() = lev("kitten", "sitting") == 3;
test bool lev2() = lev("kelm", "hello") == 3;
test bool lev3() = lev("hello", "hella") == 1;
test bool lev4() = lev("hello", "") == 5;
test bool lev5() = lev("", "hello") == 5;
test bool lev6() = lev("aap", "noot") == 4;
test bool lev7() = lev("page", "pope") == 2;
test bool lev8() = lev("december", "january") == 8;
test bool lev9() = lev("march", "may") == 3;

// Similarity functions to be used by TypePal

@synopsis{WordSim represents one word from the vocabulary and its similariy to the original word}
alias WordSim = tuple[str word, int sim];

@synopsis{Compute list of words from vocabulary, that are similar to give word w with at most maxDistance edits}
list[str] similarWords(str w, list[str] vocabulary, int maxDistance)
= sort([ <v, d> | str v <- vocabulary, d := lev(w, v), d <= maxDistance ], bool (WordSim x, WordSim y){ return x.sim < y.sim;}).word;

@synopsis{Find in TModel tm, names similar to w, in gives roles, with at most maxDistance edits}
list[str] similarNames(str w, set[IdRole] idRoles, TModel tm, int maxDistance){
vocabulary = [ d.orgId | d <- tm.defines, d.idRole in idRoles ];
return similarWords(w, vocabulary, maxDistance);
}