Skip to content

Commit

Permalink
Merge pull request #795 from Golmote/prism-nix
Browse files Browse the repository at this point in the history
Add support for Nix
  • Loading branch information
Golmote committed Oct 13, 2015
2 parents 132b2a4 + 198bc99 commit 9b275c8
Show file tree
Hide file tree
Showing 13 changed files with 413 additions and 0 deletions.
4 changes: 4 additions & 0 deletions components.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ var components = {
"title": "Nim",
"owner": "Golmote"
},
"nix": {
"title": "Nix",
"owner": "Golmote"
},
"nsis": {
"title": "NSIS",
"owner": "idleberg"
Expand Down
39 changes: 39 additions & 0 deletions components/prism-nix.js
Original file line number Diff line number Diff line change
@@ -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);
1 change: 1 addition & 0 deletions components/prism-nix.min.js

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

59 changes: 59 additions & 0 deletions examples/prism-nix.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<h1>Nix</h1>
<p>To use this language, use the class "language-nix".</p>

<h2>Comments</h2>
<pre><code>#
# Single line comment
/* Multi-line
comment */</code></pre>

<h2>String</h2>
<pre><code>""
"foo\"bar"
"foo
bar"

''''
''foo'''bar''
''
foo
bar
''</code></pre>

<h2>String interpolation</h2>
<pre><code>"foo${42}bar"
"foo\${42}bar" # This is not interpolated
''foo${42}bar''
''foo''${42}bar'' # This is not interpolated</code></pre>

<h2>URLs and paths</h2>
<pre><code>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</code></pre>

<h2>Integers, booleans and null</h2>
<pre><code>0
42

true
false

null</code></pre>

<h2>Builtin functions</h2>
<pre><code>name = baseNameOf (toString url);
imap =
if builtins ? genList then
f: list: genList (n: f (n + 1) (elemAt list n)) (length list)</code></pre>

<h2>Known failures</h2>
<p>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.
</p>

<h3>Comment-like substrings</h3>
<pre><code>"This # is a broken string"</code></pre>
14 changes: 14 additions & 0 deletions tests/languages/nix/antiquotation_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
${42}

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

[
["antiquotation", "$"],
["punctuation", "{"],
["number", "42"],
["punctuation", "}"]
]

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

Checks for antiquotations outside of strings.
13 changes: 13 additions & 0 deletions tests/languages/nix/boolean_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
true
false

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

[
["boolean", "true"],
["boolean", "false"]
]

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

Checks for booleans.
18 changes: 18 additions & 0 deletions tests/languages/nix/comment_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# foobar
/**/
/* foo
bar */

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

[
["comment", "#"],
["comment", "# foobar"],
["comment", "/**/"],
["comment", "/* foo\r\nbar */"]
]

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

Checks for comments.
133 changes: 133 additions & 0 deletions tests/languages/nix/function_feature.test
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 15 additions & 0 deletions tests/languages/nix/keyword_feature.test
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 15 additions & 0 deletions tests/languages/nix/number_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
0
42
120457

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

[
["number", "0"],
["number", "42"],
["number", "120457"]
]

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

Checks for integers.
25 changes: 25 additions & 0 deletions tests/languages/nix/operator_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
= ==
! !=
< <=
> >=
+ ++
- ->
|| && //
? @

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

[
["operator", "="], ["operator", "=="],
["operator", "!"], ["operator", "!="],
["operator", "<"], ["operator", "<="],
["operator", ">"], ["operator", ">="],
["operator", "+"], ["operator", "++"],
["operator", "-"], ["operator", "->"],
["operator", "||"], ["operator", "&&"], ["operator", "//"],
["operator", "?"], ["operator", "@"]
]

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

Checks for operators.
Loading

0 comments on commit 9b275c8

Please sign in to comment.