Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

zig-codeblocks

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zig-codeblocks - pypi Package Compare versions

Comparing version
0.2.0
to
0.2.1
+25
tests/sources/formatting_results/assign_undefined.json
[
"35:const ",
"0:print = ",
"34;1:@import",
"0:(",
"32:\"std\"",
"0:).debug.print;\n\n",
"35:pub fn ",
"34:main",
"0:() ",
"33:void ",
"0:{\n ",
"35:var ",
"0:x: ",
"33:i32 ",
"0:= ",
"36:undefined",
"0:;\n x = ",
"36:1",
"0:;\n ",
"34:print",
"0:(",
"32:\"{d}\"",
"0:, .{x});\n}"
]
[
"35:const ",
"0:print = ",
"34;1:@import",
"0:(",
"32:\"std\"",
"0:).debug.print;\n\n",
"35:pub fn ",
"34:main",
"0:() ",
"33:void ",
"0:{\n ",
"30:// Comments in Zig start with \"//\" and end at the next LF byte (end of line).\n // The line below is a comment and won't be executed.\n\n //print(\"Hello?\", .{});\n\n ",
"34:print",
"0:(",
"32:\"Hello, world!",
"0:\\n",
"32:\"",
"0:, .{}); ",
"30:// another comment\n",
"0:}"
]
[
"35:const ",
"0:std = ",
"34;1:@import",
"0:(",
"32:\"std\"",
"0:);\n\n",
"35:pub fn ",
"34:main",
"0:() !",
"33:void ",
"0:{\n ",
"35:const ",
"0:stdout = std.io.",
"34:getStdOut",
"0:().",
"34:writer",
"0:();\n\n ",
"35:const ",
"0:greeting = ",
"32:\"Hello, 🌍!",
"0:\\n",
"32:\"",
"0:;\n ",
"35:try ",
"0:stdout.",
"34:print",
"0:(",
"32:\"{s}\"",
"0:, .{greeting});\n\n ",
"35:const ",
"0:emoji_identifier = ",
"32:\"✅ This is a test string with emojis 🚀🔥",
"0:\\n",
"32:\"",
"0:;\n ",
"35:try ",
"0:stdout.",
"34:print",
"0:(",
"32:\"{s}\"",
"0:, .{emoji_identifier});\n}"
]
[
"35:const ",
"0:std = ",
"34;1:@import",
"0:(",
"32:\"std\"",
"0:);\n",
"35:const ",
"0:expect = std.testing.expect;\n\n",
"35:comptime ",
"0:{\n ",
"35:asm ",
"0:(\n ",
"32:\\\\.global my_func;\n \\\\.type my_func, @function;\n \\\\my_func:\n \\\\ lea (%rdi,%rsi,1),%eax\n \\\\ retq\n ",
"0:);\n}\n\n",
"35:extern fn ",
"34:my_func",
"0:(a: ",
"33:i32",
"0:, b: ",
"33:i32",
"0:) ",
"33:i32",
"0:;\n\n",
"35:test ",
"32:\"global assembly\" ",
"0:{\n ",
"35:try ",
"34:expect",
"0:(",
"34:my_func",
"0:(",
"36:12",
"0:, ",
"36:34",
"0:) == ",
"36:46",
"0:);\n}"
]
[
"35:const ",
"0:std = ",
"34;1:@import",
"0:(",
"32:\"std\"",
"0:);\n\n",
"35:pub fn ",
"34:main",
"0:() ",
"33:void ",
"0:{\n std.debug.",
"34:print",
"0:(",
"32:\"Hello, world!",
"0:\\n",
"32:\"",
"0:, .{});\n}"
]
[
"35:const ",
"0:@\"identifier with spaces in it\" = ",
"36:0xff",
"0:;\n",
"35:const ",
"0:@\"1SmallStep4Man\" = ",
"36:112358",
"0:;\n\n",
"35:const ",
"0:c = ",
"34;1:@import",
"0:(",
"32:\"std\"",
"0:).c;\n",
"35:pub extern ",
"32:\"c\" ",
"35:fn ",
"0:@\"error\"() ",
"33:void",
"0:;\n",
"35:pub extern ",
"32:\"c\" ",
"35:fn ",
"0:@\"fstat$INODE64\"(fd: c.fd_t, buf: *c.Stat) ",
"33:c_int",
"0:;\n\n",
"35:const ",
"0:Color = ",
"35:enum ",
"0:{\n red,\n @\"really red\",\n};\n",
"35:const ",
"0:color: Color = .@\"really red\";"
]
[
{
"kind": "const",
"start": 0,
"end": 5
},
{
"kind": "identifier",
"value": "print",
"start": 6,
"end": 11
},
{
"kind": "=",
"start": 12,
"end": 13
},
{
"kind": "builtin_identifier",
"value": "@import",
"start": 14,
"end": 21
},
{
"kind": "(",
"start": 21,
"end": 22
},
{
"kind": "\"",
"start": 22,
"end": 23
},
{
"kind": "string_content",
"value": "std",
"start": 23,
"end": 26
},
{
"kind": "\"",
"start": 26,
"end": 27
},
{
"kind": ")",
"start": 27,
"end": 28
},
{
"kind": ".",
"start": 28,
"end": 29
},
{
"kind": "identifier",
"value": "debug",
"start": 29,
"end": 34
},
{
"kind": ".",
"start": 34,
"end": 35
},
{
"kind": "identifier",
"value": "print",
"start": 35,
"end": 40
},
{
"kind": ";",
"start": 40,
"end": 41
},
{
"kind": "pub",
"start": 43,
"end": 46
},
{
"kind": "fn",
"start": 47,
"end": 49
},
{
"kind": "identifier",
"value": "main",
"start": 50,
"end": 54
},
{
"kind": "(",
"start": 54,
"end": 55
},
{
"kind": ")",
"start": 55,
"end": 56
},
{
"kind": "void",
"start": 57,
"end": 61
},
{
"kind": "{",
"start": 62,
"end": 63
},
{
"kind": "var",
"start": 68,
"end": 71
},
{
"kind": "identifier",
"value": "x",
"start": 72,
"end": 73
},
{
"kind": ":",
"start": 73,
"end": 74
},
{
"kind": "builtin_type",
"value": "i32",
"start": 75,
"end": 78
},
{
"kind": "=",
"start": 79,
"end": 80
},
{
"kind": "undefined",
"start": 81,
"end": 90
},
{
"kind": ";",
"start": 90,
"end": 91
},
{
"kind": "identifier",
"value": "x",
"start": 96,
"end": 97
},
{
"kind": "=",
"start": 98,
"end": 99
},
{
"kind": "integer",
"value": "1",
"start": 100,
"end": 101
},
{
"kind": ";",
"start": 101,
"end": 102
},
{
"kind": "identifier",
"value": "print",
"start": 107,
"end": 112
},
{
"kind": "(",
"start": 112,
"end": 113
},
{
"kind": "\"",
"start": 113,
"end": 114
},
{
"kind": "string_content",
"value": "{d}",
"start": 114,
"end": 117
},
{
"kind": "\"",
"start": 117,
"end": 118
},
{
"kind": ",",
"start": 118,
"end": 119
},
{
"kind": ".",
"start": 120,
"end": 121
},
{
"kind": "{",
"start": 121,
"end": 122
},
{
"kind": "identifier",
"value": "x",
"start": 122,
"end": 123
},
{
"kind": "}",
"start": 123,
"end": 124
},
{
"kind": ")",
"start": 124,
"end": 125
},
{
"kind": ";",
"start": 125,
"end": 126
},
{
"kind": "}",
"start": 127,
"end": 128
}
]
[
{
"kind": "const",
"start": 0,
"end": 5
},
{
"kind": "identifier",
"value": "print",
"start": 6,
"end": 11
},
{
"kind": "=",
"start": 12,
"end": 13
},
{
"kind": "builtin_identifier",
"value": "@import",
"start": 14,
"end": 21
},
{
"kind": "(",
"start": 21,
"end": 22
},
{
"kind": "\"",
"start": 22,
"end": 23
},
{
"kind": "string_content",
"value": "std",
"start": 23,
"end": 26
},
{
"kind": "\"",
"start": 26,
"end": 27
},
{
"kind": ")",
"start": 27,
"end": 28
},
{
"kind": ".",
"start": 28,
"end": 29
},
{
"kind": "identifier",
"value": "debug",
"start": 29,
"end": 34
},
{
"kind": ".",
"start": 34,
"end": 35
},
{
"kind": "identifier",
"value": "print",
"start": 35,
"end": 40
},
{
"kind": ";",
"start": 40,
"end": 41
},
{
"kind": "pub",
"start": 43,
"end": 46
},
{
"kind": "fn",
"start": 47,
"end": 49
},
{
"kind": "identifier",
"value": "main",
"start": 50,
"end": 54
},
{
"kind": "(",
"start": 54,
"end": 55
},
{
"kind": ")",
"start": 55,
"end": 56
},
{
"kind": "void",
"start": 57,
"end": 61
},
{
"kind": "{",
"start": 62,
"end": 63
},
{
"kind": "comment",
"value": "// Comments in Zig start with \"//\" and end at the next LF byte (end of line).",
"start": 68,
"end": 145
},
{
"kind": "comment",
"value": "// The line below is a comment and won't be executed.",
"start": 150,
"end": 203
},
{
"kind": "comment",
"value": "//print(\"Hello?\", .{});",
"start": 209,
"end": 232
},
{
"kind": "identifier",
"value": "print",
"start": 238,
"end": 243
},
{
"kind": "(",
"start": 243,
"end": 244
},
{
"kind": "\"",
"start": 244,
"end": 245
},
{
"kind": "string_content",
"value": "Hello, world!",
"start": 245,
"end": 258
},
{
"kind": "escape_sequence",
"value": "\\n",
"start": 258,
"end": 260
},
{
"kind": "\"",
"start": 260,
"end": 261
},
{
"kind": ",",
"start": 261,
"end": 262
},
{
"kind": ".",
"start": 263,
"end": 264
},
{
"kind": "{",
"start": 264,
"end": 265
},
{
"kind": "}",
"start": 265,
"end": 266
},
{
"kind": ")",
"start": 266,
"end": 267
},
{
"kind": ";",
"start": 267,
"end": 268
},
{
"kind": "comment",
"value": "// another comment",
"start": 269,
"end": 287
},
{
"kind": "}",
"start": 288,
"end": 289
}
]
[
{
"kind": "const",
"start": 0,
"end": 5
},
{
"kind": "identifier",
"value": "std",
"start": 6,
"end": 9
},
{
"kind": "=",
"start": 10,
"end": 11
},
{
"kind": "builtin_identifier",
"value": "@import",
"start": 12,
"end": 19
},
{
"kind": "(",
"start": 19,
"end": 20
},
{
"kind": "\"",
"start": 20,
"end": 21
},
{
"kind": "string_content",
"value": "std",
"start": 21,
"end": 24
},
{
"kind": "\"",
"start": 24,
"end": 25
},
{
"kind": ")",
"start": 25,
"end": 26
},
{
"kind": ";",
"start": 26,
"end": 27
},
{
"kind": "pub",
"start": 29,
"end": 32
},
{
"kind": "fn",
"start": 33,
"end": 35
},
{
"kind": "identifier",
"value": "main",
"start": 36,
"end": 40
},
{
"kind": "(",
"start": 40,
"end": 41
},
{
"kind": ")",
"start": 41,
"end": 42
},
{
"kind": "!",
"start": 43,
"end": 44
},
{
"kind": "void",
"start": 44,
"end": 48
},
{
"kind": "{",
"start": 49,
"end": 50
},
{
"kind": "const",
"start": 55,
"end": 60
},
{
"kind": "identifier",
"value": "stdout",
"start": 61,
"end": 67
},
{
"kind": "=",
"start": 68,
"end": 69
},
{
"kind": "identifier",
"value": "std",
"start": 70,
"end": 73
},
{
"kind": ".",
"start": 73,
"end": 74
},
{
"kind": "identifier",
"value": "io",
"start": 74,
"end": 76
},
{
"kind": ".",
"start": 76,
"end": 77
},
{
"kind": "identifier",
"value": "getStdOut",
"start": 77,
"end": 86
},
{
"kind": "(",
"start": 86,
"end": 87
},
{
"kind": ")",
"start": 87,
"end": 88
},
{
"kind": ".",
"start": 88,
"end": 89
},
{
"kind": "identifier",
"value": "writer",
"start": 89,
"end": 95
},
{
"kind": "(",
"start": 95,
"end": 96
},
{
"kind": ")",
"start": 96,
"end": 97
},
{
"kind": ";",
"start": 97,
"end": 98
},
{
"kind": "const",
"start": 104,
"end": 109
},
{
"kind": "identifier",
"value": "greeting",
"start": 110,
"end": 118
},
{
"kind": "=",
"start": 119,
"end": 120
},
{
"kind": "\"",
"start": 121,
"end": 122
},
{
"kind": "string_content",
"value": "Hello, 🌍!",
"start": 122,
"end": 134
},
{
"kind": "escape_sequence",
"value": "\\n",
"start": 134,
"end": 136
},
{
"kind": "\"",
"start": 136,
"end": 137
},
{
"kind": ";",
"start": 137,
"end": 138
},
{
"kind": "try",
"start": 143,
"end": 146
},
{
"kind": "identifier",
"value": "stdout",
"start": 147,
"end": 153
},
{
"kind": ".",
"start": 153,
"end": 154
},
{
"kind": "identifier",
"value": "print",
"start": 154,
"end": 159
},
{
"kind": "(",
"start": 159,
"end": 160
},
{
"kind": "\"",
"start": 160,
"end": 161
},
{
"kind": "string_content",
"value": "{s}",
"start": 161,
"end": 164
},
{
"kind": "\"",
"start": 164,
"end": 165
},
{
"kind": ",",
"start": 165,
"end": 166
},
{
"kind": ".",
"start": 167,
"end": 168
},
{
"kind": "{",
"start": 168,
"end": 169
},
{
"kind": "identifier",
"value": "greeting",
"start": 169,
"end": 177
},
{
"kind": "}",
"start": 177,
"end": 178
},
{
"kind": ")",
"start": 178,
"end": 179
},
{
"kind": ";",
"start": 179,
"end": 180
},
{
"kind": "const",
"start": 186,
"end": 191
},
{
"kind": "identifier",
"value": "emoji_identifier",
"start": 192,
"end": 208
},
{
"kind": "=",
"start": 209,
"end": 210
},
{
"kind": "\"",
"start": 211,
"end": 212
},
{
"kind": "string_content",
"value": "✅ This is a test string with emojis 🚀🔥",
"start": 212,
"end": 258
},
{
"kind": "escape_sequence",
"value": "\\n",
"start": 258,
"end": 260
},
{
"kind": "\"",
"start": 260,
"end": 261
},
{
"kind": ";",
"start": 261,
"end": 262
},
{
"kind": "try",
"start": 267,
"end": 270
},
{
"kind": "identifier",
"value": "stdout",
"start": 271,
"end": 277
},
{
"kind": ".",
"start": 277,
"end": 278
},
{
"kind": "identifier",
"value": "print",
"start": 278,
"end": 283
},
{
"kind": "(",
"start": 283,
"end": 284
},
{
"kind": "\"",
"start": 284,
"end": 285
},
{
"kind": "string_content",
"value": "{s}",
"start": 285,
"end": 288
},
{
"kind": "\"",
"start": 288,
"end": 289
},
{
"kind": ",",
"start": 289,
"end": 290
},
{
"kind": ".",
"start": 291,
"end": 292
},
{
"kind": "{",
"start": 292,
"end": 293
},
{
"kind": "identifier",
"value": "emoji_identifier",
"start": 293,
"end": 309
},
{
"kind": "}",
"start": 309,
"end": 310
},
{
"kind": ")",
"start": 310,
"end": 311
},
{
"kind": ";",
"start": 311,
"end": 312
},
{
"kind": "}",
"start": 313,
"end": 314
}
]
[
{
"kind": "const",
"start": 0,
"end": 5
},
{
"kind": "identifier",
"value": "std",
"start": 6,
"end": 9
},
{
"kind": "=",
"start": 10,
"end": 11
},
{
"kind": "builtin_identifier",
"value": "@import",
"start": 12,
"end": 19
},
{
"kind": "(",
"start": 19,
"end": 20
},
{
"kind": "\"",
"start": 20,
"end": 21
},
{
"kind": "string_content",
"value": "std",
"start": 21,
"end": 24
},
{
"kind": "\"",
"start": 24,
"end": 25
},
{
"kind": ")",
"start": 25,
"end": 26
},
{
"kind": ";",
"start": 26,
"end": 27
},
{
"kind": "const",
"start": 28,
"end": 33
},
{
"kind": "identifier",
"value": "expect",
"start": 34,
"end": 40
},
{
"kind": "=",
"start": 41,
"end": 42
},
{
"kind": "identifier",
"value": "std",
"start": 43,
"end": 46
},
{
"kind": ".",
"start": 46,
"end": 47
},
{
"kind": "identifier",
"value": "testing",
"start": 47,
"end": 54
},
{
"kind": ".",
"start": 54,
"end": 55
},
{
"kind": "identifier",
"value": "expect",
"start": 55,
"end": 61
},
{
"kind": ";",
"start": 61,
"end": 62
},
{
"kind": "comptime",
"start": 64,
"end": 72
},
{
"kind": "{",
"start": 73,
"end": 74
},
{
"kind": "asm",
"start": 79,
"end": 82
},
{
"kind": "(",
"start": 83,
"end": 84
},
{
"kind": "multiline_string",
"value": "\\\\.global my_func;\n \\\\.type my_func, @function;\n \\\\my_func:\n \\\\ lea (%rdi,%rsi,1),%eax\n \\\\ retq",
"start": 93,
"end": 218
},
{
"kind": ")",
"start": 223,
"end": 224
},
{
"kind": ";",
"start": 224,
"end": 225
},
{
"kind": "}",
"start": 226,
"end": 227
},
{
"kind": "extern",
"start": 229,
"end": 235
},
{
"kind": "fn",
"start": 236,
"end": 238
},
{
"kind": "identifier",
"value": "my_func",
"start": 239,
"end": 246
},
{
"kind": "(",
"start": 246,
"end": 247
},
{
"kind": "identifier",
"value": "a",
"start": 247,
"end": 248
},
{
"kind": ":",
"start": 248,
"end": 249
},
{
"kind": "builtin_type",
"value": "i32",
"start": 250,
"end": 253
},
{
"kind": ",",
"start": 253,
"end": 254
},
{
"kind": "identifier",
"value": "b",
"start": 255,
"end": 256
},
{
"kind": ":",
"start": 256,
"end": 257
},
{
"kind": "builtin_type",
"value": "i32",
"start": 258,
"end": 261
},
{
"kind": ")",
"start": 261,
"end": 262
},
{
"kind": "builtin_type",
"value": "i32",
"start": 263,
"end": 266
},
{
"kind": ";",
"start": 266,
"end": 267
},
{
"kind": "test",
"start": 269,
"end": 273
},
{
"kind": "\"",
"start": 274,
"end": 275
},
{
"kind": "string_content",
"value": "global assembly",
"start": 275,
"end": 290
},
{
"kind": "\"",
"start": 290,
"end": 291
},
{
"kind": "{",
"start": 292,
"end": 293
},
{
"kind": "try",
"start": 298,
"end": 301
},
{
"kind": "identifier",
"value": "expect",
"start": 302,
"end": 308
},
{
"kind": "(",
"start": 308,
"end": 309
},
{
"kind": "identifier",
"value": "my_func",
"start": 309,
"end": 316
},
{
"kind": "(",
"start": 316,
"end": 317
},
{
"kind": "integer",
"value": "12",
"start": 317,
"end": 319
},
{
"kind": ",",
"start": 319,
"end": 320
},
{
"kind": "integer",
"value": "34",
"start": 321,
"end": 323
},
{
"kind": ")",
"start": 323,
"end": 324
},
{
"kind": "==",
"start": 325,
"end": 327
},
{
"kind": "integer",
"value": "46",
"start": 328,
"end": 330
},
{
"kind": ")",
"start": 330,
"end": 331
},
{
"kind": ";",
"start": 331,
"end": 332
},
{
"kind": "}",
"start": 333,
"end": 334
}
]
[
{
"kind": "const",
"start": 0,
"end": 5
},
{
"kind": "identifier",
"value": "std",
"start": 6,
"end": 9
},
{
"kind": "=",
"start": 10,
"end": 11
},
{
"kind": "builtin_identifier",
"value": "@import",
"start": 12,
"end": 19
},
{
"kind": "(",
"start": 19,
"end": 20
},
{
"kind": "\"",
"start": 20,
"end": 21
},
{
"kind": "string_content",
"value": "std",
"start": 21,
"end": 24
},
{
"kind": "\"",
"start": 24,
"end": 25
},
{
"kind": ")",
"start": 25,
"end": 26
},
{
"kind": ";",
"start": 26,
"end": 27
},
{
"kind": "pub",
"start": 29,
"end": 32
},
{
"kind": "fn",
"start": 33,
"end": 35
},
{
"kind": "identifier",
"value": "main",
"start": 36,
"end": 40
},
{
"kind": "(",
"start": 40,
"end": 41
},
{
"kind": ")",
"start": 41,
"end": 42
},
{
"kind": "void",
"start": 43,
"end": 47
},
{
"kind": "{",
"start": 48,
"end": 49
},
{
"kind": "identifier",
"value": "std",
"start": 54,
"end": 57
},
{
"kind": ".",
"start": 57,
"end": 58
},
{
"kind": "identifier",
"value": "debug",
"start": 58,
"end": 63
},
{
"kind": ".",
"start": 63,
"end": 64
},
{
"kind": "identifier",
"value": "print",
"start": 64,
"end": 69
},
{
"kind": "(",
"start": 69,
"end": 70
},
{
"kind": "\"",
"start": 70,
"end": 71
},
{
"kind": "string_content",
"value": "Hello, world!",
"start": 71,
"end": 84
},
{
"kind": "escape_sequence",
"value": "\\n",
"start": 84,
"end": 86
},
{
"kind": "\"",
"start": 86,
"end": 87
},
{
"kind": ",",
"start": 87,
"end": 88
},
{
"kind": ".",
"start": 89,
"end": 90
},
{
"kind": "{",
"start": 90,
"end": 91
},
{
"kind": "}",
"start": 91,
"end": 92
},
{
"kind": ")",
"start": 92,
"end": 93
},
{
"kind": ";",
"start": 93,
"end": 94
},
{
"kind": "}",
"start": 95,
"end": 96
}
]
[
{
"kind": "const",
"start": 0,
"end": 5
},
{
"kind": "@",
"start": 6,
"end": 7
},
{
"kind": "\"",
"start": 7,
"end": 8
},
{
"kind": "string_content",
"value": "identifier with spaces in it",
"start": 8,
"end": 36
},
{
"kind": "\"",
"start": 36,
"end": 37
},
{
"kind": "=",
"start": 38,
"end": 39
},
{
"kind": "integer",
"value": "0xff",
"start": 40,
"end": 44
},
{
"kind": ";",
"start": 44,
"end": 45
},
{
"kind": "const",
"start": 46,
"end": 51
},
{
"kind": "@",
"start": 52,
"end": 53
},
{
"kind": "\"",
"start": 53,
"end": 54
},
{
"kind": "string_content",
"value": "1SmallStep4Man",
"start": 54,
"end": 68
},
{
"kind": "\"",
"start": 68,
"end": 69
},
{
"kind": "=",
"start": 70,
"end": 71
},
{
"kind": "integer",
"value": "112358",
"start": 72,
"end": 78
},
{
"kind": ";",
"start": 78,
"end": 79
},
{
"kind": "const",
"start": 81,
"end": 86
},
{
"kind": "identifier",
"value": "c",
"start": 87,
"end": 88
},
{
"kind": "=",
"start": 89,
"end": 90
},
{
"kind": "builtin_identifier",
"value": "@import",
"start": 91,
"end": 98
},
{
"kind": "(",
"start": 98,
"end": 99
},
{
"kind": "\"",
"start": 99,
"end": 100
},
{
"kind": "string_content",
"value": "std",
"start": 100,
"end": 103
},
{
"kind": "\"",
"start": 103,
"end": 104
},
{
"kind": ")",
"start": 104,
"end": 105
},
{
"kind": ".",
"start": 105,
"end": 106
},
{
"kind": "identifier",
"value": "c",
"start": 106,
"end": 107
},
{
"kind": ";",
"start": 107,
"end": 108
},
{
"kind": "pub",
"start": 109,
"end": 112
},
{
"kind": "extern",
"start": 113,
"end": 119
},
{
"kind": "\"",
"start": 120,
"end": 121
},
{
"kind": "string_content",
"value": "c",
"start": 121,
"end": 122
},
{
"kind": "\"",
"start": 122,
"end": 123
},
{
"kind": "fn",
"start": 124,
"end": 126
},
{
"kind": "@",
"start": 127,
"end": 128
},
{
"kind": "\"",
"start": 128,
"end": 129
},
{
"kind": "string_content",
"value": "error",
"start": 129,
"end": 134
},
{
"kind": "\"",
"start": 134,
"end": 135
},
{
"kind": "(",
"start": 135,
"end": 136
},
{
"kind": ")",
"start": 136,
"end": 137
},
{
"kind": "void",
"start": 138,
"end": 142
},
{
"kind": ";",
"start": 142,
"end": 143
},
{
"kind": "pub",
"start": 144,
"end": 147
},
{
"kind": "extern",
"start": 148,
"end": 154
},
{
"kind": "\"",
"start": 155,
"end": 156
},
{
"kind": "string_content",
"value": "c",
"start": 156,
"end": 157
},
{
"kind": "\"",
"start": 157,
"end": 158
},
{
"kind": "fn",
"start": 159,
"end": 161
},
{
"kind": "@",
"start": 162,
"end": 163
},
{
"kind": "\"",
"start": 163,
"end": 164
},
{
"kind": "string_content",
"value": "fstat$INODE64",
"start": 164,
"end": 177
},
{
"kind": "\"",
"start": 177,
"end": 178
},
{
"kind": "(",
"start": 178,
"end": 179
},
{
"kind": "identifier",
"value": "fd",
"start": 179,
"end": 181
},
{
"kind": ":",
"start": 181,
"end": 182
},
{
"kind": "identifier",
"value": "c",
"start": 183,
"end": 184
},
{
"kind": ".",
"start": 184,
"end": 185
},
{
"kind": "identifier",
"value": "fd_t",
"start": 185,
"end": 189
},
{
"kind": ",",
"start": 189,
"end": 190
},
{
"kind": "identifier",
"value": "buf",
"start": 191,
"end": 194
},
{
"kind": ":",
"start": 194,
"end": 195
},
{
"kind": "*",
"start": 196,
"end": 197
},
{
"kind": "identifier",
"value": "c",
"start": 197,
"end": 198
},
{
"kind": ".",
"start": 198,
"end": 199
},
{
"kind": "identifier",
"value": "Stat",
"start": 199,
"end": 203
},
{
"kind": ")",
"start": 203,
"end": 204
},
{
"kind": "c_int",
"start": 205,
"end": 210
},
{
"kind": ";",
"start": 210,
"end": 211
},
{
"kind": "const",
"start": 213,
"end": 218
},
{
"kind": "identifier",
"value": "Color",
"start": 219,
"end": 224
},
{
"kind": "=",
"start": 225,
"end": 226
},
{
"kind": "enum",
"start": 227,
"end": 231
},
{
"kind": "{",
"start": 232,
"end": 233
},
{
"kind": "identifier",
"value": "red",
"start": 238,
"end": 241
},
{
"kind": ",",
"start": 241,
"end": 242
},
{
"kind": "@",
"start": 247,
"end": 248
},
{
"kind": "\"",
"start": 248,
"end": 249
},
{
"kind": "string_content",
"value": "really red",
"start": 249,
"end": 259
},
{
"kind": "\"",
"start": 259,
"end": 260
},
{
"kind": ",",
"start": 260,
"end": 261
},
{
"kind": "}",
"start": 262,
"end": 263
},
{
"kind": ";",
"start": 263,
"end": 264
},
{
"kind": "const",
"start": 265,
"end": 270
},
{
"kind": "identifier",
"value": "color",
"start": 271,
"end": 276
},
{
"kind": ":",
"start": 276,
"end": 277
},
{
"kind": "identifier",
"value": "Color",
"start": 278,
"end": 283
},
{
"kind": "=",
"start": 284,
"end": 285
},
{
"kind": ".",
"start": 286,
"end": 287
},
{
"kind": "@",
"start": 287,
"end": 288
},
{
"kind": "\"",
"start": 288,
"end": 289
},
{
"kind": "string_content",
"value": "really red",
"start": 289,
"end": 299
},
{
"kind": "\"",
"start": 299,
"end": 300
},
{
"kind": ";",
"start": 300,
"end": 301
}
]
const print = @import("std").debug.print;
pub fn main() void {
var x: i32 = undefined;
x = 1;
print("{d}", .{x});
}
const print = @import("std").debug.print;
pub fn main() void {
// Comments in Zig start with "//" and end at the next LF byte (end of line).
// The line below is a comment and won't be executed.
//print("Hello?", .{});
print("Hello, world!\n", .{}); // another comment
}
const std = @import("std");
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
const greeting = "Hello, 🌍!\n";
try stdout.print("{s}", .{greeting});
const emoji_identifier = "✅ This is a test string with emojis 🚀🔥\n";
try stdout.print("{s}", .{emoji_identifier});
}
const std = @import("std");
const expect = std.testing.expect;
comptime {
asm (
\\.global my_func;
\\.type my_func, @function;
\\my_func:
\\ lea (%rdi,%rsi,1),%eax
\\ retq
);
}
extern fn my_func(a: i32, b: i32) i32;
test "global assembly" {
try expect(my_func(12, 34) == 46);
}
const std = @import("std");
pub fn main() void {
std.debug.print("Hello, world!\n", .{});
}
const @"identifier with spaces in it" = 0xff;
const @"1SmallStep4Man" = 112358;
const c = @import("std").c;
pub extern "c" fn @"error"() void;
pub extern "c" fn @"fstat$INODE64"(fd: c.fd_t, buf: *c.Stat) c_int;
const Color = enum {
red,
@"really red",
};
const color: Color = .@"really red";
import json
from pathlib import Path
import pytest
from zig_codeblocks.formatting import highlight_zig_code, process_markdown
from zig_codeblocks.styling import Color, Style, Theme
SOURCE_DIR = Path(__file__).parent / "sources"
def read_expected_styling(test_name: str) -> str:
expected_style: list[str] = []
for line in json.loads(
(SOURCE_DIR / "formatting_results" / f"{test_name}.json").read_bytes()
):
style, _, content = line.partition(":")
expected_style.append(f"\x1b[{style}m{content}")
return "".join(expected_style)
@pytest.mark.parametrize("only_code", [True, False])
@pytest.mark.parametrize(
"test_name",
[
"assign_undefined",
"comments",
"emoji",
"global_assembly",
"hello_again",
"identifiers",
],
)
def test_zig_highlighting(test_name: str, only_code: bool) -> None:
source = (
(SOURCE_DIR / "zig_inputs" / f"{test_name}.zig")
.read_bytes()
.replace(b"\r\n", b"\n")
.decode()
)
source = f"```zig\n{source}```"
expected_styling = f"```ansi\n{read_expected_styling(test_name)}\n```"
assert process_markdown(source, only_code=only_code) == expected_styling
assert process_markdown(source.encode(), only_code=only_code) == expected_styling
@pytest.mark.parametrize(
"theme",
[
{"identifiers": Style(Color.RED)},
{"identifiers": Style(Color.RED), "strings": Style(Color.GREEN)},
],
)
def test_highlighting_backtrack_identifier_case(theme: Theme) -> None:
source = 'const @"identifier with spaces in it" = 0xff;'
expected_highlighting = (
'const @\033[31m"identifier with spaces in it" \033[0m= 0xff;'
)
assert highlight_zig_code(source, theme) == expected_highlighting
def test_reset_optimization() -> None:
source = " const x = 0xff;"
theme = Theme(
identifiers=Style(Color.RED),
keywords=Style(Color.BLUE, underline=True),
)
expected_highlighting = " \033[34;4mconst\033[0m \033[31mx\033[0m = 0xff;"
assert highlight_zig_code(source, theme) == expected_highlighting
+14
-0

