Skip to content

Commit

Permalink
Document numeric type literals in lexical conventions. (#4842)
Browse files Browse the repository at this point in the history
Also improve the precision of some other nearby documentation.
  • Loading branch information
zygoloid authored Jan 24, 2025
1 parent ad49647 commit bc952b1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
4 changes: 2 additions & 2 deletions docs/design/expressions/literals.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

- [Numeric literals](#numeric-literals)
- [Numeric literal syntax](#numeric-literal-syntax)
- [Defined Types](#defined-types)
- [Defined types](#defined-types)
- [Implicit conversions](#implicit-conversions)
- [Examples](#examples)
- [Alternatives Considered](#alternatives-considered)
Expand Down Expand Up @@ -70,7 +70,7 @@ Numeric literal syntax is covered in the
doc. Both Integer and Real-Number syntax is defined, with decimal, hexadecimal
and binary integer literals, and decimal and hexadecimal real number literals.

### Defined Types
### Defined types

The following types are defined in the Carbon prelude:

Expand Down
45 changes: 38 additions & 7 deletions docs/design/lexical_conventions/words.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

- [Overview](#overview)
- [Keywords](#keywords)
- [Type literals](#type-literals)
- [Identifiers](#identifiers)
- [Raw identifiers](#raw-identifiers)
- [Alternatives considered](#alternatives-considered)
- [References](#references)
Expand All @@ -21,7 +23,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
## Overview

A _word_ is a lexical element formed from a sequence of letters or letter-like
characters, such as `fn` or `Foo` or `Int`.
characters, such as `fn` or `Foo` or `Int`, optionally preceded by `r#`.

The exact lexical form of words has not yet been settled. However, Carbon will
follow lexical conventions for identifiers based on
Expand Down Expand Up @@ -98,20 +100,47 @@ The following words are interpreted as keywords:
- `where`
- `while`

### Type literals

A word starting with `i`, `u`, or `f`, followed by a decimal integer, is a
[_numeric type literal_](/docs/design/expressions/literals.md#numeric-type-literals).

### Identifiers

A word is interpreted as an _identifier_ if it is neither a keyword nor a type
literal.

### Raw identifiers

A raw identifier looks like `r#<identifier>`. This can be used for identifiers
which have the same spelling as keywords; for example, `r#impl`. It can help
when using C++ code with identifiers that are keywords in Carbon.
A _raw identifier_ is a word starting with `r#`. A raw identifier is equivalent
to the word following the `r#` prefix, except that it is always interpreted as
an identifier, even if it would otherwise be a keyword or type literal.

Raw identifiers can be used to specify identifiers which have the same spelling
as keywords; for example, `r#impl`. This can be useful when interoperating with
C++ code that uses identifiers that are keywords in Carbon, and when migrating
between versions of Carbon.

The identifier doesn't need to be a keyword, in order to support forwards
compatibility when a keyword is planned to be added. When `<identifier>` is not
a keyword, it will refer to the same entity as `r#<identifier>`.
The word doesn't need to be a keyword, in order to support forwards
compatibility when a keyword is planned to be added. If `word` is an identifier,
then `word` and `r#word` have the same meaning.

## Alternatives considered

Overview:

- [Character encoding: We could restrict words to ASCII.](/proposals/p0142.md#character-encoding-1)
- [Normalization form alternatives considered](/proposals/p0142.md#normalization-forms)

Type literals:

- [Use C++ type keywords with LP64 convention](/proposals/p2015.md#c-lp64-convention)
- [Use full type name with length suffix](/proposals/p2015.md#type-name-with-length-suffix)
- [Use uppercase for type names](/proposals/p2015.md#uppercase-suffixes)
- [Support additional bit widths](/proposals/p2015.md#additional-bit-sizes)

Raw identifiers:

- [Other raw identifier syntaxes](/proposals/p3797.md#other-raw-identifier-syntaxes)
- [Restrict raw identifier syntax to current and future keywords](/proposals/p3797.md#restrict-raw-identifier-syntax-to-current-and-future-keywords)
- [Don't require syntax for references to raw identifiers](/proposals/p3797.md#dont-require-syntax-for-references-to-raw-identifiers)
Expand All @@ -121,5 +150,7 @@ a keyword, it will refer to the same entity as `r#<identifier>`.

- Proposal
[#142: Unicode source files](https://github.com/carbon-language/carbon-lang/pull/142)
- Proposal
[#2015: Numeric type literal syntax](https://github.com/carbon-language/carbon-lang/pull/2015)
- Proposal
[#3797: Raw identifier syntax](https://github.com/carbon-language/carbon-lang/pull/3797)

0 comments on commit bc952b1

Please sign in to comment.