From b027b5cf0da1a2fe51f097e2bec9e7b44ebca962 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sat, 20 Feb 2010 20:09:52 -0500 Subject: [PATCH] Allowing @[property] syntax. --- lib/grammar.js | 2 ++ lib/lexer.js | 2 +- src/grammar.coffee | 1 + src/lexer.coffee | 2 +- test/test_literals.coffee | 2 +- 5 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/grammar.js b/lib/grammar.js index a1e7c576..4ff0021a 100644 --- a/lib/grammar.js +++ b/lib/grammar.js @@ -343,6 +343,8 @@ return new ValueNode(new LiteralNode('this')); }), o("@ Identifier", function() { return new ValueNode(new LiteralNode('this'), [new AccessorNode($2)]); + }), o("@ Index", function() { + return new ValueNode(new LiteralNode('this'), [$2]); }) ], // The range literal. diff --git a/lib/lexer.js b/lib/lexer.js index 0fbdca4c..fc4cc049 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -49,7 +49,7 @@ // See: http://www.mozilla.org/js/language/js20-2002-04/rationale/syntax.html#regular-expressions NOT_REGEX = ['IDENTIFIER', 'NUMBER', 'REGEX', 'STRING', ')', '++', '--', ']', '}', 'FALSE', 'NULL', 'TRUE']; // Tokens which could legitimately be invoked or indexed. - CALLABLE = ['IDENTIFIER', 'SUPER', ')', ']', '}', 'STRING']; + CALLABLE = ['IDENTIFIER', 'SUPER', ')', ']', '}', 'STRING', '@']; // Tokens that indicate an access -- keywords immediately following will be // treated as identifiers. ACCESSORS = ['PROPERTY_ACCESS', 'PROTOTYPE_ACCESS', 'SOAK_ACCESS', '@']; diff --git a/src/grammar.coffee b/src/grammar.coffee index 9965a79f..6fb430d5 100644 --- a/src/grammar.coffee +++ b/src/grammar.coffee @@ -300,6 +300,7 @@ grammar: { This: [ o "@", -> new ValueNode(new LiteralNode('this')) o "@ Identifier", -> new ValueNode(new LiteralNode('this'), [new AccessorNode($2)]) + o "@ Index", -> new ValueNode(new LiteralNode('this'), [$2]) ] # The range literal. diff --git a/src/lexer.coffee b/src/lexer.coffee index 2e703c54..81d44b58 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -78,7 +78,7 @@ NOT_REGEX: [ ] # Tokens which could legitimately be invoked or indexed. -CALLABLE: ['IDENTIFIER', 'SUPER', ')', ']', '}', 'STRING'] +CALLABLE: ['IDENTIFIER', 'SUPER', ')', ']', '}', 'STRING', '@'] # Tokens that indicate an access -- keywords immediately following will be # treated as identifiers. diff --git a/test/test_literals.coffee b/test/test_literals.coffee index e07bfcec..6c5385f9 100644 --- a/test/test_literals.coffee +++ b/test/test_literals.coffee @@ -45,7 +45,7 @@ bob: { greet: (salutation) -> salutation + " " + @name hello: -> - @greet "Hello" + @['greet'] "Hello" 10: 'number' }