Skip to content

Commit

Permalink
D: Fixed strings (#2029)
Browse files Browse the repository at this point in the history
* Fixed D strings

* Rebuilt
  • Loading branch information
RunDevelopment authored Sep 3, 2019
1 parent 8a572af commit 010a015
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 57 deletions.
83 changes: 49 additions & 34 deletions components/prism-d.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,54 @@
Prism.languages.d = Prism.languages.extend('clike', {
'comment': [
{
// Shebang
pattern: /^\s*#!.+/,
greedy: true
},
{
pattern: RegExp(/(^|[^\\])/.source + '(?:' + [
// /+ comment +/
// Allow one level of nesting
/\/\+(?:\/\+[\s\S]*?\+\/|(?!\/\+)[\s\S])*?\+\//.source,
// // comment
/\/\/.*/.source,
// /* comment */
/\/\*[\s\S]*?\*\//.source
].join('|') + ')'),
lookbehind: true,
greedy: true
}
],
'string': [
// r"", x""
/\b[rx]"(?:\\[\s\S]|[^\\"])*"[cwd]?/,
// q"[]", q"()", q"<>", q"{}"
/\bq"(?:\[[\s\S]*?\]|\([\s\S]*?\)|<[\s\S]*?>|\{[\s\S]*?\})"/,
// q"IDENT
// ...
// IDENT"
/\bq"([_a-zA-Z][_a-zA-Z\d]*)(?:\r?\n|\r)[\s\S]*?(?:\r?\n|\r)\1"/,
// q"//", q"||", etc.
/\bq"(.)[\s\S]*?\1"/,
// Characters
/'(?:\\'|\\?[^']+)'/,
{
pattern: RegExp([
// r"", x""
/\b[rx]"(?:\\[\s\S]|[^\\"])*"[cwd]?/.source,

// q"[]", q"()", q"<>", q"{}"
/\bq"(?:\[[\s\S]*?\]|\([\s\S]*?\)|<[\s\S]*?>|\{[\s\S]*?\})"/.source,

// q"IDENT
// ...
// IDENT"
/\bq"((?!\d)\w+)$[\s\S]*?^\1"/.source,

// q"//", q"||", etc.
/\bq"(.)[\s\S]*?\2"/.source,

// Characters
// 'a', '\\', '\n', '\xFF', '\377', '\uFFFF', '\U0010FFFF', '\quot'
/'(?:\\(?:\W|\w+)|[^\\])'/.source,

/(["`])(?:\\[\s\S]|(?!\1)[^\\])*\1[cwd]?/
/(["`])(?:\\[\s\S]|(?!\3)[^\\])*\3[cwd]?/.source
].join('|'), 'm'),
greedy: true
},
{
pattern: /\bq\{(?:\{[^{}]*\}|[^{}])*\}/,
greedy: true,
alias: 'token-string'
}
],

'number': [
Expand All @@ -31,26 +66,6 @@ Prism.languages.d = Prism.languages.extend('clike', {
'operator': /\|[|=]?|&[&=]?|\+[+=]?|-[-=]?|\.?\.\.|=[>=]?|!(?:i[ns]\b|<>?=?|>=?|=)?|\bi[ns]\b|(?:<[<>]?|>>?>?|\^\^|[*\/%^~])=?/
});


Prism.languages.d.comment = [
// Shebang
/^\s*#!.+/,
// /+ +/
{
// Allow one level of nesting
pattern: /(^|[^\\])\/\+(?:\/\+[\s\S]*?\+\/|[\s\S])*?\+\//,
lookbehind: true
}
].concat(Prism.languages.d.comment);

Prism.languages.insertBefore('d', 'comment', {
'token-string': {
// Allow one level of nesting
pattern: /\bq\{(?:\{[^}]*\}|[^}])*\}/,
alias: 'string'
}
});

Prism.languages.insertBefore('d', 'keyword', {
'property': /\B@\w*/
});
Expand All @@ -61,4 +76,4 @@ Prism.languages.insertBefore('d', 'function', {
pattern: /\b(?:[ABCD][LHX]|E[ABCD]X|E?(?:BP|SP|DI|SI)|[ECSDGF]S|CR[0234]|DR[012367]|TR[3-7]|X?MM[0-7]|R[ABCD]X|[BS]PL|R[BS]P|[DS]IL|R[DS]I|R(?:[89]|1[0-5])[BWD]?|XMM(?:[89]|1[0-5])|YMM(?:1[0-5]|\d))\b|\bST(?:\([0-7]\)|\b)/,
alias: 'variable'
}
});
});
2 changes: 1 addition & 1 deletion components/prism-d.min.js

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

