adding existence soaks for indexed-lookup property accesses: obj?['property']

This commit is contained in:
Jeremy Ashkenas
2010-02-24 00:06:01 -05:00
parent 4eeb8c4bd2
commit 10d335ccb1
10 changed files with 111 additions and 83 deletions

View File

@@ -251,6 +251,7 @@ grammar: {
# Indexing into an object or array.
Index: [
o "INDEX_START Expression INDEX_END", -> new IndexNode($2)
o "SOAKED_INDEX_START Expression SOAKED_INDEX_END", -> new IndexNode($2, 'soak')
]
# An object literal.

View File

@@ -246,13 +246,21 @@ lex::literal_token: ->
value: match and match[1]
@tag_parameters() if value and value.match(CODE)
value ||= @chunk.substr(0, 1)
not_spaced: not @prev() or not @prev().spaced
tag: value
if value.match(ASSIGNMENT)
tag: 'ASSIGN'
throw new Error('SyntaxError: Reserved word "' + @value() + '" on line ' + @line + ' can\'t be assigned') if JS_FORBIDDEN.indexOf(@value()) >= 0
tag: 'TERMINATOR' if value == ';'
if CALLABLE.indexOf(@tag()) >= 0 and (not @prev() or not @prev().spaced)
else if value is ';'
tag: 'TERMINATOR'
else if value is '[' and @tag() is '?' and not_spaced
tag: 'SOAKED_INDEX_START'
@soaked_index: true
@tokens.pop()
else if value is ']' and @soaked_index
tag: 'SOAKED_INDEX_END'
@soaked_index: false
else if CALLABLE.indexOf(@tag()) >= 0 and not_spaced
tag: 'CALL_START' if value is '('
tag: 'INDEX_START' if value is '['
@token tag, value

View File

@@ -283,7 +283,7 @@ ValueNode: exports.ValueNode: inherit Node, {
for prop in props
@source: baseline
if prop instanceof AccessorNode and prop.soak
if prop.soak_node
soaked: true
if @base instanceof CallNode and prop is props[0]
temp: o.scope.free_variable()
@@ -405,7 +405,7 @@ AccessorNode: exports.AccessorNode: inherit Node, {
constructor: (name, tag) ->
@children: [@name: name]
@prototype: tag is 'prototype'
@soak: tag is 'soak'
@soak_node: tag is 'soak'
this
compile_node: (o) ->
@@ -417,8 +417,9 @@ AccessorNode: exports.AccessorNode: inherit Node, {
IndexNode: exports.IndexNode: inherit Node, {
type: 'Index'
constructor: (index) ->
@children: [@index: index]
constructor: (index, tag) ->
@children: [@index: index]
@soak_node: tag is 'soak'
this
compile_node: (o) ->

View File

@@ -7,7 +7,8 @@ exports.Rewriter: re: ->
# Tokens that must be balanced.
BALANCED_PAIRS: [['(', ')'], ['[', ']'], ['{', '}'], ['INDENT', 'OUTDENT'],
['PARAM_START', 'PARAM_END'], ['CALL_START', 'CALL_END'], ['INDEX_START', 'INDEX_END']]
['PARAM_START', 'PARAM_END'], ['CALL_START', 'CALL_END'],
['INDEX_START', 'INDEX_END'], ['SOAKED_INDEX_START', 'SOAKED_INDEX_END']]
# Tokens that signal the start of a balanced pair.
EXPRESSION_START: pair[0] for pair in BALANCED_PAIRS