-
Notifications
You must be signed in to change notification settings - Fork 4
Negative parsing fix #115
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
base: master
Are you sure you want to change the base?
Negative parsing fix #115
Conversation
Can we make sure that all these tests pass? test.equal(expr('a * - b').collapse().toString(), 'a × (−b)'); // works!
test.equal(expr('a + - b').collapse().toString(), 'a + (−b)');
test.equal(expr('a - - b').collapse().toString(), 'a − (−b)');
test.equal(expr('10% - b').collapse().toString(), '10% − b');
test.equal(expr('-(a + b)').collapse().toString(), '−(a + b)'); // works!
test.throws(() => expr('a - - - b')); |
If |
Sure, we can do that too! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
src/parser.ts
Outdated
@@ -163,6 +163,22 @@ function findBinaryFunction(tokens: ExprElement[], fn: string) { | |||
} | |||
} | |||
|
|||
// Some minuses have been parsed as functions (i.e. an `ExprFunction` with a single argument). | |||
// If they are preceded by something, we need to treat the expression as a unary minus. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello, what do you think about rephrasing this comment to something like this:
// Some minuses are initially parsed as unary functions (i.e. the string "- b" is parsed an `ExprFunction` with
// `.fn` = "−" and an `ExprIdentifier` "b" as its single argument.)
// If any unary minus function is preceded by another token, we need to merge the pair of tokens into a binary minus
// function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely clearer!
src/parser.ts
Outdated
tokens.splice(0, 2, new ExprFunction((tokens[0] as ExprOperator).o, [tokens[1]])); | ||
} | ||
findBinaryFunction(tokens, '− ±'); | ||
findBinaryFunction(tokens, '- −'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it should be a part of this PR but perhaps some day we should rename this function and findBinaryMinusFunctions
to have more descriptive names, and/or add a doc comment to the former. To help distinguish what they do.
src/parser.ts
Outdated
function findBinaryMinusFunctions(tokens: ExprElement[]) { | ||
for (let i = 1; i < tokens.length; i++) { | ||
const token = tokens[i]; | ||
if (token instanceof ExprFunction && token.fn === '−') { | ||
const a = tokens[i - 1]; | ||
const b = token.args[0]; | ||
|
||
const args = [removeBrackets(a), removeBrackets(b)]; | ||
tokens.splice(i - 1, 2, new ExprFunction('−', args)); | ||
i -= 1; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i can not understand this function, this is not return and do not modify anything except function param, just loop?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- looks like it should return tokens as result or throw some error as in
findBinaryFunction
function? - redeclaring/modifying function param makes code readability so hard.
bd368a8
to
2b1af8b
Compare
Fixes parsing problems with consecutive operators, where one operator is a '-'. This led to expressions such as '2 x -3' being invalid.