Skip to content

Commit

Permalink
Nim: Added char token and made some tokens greedy
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Nov 26, 2021
1 parent cefccdd commit 165405b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 21 deletions.
26 changes: 18 additions & 8 deletions components/prism-nim.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
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,
inside: {
'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
Expand Down
2 changes: 1 addition & 1 deletion components/prism-nim.min.js

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

9 changes: 9 additions & 0 deletions tests/languages/nim/char_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'\''
'\xFC'

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

[
["char", "'\\''"],
["char", "'\\xFC'"]
]
20 changes: 15 additions & 5 deletions tests/languages/nim/function_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Checks for functions.
14 changes: 7 additions & 7 deletions tests/languages/nim/string_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Checks for strings and character literals.

0 comments on commit 165405b

Please sign in to comment.