From 2334b4b6b7d76cbf726a36e79ff0dd6bac400028 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Tue, 7 Dec 2021 13:04:48 +0100 Subject: [PATCH] Nim: Added `char` token and made some tokens greedy (#3231) --- components/prism-nim.js | 26 ++++++++++++++++------- components/prism-nim.min.js | 2 +- tests/languages/nim/char_feature.test | 9 ++++++++ tests/languages/nim/function_feature.test | 20 ++++++++++++----- tests/languages/nim/string_feature.test | 14 ++++++------ 5 files changed, 50 insertions(+), 21 deletions(-) create mode 100644 tests/languages/nim/char_feature.test diff --git a/components/prism-nim.js b/components/prism-nim.js index 4f18bc36fa..bc53dc96e1 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 cb2b093094..e7ed392bd9 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 0000000000..c2ec9c8ef8 --- /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 899ad50f30..5c8449a5b7 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 cd41819d1d..f93191ef05 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.