From c93244768ec7031caa6359655289c1e756a90194 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Wed, 29 Apr 2020 21:54:31 +0200 Subject: [PATCH] Python: Fixed empty multiline strings (#2344) --- components/prism-python.js | 4 ++-- components/prism-python.min.js | 2 +- tests/languages/python/string-interpolation_feature.test | 2 +- tests/languages/python/triple-quoted-string_feature.test | 6 +++++- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/components/prism-python.js b/components/prism-python.js index 8619ed7a0a..2d96b154a5 100644 --- a/components/prism-python.js +++ b/components/prism-python.js @@ -4,7 +4,7 @@ Prism.languages.python = { lookbehind: true }, 'string-interpolation': { - pattern: /(?:f|rf|fr)(?:("""|''')[\s\S]+?\1|("|')(?:\\.|(?!\2)[^\\\r\n])*\2)/i, + pattern: /(?:f|rf|fr)(?:("""|''')[\s\S]*?\1|("|')(?:\\.|(?!\2)[^\\\r\n])*\2)/i, greedy: true, inside: { 'interpolation': { @@ -27,7 +27,7 @@ Prism.languages.python = { } }, 'triple-quoted-string': { - pattern: /(?:[rub]|rb|br)?("""|''')[\s\S]+?\1/i, + pattern: /(?:[rub]|rb|br)?("""|''')[\s\S]*?\1/i, greedy: true, alias: 'string' }, diff --git a/components/prism-python.min.js b/components/prism-python.min.js index 35336af28c..3045bfb177 100644 --- a/components/prism-python.min.js +++ b/components/prism-python.min.js @@ -1 +1 @@ -Prism.languages.python={comment:{pattern:/(^|[^\\])#.*/,lookbehind:!0},"string-interpolation":{pattern:/(?:f|rf|fr)(?:("""|''')[\s\S]+?\1|("|')(?:\\.|(?!\2)[^\\\r\n])*\2)/i,greedy:!0,inside:{interpolation:{pattern:/((?:^|[^{])(?:{{)*){(?!{)(?:[^{}]|{(?!{)(?:[^{}]|{(?!{)(?:[^{}])+})+})+}/,lookbehind:!0,inside:{"format-spec":{pattern:/(:)[^:(){}]+(?=}$)/,lookbehind:!0},"conversion-option":{pattern:/![sra](?=[:}]$)/,alias:"punctuation"},rest:null}},string:/[\s\S]+/}},"triple-quoted-string":{pattern:/(?:[rub]|rb|br)?("""|''')[\s\S]+?\1/i,greedy:!0,alias:"string"},string:{pattern:/(?:[rub]|rb|br)?("|')(?:\\.|(?!\1)[^\\\r\n])*\1/i,greedy:!0},function:{pattern:/((?:^|\s)def[ \t]+)[a-zA-Z_]\w*(?=\s*\()/g,lookbehind:!0},"class-name":{pattern:/(\bclass\s+)\w+/i,lookbehind:!0},decorator:{pattern:/(^\s*)@\w+(?:\.\w+)*/im,lookbehind:!0,alias:["annotation","punctuation"],inside:{punctuation:/\./}},keyword:/\b(?:and|as|assert|async|await|break|class|continue|def|del|elif|else|except|exec|finally|for|from|global|if|import|in|is|lambda|nonlocal|not|or|pass|print|raise|return|try|while|with|yield)\b/,builtin:/\b(?:__import__|abs|all|any|apply|ascii|basestring|bin|bool|buffer|bytearray|bytes|callable|chr|classmethod|cmp|coerce|compile|complex|delattr|dict|dir|divmod|enumerate|eval|execfile|file|filter|float|format|frozenset|getattr|globals|hasattr|hash|help|hex|id|input|int|intern|isinstance|issubclass|iter|len|list|locals|long|map|max|memoryview|min|next|object|oct|open|ord|pow|property|range|raw_input|reduce|reload|repr|reversed|round|set|setattr|slice|sorted|staticmethod|str|sum|super|tuple|type|unichr|unicode|vars|xrange|zip)\b/,boolean:/\b(?:True|False|None)\b/,number:/(?:\b(?=\d)|\B(?=\.))(?:0[bo])?(?:(?:\d|0x[\da-f])[\da-f]*\.?\d*|\.\d+)(?:e[+-]?\d+)?j?\b/i,operator:/[-+%=]=?|!=|\*\*?=?|\/\/?=?|<[<=>]?|>[=>]?|[&|^~]/,punctuation:/[{}[\];(),.:]/},Prism.languages.python["string-interpolation"].inside.interpolation.inside.rest=Prism.languages.python,Prism.languages.py=Prism.languages.python; \ No newline at end of file +Prism.languages.python={comment:{pattern:/(^|[^\\])#.*/,lookbehind:!0},"string-interpolation":{pattern:/(?:f|rf|fr)(?:("""|''')[\s\S]*?\1|("|')(?:\\.|(?!\2)[^\\\r\n])*\2)/i,greedy:!0,inside:{interpolation:{pattern:/((?:^|[^{])(?:{{)*){(?!{)(?:[^{}]|{(?!{)(?:[^{}]|{(?!{)(?:[^{}])+})+})+}/,lookbehind:!0,inside:{"format-spec":{pattern:/(:)[^:(){}]+(?=}$)/,lookbehind:!0},"conversion-option":{pattern:/![sra](?=[:}]$)/,alias:"punctuation"},rest:null}},string:/[\s\S]+/}},"triple-quoted-string":{pattern:/(?:[rub]|rb|br)?("""|''')[\s\S]*?\1/i,greedy:!0,alias:"string"},string:{pattern:/(?:[rub]|rb|br)?("|')(?:\\.|(?!\1)[^\\\r\n])*\1/i,greedy:!0},function:{pattern:/((?:^|\s)def[ \t]+)[a-zA-Z_]\w*(?=\s*\()/g,lookbehind:!0},"class-name":{pattern:/(\bclass\s+)\w+/i,lookbehind:!0},decorator:{pattern:/(^\s*)@\w+(?:\.\w+)*/im,lookbehind:!0,alias:["annotation","punctuation"],inside:{punctuation:/\./}},keyword:/\b(?:and|as|assert|async|await|break|class|continue|def|del|elif|else|except|exec|finally|for|from|global|if|import|in|is|lambda|nonlocal|not|or|pass|print|raise|return|try|while|with|yield)\b/,builtin:/\b(?:__import__|abs|all|any|apply|ascii|basestring|bin|bool|buffer|bytearray|bytes|callable|chr|classmethod|cmp|coerce|compile|complex|delattr|dict|dir|divmod|enumerate|eval|execfile|file|filter|float|format|frozenset|getattr|globals|hasattr|hash|help|hex|id|input|int|intern|isinstance|issubclass|iter|len|list|locals|long|map|max|memoryview|min|next|object|oct|open|ord|pow|property|range|raw_input|reduce|reload|repr|reversed|round|set|setattr|slice|sorted|staticmethod|str|sum|super|tuple|type|unichr|unicode|vars|xrange|zip)\b/,boolean:/\b(?:True|False|None)\b/,number:/(?:\b(?=\d)|\B(?=\.))(?:0[bo])?(?:(?:\d|0x[\da-f])[\da-f]*\.?\d*|\.\d+)(?:e[+-]?\d+)?j?\b/i,operator:/[-+%=]=?|!=|\*\*?=?|\/\/?=?|<[<=>]?|>[=>]?|[&|^~]/,punctuation:/[{}[\];(),.:]/},Prism.languages.python["string-interpolation"].inside.interpolation.inside.rest=Prism.languages.python,Prism.languages.py=Prism.languages.python; \ No newline at end of file diff --git a/tests/languages/python/string-interpolation_feature.test b/tests/languages/python/string-interpolation_feature.test index 789cb83243..95ca423cbd 100644 --- a/tests/languages/python/string-interpolation_feature.test +++ b/tests/languages/python/string-interpolation_feature.test @@ -144,4 +144,4 @@ f'{(lambda x: x*2)(3)}' ---------------------------------------------------- -Checks for string interpolation. \ No newline at end of file +Checks for string interpolation. diff --git a/tests/languages/python/triple-quoted-string_feature.test b/tests/languages/python/triple-quoted-string_feature.test index ab1e2ab759..13157cbc5e 100644 --- a/tests/languages/python/triple-quoted-string_feature.test +++ b/tests/languages/python/triple-quoted-string_feature.test @@ -1,7 +1,9 @@ +"""""" """foobar""" """fo"o #bar baz""" +'''''' '''foobar''' '''fo'o #bar @@ -10,12 +12,14 @@ baz''' ---------------------------------------------------- [ + ["triple-quoted-string", "\"\"\"\"\"\""], ["triple-quoted-string", "\"\"\"foobar\"\"\""], ["triple-quoted-string", "\"\"\"fo\"o\r\n#bar\r\nbaz\"\"\""], + ["triple-quoted-string", "''''''"], ["triple-quoted-string", "'''foobar'''"], ["triple-quoted-string", "'''fo'o\r\n#bar\r\nbaz'''"] ] ---------------------------------------------------- -Checks for triple-quoted strings. \ No newline at end of file +Checks for triple-quoted strings.