Skip to content

Commit e0a4e19

Browse files
authored
Add keywords documentation module (#8587)
1 parent b8d2c93 commit e0a4e19

File tree

3 files changed

+366
-0
lines changed

3 files changed

+366
-0
lines changed

corelib/src/keyword_docs.cairo

Lines changed: 362 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,362 @@
1+
/// ### The `as` keyword.
2+
/// Allows to alias an item when importing it with `use` statement.
3+
///
4+
/// ### Example
5+
/// ```cairo
6+
/// pub mod single_const {
7+
/// pub const A: u8 = 1;
8+
/// }
9+
///
10+
/// fn using_aliased_const() {
11+
/// use single_const::A as B;
12+
/// assert_eq!(B, 1);
13+
/// }
14+
/// ```
15+
mod keyword_as {}
16+
17+
/// ### The `const` keyword.
18+
/// Introduces a constant item or const generic parameter. Constants are evaluated at compile time.
19+
///
20+
/// ### Examples
21+
///
22+
/// - Module-level constant:
23+
/// ```cairo
24+
/// const TEN: u32 = 10;
25+
/// fn times_ten(x: u32) -> u32 { x * TEN }
26+
/// ```
27+
///
28+
/// - Const-generic parameter used by functions:
29+
/// ```cairo
30+
/// fn pad_with<const P: felt252>(x: felt252) -> felt252 { x + P }
31+
/// ```
32+
mod keyword_const {}
33+
34+
/// ### The `else` keyword.
35+
/// Specifies an alternative branch for `if` or a fallback with `match` guards.
36+
///
37+
/// ### Example
38+
/// ```cairo
39+
/// fn is_positive(x: i32) -> bool {
40+
/// if x >= 0 { true } else { false }
41+
/// }
42+
/// ```
43+
mod keyword_else {}
44+
45+
/// ### The `enum` keyword.
46+
/// Declares an enumeration type with named variants.
47+
///
48+
/// ### Example
49+
/// ```cairo
50+
/// #[derive(Copy, Drop)]
51+
/// enum ResultU32 {
52+
/// Ok: u32,
53+
/// Err: felt252,
54+
/// }
55+
/// ```
56+
mod keyword_enum {}
57+
58+
/// ### The `extern` keyword.
59+
/// Declares external libfuncs or types provided by the compiler.
60+
///
61+
/// ### Example
62+
/// ```cairo
63+
/// extern fn array_new<T>() -> Array<T> nopanic;
64+
/// ```
65+
mod keyword_extern {}
66+
67+
/// ### The `false` keyword.
68+
/// Boolean enum value representing logical false.
69+
///
70+
/// ### Example
71+
/// ```cairo
72+
/// fn default_bool() -> bool { false }
73+
/// ```
74+
mod keyword_false {}
75+
76+
/// ### The `fn` keyword.
77+
/// Declares a function. Functions may specify implicits, panic behavior, generics, and attrs.
78+
///
79+
/// ### Example
80+
/// ```cairo
81+
/// fn add_u32(a: u32, b: u32) -> u32 { a + b }
82+
/// ```
83+
mod keyword_fn {}
84+
85+
/// ### The `if` keyword.
86+
/// Begins a conditional branch.
87+
///
88+
/// ### Example
89+
/// ```cairo
90+
/// fn sign(x: felt252) -> felt252 {
91+
/// if x == 0 {
92+
/// 0
93+
/// } else {
94+
/// if x > 0 { 1 } else { -1 }
95+
/// }
96+
/// }
97+
/// ```
98+
mod keyword_if {}
99+
100+
/// ### The `while` keyword.
101+
/// Starts a conditional loop that runs while the condition is true.
102+
///
103+
/// ### Example
104+
/// ```cairo
105+
/// fn sum_first(n: u32) -> u32 {
106+
/// let mut i = 0;
107+
/// let mut acc = 0;
108+
/// while i < n { acc = acc + i; i = i + 1; }
109+
/// acc
110+
/// }
111+
/// ```
112+
mod keyword_while {}
113+
114+
/// ### The `for` keyword.
115+
/// Iteration construct over ranges or iterables.
116+
///
117+
/// ### Example
118+
/// ```cairo
119+
/// fn sum_range(n: u32) -> u32 {
120+
/// let mut acc = 0;
121+
/// for i in 0..n { acc = acc + i; }
122+
/// acc
123+
/// }
124+
/// ```
125+
mod keyword_for {}
126+
127+
/// ### The `loop` keyword.
128+
/// Starts an infinite loop, typically exited with `break` or `return`.
129+
///
130+
/// ### Example
131+
/// ```cairo
132+
/// fn first_positive(xs: Array<felt252>) -> felt252 {
133+
/// let mut i = 0;
134+
/// loop {
135+
/// if i >= xs.len() { return 0; }
136+
/// let v = *xs.at(i).unwrap();
137+
/// if v > 0 { return v; }
138+
/// i = i + 1;
139+
/// }
140+
/// }
141+
/// ```
142+
mod keyword_loop {}
143+
144+
/// ### The `impl` keyword.
145+
/// Introduces an implementation block for a trait or type.
146+
///
147+
/// ### Example
148+
/// ```cairo
149+
/// trait Doubler { fn double(self: @u32) -> u32; }
150+
/// impl Doubling of Doubler {
151+
/// fn double(self: @u32) -> u32 { *self + *self }
152+
/// }
153+
/// ```
154+
mod keyword_impl {}
155+
156+
/// ### The `implicits` keyword.
157+
/// Declares implicit parameters required by a function. These are usually passed automatically.
158+
///
159+
/// ### Example
160+
/// ```cairo
161+
/// extern fn check_in_u32_range(value: u32) -> (bool,) implicits(RangeCheck) nopanic;
162+
/// ```
163+
mod keyword_implicits {}
164+
165+
/// ### The `let` keyword.
166+
/// Binds a new variable.
167+
///
168+
/// ### Example
169+
/// ```cairo
170+
/// fn square(x: u32) -> u32 { let y = x * x; y }
171+
/// ```
172+
mod keyword_let {}
173+
174+
/// ### The `macro` keyword.
175+
/// Creates a declarative macro.
176+
///
177+
/// ### Example
178+
/// ```cairo
179+
/// macro add_one {
180+
/// ($x:ident) => { $x + 1 };
181+
/// }
182+
/// ```
183+
mod keyword_macro {}
184+
185+
/// ### The `match` keyword.
186+
/// Pattern matching construct that selects a branch based on a value.
187+
///
188+
/// ### Example
189+
/// ```cairo
190+
/// fn to_bool(x: u32) -> bool {
191+
/// match x { 0 => false, _ => true }
192+
/// }
193+
/// ```
194+
mod keyword_match {}
195+
196+
/// ### The `mod` keyword.
197+
/// Declares a module. Modules group items and control visibility.
198+
///
199+
/// ### Example
200+
/// ```cairo
201+
/// mod math_utils { pub fn add(a: u32, b: u32) -> u32 { a + b } }
202+
/// ```
203+
mod keyword_mod {}
204+
205+
/// ### The `mut` keyword.
206+
/// Marks a binding or reference as mutable.
207+
///
208+
/// ### Example
209+
/// ```cairo
210+
/// fn count(n: u32) -> u32 {
211+
/// let mut i = 0; while i < n { i = i + 1; } i
212+
/// }
213+
/// ```
214+
mod keyword_mut {}
215+
216+
/// ### The `nopanic` keyword.
217+
/// Marks a function as guaranteed not to panic. The compiler enforces no panicking paths.
218+
///
219+
/// ### Example
220+
/// ```cairo
221+
/// extern fn bool_to_felt252(a: bool) -> felt252 nopanic;
222+
/// fn into_felt(b: bool) -> felt252 nopanic { bool_to_felt252(b) }
223+
/// ```
224+
mod keyword_nopanic {}
225+
226+
/// ### The `of` keyword.
227+
/// Used in `impl Type of Trait` headers.
228+
///
229+
/// ### Example
230+
/// ```cairo
231+
/// trait Foo<T> { fn foo(self: @T) -> T; }
232+
/// impl FooU32 of Foo<u32> { fn foo(self: @u32) -> u32 { *self + 1 } }
233+
/// ```
234+
mod keyword_of {}
235+
236+
237+
/// ### The `ref` keyword.
238+
/// Allows functions to mutate variables by passing them as a reference.
239+
/// The value is implicitly copied and passed back to the user.
240+
///
241+
/// ### Example
242+
/// ```cairo
243+
/// fn push_four(ref ary: Array<felt252>) {
244+
/// ary.append(4)
245+
/// }
246+
///
247+
/// fn main() {
248+
/// let mut ary = array![1, 2, 3];
249+
/// push_four(ref ary);
250+
/// assert!(ary == array![1, 2, 3, 4])
251+
/// }
252+
/// ```
253+
mod keyword_ref {}
254+
255+
/// ### The `continue` keyword.
256+
/// Skips to the next iteration of a loop.
257+
///
258+
/// ### Example
259+
/// ```cairo
260+
/// fn skip_even(n: u32) -> u32 {
261+
/// let mut i = 0; let mut cnt = 0;
262+
/// while i < n {
263+
/// i = i + 1;
264+
/// if i % 2 == 0 { continue; }
265+
/// cnt = cnt + 1;
266+
/// }
267+
/// cnt
268+
/// }
269+
/// ```
270+
mod keyword_continue {}
271+
272+
/// ### The `return` keyword.
273+
/// Exits a function, optionally returning a value.
274+
///
275+
/// ### Example
276+
/// ```cairo
277+
/// fn clamp01(x: i32) -> u32 {
278+
/// if x < 0 { return 0; }
279+
/// if x > 1 { return 1; }
280+
/// x
281+
/// }
282+
/// ```
283+
mod keyword_return {}
284+
285+
/// ### The `break` keyword.
286+
/// Exits a loop early. Can only be used inside a `loop` expression.
287+
///
288+
/// ### Example
289+
/// ```cairo
290+
/// fn first_gt(xs: Array<u32>, cmp: u32) -> Option<u32> {
291+
/// let mut i = 0;
292+
/// let v = loop {
293+
/// if i > xs.len() {
294+
/// return None;
295+
/// }
296+
///
297+
/// let v = *xs.at(i);
298+
/// if v > cmp { break Some(v); }
299+
/// i = i + 1;
300+
/// };
301+
/// v
302+
/// }
303+
/// ```
304+
mod keyword_break {}
305+
306+
/// ### The `struct` keyword.
307+
/// Declares a structure type with named members.
308+
///
309+
/// ### Example
310+
/// ```cairo
311+
/// struct Point { x: felt252, y: felt252 }
312+
/// fn origin() -> Point { Point { x: 0, y: 0 } }
313+
/// ```
314+
mod keyword_struct {}
315+
316+
/// ### The `trait` keyword.
317+
/// Declares a trait containing associated items to be implemented.
318+
///
319+
/// ### Example
320+
/// ```cairo
321+
/// trait Volume<T> { fn volume(self: @T) -> usize; }
322+
/// impl ArrayVolume<T> of Volume<Array<T>> { fn volume(self: @Array<T>) -> usize { self.len() } }
323+
/// ```
324+
mod keyword_trait {}
325+
326+
/// ### The `true` keyword.
327+
/// Boolean enum value representing logical true.
328+
///
329+
/// ### Example
330+
/// ```cairo
331+
/// fn yes() -> bool { true }
332+
/// ```
333+
mod keyword_true {}
334+
335+
/// ### The `type` keyword.
336+
/// Declares a type alias.
337+
///
338+
/// ### Example
339+
/// ```cairo
340+
/// type usize = u32;
341+
/// ```
342+
mod keyword_type {}
343+
344+
/// ### The `use` keyword.
345+
/// Imports items into the current scope.
346+
///
347+
/// ### Examples
348+
/// ```cairo
349+
/// use core::panic_with_felt252;
350+
/// use super::traits::PartialEq;
351+
/// ```
352+
mod keyword_use {}
353+
354+
/// ### The `pub` keyword.
355+
/// Makes an item public within its parent module (or crate).
356+
///
357+
/// ### Example
358+
/// ```cairo
359+
/// pub fn get_zero() -> u32 { 0 }
360+
/// pub(crate) struct Pair { pub a: u32, b: u32 }
361+
/// ```
362+
mod keyword_pub {}

corelib/src/lib.cairo

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,8 @@ mod fixed_size_array;
432432

433433
pub mod iter;
434434

435+
mod keyword_docs;
436+
435437
pub mod metaprogramming;
436438

437439
#[allow(unused_imports)]

crates/cairo-lang-semantic/src/corelib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,7 @@ pub struct CoreInfo<'db> {
998998
pub downcast_fn: GenericFunctionId<'db>,
999999
pub tuple_submodule: ModuleId<'db>,
10001000
pub fixed_size_array_submodule: ModuleId<'db>,
1001+
pub keyword_docs_submodule: ModuleId<'db>,
10011002
}
10021003
impl<'db> CoreInfo<'db> {
10031004
fn new(db: &'db dyn Database) -> Self {
@@ -1138,6 +1139,7 @@ impl<'db> CoreInfo<'db> {
11381139
downcast_fn: bounded_int.generic_function_id("downcast"),
11391140
tuple_submodule,
11401141
fixed_size_array_submodule,
1142+
keyword_docs_submodule: core.submodule("keyword_docs").id,
11411143
}
11421144
}
11431145
}

0 commit comments

Comments
 (0)