Skip to content

Commit 9b5b4d8

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 9b5b4d8

File tree

2 files changed

+77
-56
lines changed

2 files changed

+77
-56
lines changed

lib/rouge/lexers/toml.rb

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,63 +11,56 @@ 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+|"[^"]+"|'[^']+')/
16-
17-
state :basic do
18-
rule %r/\s+/, Text
19-
rule %r/#.*?$/, Comment
20-
rule %r/(true|false)/, Keyword::Constant
21-
22-
rule %r/(#{identifier})(\s*)(=)(\s*)(\{)/ do
23-
groups Name::Property, Text, Operator, Text, Punctuation
24-
push :inline
25-
end
26-
end
27-
2814
state :root do
29-
mixin :basic
15+
mixin :whitespace
3016

31-
rule %r/(?<!=)\s*\[.*?\]+/, Name::Namespace
17+
mixin :key
3218

33-
rule %r/(#{identifier})(\s*)(=)/ do
34-
groups Name::Property, Text, Punctuation
19+
rule %r/(=)(\s*)/ do
20+
groups Operator, Text::Whitespace
3521
push :value
3622
end
37-
end
3823

39-
state :value do
40-
rule %r/\n/, Text, :pop!
41-
mixin :content
24+
rule %r/\[\[?/, Keyword, :table_key
4225
end
4326

44-
state :content do
45-
mixin :basic
46-
47-
rule %r/(#{identifier})(\s*)(=)/ do
48-
groups Name::Property, Text, Punctuation
49-
end
27+
state :key do
28+
rule %r/[A-Za-z0-9_-]+/, Name
5029

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
30+
rule %r/"/, Str, :dq
31+
rule %r/'/, Str, :sq
32+
rule %r/\./, Punctuation
33+
end
5734

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
35+
state :table_key do
36+
rule %r/[A-Za-z0-9_-]+/, Name
6237

63-
rule %r/"""/, Str, :mdq
6438
rule %r/"/, Str, :dq
65-
rule %r/'''/, Str, :msq
6639
rule %r/'/, Str, :sq
67-
mixin :esc_str
68-
rule %r/\,/, Punctuation
69-
rule %r/\[/, Punctuation, :array
70-
rule %r/\{/, Punctuation, :inline
40+
rule %r/\./, Keyword
41+
rule %r/\]\]?/, Keyword, :pop!
42+
rule %r/[ \t]+/, Text::Whitespace
43+
end
44+
45+
state :value do
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:\d\d(\.\d+)?/, Literal::Date, :pop!
48+
rule %r/[+-]?\d+(?:_\d+)*\.\d+(?:_\d+)*(?:[eE][+-]?\d+(?:_\d+)*)?/, Num::Float, :pop!
49+
rule %r/[+-]?\d+(?:_\d+)*[eE][+-]?\d+(?:_\d+)*/, Num::Float, :pop!
50+
rule %r/[+-]?(?:nan|inf)/, Num::Float, :pop!
51+
rule %r/0x\h+(?:_\h+)*/, Num::Hex, :pop!
52+
rule %r/0o[0-7]+(?:_[0-7]+)*/, Num::Oct, :pop!
53+
rule %r/0b[01]+(?:_[01]+)*/, Num::Bin, :pop!
54+
rule %r/[+-]?\d+(?:_\d+)*/, Num::Integer, :pop!
55+
56+
rule %r/"""/, Str, [:pop!, :mdq]
57+
rule %r/"/, Str, [:pop!, :dq]
58+
rule %r/'''/, Str, [:pop!, :msq]
59+
rule %r/'/, Str, [:pop!, :sq]
60+
61+
rule %r/(true|false)/, Keyword::Constant, :pop!
62+
rule %r/\[/, Punctuation, [:pop!, :array]
63+
rule %r/\{/, Punctuation, [:pop!, :inline]
7164
end
7265

7366
state :dq do
@@ -101,15 +94,31 @@ class TOML < RegexLexer
10194
end
10295

10396
state :array do
104-
mixin :content
97+
mixin :whitespace
98+
rule %r/,/, Punctuation
99+
105100
rule %r/\]/, Punctuation, :pop!
101+
102+
rule %r//, Token, :value
106103
end
107104

108105
state :inline do
109-
mixin :content
106+
rule %r/[ \t]+/, Text::Whitespace
110107

108+
mixin :key
109+
rule %r/(=)(\s*)/ do
110+
groups Punctuation, Text::Whitespace
111+
push :value
112+
end
113+
114+
rule %r/,/, Punctuation
111115
rule %r/\}/, Punctuation, :pop!
112116
end
117+
118+
state :whitespace do
119+
rule %r/\s+/, Text
120+
rule %r/#.*?$/, Comment
121+
end
113122
end
114123
end
115124
end

spec/visual/samples/toml

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ organization = "GitHub"
88
bio = "GitHub Cofounder & CEO\nLikes tater tots and beer."
99
# First class dates? Why not?
1010
dob = [
11-
1979-05-27T07:32:00Z,
12-
1979-05-27 07:32:00
13-
1979-05-27,
14-
07:32:00,
11+
1979-05-27T07:32:00Z,
12+
1979-05-27 07:32:00
13+
1979-05-27,
14+
07:32:00,
1515
]
1616

1717
[database]
@@ -106,16 +106,14 @@ test_string = "You'll hate me after this - #" # " Annoying, isn't it?
106106
harder_test_string = " And when \"'s are in the string, along with # \"" # "and comments are there too"
107107
# Things will get harder
108108

109-
[the.hard.bit#]
110-
what? = "You don't think some user won't do that?"
109+
[the.hard."bit#"]
110+
"what?" = "You don't think some user won't do that?"
111111
multi_line_array = [
112112
"]",
113113
"Oi!\n",
114114
# ] Oh yes I did
115115
]
116116

117-
東京都 = 123
118-
119117
# Inline table
120118
name = { first = "Tom", last = "Preston-Werner" }
121119
point = { x = 1, y = 2 }
@@ -127,6 +125,7 @@ point = { x = 1, y = 2 }
127125
"ʎǝʞ" = "value"
128126
'key2' = "value"
129127
'quoted "value"' = "value"
128+
"東京都" = 123
130129

131130
# Array
132131
integers = [ 1, 2, 3 ]
@@ -187,8 +186,21 @@ inf-3 = {inf=inf, nan=nan}
187186
str-inf-1 = 'inf'
188187
str-inf-2 = "inf"
189188
str-inf-3 = """ inf nan
190-
inf nan
189+
inf nan
191190
"""
192191
str-inf-4 = """ inf nan
193-
inf nan
192+
inf nan
194193
"""
194+
195+
[tool.poetry.dependencies]
196+
python = ">=3.10,<3.11"
197+
idna = "3.4"
198+
inflection = "0.5.1"
199+
influxdb = "5.3.1"
200+
requests = "2.28.1"
201+
pywinpty = {version = ">=1.1.0", markers = "os_name == \"nt\""}
202+
203+
dependencies = [
204+
"aiohttp>=3.11.16,<4.0.0",
205+
"backoff>=2.2.1,<3.0.0",
206+
]

0 commit comments

Comments
 (0)