-
Notifications
You must be signed in to change notification settings - Fork 728
Add Zig highlighting #925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ibrahim (ibrahimalizade)
wants to merge
5
commits into
microsoft:main
Choose a base branch
from
ibrahimalizade:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+241
−0
Open
Add Zig highlighting #925
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5722710
Add Zig highlighting
ibrahimalizade 624236a
Add Zig highlight test file
ibrahimalizade 08e8557
Move file to right folder and rename it
ibrahimalizade 089c34d
Move file to right place
ibrahimalizade afbaf4d
Merge branch 'microsoft:main' into main
ibrahimalizade File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| // Single-line comment | ||
| /// Documentation comment. | ||
|
|
||
| // Imports | ||
| const std = @import("std"); | ||
| const builtin = @import("builtin"); | ||
|
|
||
| // Constants and variables | ||
| const answer: i32 = 42; | ||
| const pi = 3.14159; | ||
| var counter: usize = 0; | ||
| pub const name = "Zig"; | ||
|
|
||
| // Integer literals | ||
| const decimal = 123; | ||
| const binary = 0b1010_0101; | ||
| const octal = 0o755; | ||
| const hex = 0xDEAD_BEEF; | ||
| const separated = 1_000_000; | ||
|
|
||
| // Floating-point literals | ||
| const float = 3.14; | ||
| const exponent = 1.5e-3; | ||
| const hex_float = 0x1.8p1; | ||
|
|
||
| // Character and string literals | ||
| const char = 'a'; | ||
| const newline = '\n'; | ||
| const unicode = '\u{1F600}'; | ||
| const string = "escapes: \" \\ \n \t"; | ||
|
|
||
| // Multiline string | ||
| const multiline = | ||
| \\first line | ||
| \\second line | ||
| ; | ||
|
|
||
| // Identifiers | ||
| const @"quoted identifier" = 123; | ||
| const snake_case = true; | ||
|
|
||
| // Operators | ||
| const arithmetic = 1 + 2 * 3 - 4 / 2 % 2; | ||
| const comparison = a == b and c != d or e >= f; | ||
| const bitwise = x & y | z ^ w; | ||
| const shifts = value << 2 >> 1; | ||
| const wrapping = a +% b; | ||
| const saturating = a +| b; | ||
|
|
||
| // Struct | ||
| const Point = struct { | ||
| x: i32, | ||
| y: i32, | ||
|
|
||
| pub fn init(x: i32, y: i32) Point { | ||
| return .{ .x = x, .y = y }; | ||
| } | ||
| }; | ||
|
|
||
| const origin = Point{ | ||
| .x = 0, | ||
| .y = 0, | ||
| }; | ||
|
|
||
| // Enum | ||
| const Color = enum { | ||
| red, | ||
| green, | ||
| blue, | ||
| }; | ||
|
|
||
| // Tagged union | ||
| const Value = union(enum) { | ||
| integer: i64, | ||
| float: f64, | ||
| boolean: bool, | ||
| }; | ||
|
|
||
| // Arrays, slices, tuples | ||
| const array = [_]u8{ 1, 2, 3, 4 }; | ||
| const slice = array[1..3]; | ||
| const tuple = .{ 42, true, "hello" }; | ||
|
|
||
| // Pointers | ||
| var value: i32 = 42; | ||
| const ptr: *i32 = &value; | ||
| const many_ptr: [*]i32 = undefined; | ||
| const sentinel_ptr: [*:0]const u8 = undefined; | ||
|
|
||
| // Functions | ||
| fn add(a: i32, b: i32) i32 { | ||
| return a + b; | ||
| } | ||
|
|
||
| fn generic(comptime T: type, value: T) T { | ||
| return value; | ||
| } | ||
|
|
||
| // If / else | ||
| fn classify(x: i32) i32 { | ||
| if (x > 10) { | ||
| return 1; | ||
| } else if (x == 10) { | ||
| return 0; | ||
| } else { | ||
| return -1; | ||
| } | ||
| } | ||
|
|
||
| // While / for / labels | ||
| fn loops() void { | ||
| var i: usize = 0; | ||
|
|
||
| while (i < 10) : (i += 1) { | ||
| if (i == 5) continue; | ||
| if (i == 8) break; | ||
| } | ||
|
|
||
| for (array, 0..) |item, index| { | ||
| _ = item; | ||
| _ = index; | ||
| } | ||
|
|
||
| outer: for (array) |item| { | ||
| if (item == 3) break :outer; | ||
| } | ||
| } | ||
|
|
||
| // Switch | ||
| fn describe(value: Value) void { | ||
| switch (value) { | ||
| .integer => |x| _ = x, | ||
| .float => |x| _ = x, | ||
| .boolean => |x| _ = x, | ||
| } | ||
| } | ||
|
|
||
| // Optionals | ||
| fn optional(value: ?i32) i32 { | ||
| return value orelse 0; | ||
| } | ||
|
|
||
| // Errors | ||
| const MyError = error{ | ||
| NotFound, | ||
| InvalidValue, | ||
| }; | ||
|
|
||
| fn mightFail() MyError!i32 { | ||
| return error.NotFound; | ||
| } | ||
|
|
||
| fn handleError() !void { | ||
| const result = try mightFail(); | ||
| _ = result; | ||
|
|
||
| _ = mightFail() catch 42; | ||
| } | ||
|
|
||
| // Defer | ||
| fn cleanup() void { | ||
| defer std.debug.print("done\n", .{}); | ||
| } | ||
|
|
||
| // Comptime / builtins | ||
| const type_of_value = @TypeOf(value); | ||
| const size_of_point = @sizeOf(Point); | ||
| const alignment = @alignOf(Point); | ||
| const field_name = @tagName(Color.red); | ||
|
|
||
| // Builtin-heavy expression | ||
| const converted = @intCast(@as(u64, 42)); | ||
| const bits = @bitCast(@as(u32, 0x3F800000)); | ||
|
|
||
| // Extern / export / calling convention | ||
| extern fn external_function(x: i32) i32; | ||
|
|
||
| export fn exported_function(x: i32) callconv(.c) i32 { | ||
| return x; | ||
| } | ||
|
|
||
| // Tests | ||
| test "basic arithmetic" { | ||
| try std.testing.expect(add(1, 2) == 3); | ||
| } | ||
|
|
||
| test "optional" { | ||
| try std.testing.expect(optional(null) == 0); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #[display_name = "Zig"] | ||
| #[path = "**/*.zig"] | ||
| #[path = "**/*.zon"] | ||
| pub fn zig() { | ||
| until /$/ { | ||
| yield other; | ||
|
|
||
| if /\/\/.*/ { | ||
| yield comment; | ||
| } else if /\\\\.*/ { | ||
| yield string; | ||
| } else if /"/ { | ||
| loop { | ||
| if /\\./ { | ||
| } else if /"/ { | ||
| yield string; | ||
| break; | ||
| } else if /$/ { | ||
| yield string; | ||
| break; | ||
| } else if /./ {} | ||
| } | ||
| } else if /'(\\.|[^'\\])'/ { | ||
| yield string; | ||
| } else if /@([a-zA-Z_]\w*)\s*\(/ { | ||
| yield $1 as method; | ||
| } else if /@[a-zA-Z_]\w*/ { | ||
| yield storage.annotation; | ||
| } else if /(?:break|continue|return|try|catch|orelse|if|else|switch|while|for|suspend|resume|async|await|defer|errdefer|unreachable)\>/ { | ||
| yield keyword.control; | ||
| } else if /(?:const|var|fn|pub|usingnamespace|struct|enum|union|error|opaque|inline|noinline|comptime|callconv|volatile|allowzero|align|linksection|threadlocal|anytype)\>/ { | ||
| yield keyword.other; | ||
| } else if /(?:true|false|null|undefined|nil)\>/ { | ||
| yield constant.language; | ||
| } else if /(?:i\d+|u\d+|f16|f32|f64|f128|isize|usize|bool|void|noreturn|type|anyerror)\>/ { | ||
| yield storage.type; | ||
| } else if /-?(?:0x[0-9a-fA-F_]+|0b[01_]+|0o[0-7_]+|\d[\d_]*(?:\.\d[\d_]*)?)\>/ { | ||
| if /\w+/ { | ||
| } else { | ||
| yield constant.numeric; | ||
| } | ||
| } else if /\.[a-zA-Z_]\w*/ { | ||
| yield variable; // Catches ZON field keys like .name, .version | ||
| } else if /(\w+)\s*\(/ { | ||
| yield $1 as method; | ||
| } else if /\w+/ { | ||
| // Gobble word chars | ||
| } | ||
|
|
||
| yield other; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be best to not highlight terms that are ambiguous. This one is, isn't it?