Skip to content

Commit

Permalink
closes #137; allowed hyphenated alphabets within identifiers as alias…
Browse files Browse the repository at this point in the history
… to their upper case
  • Loading branch information
satyr committed Jul 28, 2012
1 parent f921b24 commit f4d34db
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
35 changes: 19 additions & 16 deletions lib/lexer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var string, TABS, unlines, enlines, enslash, reslash, character, KEYWORDS_SHARED, KEYWORDS_UNUSED, KEYWORDS, ID, SYMBOL, SPACE, MULTIDENT, SIMPLESTR, JSTOKEN, BSTOKEN, NUMBER, NUMBER_OMIT, REGEX, HEREGEX_OMIT, LASTDENT, INLINEDENT, NONASCII, OPENERS, CLOSERS, INVERSES, CHAIN, ARG, slice$ = [].slice;
var string, TABS, unlines, enlines, enslash, reslash, camelize, character, KEYWORDS_SHARED, KEYWORDS_UNUSED, KEYWORDS, ID, SYMBOL, SPACE, MULTIDENT, SIMPLESTR, JSTOKEN, BSTOKEN, NUMBER, NUMBER_OMIT, REGEX, HEREGEX_OMIT, LASTDENT, INLINEDENT, NONASCII, OPENERS, CLOSERS, INVERSES, CHAIN, ARG, slice$ = [].slice;
exports.lex = function(code, options){
return (clone$(exports)).tokenize(code || '', options || {});
};
Expand Down Expand Up @@ -91,7 +91,7 @@ exports.doID = function(code, index){
if (!input) {
return 0;
}
id = match[1];
id = camelize(match[1]);
if (NONASCII.test(id)) {
try {
Function("var " + id);
Expand Down Expand Up @@ -773,7 +773,7 @@ exports.parameters = function(arrow){
this.token(')PARAM', '');
};
exports.interpolate = function(str, idx, end){
var parts, end0, pos, i, ch, id, stringified, delta, nested, ref$, clone;
var parts, end0, pos, i, ch, id, stringified, length, e, delta, nested, ref$, clone;
parts = [];
end0 = end.charAt(0);
pos = 0;
Expand All @@ -788,17 +788,7 @@ exports.interpolate = function(str, idx, end){
parts.push(['S', this.countLines(str.slice(0, i)), this.line]);
return parts.size = pos + i + end.length, parts;
case '#':
if (id = (ID.lastIndex = i + 1, ID).exec(str)[1]) {
if (id === 'this') {
break;
}
try {
Function("'use strict'; var " + id);
break;
} catch (e$) {}
this.carp("invalid variable interpolation \"" + id + "\"");
}
if ('{' !== str.charAt(i + 1)) {
if (!((id = (ID.lastIndex = i + 1, ID).exec(str)[1]) || '{' === str.charAt(i + 1))) {
continue;
}
break;
Expand All @@ -812,7 +802,17 @@ exports.interpolate = function(str, idx, end){
stringified = parts.push(['S', this.countLines(str.slice(0, i)), this.line]);
}
if (id) {
str = str.slice(delta = i + 1 + id.length);
length = id.length;
if (id !== 'this') {
id = camelize(id);
try {
Function("'use strict'; var " + id);
} catch (e$) {
e = e$;
this.carp("invalid variable interpolation \"" + id + "\"");
}
}
str = str.slice(delta = i + 1 + length);
parts.push(['TOKENS', nested = [['ID', id, this.line]]]);
} else {
clone = (ref$ = clone$(exports), ref$.inter = true, ref$.emender = this.emender, ref$);
Expand Down Expand Up @@ -995,6 +995,9 @@ enslash = replacer(/\\/g, '\\\\');
reslash = replacer(/(\\.)|\//g, function(){
return arguments[1] || '\\/';
});
camelize = replacer(/-[a-z]/ig, function(it){
return it.charAt(1).toUpperCase();
});
function lchomp(it){
return it.slice(1 + it.lastIndexOf('\n', 0));
}
Expand Down Expand Up @@ -1416,7 +1419,7 @@ function indexOfPair(tokens, i){
KEYWORDS_SHARED = ['true', 'false', 'null', 'this', 'void', 'super', 'return', 'throw', 'break', 'continue', 'if', 'else', 'for', 'while', 'switch', 'case', 'default', 'try', 'catch', 'finally', 'function', 'class', 'extends', 'implements', 'new', 'do', 'delete', 'typeof', 'in', 'instanceof', 'let', 'with', 'var', 'const', 'import', 'export', 'debugger'];
KEYWORDS_UNUSED = ['enum', 'interface', 'package', 'private', 'protected', 'public', 'static', 'yield'];
KEYWORDS = KEYWORDS_SHARED.concat(KEYWORDS_UNUSED);
ID = /((?!\d)(?:(?!\s)[\w$\xAA-\uFFDC])+)([^\n\S]*:(?![:=]))?|/g;
ID = /((?!\s)[a-z_$\xAA-\uFFDC](?:(?!\s)[\w$\xAA-\uFFDC]|-[a-z])*)([^\n\S]*:(?![:=]))?|/ig;
SYMBOL = /[-+*\/%&|^:]=|\.{1,3}|([-+&|@:])\1|[-~=|]>|[!=]==?|<(?:<(?:=|<{0,2})|[-~]|\[(?:[\s\S]*?\]>)?)|>>>?=?|[<>]\??=?|!\?|\*\*=?|[^\s#]?/g;
SPACE = /[^\n\S]*(?:#.*)?/g;
MULTIDENT = /(?:\s*#.*)*(?:\n([^\n\S]*))+/g;
Expand Down
28 changes: 17 additions & 11 deletions src/lexer.co
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports import
doID: (code, index) ->
[input] = match = (ID <<< lastIndex: index)exec code
return 0 unless input
id = match.1
id = camelize match.1
if NONASCII.test id
try Function "var #id" catch @carp "invalid identifier \"#id\""
{last} = this
Expand Down Expand Up @@ -517,18 +517,20 @@ exports import
parts.push [\S; @countLines str.slice 0 i; @line]
return parts <<< size: pos + i + end.length
case \#
if id = (ID <<< lastIndex: i+1)exec(str)1
break if id is \this
try Function "'use strict'; var #id"; break
@carp "invalid variable interpolation \"#id\""
continue unless \{ is str.charAt i+1
continue unless (id = (ID <<< lastIndex: i+1)exec str .1)
or \{ is str.charAt i+1
case \\ then ++i; fallthrough
default continue
# `"#a#{b || c}"` => `a + "" + (b || c)`
if i or nested and not stringified
stringified = parts.push [\S; @countLines str.slice 0 i; @line]
if id
str.=slice delta = i + 1 + id.length
{length} = id
unless id is \this
id = camelize id
try Function "'use strict'; var #id" catch
@carp "invalid variable interpolation \"#id\""
str.=slice delta = i + 1 + length
parts.push [\TOKENS nested = [[\ID id, @line]]]
else
clone = ^exports <<< {+inter, @emender}
Expand Down Expand Up @@ -614,7 +616,7 @@ exports import
if @tokens[* - 2 - (@last.0 of <[NEWLINE INDENT]>)]?0 is \FOR
import {-seenFor, +seenFrom}

# Complains on duplicate flag.
# Complains on bad regex flag.
validate: (flag) ->
if flag and /(.).*\1/exec flag
@carp "duplicate regex flag `#{that.1}`"
Expand Down Expand Up @@ -676,6 +678,9 @@ enslash = replacer /\\/g \\\\
# Quotes slashes unless already quoted.
reslash = replacer /(\\.)|\//g -> @@1 or \\\/

# Transforms hyphenated-words to camelCase.
camelize = replacer /-[a-z]/ig -> it.char-at 1 .to-upper-case!

# Deletes the first character if newline.
function lchomp then it.slice 1 + it.lastIndexOf \\n 0

Expand Down Expand Up @@ -951,9 +956,10 @@ KEYWORDS = KEYWORDS_SHARED.concat KEYWORDS_UNUSED
# Some of these are given `g` flag and made sure to match empty string
# so that they can lex from any index by receiving `.lastIndex` beforehand.

ID = // ( (?!\d)(?:(?!\s)[\w$\xAA-\uFFDC])+ )
( [^\n\S]* : (?![:=]) )? # Is this a property name?
|//g
ID = //
( (?!\s)[a-z_$\xAA-\uFFDC](?:(?!\s)[\w$\xAA-\uFFDC]|-[a-z])* )
( [^\n\S]* : (?![:=]) )? # Is this a property name?
|//ig
SYMBOL = //
[-+*/%&|^:]= # compound assign
| \.{1,3} # dot / `constructor` / splat/placeholder/yada*3
Expand Down
5 changes: 5 additions & 0 deletions test/literal.co
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### Identifiers
eq encodeURIComponent, encode-URI-component
eq ''.toLowerCase, ''.to-lower-case


### Numbers

eq 3-4, -1
Expand Down
1 change: 1 addition & 0 deletions test/string.co
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ eq "#{hello} #{ 1 + 2 } #{world}", "Hello 3 World"
eq "#{hello + world}", 'HelloWorld'
eq "#{hello + ' ' + world + '!'}", 'Hello World!'

eq helloWorld = hello + world, "#hello-world"

eq "\#{Escaping} first", '#{Escaping} first'
eq "Escaping \#{in} middle", 'Escaping #{in} middle'
Expand Down

0 comments on commit f4d34db

Please sign in to comment.