Skip to content
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

Allow reserved words as record names w/out quotes #7873

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

choptastic
Copy link

@choptastic choptastic commented Nov 16, 2023

The motivation for this PR comes from the Nitrogen Web Frameworks's heavy use of records, and a clash between HTML <div> element and Erlang's div operator.

In Nitrogen, HTML elements are represented by Erlang records. For example: the HTML <span> element is #span{} in Nitrogen.

Logically, the heavily-used HTML <div> element would be represented by #div{} in Nitrogen, however, that specific syntax is illegal due to div's reserved word status, and must instead be represented with #'div'. This syntax, however, is awkward, and has led to a workaround that works, but is itself awkward (using the term #panel{} instead of #div{} - but this in itself leads to a semantic clash, as some frontend HTML frameworks have their own 'panel' elements that might ideally be abstracted into a #panel{} element - but can't because #panel{} already exists).

But, As far as I understand, there is no potential syntax clash in allowing the syntax #div to be acceptable, and have the parser recognize that the div (or any reserved word) in that context should be treated as an atom.

So this change tweaks the grammar to recognize those conditions.

Further, this change does not change the way the records are defined (so the definition must still be defined with the atom properly wrapped in quotes (e.g. -record('div', {a,b}).).

This PR also adds the appropriate tests in erl_expand_records_SUITE, which I wasn't sure if that was appropriate place, but it seemed the most relevant.

If any revisions should be made, I'm certainly open to it. Or if this requires a much larger discussion than just a PR (I certainly don't think this is big enough to justify an EEP, but what do I know). In any case, this would help eliminate an odd quirk of Nitrogen since the very beginning. Thanks!

@CLAassistant
Copy link

CLAassistant commented Nov 16, 2023

CLA assistant check
All committers have signed the CLA.

@IngelaAndin IngelaAndin added the team:VM Assigned to OTP team VM label Nov 20, 2023
@jhogberg jhogberg added team:LG Assigned to OTP language group and removed team:VM Assigned to OTP team VM labels Nov 20, 2023
@essen
Copy link
Contributor

essen commented Feb 26, 2024

I have run into this in the past. I believe I wanted a #begin{} and an #end{} record.

Copy link
Contributor

github-actions bot commented Sep 10, 2024

CT Test Results

    3 files    106 suites   58m 59s ⏱️
2 271 tests 2 222 ✅ 48 💤 1 ❌
2 655 runs  2 603 ✅ 51 💤 1 ❌

For more details on these failures, see this check.

Results for commit 539299e.

♻️ This comment has been updated with latest results.

To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass.

See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally.

Artifacts

// Erlang/OTP Github Action Bot

@RaimoNiskanen
Copy link
Contributor

RaimoNiskanen commented Sep 10, 2024

I (and at least one more, we will have a meeting shortly) like this idea, and cannot really see any problems with extending the syntax like you suggest.

I added 3 suggested commits:

  1. Renamed a bit, and added the reserved words cond and let to the set. They are not used in the language but were unusable for record names since they cause syntax error when used.
  2. @bjorng had noticed that also allowing variable names would be comfortable e.g for ASN.1, so there are certainly other use cases. This allows #Abc{}
  3. It was possible to add a syntax for record declaration that also allow these new names, and that are more record like. Commit 3 allows:
-record #div{a :: integer(), b :: integer()}.
div() -> #div{a = 1, b = 2}.

More test cases are needed, documentation, maybe use the feature feature to make it possible to test for this feature.
Perhaps this feature warrants an EEP, or perhaps not...

@RaimoNiskanen
Copy link
Contributor

Fixed some lint in the latest commit message

choptastic and others added 3 commits September 11, 2024 13:33
This motivation for this comes from the Nitrogen Web Frameworks's heavy
use of records, and a clash between HTML <div> element and the Erlang
div operator.

In Nitrogen, HTML elements are represented by Erlang records. For
example: the HTML `<span>` element is `#span{}` in Nitrogen.

Logically, the heavily-used HTML `<div>` element would be represented
by `#div{}` in Nitrogen, however, that specific syntax is illegal due to
`div`'s reserved word status, and must instead be represented with
`#'div'`. This syntax, however, is awkward, and has led to a workaround
that *works*, but is itself awkward (using the term `#panel{}` instead
of `#div{}` - but this in itself leads to a semantic clash, as some
frontend HTML frameworks have their own 'panel' elements that might
ideally be abstracted into a `#panel{}` element.

But, As far as I understand, there is no potential syntax clash in
allowing the syntax `#div` ito be acceptable, and have the parser
recognize that the `div` (or any reserved word) in that `context` can
only be an atom, and would never be an operator.

So this change tweaks the grammar to recognize the circumstances of:
`#reserved_word{}`.

Further, this change does not change the way the records are defined (so
the definition must still be defined with the atom properly wrapped in
quotes (e.g. `-record('div', {a,b}).`).

This PR also adds the appropriate tests in `erl_expand_records_SUITE`,
which I wasn't sure if that was appropriate place, but it seemed the
most relevant.
@RaimoNiskanen
Copy link
Contributor

I rebased onto OTP-27.0 to get into Markdown documentation land

@choptastic
Copy link
Author

@RaimoNiskanen: I just wanted to pop in here and say thanks so much for reviewing this, pushing your changes, and (hopefully) accepting it. The new intuitive -record syntax is an excellent addition to this and I appreciate you running with it.

If there's anything else you need from me, please let me know.

Thanks!

@RaimoNiskanen
Copy link
Contributor

@choptastic: We had a language group meeting, and decided to accept this proposal, with my suggested record definition syntax extension, but need an EEP first since it is a language syntax change that also affect tools like Emacs, the language server platform, etc...

So, since you asked ;-) can you write an EEP, please? Or at least begin?

@choptastic
Copy link
Author

Sure, I think I can manage that!

@choptastic
Copy link
Author

Better late than never! I've posted the EEP to the EEP repo.

Thanks! Please let me know if there are any issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team:LG Assigned to OTP language group
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants