Skip to content

(TOML) Incorrect syntax highlighting #2594

@aduh95

Description

@aduh95

Describe the issue

The library fail to parse some valid TOML lines.

Which language seems to have the issue?

TOML, also INI.

Are you using highlight or highlightAuto?

Both.

Sample Code to Reproduce

Using this code sample as test case (copied from TOML readme):

# 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!"

JS code to render the above using highlight.js:

import hljs from "highlight.js/lib/core.js";
import toml from "highlight.js/lib/languages/ini.js";

const TOML = "toml";
hljs.registerLanguage(TOML, toml);

console.log(hljs.highlight(TOML,`
  …above toml code here…
`, true));

Actual behavior

Only bare keys and some dotted keys are highlighted, while the other ones are parsed as illegal.

<span class="hljs-comment"># Bare keys:</span>
<span class="hljs-attr">key</span> = <span class="hljs-string">"value"</span>
<span class="hljs-attr">bare_key</span> = <span class="hljs-string">"value"</span>
<span class="hljs-attr">bare-key</span> = <span class="hljs-string">"value"</span>
<span class="hljs-attr">1234</span> = <span class="hljs-string">"value"</span>

<span class="hljs-comment"># Quoted keys:</span>
"127.0.0.1" = "value"
"character encoding" = "value"
"ʎǝʞ" = "value"
'key2' = "value"
'quoted "value"' = "value"

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

<span class="hljs-comment"># empty quoted key is allowed</span>
"" = "blank"     <span class="hljs-comment"># VALID but discouraged</span>
'' = 'blank'     <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">"Orange"</span>
<span class="hljs-attr">physical.color</span> = <span class="hljs-string">"orange"</span>
<span class="hljs-attr">physical.shape</span> = <span class="hljs-string">"round"</span>
site."google.com" = true
<span class="hljs-attr">3.14159</span> = <span class="hljs-string">"pi"</span>

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

<span class="hljs-comment"># Whitespace is ignored around key names and values</span>
    hello = "World!"

Expected behavior

The code sample contains valid TOML syntax, all lines should be highlighted.

<span class="hljs-comment"># Bare keys:</span>
<span class="hljs-attr">key</span> = <span class="hljs-string">"value"</span>
<span class="hljs-attr">bare_key</span> = <span class="hljs-string">"value"</span>
<span class="hljs-attr">bare-key</span> = <span class="hljs-string">"value"</span>
<span class="hljs-attr">1234</span> = <span class="hljs-string">"value"</span>

<span class="hljs-comment"># Quoted keys:</span>
<span class="hljs-attr">"127.0.0.1"</span> = <span class="hljs-string">"value"</span>
<span class="hljs-attr">"character encoding"</span> = <span class="hljs-string">"value"</span>
<span class="hljs-attr">"ʎǝʞ"</span> = <span class="hljs-string">"value"</span>
<span class="hljs-attr">'key2'</span> = <span class="hljs-string">"value"</span>
<span class="hljs-attr">'quoted "value"'</span> = <span class="hljs-string">"value"</span>
<span class="hljs-attr">
"key "containing" backslash"</span> = <span class="hljs-number">6</span>
<span class="hljs-attr">'key "containing" backslash'</span> = <span class="hljs-number">6</span>

<span class="hljs-comment"># empty quoted key is allowed</span>
<span class="hljs-attr">""</span> = <span class="hljs-string">"blank"</span>     <span class="hljs-comment"># VALID but discouraged</span>
<span class="hljs-attr">''</span> = <span class="hljs-string">'blank'</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">"Orange"</span>
<span class="hljs-attr">physical.color</span> = <span class="hljs-string">"orange"</span>
<span class="hljs-attr">physical.shape</span> = <span class="hljs-string">"round"</span>
<span class="hljs-attr">site."google.com"</span> = <span class="hljs-literal">true</span>
<span class="hljs-attr">3.14159</span> = <span class="hljs-string">"pi"</span>

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

<span class="hljs-comment"># Whitespace is ignored around key names and values</span>
<span class="hljs-attr">    hello</span> = <span class="hljs-string">"World!"</span>

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions