Commits

gentoo90  committed b8830d8

PowerShellLexer: more fixes

* '$(...)' blocks inside double quoted strings and here-strings now highlighted as code
* Here-strings can't be nested directly. Only inside '$(...)' block
* Add new example with here-strings
* Add 'throw' keyword
* Remove redundant code

  • Participants
  • Parent commits ee3480f

Comments (0)

Files changed (2)

File pygments/lexers/shell.py

         'dynamicparam do default continue cmdletbinding break begin alias \\? '
         '% #script #private #local #global mandatory parametersetname position '
         'valuefrompipeline valuefrompipelinebypropertyname '
-        'valuefromremainingarguments helpmessage try catch').split()
+        'valuefromremainingarguments helpmessage try catch throw').split()
 
     operators = (
         'and as band bnot bor bxor casesensitive ccontains ceq cge cgt cle '
 
     tokens = {
         'root': [
+            # we need to count pairs of parentheses for correct highlight
+            # of '$(...)' blocks in strings
+            (r'\(', Punctuation, '#push'),
+            (r'\)', Punctuation, '#pop'),
             (r'\s+', Text),
             (r'^(\s*#[#\s]*)(\.(?:%s))([^\n]*$)' % '|'.join(commenthelp),
              bygroups(Comment, String.Doc, Comment)),
             (r'#[^\n]*?$', Comment),
             (r'(&lt;|<)#', Comment.Multiline, 'multline'),
             (r'@"\n', String.Heredoc, 'heredoc-double'),
-            (r"@'\n", String.Heredoc, 'heredoc-single'),
+            (r"@'\n.*?\n'@", String.Heredoc),
             # escaped syntax
             (r'`[\'"$@-]', Punctuation),
             (r'"', String.Double, 'string'),
             (r'\[[a-z_\[][a-z0-9_. `,\[\]]*\]', Name.Constant),  # .net [type]s
             (r'-[a-z_][a-z0-9_]*', Name),
             (r'\w+', Name),
-            (r'([.,;@{}\[\]$()=+*/\\&%!~?^`|<>-]|::)', Punctuation),
+            (r'[.,;@{}\[\]$()=+*/\\&%!~?^`|<>-]|::', Punctuation),
         ],
         'multline': [
             (r'[^#&.]+', Comment.Multiline),
         'string': [
             (r"`[0abfnrtv'\"\$]", String.Escape),
             (r'[^$`"]+', String.Double),
-            (r'\$\(', String.Interpol, 'interpol'),
+            (r'\$\(', Punctuation, 'root'),
             (r'""', String.Double),
             (r'[`$]', String.Double),
             (r'"', String.Double, '#pop'),
         ],
         'heredoc-double': [
-            (r'@"\n', String.Heredoc, '#push'),
-            (r"@'\n", String.Heredoc, 'heredoc-single'),
             (r'\n"@', String.Heredoc, '#pop'),
+            (r'\$\(', Punctuation, 'root'),
+            (r'[^@\n]+"]', String.Heredoc),
             (r".", String.Heredoc),
-        ],
-        'heredoc-single': [
-            (r'@"\n', String.Heredoc, 'heredoc-double'),
-            (r"@'\n", String.Heredoc, '#push'),
-            (r"\n'@", String.Heredoc, '#pop'),
-            (r".", String.Heredoc),
-        ],
-        'interpol': [
-            (r'[^$)]+', String.Interpol),
-            (r'\$\(', String.Interpol, '#push'),
-            (r'\)', String.Interpol, '#pop'),
         ]
     }

File tests/examplefiles/Get-CommandDefinitionHtml.ps1

+
+function Get-CommandDefinitionHtml {
+
+    # this tells powershell to allow advanced features,
+    # like the [validatenotnullorempty()] attribute below.
+    [CmdletBinding()]
+    param(
+        [ValidateNotNullOrEmpty()]
+        [string]$name
+    )
+
+    $command = get-command $name
+
+    # Look mom! I'm a cmdlet!
+    $PSCmdlet.WriteVerbose("Dumping HTML for " + $command)
+
+@"
+    <html>
+        <head>
+            <title>$($command.name)</title>
+        </head>
+        <body>
+            <table border="1">
+$(
+    $command.parametersets | % {
+@"
+
+            <tr>
+                <td>$($_.name)</td>
+                <td>
+                    <table border="1">
+                        <tr>
+                            <th colspan="8">Parameters</th>
+
+$(
+        $count = 0
+        $_.parameters | % {
+            if (0 -eq ($count % 8)) {
+@'
+                        </tr>
+                        <tr>
+'@
+            }
+@"
+                            <td>$($_.name)</td>
+"@
+            $count++
+    }
+)
+                        </tr>
+                    </table>
+                </td>
+            </tr>
+"@
+    }
+)
+            </table>
+        </body>
+    </html>
+"@
+}
+
+Get-CommandDefinitionHtml get-item > out.html
+
+# show in browser
+invoke-item out.html