Virtual parentheses are projection-only parentheses added when an expression AST’s grouping would otherwise be displayed ambiguously. They clarify operator-precedence grouping without changing the underlying expression AST. section 12.2 section 12.5
Implement virtual parentheses by:
- Defining operator precedence and comparing a nested binary operation with its parent. section 12.2
- Wrapping the nested operation when the parent operator has lower precedence. section 12.2 section 12.5
- Applying the same criterion to the UI projection and generated code, while leaving the AST unchanged. section 12.2
Disambiguating 1 * (2 + 3)
An AST for 1 * (2 + 3) can otherwise project like 1 * 2 + 3. Virtual parentheses make the grouping visible in the projection and preserve equivalent grouping in generated code. section 12.2 section 12.5
Figure 12.14 The criterion to decide whether a binary operation needs to be wrapped in parentheses because of order of operations in combination with operator precedence
Use the precedence criterion to determine when a nested binary operation needs projection-only parentheses. section 12.2
