From 61f6ba14709c78139d54f476ead18bb5bc8c1007 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 29 Aug 2017 01:40:47 -0400 Subject: [PATCH] prebirth: Distinguishable id generation * build-aux/bootstrap/prebirth.js (Compiler#_idFromName): Distinguish between certain types of characters. --- build-aux/bootstrap/prebirth.js | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/build-aux/bootstrap/prebirth.js b/build-aux/bootstrap/prebirth.js index c16de94..83728b2 100644 --- a/build-aux/bootstrap/prebirth.js +++ b/build-aux/bootstrap/prebirth.js @@ -375,6 +375,12 @@ class Compiler /** * 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 {boolean} global whether identifier should be globally unique * @@ -382,8 +388,31 @@ class Compiler */ _idFromName( name, global ) { - return ( global ? '$$' : '' ) - + name.replace( /[^a-zA-Z0-9_]/g, '$' ); + // just some common ones; will fall back to `$' below + 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 ] || '$' ); }