Powershell lexer

All questions regarding lexer highlighting schemes are discussed here...
ANewUser
Posts: 50
Joined: 25.01.2020 12:15

Powershell lexer

Post by ANewUser »

I have found some issues, you decide which one will be solved.
I provide a little code snippet for copy/paste:
Thank you!
edited: if you need some list just write which one (operators, built-in variables, ...)

Code: Select all

<#
   .synopsis
      it would be nice if the documentation keywords were highlighted
   .description
      xyz
#>
function Verb-Noun
{
   [CmdletBinding()] # attribute not highlighted
   
   param
   (
      # not highlighted types
      [int32] $i32, 
      [int64] $i64,
      [Parameter(ValueFromPipeline = $true)] # attribute not highlighted
      [hashtable] $ht,  
      
      [switch] $sw, # probably the "switch" is highlighted as keyword not as type
      
      [SupportsWildcards()] # attribute not highlighted
      # ok highlighted types
      [string] $str,
      [int] $ii,
      [bool] $b
   )
   
   # typecasting [...]
   # like in the param() section: [typename]
   # maybe highlighting the typename ...
   $arrlst = [System.Collections.ArrayList] @()
   
   # number specials (the SI standard KiB MiB ...)
   # <number><kb | mb | gb | tb | pb>
   $n = 1kb * 1mb 
   $nn = 1024 * 1048576 # $nn equals $n
   
   # builtin variables not highlighted as special variables (it would be nice)
   # some exmaples
   $h = $host ; $e = $error ; $o = $ofs ; $oe = $outputencoding   
   $bt = $true ; $bf = $false ; $n = $null  
   
   # variable names can contain scope reference in the form of $scope:varname
   # the scope can be: global local script private 
   # or an integer which is >=0 
   $var1 = $global:error
   $var2 = $script:somevarname
   $var3 = $1:parentscopevar
   
   # not highlighted operators
   $s = "{0}" -f $var1
   if (-not ($a -or $b) -and ($c -or $d))
   {
      $aa = $bb -split ','
   } 
}
ANewUser
Posts: 50
Joined: 25.01.2020 12:15

Post by ANewUser »

literal number suffixes ...
https://i.imgur.com/l04v9d7.png
Alexey
Posts: 1633
Joined: 05.10.2012 22:10

Post by Alexey »

Alexey
Posts: 1633
Joined: 05.10.2012 22:10

Post by Alexey »

> it would be nice if the documentation keywords were highlighted
Currently lexer needs a sublexer for comments to do it, not a good way. so do it another way, using Cud plugin "Highlight Variables".
Alexey
Posts: 1633
Joined: 05.10.2012 22:10

Post by Alexey »

Code: Select all

[CmdletBinding()] # attribute not highlighted
[Parameter(ValueFromPipeline = $true)] # attribute not highlighted
What do you suggest, highlight any id in the [] brackets if it's followed by ( ? Why current hiliting is not good? What do other editors do?

Code: Select all

   # typecasting [...]
   # like in the param() section: [typename]
   # maybe highlighting the typename ...
   $arrlst = [System.Collections.ArrayList] @()
Currently lexer don't give new style for id in [], not sure it will be nice. what do other editors do?
Alexey
Posts: 1633
Joined: 05.10.2012 22:10

Post by Alexey »

Made lot of changes. lexer is updated in GH, download and test.
https://github.com/Alexey-T/CudaText/tr ... ata/lexlib

my big test:

Code: Select all

function Verb-Noun
{
   [CmdletBinding()] # attribute not highlighted
   
   param
   (
      # not highlighted types
      [int32] $i32,
      [int64] $i64,
      [Parameter(ValueFromPipeline = $true)] # attribute not highlighted
      [hashtable] $ht, 
     
      [switch] $sw, # probably the "switch" is highlighted as keyword not as type
     
      [SupportsWildcards()] # attribute not highlighted
      # ok highlighted types
      [string] $str,
      [int] $ii,
      [bool] $b
   )
   
   # typecasting [...]
   # like in the param() section: [typename]
   # maybe highlighting the typename ...
   $arrlst = [System.Collections.ArrayList] @()
   
   # number specials (the SI standard KiB MiB ...)
   # <number><kb | mb | gb | tb | pb>
   $n = 1kb * 1mb
   $nn = 1024 * 1048576 # $nn equals $n
   
   # builtin variables not highlighted as special variables (it would be nice)
   # some exmaples
   $h = $host ; $e = $error ; $o = $ofs ; $oe = $outputencoding   
   $bt = $true ; $bf = $false ; $n = $null 
   
   # variable names can contain scope reference in the form of $scope:varname
   # the scope can be: global local script private
   # or an integer which is >=0
   $var1 = $global:error
   $var2 = $script:somevarname
   $var3 = $1:parentscopevar
   
   # not highlighted operators
   $s = "{0}" -f $var1
   if (-not ($a -or $b) -and ($c -or $d))
   {
      $aa = $bb -split ','
   }
}
 -eq, -ne, -gt, -lt, -le, -ge ! @()
       1.2L   
