@shd101wyy/yo 0.1.12 → 0.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/README.md +10 -1
  2. package/out/cjs/index.cjs +578 -572
  3. package/out/cjs/yo-cli.cjs +1447 -529
  4. package/out/esm/index.mjs +434 -428
  5. package/out/types/src/build-runner.d.ts +1 -1
  6. package/out/types/src/doc/builder.d.ts +21 -0
  7. package/out/types/src/doc/builder.test.d.ts +1 -0
  8. package/out/types/src/doc/extractor.d.ts +27 -0
  9. package/out/types/src/doc/extractor.test.d.ts +1 -0
  10. package/out/types/src/doc/model.d.ts +87 -0
  11. package/out/types/src/doc/render-html.d.ts +27 -0
  12. package/out/types/src/doc/render-html.test.d.ts +1 -0
  13. package/out/types/src/doc/render-json.d.ts +7 -0
  14. package/out/types/src/doc/render-json.test.d.ts +1 -0
  15. package/out/types/src/doc/render-markdown.d.ts +13 -0
  16. package/out/types/src/doc/render-markdown.test.d.ts +1 -0
  17. package/out/types/src/doc/sections.d.ts +7 -0
  18. package/out/types/src/doc/sections.test.d.ts +1 -0
  19. package/out/types/src/doc-command.d.ts +10 -0
  20. package/out/types/src/env.d.ts +1 -0
  21. package/out/types/src/evaluator/builtins/build.d.ts +17 -0
  22. package/out/types/src/evaluator/context.d.ts +1 -0
  23. package/out/types/src/expr.d.ts +2 -1
  24. package/out/types/src/token.d.ts +4 -0
  25. package/out/types/tsconfig.tsbuildinfo +1 -1
  26. package/package.json +2 -1
  27. package/scripts/build-site.ts +498 -0
  28. package/std/alg/hash.yo +12 -24
  29. package/std/allocator.yo +21 -29
  30. package/std/async.yo +4 -2
  31. package/std/build.yo +192 -42
  32. package/std/cli/arg_parser.yo +37 -1
  33. package/std/collections/array_list.yo +8 -20
  34. package/std/collections/btree_map.yo +15 -20
  35. package/std/collections/deque.yo +13 -15
  36. package/std/collections/hash_map.yo +15 -54
  37. package/std/collections/hash_set.yo +14 -50
  38. package/std/collections/linked_list.yo +6 -23
  39. package/std/collections/priority_queue.yo +15 -17
  40. package/std/crypto/md5.yo +10 -9
  41. package/std/crypto/random.yo +14 -12
  42. package/std/crypto/sha256.yo +9 -8
  43. package/std/encoding/base64.yo +16 -15
  44. package/std/encoding/hex.yo +17 -10
  45. package/std/encoding/html.yo +17 -11
  46. package/std/encoding/html_char_utils.yo +14 -11
  47. package/std/encoding/html_entities.yo +7 -8
  48. package/std/encoding/json.yo +39 -19
  49. package/std/encoding/punycode.yo +24 -18
  50. package/std/encoding/toml.yo +26 -9
  51. package/std/encoding/utf16.yo +19 -9
  52. package/std/error.yo +10 -23
  53. package/std/fmt/display.yo +15 -17
  54. package/std/fmt/index.yo +6 -27
  55. package/std/fmt/to_string.yo +4 -10
  56. package/std/fmt/writer.yo +33 -34
  57. package/std/fs/dir.yo +40 -30
  58. package/std/fs/file.yo +80 -75
  59. package/std/fs/metadata.yo +25 -25
  60. package/std/fs/temp.yo +21 -27
  61. package/std/fs/types.yo +20 -23
  62. package/std/fs/walker.yo +26 -31
  63. package/std/gc.yo +5 -0
  64. package/std/glob/index.yo +3 -0
  65. package/std/http/client.yo +31 -11
  66. package/std/http/http.yo +20 -0
  67. package/std/http/index.yo +1 -0
  68. package/std/io/reader.yo +4 -8
  69. package/std/io/writer.yo +4 -7
  70. package/std/libc/assert.yo +2 -2
  71. package/std/libc/ctype.yo +1 -2
  72. package/std/libc/dirent.yo +1 -2
  73. package/std/libc/errno.yo +1 -2
  74. package/std/libc/fcntl.yo +2 -2
  75. package/std/libc/float.yo +1 -2
  76. package/std/libc/limits.yo +1 -2
  77. package/std/libc/math.yo +2 -0
  78. package/std/libc/signal.yo +1 -2
  79. package/std/libc/stdatomic.yo +1 -2
  80. package/std/libc/stdint.yo +4 -3
  81. package/std/libc/stdio.yo +2 -0
  82. package/std/libc/stdlib.yo +2 -0
  83. package/std/libc/string.yo +2 -0
  84. package/std/libc/sys/stat.yo +1 -2
  85. package/std/libc/time.yo +2 -2
  86. package/std/libc/unistd.yo +2 -0
  87. package/std/libc/wctype.yo +1 -2
  88. package/std/libc/windows.yo +2 -2
  89. package/std/log/index.yo +11 -26
  90. package/std/net/addr.yo +18 -11
  91. package/std/net/dns.yo +3 -2
  92. package/std/net/errors.yo +16 -2
  93. package/std/net/tcp.yo +25 -22
  94. package/std/net/udp.yo +14 -12
  95. package/std/os/env.yo +23 -18
  96. package/std/os/signal.yo +31 -19
  97. package/std/path.yo +23 -74
  98. package/std/prelude.yo +284 -113
  99. package/std/process.yo +23 -37
  100. package/std/regex/compiler.yo +3 -5
  101. package/std/regex/flags.yo +11 -11
  102. package/std/regex/index.yo +2 -13
  103. package/std/regex/match.yo +3 -5
  104. package/std/regex/node.yo +6 -6
  105. package/std/regex/parser.yo +2 -4
  106. package/std/regex/unicode.yo +5 -5
  107. package/std/regex/vm.yo +5 -5
  108. package/std/string/index.yo +2 -1
  109. package/std/string/rune.yo +25 -1
  110. package/std/string/string.yo +31 -19
  111. package/std/string/unicode.yo +14 -15
  112. package/std/sync/channel.yo +18 -28
  113. package/std/sync/cond.yo +4 -0
  114. package/std/sync/mutex.yo +4 -1
  115. package/std/sync/once.yo +17 -19
  116. package/std/sync/rwlock.yo +19 -22
  117. package/std/sync/waitgroup.yo +21 -23
  118. package/std/sys/advise.yo +4 -4
  119. package/std/sys/bufio/buf_reader.yo +19 -16
  120. package/std/sys/bufio/buf_writer.yo +14 -11
  121. package/std/sys/clock.yo +4 -4
  122. package/std/sys/constants.yo +5 -5
  123. package/std/sys/copy.yo +9 -8
  124. package/std/sys/dir.yo +9 -8
  125. package/std/sys/dns.yo +8 -8
  126. package/std/sys/errors.yo +35 -6
  127. package/std/sys/events.yo +3 -3
  128. package/std/sys/externs.yo +3 -3
  129. package/std/sys/fallocate.yo +4 -4
  130. package/std/sys/fcntl.yo +8 -6
  131. package/std/sys/file.yo +7 -8
  132. package/std/sys/future.yo +1 -3
  133. package/std/sys/iov.yo +4 -4
  134. package/std/sys/lock.yo +7 -7
  135. package/std/sys/mmap.yo +7 -8
  136. package/std/sys/path.yo +4 -7
  137. package/std/sys/perm.yo +8 -8
  138. package/std/sys/pipe.yo +9 -8
  139. package/std/sys/process.yo +8 -8
  140. package/std/sys/seek.yo +4 -4
  141. package/std/sys/signal.yo +4 -4
  142. package/std/sys/signals.yo +4 -4
  143. package/std/sys/socket.yo +4 -4
  144. package/std/sys/socketpair.yo +4 -4
  145. package/std/sys/sockinfo.yo +4 -4
  146. package/std/sys/statfs.yo +8 -8
  147. package/std/sys/statx.yo +4 -4
  148. package/std/sys/sysinfo.yo +4 -4
  149. package/std/sys/tcp.yo +8 -8
  150. package/std/sys/temp.yo +9 -8
  151. package/std/sys/time.yo +8 -8
  152. package/std/sys/timer.yo +7 -8
  153. package/std/sys/tty.yo +13 -10
  154. package/std/sys/udp.yo +8 -8
  155. package/std/sys/umask.yo +4 -4
  156. package/std/sys/unix.yo +5 -5
  157. package/std/testing/bench.yo +21 -10
  158. package/std/thread.yo +18 -9
  159. package/std/time/datetime.yo +12 -14
  160. package/std/time/duration.yo +12 -14
  161. package/std/time/instant.yo +13 -16
  162. package/std/time/sleep.yo +9 -8
  163. package/std/url/index.yo +3 -19
  164. package/std/worker.yo +10 -18
