Skip to content

Commit 9234b55

Browse files
Fix: TOML table to avoid splitting brackets from key (#4045)
* Fix: toml parsing for table headers places punctuation and key under one parent token * comments from code review * Update src/languages/toml.js Co-authored-by: Dmitry Sharabin <dmitrysharabin@gmail.com> * remove unused regex --------- Co-authored-by: Dmitry Sharabin <dmitrysharabin@gmail.com>
1 parent b8180e3 commit 9234b55

2 files changed

Lines changed: 58 additions & 18 deletions

File tree

src/languages/toml.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,36 @@ export default {
33
id: 'toml',
44
grammar () {
55
const key = /(?:[\w-]+|'[^'\n\r]*'|"(?:\\.|[^\\"\r\n])*")/.source;
6-
7-
/**
8-
*
9-
* @param {string} pattern
10-
* @returns {string}
11-
*/
12-
function insertKey (pattern) {
13-
return pattern.replace(/__/g, () => key);
14-
}
6+
const dottedKey = key + '(?:\\s*\\.\\s*' + key + ')*';
157

168
return {
179
'comment': {
1810
pattern: /#.*/,
1911
greedy: true,
2012
},
2113
'table': {
14+
// keep entire table header (including brackets) under one parent token
2215
pattern: RegExp(
23-
insertKey(/(^[\t ]*\[\s*(?:\[\s*)?)__(?:\s*\.\s*__)*(?=\s*\])/.source),
16+
'(^[\\t ]*)(?:\\[\\[\\s*' +
17+
dottedKey +
18+
'\\s*\\]\\]|\\[\\s*' +
19+
dottedKey +
20+
'\\s*\\])(?!\\])',
2421
'm'
2522
),
2623
lookbehind: true,
2724
greedy: true,
28-
alias: 'class-name',
25+
inside: {
26+
'table-name': {
27+
pattern: RegExp('(^\\[\\[?\\s*)' + dottedKey),
28+
lookbehind: true,
29+
alias: 'variable',
30+
},
31+
'punctuation': /\[|\]/,
32+
},
2933
},
3034
'key': {
31-
pattern: RegExp(
32-
insertKey(/(^[\t ]*|[{,]\s*)__(?:\s*\.\s*__)*(?=\s*=)/.source),
33-
'm'
34-
),
35+
pattern: RegExp('(^[\\t ]*|[{,]\\s*)' + dottedKey + '(?=\\s*=)', 'm'),
3536
lookbehind: true,
3637
greedy: true,
3738
alias: 'property',

tests/languages/toml/table_feature.test

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,59 @@
11
[table]
22
[[array]]
3+
[a.b]
4+
[[a.b]]
5+
[name] # trailing comment
6+
[name]]
7+
[[name]
8+
[name
9+
name]
310

411
# not a table
512
foo = [ "bar" ]
613

714
----------------------------------------------------
815

916
[
17+
["table", [
18+
["punctuation", "["],
19+
["table-name", "table"],
20+
["punctuation", "]"]
21+
]],
22+
["table", [
23+
["punctuation", "["],
24+
["punctuation", "["],
25+
["table-name", "array"],
26+
["punctuation", "]"],
27+
["punctuation", "]"]
28+
]],
29+
["table", [
30+
["punctuation", "["],
31+
["table-name", "a.b"],
32+
["punctuation", "]"]
33+
]],
34+
["table", [
35+
["punctuation", "["],
36+
["punctuation", "["],
37+
["table-name", "a.b"],
38+
["punctuation", "]"],
39+
["punctuation", "]"]
40+
]],
41+
["table", [
42+
["punctuation", "["],
43+
["table-name", "name"],
44+
["punctuation", "]"]
45+
]],
46+
["comment", "# trailing comment"],
1047
["punctuation", "["],
11-
["table", "table"],
48+
"name",
49+
["punctuation", "]"],
1250
["punctuation", "]"],
13-
1451
["punctuation", "["],
1552
["punctuation", "["],
16-
["table", "array"],
53+
"name",
1754
["punctuation", "]"],
55+
["punctuation", "["],
56+
"name\r\nname",
1857
["punctuation", "]"],
1958

2059
["comment", "# not a table"],

0 commit comments

Comments
 (0)