unify tuple expressions (#13793)

* unify tuple expressions

* fix test

* fix test

* apply feedback

* Handle empty tuples

* Fix rendering named unary tuple

* Protect static NimNode against stripping

* Slightly less hacky

* Revert "Slightly less hacky"

This reverts commit 170c5aec0addc029f637afbc948700ca006b7942.

* Slightly less hacky

* Cleanup

* Fix test

* Fix another test

* Add condsym

* Rebase fallout

* changelog: Move from compiler changes to language changes

* Add stricter tests

* Add empty tuple example to doc/astspec

* Fix test

Co-authored-by: Clyybber <darkmine956@gmail.com>
This commit is contained in:
Arne Döring 2021-03-30 02:06:51 +02:00 • committed by GitHub
commit 159c06e045
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 136 additions and 32 deletions

View file

@ -329,19 +329,56 @@ AST:
Parentheses
-----------
Parentheses for affecting operator precedence or tuple construction
are built with the ``nnkPar`` node.
Parentheses for affecting operator precedence use the ``nnkPar`` node.
Concrete syntax:
.. code-block:: nim
(1, 2, (3))
(a + b) * c
AST:
.. code-block:: nim
nnkPar(nnkIntLit(1), nnkIntLit(2), nnkPar(nnkIntLit(3)))
nnkInfix(nnkIdent("*"),
nnkPar(
nnkInfix(nnkIdent("+"), nnkIdent("a"), nnkIdent("b"))),
nnkIdent("c"))
Tuple Constructors
------------------
Nodes for tuple construction are built with the ``nnkTupleConstr`` node.
Concrete syntax:
.. code-block:: nim
(1, 2, 3)
(a: 1, b: 2, c: 3)
()
AST:
.. code-block:: nim
nnkTupleConstr(nnkIntLit(1), nnkIntLit(2), nnkIntLit(3))
nnkTupleConstr(
nnkExprColonExpr(nnkIdent("a"), nnkIntLit(1)),
nnkExprColonExpr(nnkIdent("b"), nnkIntLit(2)),
nnkExprColonExpr(nnkIdent("c"), nnkIntLit(3)))
Since the one tuple would be syntactically identical to parentheses
with an expression in them, the parser expects a trailing comma for
them. For tuple constructors with field names, this is not necessary.
.. code-block:: nim
(1,)
(a: 1)
AST:
.. code-block:: nim
nnkTupleConstr(nnkIntLit(1))
nnkTupleConstr(
nnkExprColonExpr(nnkIdent("a"), nnkIntLit(1)))
Curly braces
------------