stan-dev / stanc3

The Stan transpiler (from Stan to C++ and beyond).
BSD 3-Clause "New" or "Revised" License
138 stars 44 forks source link

Stop reserved words in parser to prevent worse parse errors later #1343

Closed WardBrian closed 10 months ago

WardBrian commented 11 months ago

See: https://discourse.mc-stan.org/t/ill-formed-phrase-should-be-followed-by-a-statement-variable-declaration-or-expression/32400

This reverts some portions of #771, but rather than using the UNREACHABLE token and updating the messages file, we simply raise the error ourselves directly.

This unfortunately does make some errors worse, since continuing parsing let us do things like an informative error when a user writes real foo, int bar; to try to declare multiple variables of different types on one line. The slightly worse error in that case seems worth it compared to very confusing errors like in the linked thread.

Submission Checklist

Release notes

Improved errors when using a reserved word as a variable name.

Copyright and Licensing

By submitting this pull request, the copyright holder is agreeing to license the submitted work under the BSD 3-clause license (https://opensource.org/licenses/BSD-3-Clause)

WardBrian commented 11 months ago

Note: as part of preparing this it made me realize that the terms generated, quantities, and transformed are all reserved when they don't in fact need to be. It's perfectly unambiguous to use them as variable names, so we could allow them if we wanted