Method code for $foundation.match_environment()

[Turn on line numbering]
arg str;
var obj, env, found, match, target;

if (!str)
    throw(~objnf, "No object specified.", str);
str = str.strip_article();

// Handle special cases.
if (str in ["me", "my"]) {
    return this();
} else if (str[1] == "$" || str[1] == "#") {
    return (> $object_lib.to_dbref(str) <);
} else if (str[1] == "~") {
    str = str.subrange(2);
    if (str[1] != "~")
        return $user_db.match_begin(str);
} else if (str in ["it", "him", "her"]) {
    return (| .match_context(str) |) || throw(~context, "I don't see " + str + " here, do you?");
}

// Start matching
found = [];
env = .environment();

// special case ordinal references
if ((match = $parse_lib.ordinal_reference(str)))
    return (> env.match_nth(@match) <);
if ((match = $parse_lib.possessive_reference(str))) {
    if (match[1] == "me")
        obj = this();
    else
        obj = (> env.match_object(match[1]) <);
    if (!obj.match_name(str)) {
        catch ~objnf, ~ambig, ~range {
            env = (| obj.contents() |) || [];
            if ((found = $parse_lib.ordinal_reference(match[2])))
                return (> env.match_nth(@found) <);
            else
                return (> env.match_object(match[2]) <);
        } with {
            if (error() == ~objnf)
                throw(~objnf, obj.name() + " does not have " + match[2].add_indefinite() + ".");
            else if (error() == ~ambig)
                throw(~ambig, "\"" + str + "\" can match " + traceback()[1][3].mmap('namef, 'ref).to_english("", " or ") + ".");
            else
                throw(~objnf, obj.name() + "'s what?");
        }
    }
}
catch any {
    return (> env.match_object(str) <);
} with {
    if (error() == ~badfrob) {
        target = traceback()[1][3];
        obj = frob_value(target)['location];
        obj.del_frob_from_contents(target);
        throw(~badfrob, "Cleaned up bogus frob in your environment, try again..");
    }
    if (error() == ~objnf)
        throw(~objnf, "You do not see " + str.add_indefinite() + " anywhere.");
    if (error() == ~ambig) {
        found = filter obj in (traceback()[1][3]) where (obj.name() == str);
        if (listlen(found) == 1)
            return found[1];
        if (found)
            found = found.mmap('namef, 'ref).to_english("", " or ");
        else
            found = traceback()[1][3].mmap('namef, 'ref).to_english("", " or ");
        throw(~ambig, "\"" + str + "\" can match " + found + ".");
    }
    rethrow(error());
}

// Created 27-Mar-1995 as a part of ColdCore, see: @help Credit

Tlon