vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

powershell.lua

(2047B)


      1 -- Copyright 2015-2025 Mitchell. See LICENSE.
      2 -- PowerShell LPeg lexer.
      3 -- Contributed by Jeff Stone.
      4 
      5 local lexer = require('lexer')
      6 local token, word_match = lexer.token, lexer.word_match
      7 local P, S = lpeg.P, lpeg.S
      8 
      9 local lex = lexer.new('powershell')
     10 
     11 -- Whitespace.
     12 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
     13 
     14 -- Comments.
     15 lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol('#')))
     16 
     17 -- Keywords.
     18 lex:add_rule('keyword', token(lexer.KEYWORD, word_match({
     19 	'Begin', 'Break', 'Continue', 'Do', 'Else', 'End', 'Exit', 'For', 'ForEach', 'ForEach-Object',
     20 	'Get-Date', 'Get-Random', 'If', 'Param', 'Pause', 'Powershell', 'Process', 'Read-Host', 'Return',
     21 	'Switch', 'While', 'Write-Host'
     22 }, true)))
     23 
     24 -- Comparison Operators.
     25 lex:add_rule('comparison', token(lexer.KEYWORD, '-' * word_match({
     26 	'and', 'as', 'band', 'bor', 'contains', 'eq', 'ge', 'gt', 'is', 'isnot', 'le', 'like', 'lt',
     27 	'match', 'ne', 'nomatch', 'not', 'notcontains', 'notlike', 'or', 'replace'
     28 }, true)))
     29 
     30 -- Parameters.
     31 lex:add_rule('parameter', token(lexer.KEYWORD, '-' *
     32 	word_match('Confirm Debug ErrorAction ErrorVariable OutBuffer OutVariable Verbose WhatIf', true)))
     33 
     34 -- Properties.
     35 lex:add_rule('property', token(lexer.KEYWORD, '.' *
     36 	word_match('day dayofweek dayofyear hour millisecond minute month second timeofday year', true)))
     37 
     38 -- Types.
     39 lex:add_rule('type', token(lexer.KEYWORD, '[' * word_match({
     40 	'array', 'boolean', 'byte', 'char', 'datetime', 'decimal', 'double', 'hashtable', 'int', 'long',
     41 	'single', 'string', 'xml'
     42 }, true) * ']'))
     43 
     44 -- Variables.
     45 lex:add_rule('variable', token(lexer.VARIABLE,
     46 	'$' * (lexer.digit^1 + lexer.word + lexer.range('{', '}', true))))
     47 
     48 -- Strings.
     49 lex:add_rule('string', token(lexer.STRING, lexer.range('"', true)))
     50 
     51 -- Numbers.
     52 lex:add_rule('number', token(lexer.NUMBER, lexer.number))
     53 
     54 -- Operators.
     55 lex:add_rule('operator', token(lexer.OPERATOR, S('=!<>+-/*^&|~.,:;?()[]{}%`')))
     56 
     57 -- Fold points.
     58 lex:add_fold_point(lexer.OPERATOR, '{', '}')
     59 
     60 lexer.property['scintillua.comment'] = '#'
     61 
     62 return lex