diff --git a/Objective-J/ObjJAcornCompiler.js b/Objective-J/ObjJAcornCompiler.js index 664cfe371..57657769b 100644 --- a/Objective-J/ObjJAcornCompiler.js +++ b/Objective-J/ObjJAcornCompiler.js @@ -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; diff --git a/Objective-J/acorn.js b/Objective-J/acorn.js index 826b7af2e..d40104433 100644 --- a/Objective-J/acorn.js +++ b/Objective-J/acorn.js @@ -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; diff --git a/Objective-J/acornwalk.js b/Objective-J/acornwalk.js index 7e59ba067..470fbb88a 100644 --- a/Objective-J/acornwalk.js +++ b/Objective-J/acornwalk.js @@ -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"); diff --git a/Tests/Foundation/CPArrayTest.j b/Tests/Foundation/CPArrayTest.j index e2fec2da7..027d6ca84 100644 --- a/Tests/Foundation/CPArrayTest.j +++ b/Tests/Foundation/CPArrayTest.j @@ -1,5 +1,6 @@ @import +@import @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