From c9aeae757b19704a4bd3045c93a10d8549532406 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sat, 13 Feb 2010 00:15:34 -0500 Subject: [PATCH] pulling the lexer and rewriter updates without the grammar change --- lib/coffee_script/lexer.js | 2 +- lib/coffee_script/rewriter.js | 6 +++--- src/lexer.coffee | 2 +- src/rewriter.coffee | 7 +++---- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/coffee_script/lexer.js b/lib/coffee_script/lexer.js index 1e27e9b9..5a47c482 100644 --- a/lib/coffee_script/lexer.js +++ b/lib/coffee_script/lexer.js @@ -117,7 +117,7 @@ this.tag(1, 'PROPERTY_ACCESS'); } } - tag === 'ELSE' && this.tag() === 'IF' ? this.tag(1, 'ELSIF') : this.token(tag, id); + tag === 'IF' && this.tag() === 'ELSE' ? this.tag(1, 'ELSIF') : this.token(tag, id); this.i += id.length; return true; }; diff --git a/lib/coffee_script/rewriter.js b/lib/coffee_script/rewriter.js index 0dc0dc4f..dddc6e5a 100644 --- a/lib/coffee_script/rewriter.js +++ b/lib/coffee_script/rewriter.js @@ -26,7 +26,7 @@ return __d; }).call(this); // Tokens that indicate the close of a clause of an expression. - EXPRESSION_CLOSE = ['CATCH', 'WHEN', 'ELSE', 'FINALLY'].concat(EXPRESSION_TAIL); + EXPRESSION_CLOSE = ['CATCH', 'WHEN', 'ELSE', 'ELSIF', 'FINALLY'].concat(EXPRESSION_TAIL); // Tokens pairs that, in immediate succession, indicate an implicit call. IMPLICIT_FUNC = ['IDENTIFIER', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END']; IMPLICIT_END = ['IF', 'UNLESS', 'FOR', 'WHILE', 'TERMINATOR', 'OUTDENT']; @@ -43,7 +43,7 @@ // Single-line flavors of block expressions that have unclosed endings. // The grammar can't disambiguate them, so we insert the implicit indentation. SINGLE_LINERS = ['ELSE', "->", "=>", 'TRY', 'FINALLY', 'THEN']; - SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN', 'PARAM_START']; + SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'ELSIF', 'OUTDENT', 'LEADING_WHEN', 'PARAM_START']; // Rewrite the token stream in multiple passes, one logical filter at // a time. This could certainly be changed into a single pass through the // stream, with a big ol' efficient switch, but it's much nicer like this. @@ -229,7 +229,7 @@ return this.scan_tokens((function(__this) { var __func = function(prev, token, post, i) { var idx, insertion, parens, starter, tok; - if (!(SINGLE_LINERS.indexOf(token[0]) >= 0 && post[0] !== 'INDENT' && !(token[0] === 'ELSE' && post[0] === 'IF'))) { + if (!(SINGLE_LINERS.indexOf(token[0]) >= 0 && post[0] !== 'INDENT')) { return 1; } starter = token[0]; diff --git a/src/lexer.coffee b/src/lexer.coffee index 3d430088..8f657f40 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -105,7 +105,7 @@ lex::identifier_token: -> @tokens.splice(-2, 1) else @tag(1, 'PROPERTY_ACCESS') - if tag is 'ELSE' and @tag() is 'IF' + if tag is 'IF' and @tag() is 'ELSE' @tag(1, 'ELSIF') else @token(tag, id) diff --git a/src/rewriter.coffee b/src/rewriter.coffee index b1d99491..eab4fe2f 100644 --- a/src/rewriter.coffee +++ b/src/rewriter.coffee @@ -14,7 +14,7 @@ EXPRESSION_START: pair[0] for pair in BALANCED_PAIRS EXPRESSION_TAIL: pair[1] for pair in BALANCED_PAIRS # Tokens that indicate the close of a clause of an expression. -EXPRESSION_CLOSE: ['CATCH', 'WHEN', 'ELSE', 'FINALLY'].concat(EXPRESSION_TAIL) +EXPRESSION_CLOSE: ['CATCH', 'WHEN', 'ELSE', 'ELSIF', 'FINALLY'].concat(EXPRESSION_TAIL) # Tokens pairs that, in immediate succession, indicate an implicit call. IMPLICIT_FUNC: ['IDENTIFIER', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END'] @@ -33,7 +33,7 @@ for pair in BALANCED_PAIRS # Single-line flavors of block expressions that have unclosed endings. # The grammar can't disambiguate them, so we insert the implicit indentation. SINGLE_LINERS: ['ELSE', "->", "=>", 'TRY', 'FINALLY', 'THEN'] -SINGLE_CLOSERS: ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN', 'PARAM_START'] +SINGLE_CLOSERS: ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'ELSIF', 'OUTDENT', 'LEADING_WHEN', 'PARAM_START'] # Rewrite the token stream in multiple passes, one logical filter at # a time. This could certainly be changed into a single pass through the @@ -160,8 +160,7 @@ re::add_implicit_parentheses: -> # ')' can close a single-line block, but we need to make sure it's balanced. re::add_implicit_indentation: -> this.scan_tokens (prev, token, post, i) => - return 1 unless SINGLE_LINERS.indexOf(token[0]) >= 0 and post[0] isnt 'INDENT' and - not (token[0] is 'ELSE' and post[0] is 'IF') + return 1 unless SINGLE_LINERS.indexOf(token[0]) >= 0 and post[0] isnt 'INDENT' starter: token[0] this.tokens.splice(i + 1, 0, ['INDENT', 2, token[2]]) idx: i + 1