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

powershell: more robust handling of data structure literals #1595

Merged
merged 1 commit into from
Nov 9, 2020
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
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
pyrmont marked this conversation as resolved.
Show resolved Hide resolved
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