Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nim: Added char token and made some tokens greedy #3231

Merged
merged 1 commit into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.