Skip to content

Commit

Permalink
simplified pseq hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
satyr committed Jan 18, 2011
1 parent 89d6d63 commit 1ad4648
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 216 deletions.
2 changes: 1 addition & 1 deletion extras/coco.js

Large diffs are not rendered by default.

35 changes: 4 additions & 31 deletions lib/grammar.js
Expand Up @@ -10,8 +10,10 @@ bnf = {
Value: [
o('Assignable', function(){
return Value($1);
}), o('Primitive', function(){
return Value($1);
}), o('STRNUM', function(){
return Value(L(Literal($1)));
}), o('LITERAL', function(){
return Value(L(Literal($1, true)));
}), o('Parenthetical', function(){
return Value($1);
}), o('THIS', function(){
Expand All @@ -20,13 +22,6 @@ bnf = {
return Value(L(Call($1, $3, $2)));
})
],
Primitive: [
o('STRNUM', function(){
return L(Literal($1));
}), o('LITERAL', function(){
return L(Literal($1, true));
})
],
SimpleAssignable: [
o('IDENTIFIER', function(){
return L(Literal($1));
Expand Down Expand Up @@ -166,17 +161,6 @@ bnf = {
return $1.concat($4);
}), o('ArgList OptComma INDENT ArgList OptComma DEDENT', function(){
return $1.concat($4);
}), o('ArgSeq'), o('ArgList , ArgSeq', function(){
return $1.concat($3);
}), o('ArgList OptComma TERMINATOR ArgSeq', function(){
return $1.concat($4);
})
],
ArgSeq: [
o('Primitive Primitive', function(){
return [$1, $2];
}), o('ArgSeq Primitive', function(){
return $1.concat($2);
})
],
ThisProperty: [
Expand Down Expand Up @@ -243,17 +227,6 @@ bnf = {
return $1.concat($4);
}), o('Properties OptComma INDENT Properties OptComma DEDENT', function(){
return $1.concat($4);
}), o('KeySeq'), o('Properties , KeySeq', function(){
return $1.concat($3);
}), o('Properties OptComma TERMINATOR KeySeq', function(){
return $1.concat($4);
})
],
KeySeq: [
o('STRNUM STRNUM', function(){
return [L(Literal($1)), L(Literal($2))];
}), o('KeySeq STRNUM', function(){
return $1.concat(L(Literal($2)));
})
],
Object: [
Expand Down
5 changes: 3 additions & 2 deletions lib/lexer.js
Expand Up @@ -360,17 +360,18 @@ exports.heregexToken = function(it){
return heregex.length;
};
exports.wordsToken = function(it){
var end, line, re, that, word, _i, _ref, _len, _j, _len2;
var end, tokens, line, re, that, word, _i, _ref, _len, _j, _len2, _ref2;
if (!~(end = it.indexOf(']>', 2))) {
this.carp('unterminated words');
}
this.token('[', '[');
tokens = this.tokens;
for (_i = 0, _len = (_ref = it.slice(2, end).split('\n')).length; _i < _len; ++_i) {
line = _ref[_i];
if (that = line.match(re || (re = /\S+/g))) {
for (_j = 0, _len2 = that.length; _j < _len2; ++_j) {
word = that[_j];
this.tokens.push(['STRNUM', string(word, '\''), this.line]);
tokens.push((_ref2 = ['STRNUM', string(word, '\''), this.line], _ref2.spaced = true, _ref2));
}
}
++this.line;
Expand Down
244 changes: 111 additions & 133 deletions lib/parser.js

Large diffs are not rendered by default.

25 changes: 16 additions & 9 deletions lib/rewriter.js
Expand Up @@ -225,7 +225,7 @@ function addImplicitParentheses(tokens){
token.pair[0] = ')CALL';
continue;
}
if (!(__indexOf.call(ARG, tag) >= 0 || (tag === 'STRNUM' || tag === 'LITERAL') || tag === '+-' && !(token.spaced || token.eol))) {
if (!(__indexOf.call(ARG, tag) >= 0 || tag === '+-' && !(token.spaced || token.eol))) {
continue;
}
skipBlock = seenSwitch = false;
Expand Down Expand Up @@ -341,7 +341,7 @@ function rewriteClosingParens(tokens){
}
}
function expandNumbers(tokens){
var i, token, num, sig, ts, lno, to, n, _ref, _step;
var i, token, num, sig, ts, lno, to, n, _step, _ref;
i = -1;
while (token = tokens[++i]) {
switch (token[0]) {
Expand All @@ -350,24 +350,31 @@ function expandNumbers(tokens){
token[1] = num.slice(1);
tokens.splice(i++, 0, ['+-', sig, token[2]]);
}
/* fallthrough */
break;
case 'LITERAL':
if (_ref = tokens[i + 1][0], __indexOf.call(ARG, _ref) >= 0) {
tokens.splice(++i, 0, [',', ',', token[2]]);
}
break;
case 'RANGE':
ts = [];
lno = token[2];
to = token.to - (token.op === 'to' ? 0 : 1e-15);
for (n = +token[1], _step = +token.by || 1; _step < 0 ? n >= to : n <= to; n += _step) {
if (1024 < ts.push(['STRNUM', n, lno])) {
if (2048 < ts.push(['STRNUM', n, lno], [',', ',', lno])) {
carp('range limit exceeded', lno);
}
}
ts.length || carp('empty range', lno);
if (ts.length) {
ts.pop();
} else {
carp('empty range', lno);
}
tokens.splice.apply(tokens, [i, 1].concat(__slice.call(ts)));
i += ts.length - 1;
break;
default:
continue;
}
if (token.spaced && (_ref = tokens[i + 1][0], __indexOf.call(ARG, _ref) >= 0)) {
tokens.splice(++i, 0, [',', ',', token[2]]);
}
}
}
Expand Down Expand Up @@ -428,4 +435,4 @@ for (_i = 0, _len = BALANCED_PAIRS.length; _i < _len; ++_i) {
EXPR_START.push(INVERSES[rite] = left);
EXPR_END.push(INVERSES[left] = rite);
}
ARG = ['(', '[', '{', '...', 'IDENTIFIER', 'THISPROP', 'THIS', 'RANGE', 'SUPER', 'PARAM(', 'FUNC_ARROW', 'FUNCTION', 'UNARY', 'CREMENT', 'IF', 'TRY', 'CLASS', 'SWITCH'];
ARG = ['(', '[', '{', '...', 'IDENTIFIER', 'THISPROP', 'THIS', 'STRNUM', 'LITERAL', 'RANGE', 'SUPER', 'PARAM(', 'FUNC_ARROW', 'FUNCTION', 'UNARY', 'CREMENT', 'IF', 'TRY', 'CLASS', 'SWITCH'];
25 changes: 2 additions & 23 deletions src/grammar.co
Expand Up @@ -58,18 +58,14 @@ bnf =
# The types of things that can be accessed or indexed into.
Value: [
o \Assignable ,-> Value $1
o \Primitive ,-> Value $1
o \STRNUM ,-> Value L Literal $1
o \LITERAL ,-> Value L Literal $1, true
o \Parenthetical ,-> Value $1
o \THIS ,-> Value L Literal \this, true

o 'Value CALL( ArgList OptComma )CALL' ,-> Value L Call $1, $3, $2
]

Primitive: [
o \STRNUM ,-> L Literal $1
o \LITERAL ,-> L Literal $1, true
]

# Variables and properties that can be assigned to.
SimpleAssignable: [
o \IDENTIFIER ,-> L Literal $1
Expand Down Expand Up @@ -198,15 +194,6 @@ bnf =
o 'ArgList , Arg' ,-> $1.concat $3
o 'ArgList OptComma TERMINATOR Arg' ,-> $1.concat $4
o 'ArgList OptComma INDENT ArgList OptComma DEDENT' ,-> $1.concat $4

o \ArgSeq
o 'ArgList , ArgSeq' ,-> $1.concat $3
o 'ArgList OptComma TERMINATOR ArgSeq' ,-> $1.concat $4
]
# Sequence of primitive values without explicit separators.
ArgSeq: [
o 'Primitive Primitive' ,-> [$1, $2]
o 'ArgSeq Primitive' ,-> $1.concat $2
]

# A reference to a property on `this`.
Expand Down Expand Up @@ -264,14 +251,6 @@ bnf =
o 'Properties , Property' ,-> $1.concat $3
o 'Properties OptComma TERMINATOR Property' ,-> $1.concat $4
o 'Properties OptComma INDENT Properties OptComma DEDENT' ,-> $1.concat $4

o \KeySeq
o 'Properties , KeySeq' ,-> $1.concat $3
o 'Properties OptComma TERMINATOR KeySeq' ,-> $1.concat $4
]
KeySeq: [
o 'STRNUM STRNUM' ,-> [L Literal $1; L Literal $2]
o 'KeySeq STRNUM' ,-> $1.concat L Literal $2
]
Object: [
o '{ Properties OptComma }' ,-> L Obj $2
Expand Down
5 changes: 3 additions & 2 deletions src/lexer.co
Expand Up @@ -284,9 +284,10 @@ exports import
wordsToken: ->
@carp 'unterminated words' unless ~end = it.indexOf \]> 2
@token \[ \[
{tokens} = this
for line of it.slice(2, end).split \\n
if line.match re ||= /\S+/g
@tokens.push [\STRNUM; string word, \'; @line] for word of that
if line.match re ||= /\S+/g then for word of that
tokens.push [\STRNUM; string word, \'; @line] <<< {+spaced}
++@line
--@line
@token \STRNUM "''" unless word
Expand Down
28 changes: 14 additions & 14 deletions src/rewriter.co
Expand Up @@ -80,7 +80,7 @@ function addImplicitIndentation (tokens) ->
continue if next is \THEN
continue unless tag of <[ THEN FUNC_ARROW DEFAULT TRY CATCH FINALLY ]> or
tag is \ELSE and next is not \IF
indent = [\INDENT 0, token.2]; dedent = [\DEDENT 0]
indent = [\INDENT 0 token.2]; dedent = [\DEDENT 0]
if tag is \THEN
tokens.splice --i, 1 if tokens[i-1]?.0 is \TERMINATOR
tokens[i] = indent
Expand Down Expand Up @@ -137,7 +137,7 @@ function addImplicitBraces (tokens) ->
stack.push [\{]
idx = if paren then start.1 else i-1
idx -= 2 while tokens[idx-2]?.0 is \COMMENT
tokens.splice idx, 0, [\{ \{, token.2]
tokens.splice idx, 0, [\{ \{ token.2]
detectEnd tokens, ++i+1, ok, go
void

Expand All @@ -159,11 +159,10 @@ function addImplicitParentheses (tokens) ->
token. 0 = \CALL(
token.pair.0 = \)CALL
continue
continue unless tag of ARG or tag of <[ STRNUM LITERAL ]> or
tag is \+- and not (token.spaced or token.eol)
continue unless tag of ARG or tag is \+- and !(token.spaced or token.eol)
skipBlock = seenSwitch = false
tokens.splice --i, 1 if soak = prev.0 is \?
tokens.splice i++, 0, [\CALL(, if soak then \?( else \(, token.2]
tokens.splice i++, 0, [\CALL( if soak then \?( else \(, token.2]
detectEnd tokens, i, ok, go
function ok (token, i) ->
return true if token.alias and token.1 of <[ && || ]>
Expand All @@ -183,7 +182,7 @@ function addImplicitParentheses (tokens) ->
false
function go (token, i) ->
++i if token.0 is \DEDENT
tokens.splice i, 0, [\)CALL \), token.2]
tokens.splice i, 0, [\)CALL \) token.2]
void

# Ensure that all listed pairs of tokens are correctly balanced throughout
Expand Down Expand Up @@ -248,19 +247,20 @@ function expandNumbers (tokens) ->
case \STRNUM
if ~'-+'.indexOf sig = (num = '' + token.1).charAt 0
token.1 = num.slice 1
tokens.splice i++, 0, [\+-, sig, token.2]
fallthrough
case \LITERAL
tokens.splice ++i, 0, [\, \,; token.2] if tokens[i+1].0 of ARG
tokens.splice i++, 0, [\+- sig, token.2]
case \LITERAL then break
case \RANGE
ts = []; lno = token.2
to = token.to - if token.op is \to then 0 else 1e-15
for n from +token.1 to to by +token.by or 1
if 32r100 < ts.push [\STRNUM, n, lno]
if 32r200 < ts.push [\STRNUM n, lno], [\, \, lno]
carp 'range limit exceeded', lno
ts.length or carp 'empty range', lno
if ts.length then ts.pop() else carp 'empty range', lno
tokens.splice i, 1, ...ts
i += ts.length - 1
default continue
if token.spaced and tokens[i+1].0 of ARG
tokens.splice ++i, 0, [\, \, token.2]
void

### Helpers
Expand Down Expand Up @@ -317,6 +317,6 @@ for [left, rite] of BALANCED_PAIRS
EXPR_START.push INVERSES[rite] = left
EXPR_END .push INVERSES[left] = rite

# Tokens that can start an argument list, sans-primitives.
ARG = <[ ( [ { ... IDENTIFIER THISPROP THIS RANGE SUPER
# Tokens that can start an argument list.
ARG = <[ ( [ { ... IDENTIFIER THISPROP THIS STRNUM LITERAL RANGE SUPER
PARAM( FUNC_ARROW FUNCTION UNARY CREMENT IF TRY CLASS SWITCH ]>
2 changes: 1 addition & 1 deletion test/literals.co
Expand Up @@ -2,7 +2,7 @@ a = [((x) -> x), ((x) -> x * x)]
eq a.length, 2


eq (3 -4), -1
eq 3-4, -1


# Decimal number literals.
Expand Down

0 comments on commit 1ad4648

Please sign in to comment.