Nim: Added `char` token and made some tokens greedy (#3231)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
diff --git a/components/prism-nim.js b/components/prism-nim.js
index 4f18bc3..bc53dc9 100644
--- a/components/prism-nim.js
+++ b/components/prism-nim.js
@@ -1,21 +1,27 @@
Prism.languages.nim = {
- 'comment': /#.*/,
- // Double-quoted strings can be prefixed by an identifier (Generalized raw string literals)
- // Character literals are handled specifically to prevent issues with numeric type suffixes
+ 'comment': {
+ pattern: /#.*/,
+ greedy: true
+ },
'string': {
- pattern: /(?:(?:\b(?!\d)(?:\w|\\x[89a-fA-F][0-9a-fA-F])+)?(?:"""[\s\S]*?"""(?!")|"(?:\\[\s\S]|""|[^"\\])*")|'(?:\\(?:\d+|x[\da-fA-F]{2}|.)|[^'])')/,
+ // Double-quoted strings can be prefixed by an identifier (Generalized raw string literals)
+ pattern: /(?:\b(?!\d)(?:\w|\\x[89a-fA-F][0-9a-fA-F])+)?(?:"""[\s\S]*?"""(?!")|"(?:\\[\s\S]|""|[^"\\])*")/,
greedy: true
},
- // The negative look ahead prevents wrong highlighting of the .. operator
- 'number': /\b(?:0[xXoObB][\da-fA-F_]+|\d[\d_]*(?:(?!\.\.)\.[\d_]*)?(?:[eE][+-]?\d[\d_]*)?)(?:'?[iuf]\d*)?/,
- 'keyword': /\b(?:addr|as|asm|atomic|bind|block|break|case|cast|concept|const|continue|converter|defer|discard|distinct|do|elif|else|end|enum|except|export|finally|for|from|func|generic|if|import|include|interface|iterator|let|macro|method|mixin|nil|object|out|proc|ptr|raise|ref|return|static|template|try|tuple|type|using|var|when|while|with|without|yield)\b/,
+ 'char': {
+ // Character literals are handled specifically to prevent issues with numeric type suffixes
+ pattern: /'(?:\\(?:\d+|x[\da-fA-F]{0,2}|.)|[^'])'/,
+ greedy: true
+ },
+
'function': {
pattern: /(?:(?!\d)(?:\w|\\x[89a-fA-F][0-9a-fA-F])+|`[^`\r\n]+`)\*?(?:\[[^\]]+\])?(?=\s*\()/,
+ greedy: true,
inside: {
'operator': /\*$/
}
},
- // We don't want to highlight operators inside backticks
+ // We don't want to highlight operators (and anything really) inside backticks
'identifier': {
pattern: /`[^`\r\n]+`/,
greedy: true,
@@ -23,6 +29,10 @@ Prism.languages.nim = {
'punctuation': /`/
}
},
+
+ // The negative look ahead prevents wrong highlighting of the .. operator
+ 'number': /\b(?:0[xXoObB][\da-fA-F_]+|\d[\d_]*(?:(?!\.\.)\.[\d_]*)?(?:[eE][+-]?\d[\d_]*)?)(?:'?[iuf]\d*)?/,
+ 'keyword': /\b(?:addr|as|asm|atomic|bind|block|break|case|cast|concept|const|continue|converter|defer|discard|distinct|do|elif|else|end|enum|except|export|finally|for|from|func|generic|if|import|include|interface|iterator|let|macro|method|mixin|nil|object|out|proc|ptr|raise|ref|return|static|template|try|tuple|type|using|var|when|while|with|without|yield)\b/,
'operator': {
// Look behind and look ahead prevent wrong highlighting of punctuations [. .] {. .} (. .)
// but allow the slice operator .. to take precedence over them
diff --git a/components/prism-nim.min.js b/components/prism-nim.min.js
index cb2b093..e7ed392 100644
--- a/components/prism-nim.min.js
+++ b/components/prism-nim.min.js
@@ -1 +1 @@
-Prism.languages.nim={comment:/#.*/,string:{pattern:/(?:(?:\b(?!\d)(?:\w|\\x[89a-fA-F][0-9a-fA-F])+)?(?:"""[\s\S]*?"""(?!")|"(?:\\[\s\S]|""|[^"\\])*")|'(?:\\(?:\d+|x[\da-fA-F]{2}|.)|[^'])')/,greedy:!0},number:/\b(?:0[xXoObB][\da-fA-F_]+|\d[\d_]*(?:(?!\.\.)\.[\d_]*)?(?:[eE][+-]?\d[\d_]*)?)(?:'?[iuf]\d*)?/,keyword:/\b(?:addr|as|asm|atomic|bind|block|break|case|cast|concept|const|continue|converter|defer|discard|distinct|do|elif|else|end|enum|except|export|finally|for|from|func|generic|if|import|include|interface|iterator|let|macro|method|mixin|nil|object|out|proc|ptr|raise|ref|return|static|template|try|tuple|type|using|var|when|while|with|without|yield)\b/,function:{pattern:/(?:(?!\d)(?:\w|\\x[89a-fA-F][0-9a-fA-F])+|`[^`\r\n]+`)\*?(?:\[[^\]]+\])?(?=\s*\()/,inside:{operator:/\*$/}},identifier:{pattern:/`[^`\r\n]+`/,greedy:!0,inside:{punctuation:/`/}},operator:{pattern:/(^|[({\[](?=\.\.)|(?![({\[]\.).)(?:(?:[=+\-*\/<>@$~&%|!?^:\\]|\.\.|\.(?![)}\]]))+|\b(?:and|div|in|is|isnot|mod|not|notin|of|or|shl|shr|xor)\b)/m,lookbehind:!0},punctuation:/[({\[]\.|\.[)}\]]|[`(){}\[\],:]/};
\ No newline at end of file
+Prism.languages.nim={comment:{pattern:/#.*/,greedy:!0},string:{pattern:/(?:\b(?!\d)(?:\w|\\x[89a-fA-F][0-9a-fA-F])+)?(?:"""[\s\S]*?"""(?!")|"(?:\\[\s\S]|""|[^"\\])*")/,greedy:!0},char:{pattern:/'(?:\\(?:\d+|x[\da-fA-F]{0,2}|.)|[^'])'/,greedy:!0},function:{pattern:/(?:(?!\d)(?:\w|\\x[89a-fA-F][0-9a-fA-F])+|`[^`\r\n]+`)\*?(?:\[[^\]]+\])?(?=\s*\()/,greedy:!0,inside:{operator:/\*$/}},identifier:{pattern:/`[^`\r\n]+`/,greedy:!0,inside:{punctuation:/`/}},number:/\b(?:0[xXoObB][\da-fA-F_]+|\d[\d_]*(?:(?!\.\.)\.[\d_]*)?(?:[eE][+-]?\d[\d_]*)?)(?:'?[iuf]\d*)?/,keyword:/\b(?:addr|as|asm|atomic|bind|block|break|case|cast|concept|const|continue|converter|defer|discard|distinct|do|elif|else|end|enum|except|export|finally|for|from|func|generic|if|import|include|interface|iterator|let|macro|method|mixin|nil|object|out|proc|ptr|raise|ref|return|static|template|try|tuple|type|using|var|when|while|with|without|yield)\b/,operator:{pattern:/(^|[({\[](?=\.\.)|(?![({\[]\.).)(?:(?:[=+\-*\/<>@$~&%|!?^:\\]|\.\.|\.(?![)}\]]))+|\b(?:and|div|in|is|isnot|mod|not|notin|of|or|shl|shr|xor)\b)/m,lookbehind:!0},punctuation:/[({\[]\.|\.[)}\]]|[`(){}\[\],:]/};
\ No newline at end of file
diff --git a/tests/languages/nim/char_feature.test b/tests/languages/nim/char_feature.test
new file mode 100644
index 0000000..c2ec9c8
--- /dev/null
+++ b/tests/languages/nim/char_feature.test
@@ -0,0 +1,9 @@
+'\''
+'\xFC'
+
+----------------------------------------------------
+
+[
+ ["char", "'\\''"],
+ ["char", "'\\xFC'"]
+]
diff --git a/tests/languages/nim/function_feature.test b/tests/languages/nim/function_feature.test
index 899ad50..5c8449a 100644
--- a/tests/languages/nim/function_feature.test
+++ b/tests/languages/nim/function_feature.test
@@ -6,12 +6,22 @@ takeV[T](
----------------------------------------------------
[
- ["function", ["fo\\x9ao"]], ["punctuation", "("],
- ["function", ["class", ["operator", "*"]]], ["punctuation", "("],
- ["function", ["takeV[T]"]], ["punctuation", "("],
- ["function", ["`$`"]], ["punctuation", "("]
+ ["function", ["fo\\x9ao"]],
+ ["punctuation", "("],
+
+ ["function", [
+ "class",
+ ["operator", "*"]
+ ]],
+ ["punctuation", "("],
+
+ ["function", ["takeV[T]"]],
+ ["punctuation", "("],
+
+ ["function", ["`$`"]],
+ ["punctuation", "("]
]
----------------------------------------------------
-Checks for functions.
\ No newline at end of file
+Checks for functions.
diff --git a/tests/languages/nim/string_feature.test b/tests/languages/nim/string_feature.test
index cd41819..f93191e 100644
--- a/tests/languages/nim/string_feature.test
+++ b/tests/languages/nim/string_feature.test
@@ -15,24 +15,24 @@ fo\x8Fo"Foobar"
bar"""Foo
bar"""
-'\''
-'\xFC'
-
----------------------------------------------------
[
["string", "\"\""],
["string", "\"Fo\\\"obar\""],
+
["string", "\"\"\"\"\"\""],
["string", "\"\"\"Fo\"o\r\nbar\"\"\""],
+
["string", "R\"Raw \"\"string\""],
+
["string", "r\"Raw\r\n\"\"string\""],
+
["string", "fo\\x8Fo\"Foobar\""],
- ["string", "bar\"\"\"Foo\r\nbar\"\"\""],
- ["string", "'\\''"],
- ["string", "'\\xFC'"]
+
+ ["string", "bar\"\"\"Foo\r\nbar\"\"\""]
]
----------------------------------------------------
-Checks for strings and character literals.
\ No newline at end of file
+Checks for strings and character literals.