diff --git a/src/languages/toml.js b/src/languages/toml.js index 0eb0167eda..0c19f3b076 100644 --- a/src/languages/toml.js +++ b/src/languages/toml.js @@ -3,15 +3,7 @@ export default { id: 'toml', grammar () { const key = /(?:[\w-]+|'[^'\n\r]*'|"(?:\\.|[^\\"\r\n])*")/.source; - - /** - * - * @param {string} pattern - * @returns {string} - */ - function insertKey (pattern) { - return pattern.replace(/__/g, () => key); - } + const dottedKey = key + '(?:\\s*\\.\\s*' + key + ')*'; return { 'comment': { @@ -19,19 +11,22 @@ export default { greedy: true, }, 'table': { - pattern: RegExp( - insertKey(/(^[\t ]*\[\s*(?:\[\s*)?)__(?:\s*\.\s*__)*(?=\s*\])/.source), - 'm' - ), + // keep entire table header (including brackets) under one parent token + pattern: RegExp('(^[\\t ]*)\\[\\[?\\s*' + dottedKey + '\\s*\\]\\]?', 'm'), lookbehind: true, greedy: true, alias: 'class-name', + inside: { + 'table-name': { + pattern: RegExp('(^\\[\\[?\\s*)' + dottedKey), + lookbehind: true, + alias: 'class-name', + }, + 'punctuation': /\[|\]/, + }, }, 'key': { - pattern: RegExp( - insertKey(/(^[\t ]*|[{,]\s*)__(?:\s*\.\s*__)*(?=\s*=)/.source), - 'm' - ), + pattern: RegExp('(^[\\t ]*|[{,]\\s*)' + dottedKey + '(?=\\s*=)', 'm'), lookbehind: true, greedy: true, alias: 'property', diff --git a/tests/languages/toml/table_feature.test b/tests/languages/toml/table_feature.test index 4fbce4e88a..2652cace10 100644 --- a/tests/languages/toml/table_feature.test +++ b/tests/languages/toml/table_feature.test @@ -7,15 +7,18 @@ foo = [ "bar" ] ---------------------------------------------------- [ - ["punctuation", "["], - ["table", "table"], - ["punctuation", "]"], - - ["punctuation", "["], - ["punctuation", "["], - ["table", "array"], - ["punctuation", "]"], - ["punctuation", "]"], + ["table", [ + ["punctuation", "["], + ["table-name", "table"], + ["punctuation", "]"] + ]], + ["table", [ + ["punctuation", "["], + ["punctuation", "["], + ["table-name", "array"], + ["punctuation", "]"], + ["punctuation", "]"] + ]], ["comment", "# not a table"],