PS> 1kb
1024

PS> 1.30Dmb
1363148.80

PS> 0x10Gb
17179869184

PS> 1.4e23tb
1.5393162788864E+35

PS> 0x12Lpb
20266198323167232	
                    
100
100u
100D
100l
100uL
100us
100uy
100y
1e2
1.e2
0x1e2
0x1e2L
0x1e2D
482D
482gb
482ngb
0x1e2lgb
0b1011011
0xFFFFFFFF
-0xFFFFFFFF
0xFFFFFFFFu

[byte]
[sbyte]
[Int16]
[short]
[UInt16]
[ushort]
[Int32]
[int]
[UInt32]
[uint]
[Int64]
[long]
[UInt64]
[ulong]
[bigint]
[single]
[float]
[double]
[decimal]
		 
$$
$?
$^
$_
$args
$ConsoleFileName
$Error
$Event
$EventArgs
$EventSubscriber
$ExecutionContext
$false
$foreach
$HOME
$Host
$input
$IsCoreCLR
$IsLinux
$IsMacOS
$IsWindows
$LastExitCode
$Matches
$MyInvocation
$NestedPromptLevel
$null
$PID
$PROFILE
$PSBoundParameters
$PSCmdlet
$PSCommandPath
$PSCulture
$PSDebugContext
$PSHOME
$PSItem
$PSScriptRoot
$PSSenderInfo
$PSUICulture
$PSVersionTable
$PWD
$Sender
$ShellId
$StackTrace
$switch
$this
$true

ANewUser
Posts: 50
Joined: 25.01.2020 12:15

Post by ANewUser »

Thanks!

Ok, I have downloaded the above mentioned plug-in.

"highlight any id in the [] brackets if it's followed by ( ?", yes let's try it.

The [type-name] thing is a bit interesting, the following definitions mean the same:

Code: Select all

[hashtable] $var
[System.Collections.Hashtable] $var
[Collections.Hashtable] $var
so I think to highlight the most frequently used types (type accelerators) is just fine.
There is another type related expression, accessing the static members: [type-name]::method() ...

Thank you for your help, helpfulness!
Alexey
Posts: 1633
Joined: 05.10.2012 22:10

Post by Alexey »

About id in [] brackets: IMO it's ok as now, why to highlight them?
I don't understand what do you suggest in the other part.
ANewUser
Posts: 50
Joined: 25.01.2020 12:15

Post by ANewUser »

I was in hurry and didn't delete the sentence "There is another type related expression, accessing the static members: [type-name]::method() ...", sorry.
ANewUser
Posts: 50
Joined: 25.01.2020 12:15

Post by ANewUser »

Odds and ends:
$FormatEnumerationLimit is a built-in variable

'{0}' -f $x (the -f format operator)

The ! is an "alias" for the -not operator.

The switch statement has options/switches (highlighting them as operators?): https://docs.microsoft.com/en-us/powers ... wershell-7

Code: Select all

switch [-regex|-wildcard|-exact][-casesensitive] (<value>)
{
    "string"|number|variable|{ expression } { statementlist }
    default { statementlist }
}
Case sensitivity of operators (where applicable)
https://docs.microsoft.com/en-us/powers ... wershell-7

Code: Select all

By default, all comparison operators are case-insensitive. To make a comparison operator case-sensitive, precede the operator name with a c. For example, the case-sensitive version of -eq is -ceq. To make the case-insensitivity explicit, precede the operator with an i. For example, the explicitly case-insensitive version of -eq is -ieq.
Post Reply