prebirth: Distinguishable id generation

* build-aux/bootstrap/prebirth.js (Compiler#_idFromName): Distinguish
  between certain types of characters.
master
Mike Gerwitz 2017-08-29 01:40:47 -04:00
parent 6a736a8fcd
commit 61f6ba1470
Signed by: mikegerwitz
GPG Key ID: 8C917B7F5DC51BA2
1 changed files with 31 additions and 2 deletions

View File

@ -375,6 +375,12 @@ class Compiler
/** /**
* Generate ECMAScript-friendly name from the given id * Generate ECMAScript-friendly name from the given id
* *
* A subset of special characters that are acceptable in Scheme are
* converted in an identifiable manner; others are simply converted to
* `$' in a catch-all and therefore could result in conflicts and cannot
* be reliably distinguished from one-another. Remember: this is
* temporary code.
*
* @param {string} name source name * @param {string} name source name
* @param {boolean} global whether identifier should be globally unique * @param {boolean} global whether identifier should be globally unique
* *
@ -382,8 +388,31 @@ class Compiler
*/ */
_idFromName( name, global ) _idFromName( name, global )
{ {
return ( global ? '$$' : '' ) // just some common ones; will fall back to `$' below
+ name.replace( /[^a-zA-Z0-9_]/g, '$' ); const conv = {
'-': '$_$',
'?': '$7$',
'@': '$a$',
'!': '$b$',
'>': '$g$',
'#': '$h$',
'*': '$k$',
'<': '$l$',
'&': '$n$',
'%': '$o$',
'+': '$p$',
'=': '$q$',
'^': '$v$',
'/': '$w$',
'$': '$$',
};
if ( name === undefined ) {
throw SyntaxError( "Missing identifier name" );
}
return ( global ? '$$' : '' ) +
name.replace( /[^a-zA-Z0-9_]/g, c => conv[ c ] || '$' );
} }