Skip to content

Commit

Permalink
Improve lexing of nested data structures in PowerShell lexer (#1622)
Browse files Browse the repository at this point in the history
While #1595 improved the lexing of more complex structures, the
PowerShell lexer is limited in its support for nested data structures.
This commit moves more of the syntax for representing values from the
`:root` state to the `:expr` state so that this syntax can work when
nested.
  • Loading branch information
pyrmont authored Nov 10, 2020
1 parent f10994d commit d7e67da
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
36 changes: 17 additions & 19 deletions lib/rouge/lexers/powershell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,29 @@ class Powershell < RegexLexer
mixin :comments
rule %r/"/, Str::Double, :dq
rule %r/'/, Str::Single, :sq
rule %r/[{]/, Punctuation, :brace
rule %r/@"/, Str::Heredoc, :heredoc
rule %r/@'.*?'@/m, Str::Heredoc
rule %r/\d*\.\d+/, Num::Float
rule %r/\d+/, Num::Integer
rule %r/@\{/, Punctuation, :hasht
rule %r/@\(/, Punctuation, :array
rule %r/{/, Punctuation, :brace
rule %r/\[/, Punctuation, :bracket
end

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

state :array do
rule %r/\s+/, Text::Whitespace
rule %r/\)/, Punctuation, :pop!
rule %r/,/, Punctuation
rule %r/[,;]/, Punctuation
mixin :expr
mixin :variable
end
Expand Down Expand Up @@ -193,14 +200,6 @@ class Powershell < RegexLexer
mixin :comments
rule %r/#requires\s-version \d(?:\.\d+)?/, Comment::Preproc

rule %r/"/, Str::Double, :dq
rule %r/'/, Str::Single, :sq
rule %r/@"/, Str::Heredoc, :heredoc
rule %r/@'.*?'@/m, Str::Heredoc

rule %r/\d*\.\d+/, Num::Float
rule %r/\d+/, Num::Integer

rule %r/\.\.(?=\.?\d)/, Operator
rule %r/(?:#{OPERATORS})\b/i, Operator

Expand All @@ -220,24 +219,23 @@ class Powershell < RegexLexer
push :bracket
end

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

rule %r/(\.)?([-\w]+)(?:(\()|(\n))?/ do |m|
rule %r/(\.)([-\w]+)(?:(\()|(\n))?/ do |m|
groups Operator, Name::Function, Punctuation, Text::Whitespace
push :parameters unless m[3].nil?
end

rule %r/\?/, Name::Function, :parameters
rule %r/[-+*\/%=!.&|]/, Operator
rule %r/@\{/, Punctuation, :hasht
rule %r/@\(/, Punctuation, :array
rule %r/\[/, Punctuation, :bracket
rule %r/[{}(),:;]/, Punctuation

mixin :expr
mixin :variable

rule %r/[-+*\/%=!.&|]/, Operator
rule %r/[{}(),:;]/, Punctuation
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions spec/visual/samples/powershell
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ $my_complex_hash = @{

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

$myobject = [PSCustomObject]@{
Name = 'Alice';
Age = 42;
}

$myarray = @(
[PSCustomObject]@{
Name = 'Bob';
Age = 31;
},
[PSCustomObject]@{
Name = 'Charlie';
Age = 41;
}
)

###########################
# Commands
###########################
Expand Down

0 comments on commit d7e67da

Please sign in to comment.