Skip to content
This repository has been archived by the owner on Sep 8, 2023. It is now read-only.

Commit

Permalink
Improve handling of data structure literals in PowerShell lexer (roug…
Browse files Browse the repository at this point in the history
…e-ruby#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.
  • Loading branch information
jneen authored and mattt committed May 19, 2021
1 parent d66c0e1 commit 8069683
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
36 changes: 24 additions & 12 deletions lib/rouge/lexers/powershell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,34 +131,43 @@ class Powershell < RegexLexer
rule %r/[:,]/, Punctuation
end

state :hasht do
rule %r/\s+/, Text::Whitespace
rule %r/\}/, Punctuation, :pop!
state :expr do
mixin :comments
rule %r/"/, Str::Double, :dq
rule %r/'/, Str::Single, :sq
rule %r/[{]/, Punctuation, :brace
end

state :hasht do
rule %r/\}/, Punctuation, :pop!
rule %r/\w+/, Name::Other
rule %r/=/, Operator
rule %r/,/, Punctuation
mixin :expr
mixin :variable
end

state :array do
rule %r/\s+/, Text::Whitespace
rule %r/\)/, Punctuation, :pop!
rule %r/"/, Str::Double, :dq
rule %r/'/, Str::Single, :sq
rule %r/,/, Punctuation
mixin :expr
mixin :variable
end

state :brace do
rule %r/[}]/, Punctuation, :pop!
mixin :root
end

state :bracket do
rule %r/\]/, Punctuation, :pop!
rule %r/[A-Za-z]\w+\./, Name::Constant
rule %r/[A-Za-z]\w+\./, Name
rule %r/([A-Za-z]\w+)/ do |m|
if ATTRIBUTES.include? m[0]
token Name::Builtin::Pseudo
else
token Keyword::Type
token Name
end
end
mixin :root
Expand All @@ -174,12 +183,15 @@ class Powershell < RegexLexer
mixin :root
end

state :root do
state :comments do
rule %r/\s+/, Text::Whitespace

rule %r/#requires\s-version \d(?:\.\d+)?/, Comment::Preproc
rule %r/#.*/, Comment
rule %r/<#/, Comment::Multiline, :multiline
end

state :root do
mixin :comments
rule %r/#requires\s-version \d(?:\.\d+)?/, Comment::Preproc

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

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

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

Expand Down
10 changes: 10 additions & 0 deletions spec/visual/samples/powershell
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ $my_hash = @{
thing = "table"
}

$my_complex_hash = @{
# comment
foo = {
if ($var1 -eq $var2)
{
return $true
}
}
}

$my_array = @("my" "array")

###########################
Expand Down

0 comments on commit 8069683

Please sign in to comment.