From 09a0e2ba1bf44036ff86a8e3f87f4a1ddf1d93f5 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Tue, 5 Oct 2021 21:27:49 +0200 Subject: [PATCH] Zig: Fixed module comments and astral chars (#3129) --- components/prism-zig.js | 4 +- components/prism-zig.min.js | 2 +- tests/languages/zig/comment_feature.test | 21 +++ tests/languages/zig/string_feature.test | 160 +++++++++++++++++++++++ 4 files changed, 184 insertions(+), 3 deletions(-) create mode 100644 tests/languages/zig/comment_feature.test create mode 100644 tests/languages/zig/string_feature.test diff --git a/components/prism-zig.js b/components/prism-zig.js index 6517294f7c..e7bf61be88 100644 --- a/components/prism-zig.js +++ b/components/prism-zig.js @@ -32,7 +32,7 @@ Prism.languages.zig = { 'comment': [ { - pattern: /\/{3}.*/, + pattern: /\/\/[/!].*/, alias: 'doc-comment' }, /\/{2}.*/ @@ -52,7 +52,7 @@ }, { // characters 'a', '\n', '\xFF', '\u{10FFFF}' - pattern: /(^|[^\\])'(?:[^'\\\r\n]|\\(?:.|x[a-fA-F\d]{2}|u\{[a-fA-F\d]{1,6}\}))'/, + pattern: /(^|[^\\])'(?:[^'\\\r\n]|[\uD800-\uDFFF]{2}|\\(?:.|x[a-fA-F\d]{2}|u\{[a-fA-F\d]{1,6}\}))'/, lookbehind: true, greedy: true } diff --git a/components/prism-zig.min.js b/components/prism-zig.min.js index b03a228f8a..98cb6fea37 100644 --- a/components/prism-zig.min.js +++ b/components/prism-zig.min.js @@ -1 +1 @@ -!function(n){function e(e){return function(){return e}}var r=/\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/,a="\\b(?!"+r.source+")(?!\\d)\\w+\\b",o="align\\s*\\((?:[^()]|\\([^()]*\\))*\\)",s="(?!\\s)(?:!?\\s*(?:"+"(?:\\?|\\bpromise->|(?:\\[[^[\\]]*\\]|\\*(?!\\*)|\\*\\*)(?:\\s*|\\s*const\\b|\\s*volatile\\b|\\s*allowzero\\b)*)".replace(//g,e(o))+"\\s*)*"+"(?:\\bpromise\\b|(?:\\berror\\.)?(?:\\.)*(?!\\s+))".replace(//g,e(a))+")+";n.languages.zig={comment:[{pattern:/\/{3}.*/,alias:"doc-comment"},/\/{2}.*/],string:[{pattern:/(^|[^\\@])c?"(?:[^"\\\r\n]|\\.)*"/,lookbehind:!0,greedy:!0},{pattern:/([\r\n])([ \t]+c?\\{2}).*(?:(?:\r\n?|\n)\2.*)*/,lookbehind:!0,greedy:!0},{pattern:/(^|[^\\])'(?:[^'\\\r\n]|\\(?:.|x[a-fA-F\d]{2}|u\{[a-fA-F\d]{1,6}\}))'/,lookbehind:!0,greedy:!0}],builtin:/\B@(?!\d)\w+(?=\s*\()/,label:{pattern:/(\b(?:break|continue)\s*:\s*)\w+\b|\b(?!\d)\w+\b(?=\s*:\s*(?:\{|while\b))/,lookbehind:!0},"class-name":[/\b(?!\d)\w+(?=\s*=\s*(?:(?:extern|packed)\s+)?(?:enum|struct|union)\s*[({])/,{pattern:RegExp("(:\\s*)(?=\\s*(?:\\s*)?[=;,)])|(?=\\s*(?:\\s*)?\\{)".replace(//g,e(s)).replace(//g,e(o))),lookbehind:!0,inside:null},{pattern:RegExp("(\\)\\s*)(?=\\s*(?:\\s*)?;)".replace(//g,e(s)).replace(//g,e(o))),lookbehind:!0,inside:null}],"builtin-types":{pattern:/\b(?:anyerror|bool|c_u?(?:int|long|longlong|short)|c_longdouble|c_void|comptime_(?:float|int)|f(?:16|32|64|128)|[iu](?:8|16|32|64|128|size)|noreturn|type|void)\b/,alias:"keyword"},keyword:r,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:/[.:,;(){}[\]]/},n.languages.zig["class-name"].forEach(function(e){null===e.inside&&(e.inside=n.languages.zig)})}(Prism); \ No newline at end of file +!function(n){function e(e){return function(){return e}}var r=/\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/,a="\\b(?!"+r.source+")(?!\\d)\\w+\\b",o="align\\s*\\((?:[^()]|\\([^()]*\\))*\\)",s="(?!\\s)(?:!?\\s*(?:"+"(?:\\?|\\bpromise->|(?:\\[[^[\\]]*\\]|\\*(?!\\*)|\\*\\*)(?:\\s*|\\s*const\\b|\\s*volatile\\b|\\s*allowzero\\b)*)".replace(//g,e(o))+"\\s*)*"+"(?:\\bpromise\\b|(?:\\berror\\.)?(?:\\.)*(?!\\s+))".replace(//g,e(a))+")+";n.languages.zig={comment:[{pattern:/\/\/[/!].*/,alias:"doc-comment"},/\/{2}.*/],string:[{pattern:/(^|[^\\@])c?"(?:[^"\\\r\n]|\\.)*"/,lookbehind:!0,greedy:!0},{pattern:/([\r\n])([ \t]+c?\\{2}).*(?:(?:\r\n?|\n)\2.*)*/,lookbehind:!0,greedy:!0},{pattern:/(^|[^\\])'(?:[^'\\\r\n]|[\uD800-\uDFFF]{2}|\\(?:.|x[a-fA-F\d]{2}|u\{[a-fA-F\d]{1,6}\}))'/,lookbehind:!0,greedy:!0}],builtin:/\B@(?!\d)\w+(?=\s*\()/,label:{pattern:/(\b(?:break|continue)\s*:\s*)\w+\b|\b(?!\d)\w+\b(?=\s*:\s*(?:\{|while\b))/,lookbehind:!0},"class-name":[/\b(?!\d)\w+(?=\s*=\s*(?:(?:extern|packed)\s+)?(?:enum|struct|union)\s*[({])/,{pattern:RegExp("(:\\s*)(?=\\s*(?:\\s*)?[=;,)])|(?=\\s*(?:\\s*)?\\{)".replace(//g,e(s)).replace(//g,e(o))),lookbehind:!0,inside:null},{pattern:RegExp("(\\)\\s*)(?=\\s*(?:\\s*)?;)".replace(//g,e(s)).replace(//g,e(o))),lookbehind:!0,inside:null}],"builtin-types":{pattern:/\b(?:anyerror|bool|c_u?(?:int|long|longlong|short)|c_longdouble|c_void|comptime_(?:float|int)|f(?:16|32|64|128)|[iu](?:8|16|32|64|128|size)|noreturn|type|void)\b/,alias:"keyword"},keyword:r,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:/[.:,;(){}[\]]/},n.languages.zig["class-name"].forEach(function(e){null===e.inside&&(e.inside=n.languages.zig)})}(Prism); \ No newline at end of file diff --git a/tests/languages/zig/comment_feature.test b/tests/languages/zig/comment_feature.test new file mode 100644 index 0000000000..0bbe6b193b --- /dev/null +++ b/tests/languages/zig/comment_feature.test @@ -0,0 +1,21 @@ +// normal comment + +/// A structure for storing a timestamp, with nanosecond precision (this is a +/// multiline doc comment). + +//! This module provides functions for retrieving the current date and +//! time with varying degrees of precision and accuracy. It does not +//! depend on libc, but will use functions from it if available. + +---------------------------------------------------- + +[ + ["comment", "// normal comment"], + + ["comment", "/// A structure for storing a timestamp, with nanosecond precision (this is a"], + ["comment", "/// multiline doc comment)."], + + ["comment", "//! This module provides functions for retrieving the current date and"], + ["comment", "//! time with varying degrees of precision and accuracy. It does not"], + ["comment", "//! depend on libc, but will use functions from it if available."] +] diff --git a/tests/languages/zig/string_feature.test b/tests/languages/zig/string_feature.test new file mode 100644 index 0000000000..06b8ec952d --- /dev/null +++ b/tests/languages/zig/string_feature.test @@ -0,0 +1,160 @@ +// source: https://ziglang.org/documentation/master/#String-Literals-and-Character-Literals + +const expect = @import("std").testing.expect; +const mem = @import("std").mem; + +test "string literals" { + const bytes = "hello"; + expect(@TypeOf(bytes) == *const [5:0]u8); + expect(bytes.len == 5); + expect(bytes[1] == 'e'); + expect(bytes[5] == 0); + expect('e' == '\x65'); + expect('\u{1f4a9}' == 128169); + expect('💯' == 128175); + expect(mem.eql(u8, "hello", "h\x65llo")); +} + +const hello_world_in_c = + \\#include + \\ + \\int main(int argc, char **argv) { + \\ printf("hello world\n"); + \\ return 0; + \\} +; + +---------------------------------------------------- + +[ + ["comment", "// source: https://ziglang.org/documentation/master/#String-Literals-and-Character-Literals"], + + ["keyword", "const"], + " expect ", + ["operator", "="], + ["builtin", "@import"], + ["punctuation", "("], + ["string", "\"std\""], + ["punctuation", ")"], + ["punctuation", "."], + "testing", + ["punctuation", "."], + "expect", + ["punctuation", ";"], + + ["keyword", "const"], + " mem ", + ["operator", "="], + ["builtin", "@import"], + ["punctuation", "("], + ["string", "\"std\""], + ["punctuation", ")"], + ["punctuation", "."], + "mem", + ["punctuation", ";"], + + ["keyword", "test"], + ["string", "\"string literals\""], + ["punctuation", "{"], + + ["keyword", "const"], + " bytes ", + ["operator", "="], + ["string", "\"hello\""], + ["punctuation", ";"], + + ["function", "expect"], + ["punctuation", "("], + ["builtin", "@TypeOf"], + ["punctuation", "("], + "bytes", + ["punctuation", ")"], + ["operator", "=="], + ["operator", "*"], + ["keyword", "const"], + ["punctuation", "["], + ["number", "5"], + ["punctuation", ":"], + ["number", "0"], + ["punctuation", "]"], + ["builtin-types", "u8"], + ["punctuation", ")"], + ["punctuation", ";"], + + ["function", "expect"], + ["punctuation", "("], + "bytes", + ["punctuation", "."], + "len ", + ["operator", "=="], + ["number", "5"], + ["punctuation", ")"], + ["punctuation", ";"], + + ["function", "expect"], + ["punctuation", "("], + "bytes", + ["punctuation", "["], + ["number", "1"], + ["punctuation", "]"], + ["operator", "=="], + ["string", "'e'"], + ["punctuation", ")"], + ["punctuation", ";"], + + ["function", "expect"], + ["punctuation", "("], + "bytes", + ["punctuation", "["], + ["number", "5"], + ["punctuation", "]"], + ["operator", "=="], + ["number", "0"], + ["punctuation", ")"], + ["punctuation", ";"], + + ["function", "expect"], + ["punctuation", "("], + ["string", "'e'"], + ["operator", "=="], + ["string", "'\\x65'"], + ["punctuation", ")"], + ["punctuation", ";"], + + ["function", "expect"], + ["punctuation", "("], + ["string", "'\\u{1f4a9}'"], + ["operator", "=="], + ["number", "128169"], + ["punctuation", ")"], + ["punctuation", ";"], + + ["function", "expect"], + ["punctuation", "("], + ["string", "'💯'"], + ["operator", "=="], + ["number", "128175"], + ["punctuation", ")"], + ["punctuation", ";"], + + ["function", "expect"], + ["punctuation", "("], + "mem", + ["punctuation", "."], + ["function", "eql"], + ["punctuation", "("], + ["builtin-types", "u8"], + ["punctuation", ","], + ["string", "\"hello\""], + ["punctuation", ","], + ["string", "\"h\\x65llo\""], + ["punctuation", ")"], + ["punctuation", ")"], + ["punctuation", ";"], + + ["punctuation", "}"], + + ["keyword", "const"], " hello_world_in_c ", ["operator", "="], + ["string", " \\\\#include \r\n \\\\\r\n \\\\int main(int argc, char **argv) {\r\n \\\\ printf(\"hello world\\n\");\r\n \\\\ return 0;\r\n \\\\}"], + ["punctuation", ";"] +]