New: array literals in Objective-J 2.0.

Array literals look like `@[a, b, c]`.

This syntax is supported for completeness and source compatibility, but are not terribly useful since standard JavaScript arrays are toll-free bridged to CPArray.

Array literals could be handy if you're replacing CPArray with your own implementation.
This commit is contained in:
Alexander Ljungberg
2013-02-25 14:57:05 +00:00
parent 5200f945fe
commit 472848da91
4 changed files with 48 additions and 3 deletions
+23
View File
@@ -774,6 +774,29 @@ Identifier: function(node, st, c) {
}
}
},
ArrayLiteral: function(node, st, c) {
CONCAT(st.compiler.jsBuffer, st.compiler.source.substring(st.compiler.lastPos, node.start));
st.compiler.lastPos = node.start;
if (!node.elements.length) {
CONCAT(st.compiler.jsBuffer, "objj_msgSend(objj_msgSend(CPArray, \"alloc\"), \"init\")");
} else {
CONCAT(st.compiler.jsBuffer, "objj_msgSend(objj_msgSend(CPArray, \"alloc\"), \"initWithObjects:count:\", [");
for (var i = 0; i < node.elements.length; i++) {
var elt = node.elements[i];
if (i)
CONCAT(st.compiler.jsBuffer, ", ");
st.compiler.lastPos = elt.start;
c(elt, st, "Expression");
CONCAT(st.compiler.jsBuffer, st.compiler.source.substring(st.compiler.lastPos, elt.end));
}
CONCAT(st.compiler.jsBuffer, "], " + node.elements.length + ")");
}
st.compiler.lastPos = node.end;
},
DictionaryLiteral: function(node, st, c) {
CONCAT(st.compiler.jsBuffer, st.compiler.source.substring(st.compiler.lastPos, node.start));
st.compiler.lastPos = node.start;
+16 -2
View File
@@ -273,7 +273,7 @@ if (!exports.acorn) {
var _implementation = {keyword: "implementation"}, _outlet = {keyword: "outlet"}, _accessors = {keyword: "accessors"};
var _end = {keyword: "end"}, _import = {keyword: "import", afterImport: true};
var _action = {keyword: "action"}, _selector = {keyword: "selector"}, _class = {keyword: "class"}, _global = {keyword: "global"};
var _dictionaryLiteral = {keyword: "{"};
var _dictionaryLiteral = {keyword: "{"}, _arrayLiteral = {keyword: "["};
// Objective-J keywords
@@ -663,6 +663,8 @@ if (!exports.acorn) {
return readString(next);
if (next === 123) // Read dictionary literal if "{"
return finishToken(_dictionaryLiteral);
if (next === 91) // Ready array literal if "["
return finishToken(_arrayLiteral);
var word = readWord1(),
token = objJAtKeywordTypes[word];
@@ -1851,7 +1853,7 @@ if (!exports.acorn) {
case _null: case _true: case _false:
var node = startNode();
node.value = tokType.atomValue;
node.raw = tokType.keyword
node.raw = tokType.keyword;
next();
return finishNode(node, "Literal");
@@ -1870,6 +1872,18 @@ if (!exports.acorn) {
expect(_parenR, "Expected closing ')' in expression");
return val;
case _arrayLiteral:
var node = startNode(),
firstExpr = null;
next();
expect(_bracketL, "Expected '[' at beginning of array literal");
if (tokType !== _bracketR)
firstExpr = parseExpression(true, true);
node.elements = parseExprList(_bracketR, firstExpr, true, true);
return finishNode(node, "ArrayLiteral");
case _bracketL:
var node = startNode(),
firstExpr = null;
+1 -1
View File
@@ -149,7 +149,7 @@ if (!exports.acorn) {
exports.Expression = skipThrough;
exports.ThisExpression = ignore;
exports.ArrayExpression = function(node, st, c) {
exports.ArrayExpression = exports.ArrayLiteral = function(node, st, c) {
for (var i = 0; i < node.elements.length; ++i) {
var elt = node.elements[i];
if (elt) c(elt, st, "Expression");
+8
View File
@@ -1,5 +1,6 @@
@import <Foundation/Foundation.j>
@import <OJUnit/OJTestCase.j>
@implementation CPArrayTest : OJTestCase
{
@@ -681,6 +682,13 @@
[self assertThrows:function() { [anArray removeObserver:self forKeyPath:@"self"]; }];
}
- (void)testArrayLiteral
{
var anArray = @[1, [CPNull null], @"3"];
[self assert:[1, [CPNull null], "3"] equals:anArray];
}
@end
@implementation AlwaysEqual : CPObject