Skip to content

Commit 60cbcdb

Browse files
committed
Realign TOML lexers with the Pygments counterpart
This also helps to address a number of TOML bugs around handling of the value attributes.
1 parent 327071f commit 60cbcdb

File tree

2 files changed

+65
-62
lines changed

2 files changed

+65
-62
lines changed

lib/rouge/lexers/toml.rb

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,55 +11,60 @@ class TOML < RegexLexer
1111
filenames '*.toml', 'Pipfile', 'poetry.lock'
1212
mimetypes 'text/x-toml'
1313

14-
# bare keys and quoted keys
15-
identifier = %r/(?:\S+|"[^"]+"|'[^']+')/
14+
state :root do
15+
mixin :whitespace
1616

17-
state :basic do
18-
rule %r/\s+/, Text
19-
rule %r/#.*?$/, Comment
20-
rule %r/(true|false)/, Keyword::Constant
17+
mixin :key
2118

22-
rule %r/(#{identifier})(\s*)(=)(\s*)(\{)/ do
23-
groups Name::Property, Text, Operator, Text, Punctuation
24-
push :inline
19+
rule %r/(=)(\s*)/ do
20+
groups Operator, Text::Whitespace
21+
push :value
2522
end
23+
24+
rule %r/\[\[?/, Keyword, :table_key
2625
end
2726

28-
state :root do
29-
mixin :basic
27+
state :key do
28+
rule %r/[A-Za-z0-9_-]+/, Name
3029

31-
rule %r/(?<!=)\s*\[.*?\]+/, Name::Namespace
30+
rule %r/"/, Str, :dq
31+
rule %r/'/, Str, :sq
32+
rule %r/\./, Punctuation
33+
end
3234

33-
rule %r/(#{identifier})(\s*)(=)/ do
34-
groups Name::Property, Text, Punctuation
35-
push :value
36-
end
35+
state :table_key do
36+
rule %r/[A-Za-z0-9_-]+/, Name
37+
38+
rule %r/"/, Str, :dq
39+
rule %r/'/, Str, :sq
40+
rule %r/\./, Keyword
41+
rule %r/\]\]?/, Keyword, :pop!
42+
rule %r/[ \t]+/, Text::Whitespace
3743
end
3844

3945
state :value do
40-
rule %r/\n/, Text, :pop!
41-
mixin :content
46+
rule %r/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/, Literal::Date, :pop!
47+
rule %r/[+-]?\d+(?:_\d+)*\.\d+(?:_\d+)*(?:[eE][+-]?\d+(?:_\d+)*)?/, Num::Float, :pop!
48+
rule %r/[+-]?\d+(?:_\d+)*[eE][+-]?\d+(?:_\d+)*/, Num::Float, :pop!
49+
rule %r/[+-]?(?:nan|inf)/, Num::Float, :pop!
50+
rule %r/0x\h+(?:_\h+)*/, Num::Hex, :pop!
51+
rule %r/0o[0-7]+(?:_[0-7]+)*/, Num::Oct, :pop!
52+
rule %r/0b[01]+(?:_[01]+)*/, Num::Bin, :pop!
53+
rule %r/[+-]?\d+(?:_\d+)*/, Num::Integer, :pop!
54+
55+
rule %r/"""/, Str, [:pop!, :mdq]
56+
rule %r/"/, Str, [:pop!, :dq]
57+
rule %r/'''/, Str, [:pop!, :msq]
58+
rule %r/'/, Str, [:pop!, :sq]
59+
60+
rule %r/(true|false)/, Keyword::Constant, :pop!
61+
rule %r/\[/, Punctuation, [:pop!, :array]
62+
rule %r/\{/, Punctuation, [:pop!, :inline]
4263
end
4364

4465
state :content do
4566
mixin :basic
4667

47-
rule %r/(#{identifier})(\s*)(=)/ do
48-
groups Name::Property, Text, Punctuation
49-
end
50-
51-
rule %r/\d{4}-\d{2}-\d{2}(?:[Tt ]\d{2}:\d{2}:\d{2}(?:[Zz]|[+-]\d{2}:\d{2})?)?/, Literal::Date
52-
rule %r/\d{2}:\d{2}:\d{2}/, Literal::Date
53-
54-
rule %r/[+-]?\d+(?:_\d+)*\.\d+(?:_\d+)*(?:[eE][+-]?\d+(?:_\d+)*)?/, Num::Float
55-
rule %r/[+-]?\d+(?:_\d+)*[eE][+-]?\d+(?:_\d+)*/, Num::Float
56-
rule %r/[+-]?(?:nan|inf)/, Num::Float
57-
58-
rule %r/0x\h+(?:_\h+)*/, Num::Hex
59-
rule %r/0o[0-7]+(?:_[0-7]+)*/, Num::Oct
60-
rule %r/0b[01]+(?:_[01]+)*/, Num::Bin
61-
rule %r/[+-]?\d+(?:_\d+)*/, Num::Integer
62-
6368
rule %r/"""/, Str, :mdq
6469
rule %r/"/, Str, :dq
6570
rule %r/'''/, Str, :msq
@@ -101,15 +106,31 @@ class TOML < RegexLexer
101106
end
102107

103108
state :array do
104-
mixin :content
109+
mixin :whitespace
110+
rule %r/,/, Punctuation
111+
105112
rule %r/\]/, Punctuation, :pop!
113+
114+
rule %r//, Token, :value
106115
end
107116

108117
state :inline do
109-
mixin :content
118+
rule %r/[ \t]+/, Text::Whitespace
110119

120+
mixin :key
121+
rule %r/(=)(\s*)/ do
122+
groups Punctuation, Text::Whitespace
123+
push :value
124+
end
125+
126+
rule %r/,/, Punctuation
111127
rule %r/\}/, Punctuation, :pop!
112128
end
129+
130+
state :whitespace do
131+
rule %r/\s+/, Text
132+
rule %r/#.*?$/, Comment
133+
end
113134
end
114135
end
115136
end

spec/visual/samples/toml

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ title = "TOML Example"
66
name = "Tom Preston-Werner"
77
organization = "GitHub"
88
bio = "GitHub Cofounder & CEO\nLikes tater tots and beer."
9-
# First class dates? Why not?
10-
dob = [
11-
1979-05-27T07:32:00Z,
12-
1979-05-27 07:32:00
13-
1979-05-27,
14-
07:32:00,
15-
]
9+
dob = 1979-05-27T07:32:00Z # First class dates? Why not?
1610

1711
[database]
1812
server = "192.168.1.1"
@@ -135,7 +129,6 @@ nested_arrays_of_ints = [ [ 1, 2 ], [3, 4, 5] ]
135129
nested_mixed_array = [ [ 1, 2 ], ["a", "b", "c"] ]
136130
string_array = [ "all", 'strings', """are the same""", '''type''' ]
137131
link-libraries = ["mylib::mylib"]
138-
points = [ { x = 1, y = 2 }, { x = 3, y = 4 } ]
139132

140133
# Dotted keys
141134
physical.color = "orange"
@@ -174,21 +167,10 @@ name = "Nail"
174167
sku = 284758393
175168
color = "gray"
176169

177-
[special-floats]
178-
inflection = "don't highlight"
179-
influxdb = 1
180-
181-
nan = nan
182-
183-
inf-1 = inf
184-
inf-2 = [inf, +nan, -nan, +inf, -inf]
185-
inf-3 = {inf=inf, nan=nan}
186-
187-
str-inf-1 = 'inf'
188-
str-inf-2 = "inf"
189-
str-inf-3 = """ inf nan
190-
inf nan
191-
"""
192-
str-inf-4 = """ inf nan
193-
inf nan
194-
"""
170+
[tool.poetry.dependencies]
171+
python = ">=3.10,<3.11"
172+
idna = "3.4"
173+
inflection = "0.5.1"
174+
influxdb = "5.3.1"
175+
requests = "2.28.1"
176+
pywinpty = {version = ">=1.1.0", markers = "os_name == \"nt\""}

0 commit comments

Comments
 (0)