Skip to content

Support empty function calls and be more particular about successful parses#90

Merged
cwisniew merged 4 commits intoRPTools:mainfrom
kwvanderlinde:bugfix/82-empty-function-calls
Mar 28, 2025
Merged

Support empty function calls and be more particular about successful parses#90
cwisniew merged 4 commits intoRPTools:mainfrom
kwvanderlinde:bugfix/82-empty-function-calls

Conversation

@kwvanderlinde
Copy link
Copy Markdown
Contributor

@kwvanderlinde kwvanderlinde commented Mar 28, 2025

Fixes various issues with #82

Empty function calls were being rejected since the parser grammar was requiring at least one expression between the parenthesis. This has been fixed to make the arguments optional, thus allowing zero arguments to be passed.

Another issue was that we were calling BigDecimal#toPlainString() instead of BigDecimal#toString() as the antrl2 implementation did. A related "problem" is that we cann BigDecimal#toString() where it isn't needed - when formatting NumberLiteral nodes, we use the node text rather than formatting the BigDecimal value.

The final issue is that antlr is pretty lenient in its parsing by default. Since MapTool depends on parse errors to decide whether to try treating certain values as JSON, it is important that we are more particular about parsing only what the grammars define. To this end, the lexer now consumes all tokens, even unrecognized ones, and the parser explicitly matches to the end of the input. We also installed a BailErrorStrategy that throws on parse failures and does not perform error recovery. As a result, invalid inputs such as "a" = 5 are rejected now.


This change is Reviewable

This forces parse errors in cases that are obviously not correct (e.g., assigning to a string literal). This requires
the lexer to consume all tokens and not allow dropping them. It also require explicitly matching multiple expressions in
a single line, whereas before the parse would simply stop after the first expression and return it.

In order to report unconsume input, we install a `BailErrorStrategy` which - unlike the default - does not attempt error
recovery.
Add a few more tests around parsing adjacent expressions in a single line. Also add negative parse tests that are
required to fail.
Use `BigDecimal#toString()` instead of `BigDecimal#toPlainString()` when converting number literals to strings. When
formatting `NumberLiteral` nodes, use the node text rather than calling `BigDecimal#toString()`. In practice, these
the same.
@cwisniew cwisniew merged commit 3e42478 into RPTools:main Mar 28, 2025
@kwvanderlinde kwvanderlinde moved this from Todo to Merged in MapTool 1.18 Mar 28, 2025
@kwvanderlinde kwvanderlinde deleted the bugfix/82-empty-function-calls branch March 28, 2025 21:40
@kwvanderlinde kwvanderlinde self-assigned this Mar 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Merged

Development

Successfully merging this pull request may close these issues.

2 participants