Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(toml) Improve key parsing #2595

Merged
merged 1 commit into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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!"