@@ -1,14 +1,17 @@
1
- // std/encoding/json.yo - JSON parsing and serialisation
2
- //
3
- // Provides a dynamically-typed JSON value tree.
4
- //
5
- // Example:
6
- // { json_parse, json_stringify, JsonValue } :: import "std/encoding/json";
7
- // { Exception } :: import "std/error";
8
- //
9
- // given(exn) := Exception(throw : ((err) -> { escape (); }));
10
- // v := json_parse(`{"x": 1}`);
11
- // println(json_stringify(v));
1
+ //! JSON parsing and serialization.
2
+ //!
3
+ //! Provides a dynamically-typed JSON value tree.
4
+ //!
5
+ //! # Example
6
+ //!
7
+ //! ```rust
8
+ //! { json_parse, json_stringify, JsonValue } :: import "std/encoding/json";
9
+ //! { Exception } :: import "std/error";
10
+ //!
11
+ //! given(exn) := Exception(throw : ((err) -> { escape (); }));
12
+ //! v := json_parse(`{"x": 1}`);
13
+ //! println(json_stringify(v));
14
+ //! ```
12
15
 
13
16
  open import "../string";
14
17
  { ArrayList } :: import "../collections/array_list";
@@ -20,12 +23,19 @@ open import "../string";
20
23
  // JsonError
