Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trailing commas in matrices #3154

Merged
merged 4 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 20 additions & 1 deletion test/unit-tests/expression/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,25 @@ 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 () {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some unit tests validating that multiple trailing commas/semicolons are not allowed?

Like [1,2,3,,] and [1,2;3,4;,].

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 parse an empty matrix', function () {
assert.deepStrictEqual(parseAndEval('[]'), math.matrix([]))
})
Expand Down Expand Up @@ -2333,7 +2352,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