Skip to content

Commit 36de66c

Browse files
committed
Update grammar and docs for optional trailing commas in lists
1 parent 7041994 commit 36de66c

18 files changed

+78
-30
lines changed

docs/cpp2/common.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,56 @@ The usual `#!cpp // line comments` and `#!cpp /* stream comments */` are support
5555
```
5656

5757

58+
## <a id="lists"></a> Lists and commas
59+
60+
All lists use `,` commas between list items, and may be enclosed by
61+
62+
- `(` `)` parentheses, for most lists
63+
64+
- `[` `]` brackets, for calling the subscript operator
65+
66+
- `<` `>` angle brackets, for template parameter/argument lists
67+
68+
For example:
69+
70+
``` cpp title="Lists, using optional trailing commas just because we can" hl_lines="1 4 6 7"
71+
print: <T,U> (t: T, u: U) = std::cout << t << u << "\n";
72+
73+
main: () = {
74+
array: std::array = ('A', 'B', 'C');
75+
76+
for (0, 1, 2) do (e) {
77+
print( e, array[e] );
78+
}
79+
// Prints:
80+
// 0A
81+
// 1B
82+
// 2C
83+
}
84+
```
85+
86+
87+
An extra comma at the end of the list, before the closing `)` or `>`, is always allowed but ignored if present (for details, see [Design note: Commas](https://github.com/hsutter/cppfront/wiki/Design-note%3A-Commas)).
88+
89+
For example:
90+
91+
``` cpp title="Lists, using optional trailing commas just because we can" hl_lines="1 4 6 7"
92+
print: <T,U,> (t: T, u: U,) = std::cout << t << u << "\n";
93+
94+
main: () = {
95+
array: std::array = ('A', 'B', 'C',);
96+
97+
for (0, 1, 2,) do (e) {
98+
print( e, array[e,], );
99+
}
100+
// Prints:
101+
// 0A
102+
// 1B
103+
// 2C
104+
}
105+
```
106+
107+
58108
## <a id="keywords"></a> Reserved keywords
59109

60110
Cpp2 has very few globally reserved keywords; nearly all keywords are contextual, where they have their special meaning when they appear in a particular place in the grammar. For example:
@@ -110,7 +160,7 @@ p: const * * const i32;
110160

111161
Cpp2 supports the same `#!cpp 'c'`haracter, `#!cpp "string"`, binary, integer, and floating point literals as Cpp1, including most Unicode encoding prefixes and raw string literals.
112162