21
24
  // ============================================================================
22
25
 
26
+ /// JSON parsing error type.
23
27
  JsonError :: enum(
28
+ /// Encountered an unexpected character at the given position.
24
29
  UnexpectedChar(ch: u8, pos: usize),
30
+ /// Input ended unexpectedly.
25
31
  UnexpectedEnd,
32
+ /// Failed to parse a numeric literal.
26
33
  InvalidNumber,
34
+ /// Invalid escape sequence in a string.
27
35
  InvalidEscape,
36
+ /// Invalid Unicode escape in a string.
28
37
  InvalidUnicode,
38
+ /// Other error with a descriptive message.
29
39
  Other(msg: String)
30
40
  );
31
41
 
@@ -50,12 +60,19 @@ export JsonError;
50
60
  // JsonValue
51
61
  // ============================================================================
52
62
 
63
+ /// Dynamically-typed JSON value tree.
53
64
  JsonValue :: enum(
65
+ /// JSON null.
54
66
  Null,
67
+ /// JSON boolean.
55
68
  Bool(value: bool),
69
+ /// JSON number (IEEE 754 double).
56
70
  Number(value: f64),
71
+ /// JSON string.
57
72
  Str(value: String),
73
+ /// JSON array of values.
58
74
  Array(items: ArrayList(Self)),
75
+ /// JSON object with ordered key-value pairs.
59
76
  Object(keys: ArrayList(String), values: ArrayList(Self))
60
77
  );
61
78
 
