Skip to content

Commit

Permalink
fix(toml) Improve key parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Jun 10, 2020
1 parent adb813c commit 82a422f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Language Improvements:
- enh(erlang) Add underscore separators to numeric literals (#2554) [Sergey Prokhorov][]
- enh(handlebars) Support for sub-expressions, path-expressions, hashes, block-parameters and literals (#2344) [Nils Knappmeier][]
- enh(protobuf) Support multiline comments (#2597) [Pavel Evstigneev][]
- fix(toml) Improve key parsing (#2595) [Antoine du Hamel][]

[Josh Goebel]: https://github.com/yyyc514
[Peter Plantinga]: https://github.com/pplantinga
Expand All @@ -62,6 +63,7 @@ Language Improvements:
[Jim Mason]: https://github.com/RocketMan
[lioshi]: https://github.com/lioshi
[Pavel Evstigneev]: https://github.com/Paxa
[Antoine du Hamel]: https://github.com/aduh95


## Version 10.0.2
Expand Down
15 changes: 14 additions & 1 deletion src/languages/ini.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as regex from '../lib/regex';

/*
Language: TOML, also INI
Description: TOML aims to be a minimal configuration file format that's easy to read due to obvious semantics.
Expand Down Expand Up @@ -54,6 +56,17 @@ export default function(hljs) {
relevance:0
};

var BARE_KEY = /[A-Za-z0-9_-]+/;
var QUOTED_KEY_DOUBLE_QUOTE = /"(\\"|[^"])*"/;
var QUOTED_KEY_SINGLE_QUOTE = /'[^']*'/;
var ANY_KEY = regex.either(
BARE_KEY, QUOTED_KEY_DOUBLE_QUOTE, QUOTED_KEY_SINGLE_QUOTE
);
var DOTTED_KEY = regex.concat(
ANY_KEY, '(\\s*\\.\\s*', ANY_KEY, ')*',
regex.lookahead(/\s*=\s*[^#\s]/)
);

return {
name: 'TOML, also INI',
aliases: ['toml'],
Expand All @@ -66,7 +79,7 @@ export default function(hljs) {
begin: /\[+/, end: /\]+/
},
{
begin: /^[a-z0-9\[\]_\.-]+(?=\s*=\s*)/,
begin: DOTTED_KEY,
className: 'attr',
starts: {
end: /$/,
Expand Down
32 changes: 32 additions & 0 deletions test/markup/ini/keys.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<span class="hljs-comment"># Bare keys:</span>
<span class="hljs-attr">key</span> = <span class="hljs-string">&quot;value&quot;</span>
<span class="hljs-attr">bare_key</span> = <span class="hljs-string">&quot;value&quot;</span>
<span class="hljs-attr">bare-key</span> = <span class="hljs-string">&quot;value&quot;</span>
<span class="hljs-attr">1234</span> = <span class="hljs-string">&quot;value&quot;</span>

<span class="hljs-comment"># Quoted keys:</span>
<span class="hljs-attr">&quot;127.0.0.1&quot;</span> = <span class="hljs-string">&quot;value&quot;</span>
<span class="hljs-attr">&quot;character encoding&quot;</span> = <span class="hljs-string">&quot;value&quot;</span>
<span class="hljs-attr">&quot;ʎǝʞ&quot;</span> = <span class="hljs-string">&quot;value&quot;</span>
<span class="hljs-attr">&#x27;key2&#x27;</span> = <span class="hljs-string">&quot;value&quot;</span>
<span class="hljs-attr">&#x27;quoted &quot;value&quot;&#x27;</span> = <span class="hljs-string">&quot;value&quot;</span>

<span class="hljs-attr">&quot;key \&quot;containing\&quot; backslash&quot;</span> = <span class="hljs-number">6</span>
<span class="hljs-attr">&#x27;key \&quot;containing&quot; backslash\&#x27;</span> = <span class="hljs-number">6</span>

<span class="hljs-comment"># empty quoted key is allowed</span>
<span class="hljs-attr">&quot;&quot;</span> = <span class="hljs-string">&quot;blank&quot;</span> <span class="hljs-comment"># VALID but discouraged</span>
<span class="hljs-attr">&#x27;&#x27;</span> = <span class="hljs-string">&#x27;blank&#x27;</span> <span class="hljs-comment"># VALID but discouraged</span>

<span class="hljs-comment"># Dotted keys:</span>
<span class="hljs-attr">name</span> = <span class="hljs-string">&quot;Orange&quot;</span>
<span class="hljs-attr">physical.color</span> = <span class="hljs-string">&quot;orange&quot;</span>
<span class="hljs-attr">physical.shape</span> = <span class="hljs-string">&quot;round&quot;</span>
<span class="hljs-attr">site.&quot;google.com&quot;</span> = <span class="hljs-literal">true</span>
<span class="hljs-attr">3.14159</span> = <span class="hljs-string">&quot;pi&quot;</span>

<span class="hljs-comment"># Whitespace around dot-separated parts is ignored:</span>
<span class="hljs-attr">hello . world</span> = <span class="hljs-string">&quot;!&quot;</span>

<span class="hljs-comment"># Whitespace is ignored around key names and values</span>
<span class="hljs-attr">hello</span> = <span class="hljs-string">&quot;World!&quot;</span>
32 changes: 32 additions & 0 deletions test/markup/ini/keys.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Bare keys:
key = "value"
bare_key = "value"
bare-key = "value"
1234 = "value"

# Quoted keys:
"127.0.0.1" = "value"
"character encoding" = "value"
"ʎǝʞ" = "value"
'key2' = "value"
'quoted "value"' = "value"

"key \"containing\" backslash" = 6
'key \"containing" backslash\' = 6

# empty quoted key is allowed
"" = "blank" # VALID but discouraged
'' = 'blank' # VALID but discouraged

# Dotted keys:
name = "Orange"
physical.color = "orange"
physical.shape = "round"
site."google.com" = true
3.14159 = "pi"

# Whitespace around dot-separated parts is ignored:
hello . world = "!"

# Whitespace is ignored around key names and values
hello = "World!"

0 comments on commit 82a422f

Please sign in to comment.