113-
Cpp2 supports using Cpp1 user-defined literals for compatibility, to support seamlessly using existing libraries. However, because Cpp2 has unified function call syntax (UFCS), the preferred way to author the equivalent in Cpp2 is to just write a function or type name as a `.` call suffix. For example:
163+
Cpp2 supports using Cpp1 user-defined literals for compatibility, to support seamlessly using existing libraries. However, because Cpp2 has [unified function call syntax (UFCS)](expressions.md#ufcs), the preferred way to author the equivalent in Cpp2 is to just write a function or type name as a `.` call suffix. For example:
114164

115165
- You can create a `u8` value by writing either `u8(123)` or **`123.u8()`**. [^u8using]
116166

docs/cpp2/declarations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ f:(x: int = init) = statement; // same, { } is implicit
8585

8686
### <a id="template-parameters"></a> Template parameters
8787

88-
A template parameter list is enclosed by `<` `>` angle brackets, and the parameters separated by commas. Each parameter is declared using the [same syntax as any type or object](declarations.md). If a parameter's **`:`** ***kind*** is not specified, the default is `: type`.
88+
A template parameter list is a [list](common.md#lists) enclosed by `<` `>` angle brackets, and the parameters separated by commas. Each parameter is declared using the [same syntax as any type or object](declarations.md). If a parameter's **`:`** ***kind*** is not specified, the default is `: type`.
8989

9090
For example:
9191

docs/cpp2/expressions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
## <a id="ufcs"></a> Calling functions: `f(x)` syntax, and `x.f()` UFCS syntax
55

6+
A function argument list is a [list](common.md#lists) of arguments enclosed by `(` `)` parentheses.
7+
68
A function call like `f(x)` is a normal function call that will call non-member functions only, as usual in C++.
79

810
A function call like `x.f()` is a unified function call syntax (aka UFCS) call. It will call a member function if one is available, and otherwise will call `f(x)`. Having UFCS is important for generic code that may want to call a member or a non-member function, whichever is available. It's also important to enable fluid programming styles and natural IDE autocompletion support.

docs/cpp2/functions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func: ( /* no parameters */ ) = { /* empty body */ }
1414
1515
## <a id="parameters"></a> Parameters
1616
17-
The parameter list is enclosed by `(` `)` parentheses and the parameters are separated by commas. Each parameter is declared using the [same unified syntax](declarations.md) as used for all declarations. For example:
17+
The parameter list is a [list](common.md#lists) enclosed by `(` `)` parentheses. Each parameter is declared using the [same unified syntax](declarations.md) as used for all declarations. For example:
1818
1919
``` cpp title="Declaring parameters" hl_lines="2-4"
2020
func: (

include/cpp2util.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,8 +1782,6 @@ constexpr auto unsafe_narrow( X&& x ) noexcept -> decltype(auto)
17821782
}
17831783

17841784

1785-
namespace impl {
1786-
17871785
//-----------------------------------------------------------------------
17881786
//
17891787
// args: see main() arguments as a container of string_views
@@ -1845,8 +1843,6 @@ inline auto make_args(int argc, char** argv) -> args_t
18451843
return args_t{argc, argv};
18461844
}
18471845

1848-
} // impl
1849-
18501846

18511847
//-----------------------------------------------------------------------
18521848
//

regression-tests/pure2-trailing-commas.cpp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ doubler: (a: int,) -> (i : int,) = {
88

99
vals: @struct type = { i: int; }
1010

11-
main: () -> int = {
11+
main: () = {
1212
(copy a := 42,) while false { a++; }
1313
_ = g(1, 2,);
1414

regression-tests/test-results/mixed-fixed-type-aliases.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ auto test(auto const& x) -> void{
4848

4949
#line 19 "mixed-fixed-type-aliases.cpp2"
5050
[[nodiscard]] auto main(int const argc_, char** argv_) -> int{
51-
auto const args = cpp2::impl::make_args(argc_, argv_);
51+
auto const args = cpp2::make_args(argc_, argv_);
5252
#line 20 "mixed-fixed-type-aliases.cpp2"
5353
my::u16 y {42};
5454
test(std::move(y));

regression-tests/test-results/pure2-bugfix-for-discard-precedence.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ auto main(int const argc_, char** argv_) -> int;
4545

4646
#line 7 "pure2-bugfix-for-discard-precedence.cpp2"
4747
auto main(int const argc_, char** argv_) -> int{
48-
auto const args = cpp2::impl::make_args(argc_, argv_);
48+
auto const args = cpp2::make_args(argc_, argv_);
4949
#line 8 "pure2-bugfix-for-discard-precedence.cpp2"
5050
quantity x {1729};
5151
static_cast<void>(x + x);// Not `(void) x + x`; would attempt to add a `void` to `x`.

regression-tests/test-results/pure2-for-loop-range-with-lambda.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ auto main(int const argc_, char** argv_) -> int;
2424

2525
#line 3 "pure2-for-loop-range-with-lambda.cpp2"
2626
auto main(int const argc_, char** argv_) -> int{
27-
auto const args = cpp2::impl::make_args(argc_, argv_);
27+
auto const args = cpp2::make_args(argc_, argv_);
2828
#line 4 "pure2-for-loop-range-with-lambda.cpp2"
2929
std::array const ints {1, 2, 3, 4, 5};
3030
// OK

regression-tests/test-results/pure2-initialization-safety-with-else-if.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ auto main(int const argc_, char** argv_) -> int;
1818

1919
#line 1 "pure2-initialization-safety-with-else-if.cpp2"
2020
auto main(int const argc_, char** argv_) -> int{
21-
auto const args = cpp2::impl::make_args(argc_, argv_);
21+
auto const args = cpp2::make_args(argc_, argv_);
2222
#line 2 "pure2-initialization-safety-with-else-if.cpp2"
2323
cpp2::impl::deferred_init<int*> p;
2424

0 commit comments

Comments
 (0)