Skip to content

Commit

Permalink
feat: trailing commas in matrices (#3154)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvd101x authored Feb 15, 2024
1 parent 24b79ae commit 065b3ae
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/expression/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1544,8 +1544,10 @@ export const createParse = /* #__PURE__ */ factory(name, dependencies, ({
while (state.token === ';') { // eslint-disable-line no-unmodified-loop-condition
getToken(state)

params[rows] = parseRow(state)
rows++
if (state.token !== ']') {
params[rows] = parseRow(state)
rows++
}
}

if (state.token !== ']') {
Expand Down Expand Up @@ -1599,8 +1601,10 @@ export const createParse = /* #__PURE__ */ factory(name, dependencies, ({
getToken(state)

// parse expression
params[len] = parseAssignment(state)
len++
if (state.token !== ']' && state.token !== ';') {
params[len] = parseAssignment(state)
len++
}
}

return new ArrayNode(params)
Expand Down
39 changes: 38 additions & 1 deletion test/unit-tests/expression/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,43 @@ describe('parse', function () {
assert.deepStrictEqual(parseAndEval('[[[1],[2]],[[3],[4]]]'), math.matrix([[[1], [2]], [[3], [4]]]))
})

it('should parse a matrix with trailing commas', function () {
assert.ok(parseAndEval('[1,2;3,4;]') instanceof Matrix)

const m = parseAndEval('[1,2,3;4,5,6;]')
assert.deepStrictEqual(m.size(), [2, 3])
assert.deepStrictEqual(m, math.matrix([[1, 2, 3], [4, 5, 6]]))

const b = parseAndEval('[5, 6; 1, 1;]')
assert.deepStrictEqual(b.size(), [2, 2])
assert.deepStrictEqual(b, math.matrix([[5, 6], [1, 1]]))

// from 1 to n dimensions
assert.deepStrictEqual(parseAndEval('[ ]'), math.matrix([]))
assert.deepStrictEqual(parseAndEval('[1,2,3,]'), math.matrix([1, 2, 3]))
assert.deepStrictEqual(parseAndEval('[1;2;3;]'), math.matrix([[1], [2], [3]]))
assert.deepStrictEqual(parseAndEval('[[1,2],[3,4],]'), math.matrix([[1, 2], [3, 4]]))
assert.deepStrictEqual(parseAndEval('[[[1],[2]],[[3],[4]],]'), math.matrix([[[1], [2]], [[3], [4]]]))
})

it('should throw an error when multiple trailing commas/semicolons are in a matrix', function () {
assert.throws(function () {
parseAndEval('[1,2,3,,] ')
}, /SyntaxError: Value expected/)

assert.throws(function () {
parseAndEval('[1,2;3,4;,] ')
}, /SyntaxError: Value expected/)

assert.throws(function () {
parseAndEval('[1;2;3;;]')
}, /SyntaxError: Value expected/)

assert.throws(function () {
parseAndEval('[[[1],[2]],[[3],[4]],,]')
}, /SyntaxError: Value expected/)
})

it('should parse an empty matrix', function () {
assert.deepStrictEqual(parseAndEval('[]'), math.matrix([]))
})
Expand Down Expand Up @@ -2365,7 +2402,7 @@ describe('parse', function () {

try {
mathClone.evaluate('f(x)=1;config({clone:f})')
} catch (err) {}
} catch (err) { }

assert.strictEqual(mathClone.evaluate('2'), 2)
})
Expand Down

0 comments on commit 065b3ae

Please sign in to comment.