@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.
- package/README.md +10 -1
- package/out/cjs/index.cjs +578 -572
- package/out/cjs/yo-cli.cjs +1447 -529
- package/out/esm/index.mjs +434 -428
- package/out/types/src/build-runner.d.ts +1 -1
- package/out/types/src/doc/builder.d.ts +21 -0
- package/out/types/src/doc/builder.test.d.ts +1 -0
- package/out/types/src/doc/extractor.d.ts +27 -0
- package/out/types/src/doc/extractor.test.d.ts +1 -0
- package/out/types/src/doc/model.d.ts +87 -0
- package/out/types/src/doc/render-html.d.ts +27 -0
- package/out/types/src/doc/render-html.test.d.ts +1 -0
- package/out/types/src/doc/render-json.d.ts +7 -0
- package/out/types/src/doc/render-json.test.d.ts +1 -0
- package/out/types/src/doc/render-markdown.d.ts +13 -0
- package/out/types/src/doc/render-markdown.test.d.ts +1 -0
- package/out/types/src/doc/sections.d.ts +7 -0
- package/out/types/src/doc/sections.test.d.ts +1 -0
- package/out/types/src/doc-command.d.ts +10 -0
- package/out/types/src/env.d.ts +1 -0
- package/out/types/src/evaluator/builtins/build.d.ts +17 -0
- package/out/types/src/evaluator/context.d.ts +1 -0
- package/out/types/src/expr.d.ts +2 -1
- package/out/types/src/token.d.ts +4 -0
- package/out/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -1
- package/scripts/build-site.ts +498 -0
- package/std/alg/hash.yo +12 -24
- package/std/allocator.yo +21 -29
- package/std/async.yo +4 -2
- package/std/build.yo +192 -42
- package/std/cli/arg_parser.yo +37 -1
- package/std/collections/array_list.yo +8 -20
- package/std/collections/btree_map.yo +15 -20
- package/std/collections/deque.yo +13 -15
- package/std/collections/hash_map.yo +15 -54
- package/std/collections/hash_set.yo +14 -50
- package/std/collections/linked_list.yo +6 -23
- package/std/collections/priority_queue.yo +15 -17
- package/std/crypto/md5.yo +10 -9
- package/std/crypto/random.yo +14 -12
- package/std/crypto/sha256.yo +9 -8
- package/std/encoding/base64.yo +16 -15
- package/std/encoding/hex.yo +17 -10
- package/std/encoding/html.yo +17 -11
- package/std/encoding/html_char_utils.yo +14 -11
- package/std/encoding/html_entities.yo +7 -8
- package/std/encoding/json.yo +39 -19
- package/std/encoding/punycode.yo +24 -18
- package/std/encoding/toml.yo +26 -9
- package/std/encoding/utf16.yo +19 -9
- package/std/error.yo +10 -23
- package/std/fmt/display.yo +15 -17
- package/std/fmt/index.yo +6 -27
- package/std/fmt/to_string.yo +4 -10
- package/std/fmt/writer.yo +33 -34
- package/std/fs/dir.yo +40 -30
- package/std/fs/file.yo +80 -75
- package/std/fs/metadata.yo +25 -25
- package/std/fs/temp.yo +21 -27
- package/std/fs/types.yo +20 -23
- package/std/fs/walker.yo +26 -31
- package/std/gc.yo +5 -0
- package/std/glob/index.yo +3 -0
- package/std/http/client.yo +31 -11
- package/std/http/http.yo +20 -0
- package/std/http/index.yo +1 -0
- package/std/io/reader.yo +4 -8
- package/std/io/writer.yo +4 -7
- package/std/libc/assert.yo +2 -2
- package/std/libc/ctype.yo +1 -2
- package/std/libc/dirent.yo +1 -2
- package/std/libc/errno.yo +1 -2
- package/std/libc/fcntl.yo +2 -2
- package/std/libc/float.yo +1 -2
- package/std/libc/limits.yo +1 -2
- package/std/libc/math.yo +2 -0
- package/std/libc/signal.yo +1 -2
- package/std/libc/stdatomic.yo +1 -2
- package/std/libc/stdint.yo +4 -3
- package/std/libc/stdio.yo +2 -0
- package/std/libc/stdlib.yo +2 -0
- package/std/libc/string.yo +2 -0
- package/std/libc/sys/stat.yo +1 -2
- package/std/libc/time.yo +2 -2
- package/std/libc/unistd.yo +2 -0
- package/std/libc/wctype.yo +1 -2
- package/std/libc/windows.yo +2 -2
- package/std/log/index.yo +11 -26
- package/std/net/addr.yo +18 -11
- package/std/net/dns.yo +3 -2
- package/std/net/errors.yo +16 -2
- package/std/net/tcp.yo +25 -22
- package/std/net/udp.yo +14 -12
- package/std/os/env.yo +23 -18
- package/std/os/signal.yo +31 -19
- package/std/path.yo +23 -74
- package/std/prelude.yo +284 -113
- package/std/process.yo +23 -37
- package/std/regex/compiler.yo +3 -5
- package/std/regex/flags.yo +11 -11
- package/std/regex/index.yo +2 -13
- package/std/regex/match.yo +3 -5
- package/std/regex/node.yo +6 -6
- package/std/regex/parser.yo +2 -4
- package/std/regex/unicode.yo +5 -5
- package/std/regex/vm.yo +5 -5
- package/std/string/index.yo +2 -1
- package/std/string/rune.yo +25 -1
- package/std/string/string.yo +31 -19
- package/std/string/unicode.yo +14 -15
- package/std/sync/channel.yo +18 -28
- package/std/sync/cond.yo +4 -0
- package/std/sync/mutex.yo +4 -1
- package/std/sync/once.yo +17 -19
- package/std/sync/rwlock.yo +19 -22
- package/std/sync/waitgroup.yo +21 -23
- package/std/sys/advise.yo +4 -4
- package/std/sys/bufio/buf_reader.yo +19 -16
- package/std/sys/bufio/buf_writer.yo +14 -11
- package/std/sys/clock.yo +4 -4
- package/std/sys/constants.yo +5 -5
- package/std/sys/copy.yo +9 -8
- package/std/sys/dir.yo +9 -8
- package/std/sys/dns.yo +8 -8
- package/std/sys/errors.yo +35 -6
- package/std/sys/events.yo +3 -3
- package/std/sys/externs.yo +3 -3
- package/std/sys/fallocate.yo +4 -4
- package/std/sys/fcntl.yo +8 -6
- package/std/sys/file.yo +7 -8
- package/std/sys/future.yo +1 -3
- package/std/sys/iov.yo +4 -4
- package/std/sys/lock.yo +7 -7
- package/std/sys/mmap.yo +7 -8
- package/std/sys/path.yo +4 -7
- package/std/sys/perm.yo +8 -8
- package/std/sys/pipe.yo +9 -8
- package/std/sys/process.yo +8 -8
- package/std/sys/seek.yo +4 -4
- package/std/sys/signal.yo +4 -4
- package/std/sys/signals.yo +4 -4
- package/std/sys/socket.yo +4 -4
- package/std/sys/socketpair.yo +4 -4
- package/std/sys/sockinfo.yo +4 -4
- package/std/sys/statfs.yo +8 -8
- package/std/sys/statx.yo +4 -4
- package/std/sys/sysinfo.yo +4 -4
- package/std/sys/tcp.yo +8 -8
- package/std/sys/temp.yo +9 -8
- package/std/sys/time.yo +8 -8
- package/std/sys/timer.yo +7 -8
- package/std/sys/tty.yo +13 -10
- package/std/sys/udp.yo +8 -8
- package/std/sys/umask.yo +4 -4
- package/std/sys/unix.yo +5 -5
- package/std/testing/bench.yo +21 -10
- package/std/thread.yo +18 -9
- package/std/time/datetime.yo +12 -14
- package/std/time/duration.yo +12 -14
- package/std/time/instant.yo +13 -16
- package/std/time/sleep.yo +9 -8
- package/std/url/index.yo +3 -19
- package/std/worker.yo +10 -18
package/std/encoding/json.yo
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
);
|
package/std/encoding/punycode.yo
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
126
|
-
|
|
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
|
-
|
|
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
|
-
|
|
304
|
-
|
|
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
|
-
|
|
335
|
-
|
|
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) = ``;
|
package/std/encoding/toml.yo
CHANGED
|
@@ -1,28 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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();
|
package/std/encoding/utf16.yo
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
///
|
|
29
|
-
|
|
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)
|
package/std/fmt/display.yo
CHANGED
|
@@ -1,25 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
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(
|
|
18
|
+
Display :: (fn(comptime(T) : Type) -> comptime(Trait))
|
|
21
19
|
trait(
|
|
22
|
-
display
|
|
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();
|
package/std/fmt/to_string.yo
CHANGED
|
@@ -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
|
-
|
|
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
|
);
|