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

Named arguments in commands + many grammar fixes #20994

Merged
merged 20 commits into from
Dec 6, 2022

Conversation

metagn
Copy link
Collaborator

@metagn metagn commented Dec 2, 2022

closes nim-lang/RFCs#442, closes #15050, closes #8846 and maybe some other issues, cleans up after #19181

This is a fully rebased version of #19383 I am opening again to bump it up because apparently the time is ticking on 2.0 and it doesn't make sense for it to not have this.

foo a = b now means foo(a = b) instead of foo(a) = b, for consistency with foo a, b = c. To do this, the primary expression in the grammar, which had many issues and did not reflect reality, had to be slightly overhauled.

  • Type expressions (including nominal type declarations!) are not directly part of primary anymore. Instead they have been broken up into typeDefValue (right hand sides of type sections) and typeDesc (everywhere else a type is expected).
  • typeDesc itself is broken up into 2: rawTypeDesc (expressions that start with type keywords) and typeDescExpr (general expressions that may evaluate to types, as well as binary not). We use rawTypeDesc as the ordered choice in primary.
  • Sigil operators are now in the grammar and only apply to identOrLiterals for simplicity. (This seems to break nothing as it doesn't really make sense to apply to anything else)
  • Command syntax is not part of primarySuffix anymore. This never made sense and had many issues. For example, in reality primary only parses 1 command argument, but the grammar denotes an arbitrary number of arguments.
  • Full command syntax (i.e. with multiple arguments and =) is now in exprStmt (which it already was, but not in the grammar). We indicate with a parser mode that we want to get away with parsing only a simplePrimary (which is also reflected in the grammar), and if we do we parse exprEqExpr ^+ comma for command syntax. Otherwise we just use the expression that we got.
  • As a minor unrelated grammar fix, we document for a, (b, c) in d syntax with 'for' ((varTuple / identWithPragma) ^+ comma) instead of 'for' (identWithPragma ^+ comma).

@Varriount Varriount requested a review from Araq December 2, 2022 20:16
@Varriount Varriount added the Requires Araq To Merge PR should only be merged by Araq label Dec 2, 2022
doc/grammar.txt Outdated Show resolved Hide resolved
@Araq Araq added merge_when_passes_CI mergeable once green and removed Requires Araq To Merge PR should only be merged by Araq labels Dec 6, 2022
@Araq Araq merged commit 4ca2dcb into nim-lang:devel Dec 6, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Dec 6, 2022

Thanks for your hard work on this PR!
The lines below are statistics of the Nim compiler built from 4ca2dcb

Hint: mm: orc; opt: speed; options: -d:release
165549 lines; 8.408s; 612.68MiB peakmem

survivorm pushed a commit to survivorm/Nim that referenced this pull request Feb 28, 2023
* Breaking parser changes, implement nim-lang/RFCs#442

Types are separated from expressions and better reflected in the grammar.

* add test

* more accurate grammar

* fix keyword typedescs

* accept expressions in proc argument lists

* CI "fixes"

* fixes

* allow full ref expressions again, adapt old tests

* cleanup, fix some tests

* improve grammar, try and revert semtypes change

* restrict sigil binding to identOrLiteral

* fix, should have caught this immediately

* add changelog entry, fix double not nil bug

* correct grammar

* change section

* fix

* real fix hopefully

* fix test

* support LL(1) for tuples

* make grammar.txt too
@@ -1199,8 +1200,12 @@ proc parseTypeDescKAux(p: var Parser, kind: TNodeKind,
getTok(p)
if p.tok.indent != -1 and p.tok.indent <= p.currInd: return
optInd(p, result)
let isTypedef = mode == pmTypeDef and p.tok.tokType in {tkObject, tkTuple}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that

type
  Named = tuple or int

doesn't work anymore. Is it expected?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, tuple in type sections can have a syntax similar to objects, so it becomes tuple[] which is misleading

Hence this bigger example doesn't compile in 1.6:

type Named = tuple or int

proc foo(x: Named) = echo x

foo((1, 2))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks for your explanation.

capocasa pushed a commit to capocasa/Nim that referenced this pull request Mar 31, 2023
* Breaking parser changes, implement nim-lang/RFCs#442

Types are separated from expressions and better reflected in the grammar.

* add test

* more accurate grammar

* fix keyword typedescs

* accept expressions in proc argument lists

* CI "fixes"

* fixes

* allow full ref expressions again, adapt old tests

* cleanup, fix some tests

* improve grammar, try and revert semtypes change

* restrict sigil binding to identOrLiteral

* fix, should have caught this immediately

* add changelog entry, fix double not nil bug

* correct grammar

* change section

* fix

* real fix hopefully

* fix test

* support LL(1) for tuples

* make grammar.txt too
bung87 pushed a commit to bung87/Nim that referenced this pull request Jul 29, 2023
* Breaking parser changes, implement nim-lang/RFCs#442

Types are separated from expressions and better reflected in the grammar.

* add test

* more accurate grammar

* fix keyword typedescs

* accept expressions in proc argument lists

* CI "fixes"

* fixes

* allow full ref expressions again, adapt old tests

* cleanup, fix some tests

* improve grammar, try and revert semtypes change

* restrict sigil binding to identOrLiteral

* fix, should have caught this immediately

* add changelog entry, fix double not nil bug

* correct grammar

* change section

* fix

* real fix hopefully

* fix test

* support LL(1) for tuples

* make grammar.txt too
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge_when_passes_CI mergeable once green
Projects
None yet
4 participants