Skip to content

Commit

Permalink
Added suppprt for Zig (#2019)
Browse files Browse the repository at this point in the history
This adds support for the Zig language.
https://ziglang.org/
  • Loading branch information
RunDevelopment committed Aug 29, 2019
1 parent d03d19b commit a7cf56b
Show file tree
Hide file tree
Showing 13 changed files with 1,441 additions and 1 deletion.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,10 @@
"title": "YAML",
"alias": "yml",
"owner": "hason"
},
"zig": {
"title": "Zig",
"owner": "RunDevelopment"
}
},
"plugins": {
Expand Down
97 changes: 97 additions & 0 deletions components/prism-zig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
(function (Prism) {

var keyword = /\b(?:align|allowzero|and|asm|async|await|break|cancel|catch|comptime|const|continue|defer|else|enum|errdefer|error|export|extern|fn|for|if|inline|linksection|nakedcc|noalias|null|or|orelse|packed|promise|pub|resume|return|stdcallcc|struct|suspend|switch|test|threadlocal|try|undefined|union|unreachable|usingnamespace|var|volatile|while)\b/;

var IDENTIFIER = '\\b(?!' + keyword.source + ')(?!\\d)\\w+\\b';
var ALIGN = /align\s*\((?:[^()]|\([^()]*\))*\)/.source;
var PREFIX_TYPE_OP = /(?:\?|\bpromise->|(?:\[[^[\]]*\]|\*(?!\*)|\*\*)(?:\s*<ALIGN>|\s*const\b|\s*volatile\b|\s*allowzero\b)*)/.source.replace(/<ALIGN>/g, ALIGN);
var SUFFIX_EXPR = /(?:\bpromise\b|(?:\berror\.)?<ID>(?:\.<ID>)*(?!\s+<ID>))/.source.replace(/<ID>/g, IDENTIFIER);
var TYPE = '(?!\\s)(?:!?\\s*(?:' + PREFIX_TYPE_OP + '\\s*)*' + SUFFIX_EXPR + ')+';

/*
* A simplified grammar for Zig compile time type literals:
*
* TypeExpr = ( "!"? PREFIX_TYPE_OP* SUFFIX_EXPR )+
*
* SUFFIX_EXPR = ( \b "promise" \b | ( \b "error" "." )? IDENTIFIER ( "." IDENTIFIER )* (?! \s+ IDENTIFIER ) )
*
* PREFIX_TYPE_OP = "?"
* | \b "promise" "->"
* | ( "[" [^\[\]]* "]" | "*" | "**" ) ( ALIGN | "const" \b | "volatile" \b | "allowzero" \b )*
*
* ALIGN = "align" "(" ( [^()] | "(" [^()]* ")" )* ")"
*
* IDENTIFIER = \b (?! KEYWORD ) [a-zA-Z_] \w* \b
*
*/

Prism.languages.zig = {
'comment': [
{
pattern: /\/{3}.*/,
alias: 'doc-comment'
},
/\/{2}.*/
],
'string': [
{
// "string" and c"string"
pattern: /(^|[^\\@])c?"(?:[^"\\\r\n]|\\.)*"/,
lookbehind: true,
greedy: true
},
{
// multiline strings and c-strings
pattern: /([\r\n])([ \t]+c?\\{2}).*(?:(?:\r\n?|\n)\2.*)*/,
lookbehind: true,
greedy: true
},
{
// characters 'a', '\n', '\xFF', '\u{10FFFF}'
pattern: /(^|[^\\])'(?:[^'\\\r\n]|\\(?:.|x[a-fA-F\d]{2}|u\{[a-fA-F\d]{1,6}\}))'/,
lookbehind: true,
greedy: true
}
],
'builtin': /\B@(?!\d)\w+(?=\s*\()/,
'label': {
pattern: /(\b(?:break|continue)\s*:\s*)\w+\b|\b(?!\d)\w+\b(?=\s*:\s*(?:\{|while\b))/,
lookbehind: true
},
'class-name': [
// const Foo = struct {};
/\b(?!\d)\w+(?=\s*=\s*(?:(?:extern|packed)\s+)?(?:enum|struct|union)\s*[({])/,
{
// const x: i32 = 9;
// var x: Bar;
// fn foo(x: bool, y: f32) void {}
pattern: RegExp(/(:\s*)<TYPE>(?=\s*(?:<ALIGN>\s*)?[=;,)])|<TYPE>(?=\s*(?:<ALIGN>\s*)?\{)/.source.replace(/<TYPE>/g, TYPE).replace(/<ALIGN>/g, ALIGN)),
lookbehind: true,
inside: null // see below
},
{
// extern fn foo(x: f64) f64; (optional alignment)
pattern: RegExp(/(\)\s*)<TYPE>(?=\s*(?:<ALIGN>\s*)?;)/.source.replace(/<TYPE>/g, TYPE).replace(/<ALIGN>/g, ALIGN)),
lookbehind: true,
inside: null // see below
}
],
'builtin-types': {
pattern: /\b(?:anyerror|bool|c_u?(?:short|int|long|longlong)|c_longdouble|c_void|comptime_(?:float|int)|[iu](?:8|16|32|64|128|size)|f(?:16|32|64|128)|noreturn|type|void)\b/,
alias: 'keyword'
},
'keyword': keyword,
'function': /\b(?!\d)\w+(?=\s*\()/,
'number': /\b(?:0b[01]+|0o[0-7]+|0x[a-fA-F\d]+\.?[a-fA-F\d]*(?:[pP][+-]?[a-fA-F\d]+)?|\d+\.?\d*(?:[eE][+-]?\d+)?)\b/,
'boolean': /\b(?:false|true)\b/,
'operator': /\.[*?]|\.{2,3}|[-=]>|\*\*|\+\+|\|\||(?:<<|>>|[-+*]%|[-+*/%^&|<>!=])=?|[?~]/,
'punctuation': /[.:,;(){}[\]]/
};

Prism.languages.zig['class-name'].forEach(function (obj) {
if (obj.inside === null) {
obj.inside = Prism.languages.zig;
}
});

}(Prism));
1 change: 1 addition & 0 deletions components/prism-zig.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions examples/prism-zig.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<h2>Full example</h2>
<pre><code>const std = @import("std");

pub fn main() !void {
// If this program is run without stdout attached, exit with an error.
const stdout_file = try std.io.getStdOut();
// If this program encounters pipe failure when printing to stdout, exit
// with an error.
try stdout_file.write("Hello, world!\n");
}

const warn = @import("std").debug.warn;

pub fn main() void {
warn("Hello, world!\n");
}

const assert = @import("std").debug.assert;

test "comments" {
// Comments in Zig start with "//" and end at the next LF byte (end of line).
// The below line is a comment, and won't be executed.

//assert(false);

const x = true; // another comment
assert(x);
}

/// A structure for storing a timestamp, with nanosecond precision (this is a
/// multiline doc comment).
const Timestamp = struct {
/// The number of seconds since the epoch (this is also a doc comment).
seconds: i64, // signed so we can represent pre-1970 (not a doc comment)
/// The number of nanoseconds past the second (doc comment again).
nanos: u32,

/// Returns a `Timestamp` struct representing the Unix epoch; that is, the
/// moment of 1970 Jan 1 00:00:00 UTC (this is a doc comment too).
pub fn unixEpoch() Timestamp {
return Timestamp{
.seconds = 0,
.nanos = 0,
};
}
};</code></pre>
75 changes: 75 additions & 0 deletions tests/languages/zig/builtin-types_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
i8
u8
i16
u16
i32
u32
i64
u64
i128
u128
isize
usize
c_short
c_ushort
c_int
c_uint
c_long
c_ulong
c_longlong
c_ulonglong
c_longdouble
c_void
f16
f32
f64
f128
bool
void
noreturn
type
anyerror
comptime_int
comptime_float

----------------------------------------------------

[
["builtin-types", "i8"],
["builtin-types", "u8"],
["builtin-types", "i16"],
["builtin-types", "u16"],
["builtin-types", "i32"],
["builtin-types", "u32"],
["builtin-types", "i64"],
["builtin-types", "u64"],
["builtin-types", "i128"],
["builtin-types", "u128"],
["builtin-types", "isize"],
["builtin-types", "usize"],
["builtin-types", "c_short"],
["builtin-types", "c_ushort"],
["builtin-types", "c_int"],
["builtin-types", "c_uint"],
["builtin-types", "c_long"],
["builtin-types", "c_ulong"],
["builtin-types", "c_longlong"],
["builtin-types", "c_ulonglong"],
["builtin-types", "c_longdouble"],
["builtin-types", "c_void"],
["builtin-types", "f16"],
["builtin-types", "f32"],
["builtin-types", "f64"],
["builtin-types", "f128"],
["builtin-types", "bool"],
["builtin-types", "void"],
["builtin-types", "noreturn"],
["builtin-types", "type"],
["builtin-types", "anyerror"],
["builtin-types", "comptime_int"],
["builtin-types", "comptime_float"]
]

----------------------------------------------------

Checks for builtin types.
Loading

0 comments on commit a7cf56b

Please sign in to comment.