8 changes: 6 additions & 2 deletions tests/languages/d/comment_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ bar +/
baz +/
// This q{is} a comment
// This /* is a */ comment
/*
comment
*/

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

Expand All @@ -18,10 +21,11 @@ baz +/
["comment", "/+ foo\r\nbar +/"],
["comment", "/+ foo\r\n\t/+\r\n\tbar +/\r\nbaz +/"],
["comment", "// This q{is} a comment"],
["comment", "// This /* is a */ comment"]
["comment", "// This /* is a */ comment"],
["comment", "/*\r\ncomment\r\n*/"]
]

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

Checks for shebang and nestable multi-line comments.
Other comments are tested in clike.
Other comments are tested in clike.
47 changes: 47 additions & 0 deletions tests/languages/d/issue2027.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
string foo = "It's";
foo = "It's";
foo = "It's";

string bar = `It's`;
bar = `It's`;
bar = `It's`;
bar = `It's`;

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

[
["keyword", "string"],
" foo ",
["operator", "="],
["string", "\"It's\""],
["punctuation", ";"],
"\r\nfoo ",
["operator", "="],
["string", "\"It's\""],
["punctuation", ";"],
"\r\nfoo ",
["operator", "="],
["string", "\"It's\""],
["punctuation", ";"],
["keyword", "string"],
" bar ",
["operator", "="],
["string", "`It's`"],
["punctuation", ";"],
"\r\nbar ",
["operator", "="],
["string", "`It's`"],
["punctuation", ";"],
"\r\nbar ",
["operator", "="],
["string", "`It's`"],
["punctuation", ";"],
"\r\nbar ",
["operator", "="],
["string", "`It's`"],
["punctuation", ";"]
]

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

Checks for issue #2027 which was about strings not being matched correctly.
34 changes: 27 additions & 7 deletions tests/languages/d/string_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,26 @@ bar/"
q"|fo"o
bar|"

'a' '\'' '\u000A'
'a' '\'' '\\' '\n' '\xFF' '\377' '\uFFFF' '\U0010FFFF' '\quot'

""
"foo"c "bar"w "baz"d
"fo\"o
bar"
`foo`

q{foo}
q{ q{bar} }

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

[
["string", "r\"\""], ["string", "x\"\""],
["string", "r\"fo\\\"o\"c"], ["string", "x\"00 FBCD\"w"], ["string", "r\"baz\"d"],
["string", "r\"\""],
["string", "x\"\""],

["string", "r\"fo\\\"o\"c"],
["string", "x\"00 FBCD\"w"],
["string", "r\"baz\"d"],

["string", "q\"[fo\"o\r\nbar]\""],
["string", "q\"(fo\"o\r\nbar)\""],
Expand All @@ -43,14 +50,27 @@ bar"
["string", "q\"/fo\"o\r\nbar/\""],
["string", "q\"|fo\"o\r\nbar|\""],

["string", "'a'"], ["string", "'\\''"], ["string", "'\\u000A'"],
["string", "'a'"],
["string", "'\\''"],
["string", "'\\\\'"],
["string", "'\\n'"],
["string", "'\\xFF'"],
["string", "'\\377'"],
["string", "'\\uFFFF'"],
["string", "'\\U0010FFFF'"],
["string", "'\\quot'"],

["string", "\"\""],
["string", "\"foo\"c"], ["string", "\"bar\"w"], ["string", "\"baz\"d"],
["string", "\"foo\"c"],
["string", "\"bar\"w"],
["string", "\"baz\"d"],
["string", "\"fo\\\"o\r\nbar\""],
["string", "`foo`"]
["string", "`foo`"],

["string", "q{foo}"],
["string", "q{ q{bar} }"]
]

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

Checks for strings.
Checks for strings and token strings.
13 changes: 0 additions & 13 deletions tests/languages/d/token-string_feature.test

This file was deleted.

0 comments on commit 010a015

Please sign in to comment.