@@ -8,2 +8,15 @@ # Changelog

## [v0.2.1] - 2025-02-11
### Changed
- Optimized reset code insertion
- The [`Theme`](https://github.com/trag1c/zig-codeblocks/blob/b6d25f780ad260be4ff90ed0657ee08d69cf2e86/README.md#theme)
`dataclass` was turned into a `TypedDict`
### Fixed
- A leading reset code is no longer inserted
- `process_markdown(only_code=False)` works as intended now
- String identifiers are now properly highlighted when string syntax
highlighting is disabled
## [v0.2.0] - 2025-02-09

@@ -46,1 +59,2 @@

[v0.2.0]: https://github.com/trag1c/ixia/compare/v0.1.2...v0.2.0
[v0.2.1]: https://github.com/trag1c/ixia/compare/v0.2.0...v0.2.1
+27
-19
Metadata-Version: 2.4
Name: zig-codeblocks
Version: 0.2.0
Version: 0.2.1
Summary: Zig ANSI syntax highlighting library

@@ -170,23 +170,32 @@ Project-URL: repository, https://github.com/trag1c/zig-codeblocks

```py
@dataclass(slots=True, kw_only=True)
class Theme:
builtin_identifiers: Style | None = None
calls: Style | None = None
comments: Style | None = None
identifiers: Style | None = None
keywords: Style | None = None
numeric: Style | None = None
strings: Style | None = None
primitive_values: Style | None = None
types: Style | None = None
class Theme(TypedDict, total=False):
builtin_identifiers: Style
calls: Style
comments: Style
identifiers: Style
keywords: Style
numeric: Style
strings: Style
primitive_values: Style
types: Style
```
A theme for syntax highlighting Zig code.
Each field is optional and can be provided a [`Style`](#style) to apply to the
A theme dict for syntax highlighting Zig code.
Each key is optional and can be provided a [`Style`](#style) to apply to the
corresponding token type.
Can be instantiated with a dict literal or with a `Theme` call:
```py
from zig_codeblocks import Color, Style, Theme
#### `Theme.copy`
```py
def copy(self) -> Theme
theme_foo = Theme(
numeric=Style(Color.BLUE),
strings=Style(Color.GREEN),
)
theme_bar: Theme = {
"numeric": Style(Color.BLUE),
"strings": Style(Color.GREEN),
}
assert theme_foo == theme_bar
```
Returns a copy of the theme.

@@ -201,3 +210,2 @@

comments=Style(Color.GRAY),
identifiers=None,
keywords=Style(Color.MAGENTA),

@@ -204,0 +212,0 @@ numeric=Style(Color.CYAN),

[project]
name = "zig-codeblocks"
version = "0.2.0"
version = "0.2.1"
description = "Zig ANSI syntax highlighting library"

@@ -5,0 +5,0 @@ readme = "README.md"

+26
-18

@@ -157,23 +157,32 @@ [![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json)](https://github.com/astral-sh/uv)

```py
@dataclass(slots=True, kw_only=True)
class Theme:
builtin_identifiers: Style | None = None
calls: Style | None = None
comments: Style | None = None
identifiers: Style | None = None
keywords: Style | None = None
numeric: Style | None = None
strings: Style | None = None
primitive_values: Style | None = None
types: Style | None = None
class Theme(TypedDict, total=False):
builtin_identifiers: Style
calls: Style
comments: Style
identifiers: Style
keywords: Style
numeric: Style
strings: Style
primitive_values: Style
types: Style
```
A theme for syntax highlighting Zig code.
Each field is optional and can be provided a [`Style`](#style) to apply to the
A theme dict for syntax highlighting Zig code.
Each key is optional and can be provided a [`Style`](#style) to apply to the
corresponding token type.
Can be instantiated with a dict literal or with a `Theme` call:
```py
from zig_codeblocks import Color, Style, Theme
#### `Theme.copy`
```py
def copy(self) -> Theme
theme_foo = Theme(
numeric=Style(Color.BLUE),
strings=Style(Color.GREEN),
)
theme_bar: Theme = {
"numeric": Style(Color.BLUE),
"strings": Style(Color.GREEN),
}
assert theme_foo == theme_bar
```
Returns a copy of the theme.

@@ -188,3 +197,2 @@

comments=Style(Color.GRAY),
identifiers=None,
keywords=Style(Color.MAGENTA),

@@ -191,0 +199,0 @@ numeric=Style(Color.CYAN),

from __future__ import annotations
import re
from itertools import tee
from typing import TYPE_CHECKING, TypeVar
from typing import TYPE_CHECKING, TypeVar, cast
from zig_codeblocks import consts
from zig_codeblocks.parsing import extract_codeblocks, tokenize_zig
from zig_codeblocks.styling import Reset, Style, Theme
from zig_codeblocks.styling import RESET, Style, Theme

@@ -15,3 +16,3 @@ if TYPE_CHECKING:

Body = list[str | Style | Reset]
Body = list[str | Style]
T = TypeVar("T")

@@ -22,8 +23,8 @@

for options, style in (
(consts.IDENTIFIERS, theme.identifiers),
(consts.KEYWORDS, theme.keywords),
(consts.NUMERIC_LITERALS, theme.numeric),
(consts.PRIMITIVE_VALUES, theme.primitive_values),
(consts.STRINGLIKE, theme.strings),
(consts.TYPES, theme.types),
(consts.IDENTIFIERS, theme.get("identifiers")),
(consts.KEYWORDS, theme.get("keywords")),
(consts.NUMERIC_LITERALS, theme.get("numeric")),
(consts.PRIMITIVE_VALUES, theme.get("primitive_values")),
(consts.STRINGLIKE, theme.get("strings")),
(consts.TYPES, theme.get("types")),
):

@@ -33,5 +34,5 @@ if kind in options:

if kind == "comment":
return theme.comments
return theme.get("comments")
if kind == "builtin_identifier":
return theme.builtin_identifiers
return theme.get("builtin_identifiers")
return None

@@ -44,4 +45,7 @@

def _last_applied_style(body: Body) -> Style | Reset | None:
return next((item for item in body[::-1] if not isinstance(item, str)), None)
def _last_applied_style(body: Body) -> Style | str | None:
return next(
(item for item in body[::-1] if not isinstance(item, str) or item == RESET),
None,
)

@@ -52,10 +56,13 @@

# which is an identifier, so should have no string highlighting
if (
token.kind == '"'
and _last_applied_style(body) == theme.strings
and len(body) > 3
and body[-4] == "@"
):
if theme.identifiers:
body[-3] = theme.identifiers
identifier_style = theme.get("identifiers")
string_style = theme.get("strings")
if not ((identifier_style or string_style) and token.kind == '"'):
return
if not string_style:
if len(body) > 2 and body[-3] == "@":
body.insert(-2, cast(Style, identifier_style))
return
if _last_applied_style(body) == string_style and len(body) > 3 and body[-4] == "@":
if identifier_style:
body[-3] = identifier_style
else:

@@ -76,4 +83,6 @@ del body[-3]

match filler.isspace(), _last_applied_style(body):
case (False, _) | (True, Style(underline=True) | Style(bold=True)):
body.append(Reset.FULL)
case (_, None):
pass
case (True, Style(underline=True) | Style(bold=True)) | (False, Style()):
body.append(RESET)
body.append(filler.decode())

@@ -88,3 +97,3 @@ pointer = token.byte_range.start

style = (
theme.calls
theme.get("calls")
if token.kind == "identifier" and _peek(tokens).kind == "("

@@ -94,4 +103,4 @@ else _get_style(token.kind, theme)

if style is None:
if _last_applied_style(body) is not Reset.FULL:
body.append(Reset.FULL)
if _last_applied_style(body) not in (RESET, None):
body.append(RESET)
elif _last_applied_style(body) is not style:

@@ -141,5 +150,8 @@ body.append(style)

for codeblock in zig_codeblocks:
original_source = f"```zig\n{codeblock}```"
highlighted_source = f"```ansi\n{highlight_zig_code(codeblock, theme)}\n```"
source = source.replace(original_source, highlighted_source)
source = re.sub(
f"```zig\n{re.escape(codeblock)}(?:\r?\n)*```",
highlighted_source.replace("\\", r"\\"),
source,
)
return source
from __future__ import annotations
from dataclasses import KW_ONLY, dataclass, replace
from dataclasses import KW_ONLY, dataclass
from enum import Enum
from itertools import compress
from typing import TypedDict

@@ -12,11 +13,5 @@

class Reset(Enum):
FULL = "0"
BOLD = "21"
UNDERLINE = "24"
RESET = _to_sgr("0")
def __str__(self) -> str:
return _to_sgr(self.value)
class Color(Enum):

@@ -56,18 +51,13 @@ """

@dataclass(slots=True, kw_only=True)
class Theme:
class Theme(TypedDict, total=False):
"""A theme for syntax highlighting Zig code."""
builtin_identifiers: Style | None = None
calls: Style | None = None
comments: Style | None = None
identifiers: Style | None = None
keywords: Style | None = None
numeric: Style | None = None
strings: Style | None = None
primitive_values: Style | None = None
types: Style | None = None
def copy(self) -> Theme:
"""Create a copy of the `Theme`."""
return replace(self)
builtin_identifiers: Style
calls: Style
comments: Style
identifiers: Style
keywords: Style
numeric: Style
strings: Style
primitive_values: Style
types: Style

@@ -0,1 +1,2 @@

import json
from pathlib import Path

@@ -5,3 +6,3 @@

from zig_codeblocks.parsing import CodeBlock, extract_codeblocks
from zig_codeblocks.parsing import CodeBlock, Token, extract_codeblocks, tokenize_zig

@@ -46,1 +47,34 @@ SOURCE_DIR = Path(__file__).parent / "sources"

assert codeblocks[0] == CodeBlock(*expected)
def read_expected_tokens(test_name: str) -> list[Token]:
return [
Token(
kind=t["kind"],
value=(t.get("value") or t["kind"]).encode(),
byte_range=range(t["start"], t["end"]),
)
for t in json.loads(
(SOURCE_DIR / "parsing_results" / f"{test_name}.json").read_bytes()
)
]
@pytest.mark.parametrize(
"test_name",
[
"assign_undefined",
"comments",
"emoji",
"global_assembly",
"hello_again",
"identifiers",
],
)
def test_zig_parser(test_name: str) -> None:
source = (
(SOURCE_DIR / "zig_inputs" / f"{test_name}.zig")
.read_bytes()
.replace(b"\r\n", b"\n")
)
assert list(tokenize_zig(source)) == read_expected_tokens(test_name)
import pytest
from zig_codeblocks.styling import Color, Reset, Style, Theme
from zig_codeblocks.styling import Color, Style

@@ -24,20 +24,1 @@

assert str(style) == expected_sgr
@pytest.mark.parametrize(
("reset_type", "expected_sgr"),
[("FULL", "\033[0m"), ("BOLD", "\033[21m"), ("UNDERLINE", "\033[24m")],
)
def test_reset(reset_type: str, expected_sgr: str) -> None:
assert str(Reset[reset_type]) == expected_sgr
def test_theme_copy() -> None:
original = Theme(calls=Style(Color.BLUE))
copy = original.copy()
assert original == copy
assert original is not copy
copy.calls = None
assert original != copy
assert original.calls is not None

@@ -316,3 +316,3 @@ version = 1

name = "zig-codeblocks"
version = "0.2.0"
version = "0.2.1"
source = { editable = "." }

@@ -319,0 +319,0 @@ dependencies = [