From 75b5925588bd62eedd5151909b77efdd715c3f4e Mon Sep 17 00:00:00 2001 From: "http://jneen.net/" Date: Sat, 26 Sep 2020 09:15:58 -0400 Subject: [PATCH] powershell: more robust handling of data structure literals fixes #1579 --- lib/rouge/lexers/powershell.rb | 36 ++++++++++++++++++++++------------ spec/visual/samples/powershell | 10 ++++++++++ 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/lib/rouge/lexers/powershell.rb b/lib/rouge/lexers/powershell.rb index f87c3846a1..3b3e25e074 100644 --- a/lib/rouge/lexers/powershell.rb +++ b/lib/rouge/lexers/powershell.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/visual/samples/powershell b/spec/visual/samples/powershell index ee5567d6c9..270e146426 100644 --- a/spec/visual/samples/powershell +++ b/spec/visual/samples/powershell @@ -52,6 +52,16 @@ $my_hash = @{ thing = "table" } +$my_complex_hash = @{ + # comment + foo = { + if ($var1 -eq $var2) + { + return $true + } + } +} + $my_array = @("my" "array") ###########################