@@ -63,13 +80,14 @@ JsonValue :: enum(
63
80
  // JsonKV - key/value pair convenience struct for Object
64
81
  // ============================================================================
65
82
 
83
+ /// Key-value pair for JSON object entries.
66
84
  JsonKV :: struct(
67
85
  key : String,
68
86
  value : JsonValue
69
87
  );
70
88
 
71
89
  impl(JsonValue,
72
- // Get a field from an Object value. Returns .None if not an Object or key missing.
90
+ /// Get a field from an Object value by key. Returns `.None` if not an Object or key is missing.
73
91
  get : (fn(self: Self, key: String) -> Option(JsonValue))(
74
92
  match(self,
75
93
  .Object(keys, values) => {
@@ -88,7 +106,7 @@ impl(JsonValue,
88
106
  )
89
107
  ),
90
108
 
91
- // Get an element from an Array value by index.
109
+ /// Get an element from an Array value by index.
92
110
  at : (fn(self: Self, index: usize) -> Option(JsonValue))(
93
111
  match(self,
94
112
  .Array(items) => items.get(index),
@@ -96,7 +114,7 @@ impl(JsonValue,
96
114
  )
97
115
  ),
98
116
 
99
- // Extract a bool.
117
+ /// Extract the boolean value, or `.None` if not a `Bool`.
100
118
  as_bool : (fn(self: Self) -> Option(bool))(
101
119
  match(self,
102
120
  .Bool(b) => .Some(b),
@@ -104,7 +122,7 @@ impl(JsonValue,
104
122
  )
105
123
  ),
106
124
 
107
- // Extract a number.
125
+ /// Extract the numeric value, or `.None` if not a `Number`.
108
126
  as_number : (fn(self: Self) -> Option(f64))(
109
127
  match(self,
110
128
  .Number(n) => .Some(n),
@@ -112,7 +130,7 @@ impl(JsonValue,
112
130
  )
113
131
  ),
114
132
 
115
- // Extract a string.
133
+ /// Extract the string value, or `.None` if not a `Str`.
116
134
  as_string : (fn(self: Self) -> Option(String))(
117
135
  match(self,
118
136
  .Str(s) => .Some(s),
@@ -120,7 +138,7 @@ impl(JsonValue,
120
138
  )
121
139
  ),
122
140
 
123
- // Extract an array.
141
+ /// Extract the array items, or `.None` if not an `Array`.
124
142
  as_array : (fn(self: Self) -> Option(ArrayList(JsonValue)))(
125
143
  match(self,
126
144
  .Array(items) => .Some(items),
@@ -128,7 +146,7 @@ impl(JsonValue,
128
146
  )
129
147
  ),
130
148
 
131
- // Extract object entries as ArrayList(JsonKV).
149
+ /// Extract object entries as `ArrayList(JsonKV)`, or `.None` if not an `Object`.
132
150
  as_object : (fn(self: Self) -> Option(ArrayList(JsonKV)))(
133
151
  match(self,
134
152
  .Object(keys, values) => {
@@ -468,6 +486,7 @@ _parse_value :: (fn(p: _Parser) -> Result(JsonValue, JsonError))({
468
486
  // Public API
469
487
  // ============================================================================
470
488
 
489
+ /// Parse a JSON string into a `JsonValue` tree. Throws via `Exception` on invalid input.
471
490
  json_parse :: (fn(s: str, using(exn : Exception)) -> JsonValue)({
472
491
  p := _Parser.new(s);
473
492
  match(_parse_value(p),
@@ -558,13 +577,14 @@ _stringify_into :: (fn(value: JsonValue, out: ArrayList(u8)) -> unit)({
558
577
  );
559
578
  });
560
579
 
580
+ /// Serialize a `JsonValue` to a compact JSON string.
561
581
  json_stringify :: (fn(value: JsonValue) -> String)({
562
582
  out := ArrayList(u8).new();
563
583
  _stringify_into(value, out);
564
584
  String.from_bytes(out)
565
585
  });
566
586
 
567
- // Serialise with pretty-printing (delegates to compact for now).
587
+ /// Serialize a `JsonValue` to a pretty-printed JSON string.
568
588
  json_stringify_pretty :: (fn(value: JsonValue, _indent: usize) -> String)(
569
589
  json_stringify(value)
570
590
  );
@@ -1,14 +1,17 @@
1
- // Punycode codec (RFC 3492)
2
- //
3
- // Provides punycode encoding/decoding and IDN hostname conversion.
4
- //
5
- // Example:
6
- // { punycode_decode, punycode_encode, to_unicode, to_ascii } :: import "std/encoding/punycode";
7
- //
8
- // encoded := punycode_encode(`München`);
9
- // decoded := punycode_decode(encoded);
10
- // ascii_domain := to_ascii(`münchen.de`); // "xn--mnchen-3ya.de"
11
- // unicode_domain := to_unicode(ascii_domain); // "münchen.de"
1
+ //! Punycode encoding/decoding (RFC 3492) for internationalized domain names.
2
+ //!
3
+ //! Provides punycode encoding/decoding and IDN hostname conversion.
4
+ //!
5
+ //! # Example
6
+ //!
7
+ //! ```rust
8
+ //! { punycode_decode, punycode_encode, to_unicode, to_ascii } :: import "std/encoding/punycode";
9
+ //!
10
+ //! encoded := punycode_encode(`München`);
11
+ //! decoded := punycode_decode(encoded);
12
+ //! ascii_domain := to_ascii(`münchen.de`); // "xn--mnchen-3ya.de"
13
+ //! unicode_domain := to_unicode(ascii_domain); // "münchen.de"
14
+ //! ```
12
15
 
13
16
  open import "../string";
14
17
  { ArrayList } :: import "../collections/array_list";
@@ -122,8 +125,9 @@ _string_to_codepoints :: (fn(s: String) -> ArrayList(i32))({
122
125
  cps
123
126
  });
124
127
 
125
- // Decode a punycode-encoded string (without the xn-- prefix).
126
- // Returns .Some(decoded) on success, .None on error.
128
+ /// Decode a punycode-encoded string (without the `xn--` prefix).
129
+ ///
130
+ /// Returns `.Some(decoded)` on success, `.None` on invalid input.
127
131
  punycode_decode :: (fn(input: String) -> Option(String))({
128
132
  (bytes : ArrayList(u8)) = input.as_bytes();
129
133
  (input_len : i32) = i32(bytes.len());
@@ -220,7 +224,7 @@ punycode_decode :: (fn(input: String) -> Option(String))({
220
224
  .Some(String.from_bytes(result_bytes))
221
225
  });
222
226
 
223
- // Encode a Unicode string to punycode (without the xn-- prefix).
227
+ /// Encode a Unicode string to punycode (without the `xn--` prefix).
224
228
  punycode_encode :: (fn(input: String) -> String)({
225
229
  (cps : ArrayList(i32)) = _string_to_codepoints(input);
226
230
  (cp_count : i32) = i32(cps.len());
@@ -300,8 +304,9 @@ punycode_encode :: (fn(input: String) -> String)({
300
304
  String.from_bytes(out)
301
305
  });
302
306
 
303
- // Convert an IDN hostname to Unicode display form.
304
- // Splits on '.', decodes xn-- labels, keeps original on failure.
307
+ /// Convert an IDN hostname to Unicode display form.
308
+ ///
309
+ /// Splits on `.`, decodes `xn--` labels via punycode, and keeps original labels on failure.
305
310
  to_unicode :: (fn(hostname: String) -> String)({
306
311
  (parts : ArrayList(String)) = hostname.split(`.`);
307
312
  (result : String) = ``;
@@ -331,8 +336,9 @@ to_unicode :: (fn(hostname: String) -> String)({
331
336
  result
332
337
  });
333
338
 
334
- // Convert a Unicode hostname to ASCII punycode form.
335
- // Non-ASCII labels get xn-- prefix.
339
+ /// Convert a Unicode hostname to ASCII-compatible encoding.
340
+ ///
341
+ /// Non-ASCII labels are punycode-encoded with the `xn--` prefix.
336
342
  to_ascii :: (fn(hostname: String) -> String)({
337
343
  (parts : ArrayList(String)) = hostname.split(`.`);
338
344
  (result : String) = ``;
@@ -1,28 +1,38 @@
1
- // std/encoding/toml.yo - Basic TOML parser
2
- //
3
- // Parses a subset of TOML into a TomlValue tree.
4
- // Supports: strings, integers, booleans, table sections, comments.
5
- //
6
- // Example:
7
- // { toml_parse, TomlValue } :: import "std/encoding/toml";
8
- // // Or via the encoding index:
9
- // { toml_parse, TomlValue } :: import "std/encoding";
1
+ //! Basic TOML parser for configuration files.
2
+ //!
3
+ //! Parses a subset of TOML into a `TomlValue` tree.
4
+ //! Supports: strings, integers, booleans, table sections, comments.
5
+ //!
6
+ //! # Example
7
+ //!
8
+ //! ```rust
9
+ //! { toml_parse, TomlValue } :: import "std/encoding/toml";
10
+ //! // Or via the encoding index:
11
+ //! { toml_parse, TomlValue } :: import "std/encoding";
12
+ //! ```
10
13
 
11
14
  open import "../string";
12
15
  { ArrayList } :: import "../collections/array_list";
13
16
 
17
+ /// TOML value type — the result of parsing a TOML document.
14
18
  TomlValue :: enum(
19
+ /// A TOML string value.
15
20
  Str(value: String),
21
+ /// A TOML integer value.
16
22
  Int(value: i64),
23
+ /// A TOML boolean value.
17
24
  Bool(value: bool),
25
+ /// A TOML table (ordered key-value map).
18
26
  Table(keys: ArrayList(String), values: ArrayList(Self))
19
27
  );
20
28
 
21
29
  impl(TomlValue,
30
+ /// Create a new empty table.
22
31
  new_table : (fn() -> Self)(
23
32
  Self.Table(keys: ArrayList(String).new(), values: ArrayList(TomlValue).new())
24
33
  ),
25
34
 
35
+ /// Look up a value by key in a table. Returns `.None` if not a table or key is missing.
26
36
  get : (fn(self: Self, key: String) -> Option(TomlValue))(
27
37
  match(self,
28
38
  .Table(keys, values) => {
@@ -41,10 +51,12 @@ impl(TomlValue,
41
51
  )
42
52
  ),
43
53
 
54
+ /// Check whether the table contains the given key.
44
55
  has_key : (fn(self: Self, key: String) -> bool)(
45
56
  self.get(key).is_some()
46
57
  ),
47
58
 
59
+ /// Insert or update a key-value pair in the table.
48
60
  set : (fn(self: Self, key: String, value: TomlValue) -> unit)(
49
61
  match(self,
50
62
  .Table(keys, values) => {
@@ -72,6 +84,7 @@ impl(TomlValue,
72
84
  )
73
85
  ),
74
86
 
87
+ /// Return the number of keys in the table, or `0` if not a table.
75
88
  table_len : (fn(self: Self) -> usize)(
76
89
  match(self,
77
90
  .Table(keys, _) => keys.len(),
@@ -79,14 +92,17 @@ impl(TomlValue,
79
92
  )
80
93
  ),
81
94
 
95
+ /// Extract the string value, or `.None` if not a `Str`.
82
96
  as_string : (fn(self: Self) -> Option(String))(
83
97
  match(self, .Str(s) => .Some(s), _ => .None)
84
98
  ),
85
99
 
100
+ /// Extract the integer value, or `.None` if not an `Int`.
86
101
  as_int : (fn(self: Self) -> Option(i64))(
87
102
  match(self, .Int(n) => .Some(n), _ => .None)
88
103
  ),
89
104
 
105
+ /// Extract the boolean value, or `.None` if not a `Bool`.
90
106
  as_bool : (fn(self: Self) -> Option(bool))(
91
107
  match(self, .Bool(b) => .Some(b), _ => .None)
92
108
  )
@@ -129,6 +145,7 @@ _parse_toml_value :: (fn(raw: String) -> Result(TomlValue, String))({
129
145
  .Err(`Unknown value: ${trimmed}`)
130
146
  });
131
147
 
148
+ /// Parse a TOML string into a `TomlValue` tree. Returns `Result(TomlValue, String)`.
132
149
  toml_parse :: (fn(input: String) -> Result(TomlValue, String))({
133
150
  root_keys := ArrayList(String).new();
134
151
  root_vals := ArrayList(TomlValue).new();
@@ -1,12 +1,15 @@
1
- // std/encoding/utf16.yo - UTF-8 <-> UTF-16 conversion
2
- //
3
- // Converts between Yo's UTF-8 String and UTF-16LE/BE (as ArrayList(u16)).
4
- //
5
- // Example:
6
- // { utf8_to_utf16, utf16_to_utf8 } :: import "std/encoding/utf16";
7
- //
8
- // words := utf8_to_utf16("hello");
9
- // s := utf16_to_utf8(words);
1
+ //! UTF-8 to UTF-16 conversion and vice versa.
2
+ //!
3
+ //! Converts between Yo's UTF-8 `str` and UTF-16 code units (`ArrayList(u16)`).
4
+ //!
5
+ //! # Example
6
+ //!
7
+ //! ```rust
8
+ //! { utf8_to_utf16, utf16_to_utf8 } :: import "std/encoding/utf16";
9
+ //!
10
+ //! words := utf8_to_utf16("hello");
11
+ //! s := utf16_to_utf8(words);
12
+ //! ```
10
13
 
11
14
  open import "../string";
12
15
  { ArrayList } :: import "../collections/array_list";
@@ -17,6 +20,9 @@ open import "../string";
17
20
  // UTF-8 → UTF-16
18
21
  // ============================================================================
19
22
 
23
+ /// Convert a UTF-8 string to an `ArrayList(u16)` of UTF-16 code units.
24
+ ///
25
+ /// Handles BMP characters directly and encodes supplementary characters as surrogate pairs.
20
26
  utf8_to_utf16 :: (fn(s: str) -> ArrayList(u16))({
21
27
  out := ArrayList(u16).new();
22
28
  i := usize(0);
@@ -64,6 +70,10 @@ export utf8_to_utf16;
64
70
  // UTF-16 → UTF-8
65
71
  // ============================================================================
66
72
 
73
+ /// Convert UTF-16 code units to a UTF-8 `String`.
74
+ ///
75
+ /// Decodes surrogate pairs back into supplementary code points.
76
+ /// Throws via `Exception` on unpaired surrogates.
67
77
  utf16_to_utf8 :: (fn(data: ArrayList(u16), using(exn : Exception)) -> String)({
68
78
  out := ArrayList(u8).new();
69
79
  i := usize(0);
package/std/error.yo CHANGED
@@ -1,14 +1,10 @@
1
- // std/error.yo - Error trait
2
- //
3
- // Provides a standard Error trait for typed error propagation.
4
- // All error types in the standard library should implement this trait.
1
+ //! Standard error handling types and traits for Yo.
2
+
5
3
  open import "./string";
6
4
  open import "./fmt";
7
5
 
8
- // ============================================================================
9
- // Error trait
10
- // ============================================================================
11
-
6
+ /// Standard error trait for typed error propagation.
7
+ /// All error types should implement this trait along with `ToString`.
12
8
  Error :: trait(
13
9
  (source : (fn(self: *(Self)) -> Option(Dyn(SelfTrait)))) ?= ((self) -> .None),
14
10
 
@@ -16,32 +12,23 @@ Error :: trait(
16
12
  );
17
13
  export Error;
18
14
 
15
+ /// Type-erased dynamic error type. Wraps any type implementing `Error`.
19
16
  AnyError :: Dyn(Error);
20
17
  export AnyError;
21
18
 
22
- // ============================================================================
23
- // Error implementations for standard types
24
- // ============================================================================
25
-
19
+ /// Allows using `String` directly as an error type.
26
20
  impl(String, Error());
27
21
 
28
- /// === Exception ===
29
- // This is a non-resumable exception handling.
30
- //
31
- // Example (function that may throw):
32
- //
33
- // safe_div :: (fn(x: i32, y: i32, using(exn : Exception)) -> i32)(
34
- // cond(
35
- // (y == i32(0)) => exn.throw(dyn `division by zero`)),
36
- // true => (x / y)
37
- // )
38
- // );
22
+ /// Non-resumable exception handling effect module.
23
+ /// Use `throw` to raise an `AnyError` and abort the current computation.
39
24
  Exception :: module(
40
25
  throw : (fn(forall(ResumeType : Type), error : AnyError) -> ResumeType)
41
26
  );
42
27
  export Exception;
43
28
 
44
29
 
30
+ /// Creates a resumable exception module parameterized by the resume type.
31
+ /// Unlike `Exception`, the handler can return a value to resume the computation.
45
32
  ResumableException :: (fn(comptime(ResumeType) : Type) -> comptime(Module))(
46
33
  module(
47
34
  throw : (fn(error : AnyError) -> ResumeType)
@@ -1,25 +1,23 @@
1
- // std/fmt/display.yo - Display trait
2
- //
3
- // The Display trait provides a standard way to format a value for
4
- // human-readable output using a Writer. It complements ToString
5
- // (which always returns a String) by streaming into an existing Writer.
6
- //
7
- // Example:
8
- // impl(MyType, Display(
9
- // display : (fn(self: *(Self), writer: Writer) -> Writer)(
10
- // writer.write_str("MyType(").write_i64(i64(self.value)).write_str(")")
11
- // )
12
- // ));
1
+ //! Display trait for streaming formatted output to a Writer.
2
+ //!
3
+ //! # Example
4
+ //!
5
+ //! ```rust
6
+ //! impl(MyType, Display(
7
+ //! display : (fn(self: *(Self), writer: Writer) -> Writer)(
8
+ //! writer.write_str("MyType(").write_i64(i64(self.value)).write_str(")")
9
+ //! )
10
+ //! ));
11
+ //! ```
13
12
 
14
13
  { Writer } :: import "./writer";
15
14
 
16
- // ============================================================================
17
- // Display trait
18
- // ============================================================================
15
+ /// Trait for formatting values by writing to a `Writer` rather than allocating a `String`.
16
+ /// Complement to `ToString` for streaming output into an existing buffer.
19
17
 
20
- Display :: (fn(comptime(T) : Type) -> comptime(Type))
18
+ Display :: (fn(comptime(T) : Type) -> comptime(Trait))
21
19
  trait(
22
- display :: (fn(self: *(T), writer: Writer) -> Writer)
20
+ display : (fn(self: *(T), writer: Writer) -> Writer)
23
21
  )
24
22
  ;
25
23
 
package/std/fmt/index.yo CHANGED
@@ -1,16 +1,12 @@
1
+ //! Formatted output to stdout and stderr.
2
+
1
3
  { fwrite, stdout, stderr } :: import "../libc/stdio";
2
4
  { String } :: import "../string";
3
5
  { ToString } :: import "./to_string.yo";
4
6
 
5
7
  export ToString;
6
8
 
7
- /**
8
- * Print a value that implements ToString to stdout, with a newline.
9
- *
10
- * Example:
11
- * println(42); // prints "42\n"
12
- * println("hello"); // prints "hello\n"
13
- */
9
+ /// Print a value that implements `ToString` to stdout, followed by a newline.
14
10
  println :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
15
11
  s := v.to_string();
16
12
  str_bytes := s.as_bytes();
@@ -26,13 +22,7 @@ println :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
26
22
  });
27
23
  export println;
28
24
 
29
- /**
30
- * Print a value that implements ToString to stdout, without a newline.
31
- *
32
- * Example:
33
- * print("hello ");
34
- * print("world"); // prints "hello world"
35
- */
25
+ /// Print a value that implements `ToString` to stdout, without a trailing newline.
36
26
  print :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
37
27
  s := v.to_string();
38
28
  str_bytes := s.as_bytes();
@@ -47,12 +37,7 @@ print :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
47
37
  });
48
38
  export print;
49
39
 
50
- /**
51
- * Print a value that implements ToString to stderr, with a newline.
52
- *
53
- * Example:
54
- * eprintln("Error: something went wrong");
55
- */
40
+ /// Print a value that implements `ToString` to stderr, followed by a newline.
56
41
  eprintln :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
57
42
  s := v.to_string();
58
43
  str_bytes := s.as_bytes();
@@ -68,13 +53,7 @@ eprintln :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
68
53
  });
69
54
  export eprintln;
70
55
 
71
- /**
72
- * Print a value that implements ToString to stderr, without a newline.
73
- *
74
- * Example:
75
- * eprint("Warning: ");
76
- * eprintln("be careful");
77
- */
56
+ /// Print a value that implements `ToString` to stderr, without a trailing newline.
78
57
  eprint :: (fn(forall(T : Type), v : T, where(T <: ToString)) -> unit)({
79
58
  s := v.to_string();
80
59
  str_bytes := s.as_bytes();
@@ -1,16 +1,10 @@
1
+ //! ToString trait and implementations for all primitive types.
2
+
1
3
  { String, rune } :: import "../string";
2
4
  { snprintf } :: import "../libc/stdio";
3
5
 
4
- /**
5
- * ToString - A trait for converting values to String representation.
6
- *
7
- * This is similar to:
8
- * - Rust's `std::fmt::Display` / `ToString` trait
9
- * - Go's `fmt.Stringer` interface
10
- * - Java's `Object.toString()` method
11
- *
12
- * Types implementing this trait can be used with `println`, `print`, and `format`.
13
- */
6
+ /// Trait for converting values to their `String` representation.
7
+ /// Types implementing this trait can be used with `println`, `print`, and template strings.
14
8
  ToString :: trait(
15
9
  to_string : (fn(self: *(Self)) -> String)
16
10
  );