wiki:RefactoringSteps/UpgradeModuleInterface

Upgrade module interface

This transformation upgrades the calls of the old regular expression module regexp to the new re module. The transformation is based on a generic interface upgrade module, which is invoked with the right structural and semantic change descriptors.

An example of upgrading regexp interface:

case regexp:match(String, RE) of
   {error, Reason} -> throw("Error: " ++ lists:flatten(
                        regexp:format_error(Reason)));
   {match, 1, Len} -> Len;
   {match, St, Len} -> use(St, Len);
   nomatch -> ok
end

Result:

try re:run(String, RE, [{capture, first}]) of
   {match, [{0, Len}]} -> Len;      % [{1-1, Len}] works too
   {match, [{St, Len}]} -> use(St+1, Len);
   nomatch -> ok
catch
   error:badarg -> throw("Error: " ++ lists:flatten(
                     "Bad regular expression"))
end

Side conditions

  • Patterns belong to applications to be updated should match at least one of the patterns written in the change descriptors.

Transformation steps and compensations

  1. Updating the function application:
    • Modifying the called function’s identifier regexp:match ->re:run
    • Matching the old function arguments and creating the new argument list (String, RE) -> (String, RE, [capture, first])
  1. Finding the patterns to be updated and performing on them the matching and the replacing. Furthermore, applying the correction functions on the variable bindings or/and usages.
    • Pattern: match, 1, Len -> match, [0, Len]
    • Expression: use(St, Len) -> use(St+1, Len)
  1. Turning the case expression to a try expression and moving error pattern to the catch block (error:badarg is moved to a catch block after we changed case expression to try expression).
Last modified 12 years ago Last modified on Feb 19, 2012, 11:31:51 PM