mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-19 03:44:23 -05:00
finally, think we have this if/else/if/else/else thing licke
This commit is contained in:
@@ -34,7 +34,7 @@ operators: [
|
||||
["right", 'THROW', 'FOR', 'NEW', 'SUPER']
|
||||
["left", 'EXTENDS']
|
||||
["right", 'ASSIGN', 'RETURN']
|
||||
["right", '->', '=>', 'UNLESS', 'IF', 'ELSE', 'ELSIF', 'WHILE']
|
||||
["right", '->', '=>', 'UNLESS', 'IF', 'ELSE', 'WHILE']
|
||||
]
|
||||
|
||||
# Grammar ==============================================================
|
||||
@@ -410,25 +410,25 @@ grammar: {
|
||||
]
|
||||
|
||||
# The most basic form of "if".
|
||||
IfBlock: [
|
||||
IfStart: [
|
||||
o "IF Expression Block", -> new IfNode($2, $3)
|
||||
o "IF Expression Block ElsIfs", -> (new IfNode($2, $3)).add_else($4)
|
||||
o "IfStart ElsIfs", -> $1.add_else($2)
|
||||
]
|
||||
|
||||
ElsIf: [
|
||||
o "ELSEIF Expression Block", -> new IfNode($2, $3)
|
||||
IfBlock: [
|
||||
o "IfStart", -> $1
|
||||
o "IfStart ELSE Block", -> $1.add_else($3)
|
||||
]
|
||||
|
||||
# Multiple elsifs can be chained together.
|
||||
ElsIfs: [
|
||||
o "ElsIf", -> $1.force_statement()
|
||||
o "ELSE IF Expression Block", -> (new IfNode($3, $4)).force_statement()
|
||||
o "ElsIfs ElsIf", -> $1.add_else($2)
|
||||
]
|
||||
|
||||
# The full complement of if blocks, including postfix one-liner ifs and unlesses.
|
||||
If: [
|
||||
o "IfBlock", -> $1
|
||||
o "IfBlock ELSE Block", -> $1.add_else($3)
|
||||
o "Expression IF Expression", -> new IfNode($3, Expressions.wrap([$1]), null, {statement: true})
|
||||
o "Expression UNLESS Expression", -> new IfNode($3, Expressions.wrap([$1]), null, {statement: true, invert: true})
|
||||
]
|
||||
|
||||
@@ -105,10 +105,7 @@ lex::identifier_token: ->
|
||||
@tokens.splice(-2, 1)
|
||||
else
|
||||
@tag(1, 'PROPERTY_ACCESS')
|
||||
if tag is 'IF' and @tag() is 'ELSE'
|
||||
@tag(1, 'ELSIF')
|
||||
else
|
||||
@token(tag, id)
|
||||
@token(tag, id)
|
||||
@i += id.length
|
||||
true
|
||||
|
||||
|
||||
@@ -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', 'ELSIF', 'FINALLY'].concat(EXPRESSION_TAIL)
|
||||
EXPRESSION_CLOSE: ['CATCH', 'WHEN', 'ELSE', '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', 'ELSIF', 'OUTDENT', 'LEADING_WHEN', 'PARAM_START']
|
||||
SINGLE_CLOSERS: ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', '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,7 +160,8 @@ 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'
|
||||
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')
|
||||
starter: token[0]
|
||||
this.tokens.splice(i + 1, 0, ['INDENT', 2, token[2]])
|
||||
idx: i + 1
|
||||
|
||||
Reference in New Issue
Block a user