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

Support quoted keys in TOML #1777

Merged
merged 1 commit into from
Jan 6, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Support quoted keys for TOML
This commit supports quoted keys in standard key/value pair as well as
ones inside inline table.
  • Loading branch information
tancnle committed Jan 5, 2022
commit 568feca8d379e3afa66479e59ed9e3e514c668ed
14 changes: 8 additions & 6 deletions lib/rouge/lexers/toml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ class TOML < RegexLexer
filenames '*.toml', 'Pipfile'
mimetypes 'text/x-toml'

identifier = /\S+/
# bare keys and quoted keys
identifier = %r/(?:\S+|"[^"]+"|'[^']+')/

state :basic do
rule %r/\s+/, Text
rule %r/#.*?$/, Comment
rule %r/(true|false)/, Keyword::Constant

rule %r/(\S+)(\s*)(=)(\s*)(\{)/ do |m|
rule %r/(#{identifier})(\s*)(=)(\s*)(\{)/ do
groups Name::Namespace, Text, Operator, Text, Punctuation
push :inline
end
Expand Down Expand Up @@ -48,6 +49,11 @@ class TOML < RegexLexer

state :content do
mixin :basic

rule %r/(#{identifier})(\s*)(=)/ do
groups Name::Property, Text, Punctuation
end

rule %r/"""/, Str, :mdq
rule %r/"/, Str, :dq
rule %r/'''/, Str, :msq
Expand Down Expand Up @@ -95,10 +101,6 @@ class TOML < RegexLexer
state :inline do
mixin :content

rule %r/(#{identifier})(\s*)(=)/ do
groups Name::Property, Text, Punctuation
end

rule %r/\}/, Punctuation, :pop!
end
end
Expand Down
14 changes: 14 additions & 0 deletions spec/visual/samples/toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,19 @@ test_string = "You'll hate me after this - #" # " Annoying, isn't it?

東京都 = 123

# Inline table
name = { first = "Tom", last = "Preston-Werner" }
point = { x = 1, y = 2 }
'student names' = { "first name" = "Tom", "last name" = "Preston-Werner" }

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

# Dotted keys
physical.color = "orange"
physical.shape = "round"
site."google.com" = true