From 198bc99a644fcd9afe2d0cdff036aa618a2597ba Mon Sep 17 00:00:00 2001 From: Golmote Date: Wed, 7 Oct 2015 00:28:30 +0200 Subject: [PATCH] Add support for Nix --- components.js | 4 + components/prism-nix.js | 39 +++++ components/prism-nix.min.js | 1 + examples/prism-nix.html | 59 ++++++++ .../languages/nix/antiquotation_feature.test | 14 ++ tests/languages/nix/boolean_feature.test | 13 ++ tests/languages/nix/comment_feature.test | 18 +++ tests/languages/nix/function_feature.test | 133 ++++++++++++++++++ tests/languages/nix/keyword_feature.test | 15 ++ tests/languages/nix/number_feature.test | 15 ++ tests/languages/nix/operator_feature.test | 25 ++++ tests/languages/nix/string_feature.test | 56 ++++++++ tests/languages/nix/url_feature.test | 21 +++ 13 files changed, 413 insertions(+) create mode 100644 components/prism-nix.js create mode 100644 components/prism-nix.min.js create mode 100644 examples/prism-nix.html create mode 100644 tests/languages/nix/antiquotation_feature.test create mode 100644 tests/languages/nix/boolean_feature.test create mode 100644 tests/languages/nix/comment_feature.test create mode 100644 tests/languages/nix/function_feature.test create mode 100644 tests/languages/nix/keyword_feature.test create mode 100644 tests/languages/nix/number_feature.test create mode 100644 tests/languages/nix/operator_feature.test create mode 100644 tests/languages/nix/string_feature.test create mode 100644 tests/languages/nix/url_feature.test diff --git a/components.js b/components.js index e236cd7827..12970b1a63 100644 --- a/components.js +++ b/components.js @@ -304,6 +304,10 @@ var components = { "title": "Nim", "owner": "Golmote" }, + "nix": { + "title": "Nix", + "owner": "Golmote" + }, "nsis": { "title": "NSIS", "owner": "idleberg" diff --git a/components/prism-nix.js b/components/prism-nix.js new file mode 100644 index 0000000000..7dee574746 --- /dev/null +++ b/components/prism-nix.js @@ -0,0 +1,39 @@ +Prism.languages.nix = { + 'comment': /\/\*[\s\S]*?\*\/|#.*/, + 'string': { + pattern: /"(?:[^"\\]|\\[\s\S])*"|''(?:(?!'')[\s\S]|''(?:'|\\|\$\{))*''/, + inside: { + 'interpolation': { + // The lookbehind ensures the ${} is not preceded by \ or '' + pattern: /(^|(?:^|(?!'').)[^\\])\$\{(?:[^}]|\{[^}]*\})*}/, + lookbehind: true, + inside: { + 'antiquotation': { + pattern: /^\$(?=\{)/, + alias: 'variable' + } + // See rest below + } + } + } + }, + 'url': [ + /\b(?:[a-z]{3,7}:\/\/)[\w\-+%~\/.:#=?&]+/, + { + pattern: /([^\/])(?:[\w\-+%~.:#=?&]*(?!\/\/)[\w\-+%~\/.:#=?&])?(?!\/\/)\/[\w\-+%~\/.:#=?&]*/, + lookbehind: true + } + ], + 'antiquotation': { + pattern: /\$(?=\{)/, + alias: 'variable' + }, + 'number': /\b\d+\b/, + 'keyword': /\b(?:assert|builtins|else|if|in|inherit|let|null|or|then|with)\b/, + 'function': /\b(?:abort|add|all|any|attrNames|attrValues|baseNameOf|compareVersions|concatLists|currentSystem|deepSeq|derivation|dirOf|div|elem(?:At)?|fetch(?:url|Tarball)|filter(?:Source)?|fromJSON|genList|getAttr|getEnv|hasAttr|hashString|head|import|intersectAttrs|is(?:Attrs|Bool|Function|Int|List|Null|String)|length|lessThan|listToAttrs|map|mul|parseDrvName|pathExists|read(?:Dir|File)|removeAttrs|replaceStrings|seq|sort|stringLength|sub(?:string)?|tail|throw|to(?:File|JSON|Path|String|XML)|trace|typeOf)\b|\bfoldl'\B/, + 'boolean': /\b(?:true|false)\b/, + 'operator': /[=!<>]=?|\+\+?|\|\||&&|\/\/|->?|[?@]/, + 'punctuation': /[{}()[\].,:;]/ +}; + +Prism.languages.nix.string.inside.interpolation.inside.rest = Prism.util.clone(Prism.languages.nix); \ No newline at end of file diff --git a/components/prism-nix.min.js b/components/prism-nix.min.js new file mode 100644 index 0000000000..fd9e0769e1 --- /dev/null +++ b/components/prism-nix.min.js @@ -0,0 +1 @@ +Prism.languages.nix={comment:/\/\*[\s\S]*?\*\/|#.*/,string:{pattern:/"(?:[^"\\]|\\[\s\S])*"|''(?:(?!'')[\s\S]|''(?:'|\\|\$\{))*''/,inside:{interpolation:{pattern:/(^|(?:^|(?!'').)[^\\])\$\{(?:[^}]|\{[^}]*\})*}/,lookbehind:!0,inside:{antiquotation:{pattern:/^\$(?=\{)/,alias:"variable"}}}}},url:[/\b(?:[a-z]{3,7}:\/\/)[\w\-+%~\/.:#=?&]+/,{pattern:/([^\/])(?:[\w\-+%~.:#=?&]*(?!\/\/)[\w\-+%~\/.:#=?&])?(?!\/\/)\/[\w\-+%~\/.:#=?&]*/,lookbehind:!0}],antiquotation:{pattern:/\$(?=\{)/,alias:"variable"},number:/\b\d+\b/,keyword:/\b(?:assert|builtins|else|if|in|inherit|let|null|or|then|with)\b/,"function":/\b(?:abort|add|all|any|attrNames|attrValues|baseNameOf|compareVersions|concatLists|currentSystem|deepSeq|derivation|dirOf|div|elem(?:At)?|fetch(?:url|Tarball)|filter(?:Source)?|fromJSON|genList|getAttr|getEnv|hasAttr|hashString|head|import|intersectAttrs|is(?:Attrs|Bool|Function|Int|List|Null|String)|length|lessThan|listToAttrs|map|mul|parseDrvName|pathExists|read(?:Dir|File)|removeAttrs|replaceStrings|seq|sort|stringLength|sub(?:string)?|tail|throw|to(?:File|JSON|Path|String|XML)|trace|typeOf)\b|\bfoldl'\B/,"boolean":/\b(?:true|false)\b/,operator:/[=!<>]=?|\+\+?|\|\||&&|\/\/|->?|[?@]/,punctuation:/[{}()[\].,:;]/},Prism.languages.nix.string.inside.interpolation.inside.rest=Prism.util.clone(Prism.languages.nix); \ No newline at end of file diff --git a/examples/prism-nix.html b/examples/prism-nix.html new file mode 100644 index 0000000000..ee79b0d460 --- /dev/null +++ b/examples/prism-nix.html @@ -0,0 +1,59 @@ +

Nix

+

To use this language, use the class "language-nix".

+ +

Comments

+
#
+# Single line comment
+/* Multi-line
+comment */
+ +

String

+
""
+"foo\"bar"
+"foo
+bar"
+
+''''
+''foo'''bar''
+''
+foo
+bar
+''
+ +

String interpolation

+
"foo${42}bar"
+"foo\${42}bar" # This is not interpolated
+''foo${42}bar''
+''foo''${42}bar'' # This is not interpolated
+ +

URLs and paths

+
ftp://ftp.nluug.nl/pub/gnu/hello/hello-2.1.1.tar.gz
+http://example.org/foo.tar.bz2
+/bin/sh
+./builder.sh
+~/foo.bar
+ +

Integers, booleans and null

+
0
+42
+
+true
+false
+
+null
+ +

Builtin functions

+
name = baseNameOf (toString url);
+imap =
+	if builtins ? genList then
+		f: list: genList (n: f (n + 1) (elemAt list n)) (length list)
+ +

Known failures

+

There are certain edge cases where Prism will fail. + There are always such cases in every regex-based syntax highlighter. + However, Prism dares to be open and honest about them. + If a failure is listed here, it doesn’t mean it will never be fixed. This is more of a “known bugs” list, just with a certain type of bug. +

+ +

Comment-like substrings

+
"This # is a broken string"
\ No newline at end of file diff --git a/tests/languages/nix/antiquotation_feature.test b/tests/languages/nix/antiquotation_feature.test new file mode 100644 index 0000000000..b0e6e51c7b --- /dev/null +++ b/tests/languages/nix/antiquotation_feature.test @@ -0,0 +1,14 @@ +${42} + +---------------------------------------------------- + +[ + ["antiquotation", "$"], + ["punctuation", "{"], + ["number", "42"], + ["punctuation", "}"] +] + +---------------------------------------------------- + +Checks for antiquotations outside of strings. \ No newline at end of file diff --git a/tests/languages/nix/boolean_feature.test b/tests/languages/nix/boolean_feature.test new file mode 100644 index 0000000000..4019c444f8 --- /dev/null +++ b/tests/languages/nix/boolean_feature.test @@ -0,0 +1,13 @@ +true +false + +---------------------------------------------------- + +[ + ["boolean", "true"], + ["boolean", "false"] +] + +---------------------------------------------------- + +Checks for booleans. \ No newline at end of file diff --git a/tests/languages/nix/comment_feature.test b/tests/languages/nix/comment_feature.test new file mode 100644 index 0000000000..f44b3d423a --- /dev/null +++ b/tests/languages/nix/comment_feature.test @@ -0,0 +1,18 @@ +# +# foobar +/**/ +/* foo +bar */ + +---------------------------------------------------- + +[ + ["comment", "#"], + ["comment", "# foobar"], + ["comment", "/**/"], + ["comment", "/* foo\r\nbar */"] +] + +---------------------------------------------------- + +Checks for comments. \ No newline at end of file diff --git a/tests/languages/nix/function_feature.test b/tests/languages/nix/function_feature.test new file mode 100644 index 0000000000..1720b4523f --- /dev/null +++ b/tests/languages/nix/function_feature.test @@ -0,0 +1,133 @@ +abort +add +all +any +attrNames +attrValues +baseNameOf +compareVersions +concatLists +currentSystem +deepSeq +derivation +dirOf +div +elem +elemAt +fetchurl +fetchTarball +filter +filterSource +fromJSON +genList +getAttr +getEnv +hasAttr +hashString +head +import +intersectAttrs +isAttrs +isBool +isFunction +isInt +isList +isNull +isString +length +lessThan +listToAttrs +map +mul +parseDrvName +pathExists +readDir +readFile +removeAttrs +replaceStrings +seq +sort +stringLength +sub +substring +tail +throw +toFile +toJSON +toPath +toString +toXML +trace +typeOf +foldl' + +---------------------------------------------------- + +[ + ["function", "abort"], + ["function", "add"], + ["function", "all"], + ["function", "any"], + ["function", "attrNames"], + ["function", "attrValues"], + ["function", "baseNameOf"], + ["function", "compareVersions"], + ["function", "concatLists"], + ["function", "currentSystem"], + ["function", "deepSeq"], + ["function", "derivation"], + ["function", "dirOf"], + ["function", "div"], + ["function", "elem"], + ["function", "elemAt"], + ["function", "fetchurl"], + ["function", "fetchTarball"], + ["function", "filter"], + ["function", "filterSource"], + ["function", "fromJSON"], + ["function", "genList"], + ["function", "getAttr"], + ["function", "getEnv"], + ["function", "hasAttr"], + ["function", "hashString"], + ["function", "head"], + ["function", "import"], + ["function", "intersectAttrs"], + ["function", "isAttrs"], + ["function", "isBool"], + ["function", "isFunction"], + ["function", "isInt"], + ["function", "isList"], + ["function", "isNull"], + ["function", "isString"], + ["function", "length"], + ["function", "lessThan"], + ["function", "listToAttrs"], + ["function", "map"], + ["function", "mul"], + ["function", "parseDrvName"], + ["function", "pathExists"], + ["function", "readDir"], + ["function", "readFile"], + ["function", "removeAttrs"], + ["function", "replaceStrings"], + ["function", "seq"], + ["function", "sort"], + ["function", "stringLength"], + ["function", "sub"], + ["function", "substring"], + ["function", "tail"], + ["function", "throw"], + ["function", "toFile"], + ["function", "toJSON"], + ["function", "toPath"], + ["function", "toString"], + ["function", "toXML"], + ["function", "trace"], + ["function", "typeOf"], + ["function", "foldl'"] +] + +---------------------------------------------------- + +Checks for built-in functions. \ No newline at end of file diff --git a/tests/languages/nix/keyword_feature.test b/tests/languages/nix/keyword_feature.test new file mode 100644 index 0000000000..46fc4434d9 --- /dev/null +++ b/tests/languages/nix/keyword_feature.test @@ -0,0 +1,15 @@ +assert builtins else if +in inherit let null +or then with + +---------------------------------------------------- + +[ + ["keyword", "assert"], ["keyword", "builtins"], ["keyword", "else"], ["keyword", "if"], + ["keyword", "in"], ["keyword", "inherit"], ["keyword", "let"], ["keyword", "null"], + ["keyword", "or"], ["keyword", "then"], ["keyword", "with"] +] + +---------------------------------------------------- + +Checks for keywords. \ No newline at end of file diff --git a/tests/languages/nix/number_feature.test b/tests/languages/nix/number_feature.test new file mode 100644 index 0000000000..25fc0310d5 --- /dev/null +++ b/tests/languages/nix/number_feature.test @@ -0,0 +1,15 @@ +0 +42 +120457 + +---------------------------------------------------- + +[ + ["number", "0"], + ["number", "42"], + ["number", "120457"] +] + +---------------------------------------------------- + +Checks for integers. \ No newline at end of file diff --git a/tests/languages/nix/operator_feature.test b/tests/languages/nix/operator_feature.test new file mode 100644 index 0000000000..461f1a257e --- /dev/null +++ b/tests/languages/nix/operator_feature.test @@ -0,0 +1,25 @@ += == +! != +< <= +> >= ++ ++ +- -> +|| && // +? @ + +---------------------------------------------------- + +[ + ["operator", "="], ["operator", "=="], + ["operator", "!"], ["operator", "!="], + ["operator", "<"], ["operator", "<="], + ["operator", ">"], ["operator", ">="], + ["operator", "+"], ["operator", "++"], + ["operator", "-"], ["operator", "->"], + ["operator", "||"], ["operator", "&&"], ["operator", "//"], + ["operator", "?"], ["operator", "@"] +] + +---------------------------------------------------- + +Checks for operators. \ No newline at end of file diff --git a/tests/languages/nix/string_feature.test b/tests/languages/nix/string_feature.test new file mode 100644 index 0000000000..e8e7f35b76 --- /dev/null +++ b/tests/languages/nix/string_feature.test @@ -0,0 +1,56 @@ +"" +"foo\"b\\ar" +"f''o'o'\"bar" +"foo +bar" +"foo ${ 42 } baz" +"foo \${ 42 } baz" + +'''' +'' +foo +bar +'' +'' +f'oo'''ba'r +foo ${ 42 } baz +foo ''${ 42 } baz +'' + +---------------------------------------------------- + +[ + ["string", ["\"\""]], + ["string", ["\"foo\\\"b\\\\ar\""]], + ["string", ["\"f''o'o'\\\"bar\""]], + ["string", ["\"foo\r\nbar\""]], + ["string", [ + "\"foo ", + ["interpolation", [ + ["antiquotation", "$"], + ["punctuation", "{"], + ["number", "42"], + ["punctuation", "}"] + ]], + " baz\"" + ]], + ["string", ["\"foo \\${ 42 } baz\""]], + + ["string", ["''''"]], + ["string", ["''\r\nfoo\r\nbar\r\n''"]], + ["string", [ + "''\r\nf'oo'''ba'r\r\nfoo ", + ["interpolation", [ + ["antiquotation", "$"], + ["punctuation", "{"], + ["number", "42"], + ["punctuation", "}"] + ]], + " baz\r\nfoo ''${ 42 } baz\r\n''" + ]] +] + +---------------------------------------------------- + +Checks for strings and string interpolation. +Also checks that escaped interpolations are not interpreted. \ No newline at end of file diff --git a/tests/languages/nix/url_feature.test b/tests/languages/nix/url_feature.test new file mode 100644 index 0000000000..9b69ee3a3b --- /dev/null +++ b/tests/languages/nix/url_feature.test @@ -0,0 +1,21 @@ +http://example.org/foo.tar.bz2 +ftp://ftp.nluug.nl/pub/gnu/hello/hello-2.1.1.tar.gz + +/bin/sh +./builder.sh +~/foo.bar + +---------------------------------------------------- + +[ + ["url", "http://example.org/foo.tar.bz2"], + ["url", "ftp://ftp.nluug.nl/pub/gnu/hello/hello-2.1.1.tar.gz"], + + ["url", "/bin/sh"], + ["url", "./builder.sh"], + ["url", "~/foo.bar"] +] + +---------------------------------------------------- + +Checks for URLs and paths. \ No newline at end of file