Skip to content

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

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open

Negative parsing fix #115

wants to merge 17 commits into from

Conversation

joshestein
Copy link

@joshestein joshestein commented Sep 7, 2022

Fixes parsing problems with consecutive operators, where one operator is a '-'. This led to expressions such as '2 x -3' being invalid.

@joshestein joshestein marked this pull request as ready for review September 7, 2022 09:52
@plegner
Copy link
Member

plegner commented Nov 8, 2022

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'));

@joshestein
Copy link
Author

joshestein commented Nov 11, 2022

If a - -b should be parsed as a - (-b), should a - - - b not be parsed as a - (-(-b))?

@plegner
Copy link
Member

plegner commented Nov 11, 2022

Sure, we can do that too!

Copy link

@leon-arndt leon-arndt left a 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.
Copy link
Contributor

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.

Copy link
Author

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, '- −');
Copy link
Contributor

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.

@swarty swarty requested a review from plegner December 11, 2023 11:21
src/parser.ts Outdated
Comment on lines 170 to 182
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;
}
}
}
Copy link
Contributor

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?

Copy link
Contributor

Choose a reason for hiding this comment

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

  1. looks like it should return tokens as result or throw some error as in findBinaryFunction function?
  2. redeclaring/modifying function param makes code readability so hard.

@joshestein joshestein force-pushed the negative-parsing-fix branch from bd368a8 to 2b1af8b Compare May 14, 2025 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants