Skip to content

Commit d7fd623

Browse files
authored
Improve handling of data structure literals in PowerShell lexer (#1595)
The PowerShell lexer includes a hash table state as a special case and is not tested on more complex tables (e.g. including comments and code). This commit factors out the hash table state into a more generic `:expr` state that can handle more complex structures. This commit also reduces the overall level of highlighting. This improves the visibility of the more important syntax.
1 parent b4cdfaf commit d7fd623

2 files changed

Lines changed: 34 additions & 12 deletions

File tree

lib/rouge/lexers/powershell.rb

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,34 +131,43 @@ class Powershell < RegexLexer
131131
rule %r/[:,]/, Punctuation
132132
end
133133

134-
state :hasht do
135-
rule %r/\s+/, Text::Whitespace
136-
rule %r/\}/, Punctuation, :pop!
134+
state :expr do
135+
mixin :comments
137136
rule %r/"/, Str::Double, :dq
138137
rule %r/'/, Str::Single, :sq
138+
rule %r/[{]/, Punctuation, :brace
139+
end
140+
141+
state :hasht do
142+
rule %r/\}/, Punctuation, :pop!
139143
rule %r/\w+/, Name::Other
140144
rule %r/=/, Operator
141145
rule %r/,/, Punctuation
146+
mixin :expr
142147
mixin :variable
143148
end
144149

145150
state :array do
146151
rule %r/\s+/, Text::Whitespace
147152
rule %r/\)/, Punctuation, :pop!
148-
rule %r/"/, Str::Double, :dq
149-
rule %r/'/, Str::Single, :sq
150153
rule %r/,/, Punctuation
154+
mixin :expr
151155
mixin :variable
152156
end
153157

158+
state :brace do
159+
rule %r/[}]/, Punctuation, :pop!
160+
mixin :root
161+
end
162+
154163
state :bracket do
155164
rule %r/\]/, Punctuation, :pop!
156-
rule %r/[A-Za-z]\w+\./, Name::Constant
165+
rule %r/[A-Za-z]\w+\./, Name
157166
rule %r/([A-Za-z]\w+)/ do |m|
158167
if ATTRIBUTES.include? m[0]
159168
token Name::Builtin::Pseudo
160169
else
161-
token Keyword::Type
170+
token Name
162171
end
163172
end
164173
mixin :root
@@ -174,12 +183,15 @@ class Powershell < RegexLexer
174183
mixin :root
175184
end
176185

177-
state :root do
186+
state :comments do
178187
rule %r/\s+/, Text::Whitespace
179-
180-
rule %r/#requires\s-version \d(?:\.\d+)?/, Comment::Preproc
181188
rule %r/#.*/, Comment
182189
rule %r/<#/, Comment::Multiline, :multiline
190+
end
191+
192+
state :root do
193+
mixin :comments
194+
rule %r/#requires\s-version \d(?:\.\d+)?/, Comment::Preproc
183195

184196
rule %r/"/, Str::Double, :dq
185197
rule %r/'/, Str::Single, :sq
@@ -204,12 +216,12 @@ class Powershell < RegexLexer
204216
rule %r/-{1,2}\w+/, Name::Tag
205217

206218
rule %r/(\.)?([-\w]+)(\[)/ do |m|
207-
groups Operator, Name::Function, Punctuation
219+
groups Operator, Name, Punctuation
208220
push :bracket
209221
end
210222

211223
rule %r/([\/\\~\w][-.:\/\\~\w]*)(\n)?/ do |m|
212-
groups Name::Function, Text::Whitespace
224+
groups Name, Text::Whitespace
213225
push :parameters
214226
end
215227

spec/visual/samples/powershell

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ $my_hash = @{
5252
thing = "table"
5353
}
5454

55+
$my_complex_hash = @{
56+
# comment
57+
foo = {
58+
if ($var1 -eq $var2)
59+
{
60+
return $true
61+
}
62+
}
63+
}
64+
5565
$my_array = @("my" "array")
5666

5767
###########################

0 commit comments

Comments
 (0)