Tcl Library Source Code

View Ticket
Login
Ticket UUID: 2063144
Title: Snit method name collision
Type: Bug Version: None
Submitter: duquette Created on: 2008-08-20 22:27:46
Subsystem: snit Assigned To: duquette
Priority: 5 Medium Severity:
Status: Open Last Modified: 2008-08-23 07:16:23
Resolution: None Closed By:
    Closed on:
Description:
The following code creates two distinct typemethods
with but a single body between them:

snit::type foobar {
    typemethod {a_b c} {} { return 1 }
    typemethod {a b_c} {} { return 2 }
}

The command "foobar a_b c" will return 2, rather than
1; the second typemethod overwrites the first 
typemethod's proc.  The problem is that the name
mangling for hierarchical typemethods assumes that
it can use "_" as a token separator.

Methods have the same problem, naturally.

At a minimum, Snit should detect this case and
throw an error; ideally, the two typemethods should
have distinct bodies.
User Comments: duquette added on 2008-08-23 07:16:23:
Logged In: YES 
user_id=372859
Originator: YES

And that looks like it would work; actually, both the "__"/"_%" scheme and the hex coding scheme.  Cool!

andreas_kupries added on 2008-08-23 05:01:06:
Logged In: YES 
user_id=75003
Originator: NO

So we need a quoting schema which converts ' ' and the quoting character itself distinctively. Actually, the problem may be that the coding for <space> is a prefix for the coding of <underscore>, leading to ambiguity when decoding, i.e. we cannot say exactly where a code ends/starts, as demonstrated. We need a prefix-code with unique prefixes for anything we code.

Say, hex-coding: ' ' => %20, and the quoting char is '%', encoded as %25, using the hex-coded ascii values.

Now:
    a_b c == a_b%20c
    a b_c == a%20b_c
    % %   == %25%20%25
    %% %  == %25%25%20%25
    % %%  == %25%20%25%25

Now lets try this

    ' ' => '__' 
and '_' => '_%'

Now the encoding of <space> is not a prefix of the encoding for underscore

__ _ => _%_%___%
_ __ => _%___%_%
a_b c => a_%b__c
a b_c => a__b_%c

Now what happens if we have % in the name ... There should be no ambiguity in the encoding result, as newly introduced % will be prefixed by _, making decoding proper.

a%b => a%b
a_%b => a_%%b

It looks relatively readable as well.

duquette added on 2008-08-23 04:44:46:
Logged In: YES 
user_id=372859
Originator: YES

Hmmm.  "__ _" and "_ __" both expand to "______".  Granted, it's an even obscurer case than "a_b c" and "a b_c", but still troubling.

What I'd like is a solution that:

1. Mangles every possible method name distinctly
2. Supports demangling
3. Does not place any new constraints on method names.
4. Can be demangled easily "by eye".

It's easy to get 2 *or* 3; I'm not sure how easy it easy
to get both.

Unless Snit provided a demangler that took the type into
account.  Then my scheme of providing an index number for
each method meets all three constraints without playing
weird text-manipulation games.  I.e., mangle method
"a b_c" and "a_b c" as

   Snit_hmethod_4_a_b_c
   Snit_hmethod_5_a_b_c

(assuming that they were the 4th and 5th methods to be
defined).

A demangling routine would pick out the index, and look up
the method name in the Snit variables in the same namespace.
And a human being can unambiguously demangle such names
"by eye" if they avoid pathological cases in their code 
(as is usually the case).

andreas_kupries added on 2008-08-23 03:31:51:
Logged In: YES 
user_id=75003
Originator: NO

Me neither. OTOH, I proposed it, I may be too near to the thing.

duquette added on 2008-08-23 03:11:27:
Logged In: YES 
user_id=372859
Originator: YES

Here are some procs that mangle and demangle hierarchical 
method names using Andreas' scheme.  I don't *see* any obvious
corner cases....

proc method2proc {name} {
    set subs [list _ __ " " _]

    return "Snit_hmethod[string map $subs $name]"
}

proc proc2method {name} {
    set subs [list __ _ _ " " Snit_hmethod ""]

    return [string map $subs $name]
}

proc rt {name} {
    set proc [method2proc $name]
    list $proc [proc2method $proc]
}

duquette added on 2008-08-23 02:55:05:
Logged In: YES 
user_id=372859
Originator: YES

Good thought.  Alternatively, Snit could assign each method an index number when it's defined; the index number
would then appear in the name.  Then, "a b_c" and "a_b c" would have different numbers.

But I rather like yours, as it means that you can determine the method name from the proc name, which is useful
during debugging.

andreas_kupries added on 2008-08-21 05:33:20:
Logged In: YES 
user_id=75003
Originator: NO

Maybe duplicate existing underscores to quote them ?

a_b c ==> a__b_c
a b_c ==> a_b__c