这是indexloc提供的服务,不要输入任何密码
Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Conversation

@annettejanewilson
Copy link
Contributor

@annettejanewilson annettejanewilson commented Feb 25, 2020

Description

Problem

When showing evaluated expressions (e.g. in the computations column when
testing an expression or an alert), the expressions are ambiguous (and
misleading) because they include no parentheses, even where they are
necessary.

E.g. evaluate the following expression:

(1 + 2) / 3

and the computations column will show:

1 + 2 / 3 = 1
1 + 2     = 3

Compare to evaluating the expression:

1 + (2 / 3)

which will show:

1 + 2 / 3 = 1.66667
2 / 3     = 0.66667

Solution

The simplest fix is to wrap all binary expressions in parentheses. This
does emit redundant parentheses, but it's much simpler than figuring
out when precedence rules allow the safe elision of parentheses. So for
example this expression:

1 + 2 + 3 + 4

will be rendered:

(((1 + 2) + 3) + 4)

I think any extra verbosity here is worth paying for being explicit and
unambiguous.

An alternative solution would be to apply the parentheses in the
parent node - unconditionally if the parent node is a unary expression
and the child is a binary expression, and conditionally if the parent
node is a binary expression and the child is a binary expression: in
this case parentheses are required on the left child node if it binds
less tightly than the parent's operator, and are required on the right
child node if it binds less or equally tightly than the parent's
operator. I have now implemented this and added it to the PR.

Fixes #2455 - Feature request: show parentheses in formatted expressions

Type of change

From the following, please check the options that are relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How has this been tested?

Please describe the tests that you ran to verify your changes.
Provide instructions so we can reproduce.
Please also list any relevant details for your test configuration

  • Pasted the above expressions in the expression pane
  • Updated the expected results in the unit tests
  • Test against live data in the rule editor (not done)
  • Test anywhere else these expressions appear in the UI (not done)

Checklist:

  • This contribution follows the project's code of conduct
  • This contribution follows the project's contributing guidelines
  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added updated tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Show parentheses when emitting expressions

Problem
=======

When showing evaluated expressions (e.g. in the computations column when
testing an expression or an alert), the expressions are ambiguous (and
misleading) because they include no parentheses, even where they are
necessary.

E.g. evaluate the following expression:

```
(1 + 2) / 3
```

and the computations column will show:

```
1 + 2 / 3 = 1
1 + 2     = 3
```

Compare to evaluating the expression:

```
1 + (2 / 3)
```

which will show:

```
1 + 2 / 3 = 1.66667
2 / 3     = 0.66667
```

Solution
========

The simplest fix is to wrap all binary expressions in parentheses. This
_does_ emit redundant parentheses, but it's much simpler than figuring
out when precedence rules allow the safe elision of parentheses. So for
example this expression:

```
1 + 2 + 3 + 4
```

will be rendered:

```
(((1 + 2) + 3) + 4)
```

I think any extra verbosity here is worth paying for being explicit and
unambiguous.

An alternative solution would be to apply the parentheses in the
_parent_ node - unconditionally if the parent node is a unary expression
and the child is a binary expression, and conditionally if the parent
node is a binary expression and the child is a binary expression: in
this case parentheses are required on the left child node if it binds
less tightly than the parent's operator, and are required on the right
child node if it binds less or equally tightly than the parent's
operator. This is far too painful to think about so I'm not going to.
Problem: Now that we _always_ wrap parentheses around binary operations
we can produce expressions that are needlessly verbose. E.g.

```
(((1 + 2) + 3) + 4) + 5
```

Solution: Eliminate all the redundant parens - i.e. those that won't
change the parsed expression tree. The main place parentheses need to
remain are:

1. when we want to apply a lower precedence binary operation (e.g. +)
before a higher precedence operation (e.g. *),

2. if we want to apply a left-associative binary operator from the right
instead (e.g.  subtractions such as `3 - (2 - 1)` ), and

3. if we want to apply a binary operation before a unary operation (all
binary operations in the expression language have lower precedence than
all unary operations).
@stale
Copy link

stale bot commented Apr 5, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Apr 5, 2021
@stale stale bot closed this May 6, 2021
@annettejanewilson
Copy link
Contributor Author

I would still like to propose this. It solves a real problem.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: show parentheses in formatted expressions

1 participant