mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-15 06:48:31 +00:00
This is a re-do of #7888 Outside a call, the grammar scoped a name as a type or a variable by its capitalization. A theme could not distinguish a type from a value with the same spelling, or a parameter, a member, or an implemented interface from any other name. Pointer member access `->` was scoped as a result arrow, which scoped `Lookup` in `regions->Lookup(x)` as a type. `entity.name.type` now applies only in a type position (after `:`, `->`, `as`, `adapt`, or `require`, or in an `impl`) and to the name a nominal declaration declares. A qualifier, in a declared name like `fn N.Base(T: type).F()` or in a type, is `entity.name.scope-resolution` nested in `entity.name.type`. As in the parser, a name is a qualifier when its parameter lists are followed by `.`. Parameters, `let` bindings, choice alternatives, members, implemented interfaces, primitive types, `import`, and the strings naming a builtin or a library have their own scopes, and brackets use the C++ grammar's scopes. A `->` written directly after its operand is member access; no result arrow in the repository is written that way. The sample palette renders each scope that depends on context differently, and `tmlanguage.py` now supports nested captures. In 17 of the 19 themes measured, the VS Code built-ins plus One Dark Pro, Night Owl, Tokyo Night, and Nord, 13% to 27% of the characters in hand-written Carbon change color or style. A theme loses a color where `.` is now punctuation, where word operators like `as` are no longer control-flow keywords, or where a name outside a type position is no longer a type. Assisted-by: Claude Code
68 lines
1.2 KiB
Plaintext
68 lines
1.2 KiB
Plaintext
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
fn F;
|
|
fn F1();
|
|
fn Func(a: i32, b: A);
|
|
fn Generic[T: type, U: I](a: T, b: U) -> U;
|
|
fn Lambda(a: i32) -> i32 => return a + 5;
|
|
fn
|
|
|
|
MultiLine[
|
|
A: type,
|
|
B: C1,
|
|
C: C2
|
|
](a: A(B, C),
|
|
b: B,
|
|
c: i32);
|
|
fn Invalid#+?Name();
|
|
x.(I.F)();
|
|
|
|
a: A;
|
|
a: A = (b as A);
|
|
a: i32;
|
|
a: array(A, 5);
|
|
generic a: type;
|
|
generic a: T = b;
|
|
a: A in some_list;
|
|
var a: T;
|
|
let b: T;
|
|
let unused c: T = d;
|
|
binding: T;
|
|
generic binding: T;
|
|
var
|
|
multiline: T;
|
|
|
|
fn F(ref a: A, const ref b: B);
|
|
fn F() -> T;
|
|
fn F() -> T.U { return val; }
|
|
fn F() -> i32 => return val;
|
|
|
|
// --- lambdas and positional parameters
|
|
|
|
var explicit: auto = fn (x: i32) -> i32 { return x + 1; };
|
|
var expression: auto = fn (x: i32) => x + 1;
|
|
var positional: auto = fn { Print($0, $1, $42); };
|
|
|
|
// --- raw identifiers
|
|
|
|
r#class
|
|
r#if
|
|
r#i32
|
|
var r#var: i32 = r#class;
|
|
|
|
// --- member access through a pointer
|
|
|
|
p->field;
|
|
p->Method(a);
|
|
p->0;
|
|
(*p).field;
|
|
t.0.1;
|
|
q.value()->size();
|
|
|
|
// --- builtin function definitions
|
|
|
|
fn Add(a: i32, b: i32) -> i32 = "int.sadd";
|
|
fn Bits(n: IntLiteral) -> type = "int.make_type_signed";
|