1 -- Copyright 2015-2017 Mitchell mitchell.att.foicica.com. See LICENSE.
2 -- PowerShell LPeg lexer.
3 -- Contributed by Jeff Stone.
5 local l
= require('lexer')
6 local token
, word_match
= l
.token
, l
.word_match
7 local P
, R
, S
= lpeg
.P
, lpeg
.R
, lpeg
.S
9 local M
= {_NAME
= 'powershell'}
12 local ws
= token(l
.WHITESPACE
, l
.space^
1)
15 local comment
= token(l
.COMMENT
, '#' * l
.nonnewline^
0)
18 local keyword
= token(l
.KEYWORD
, word_match({
19 'Begin', 'Break', 'Continue', 'Do', 'Else', 'End', 'Exit', 'For', 'ForEach',
20 'ForEach-Object', 'Get-Date', 'Get-Random', 'If', 'Param', 'Pause',
21 'Powershell', 'Process', 'Read-Host', 'Return', 'Switch', 'While',
25 -- Comparison Operators.
26 local comparison
= token(l
.KEYWORD
, '-' * word_match({
27 'and', 'as', 'band', 'bor', 'contains', 'eq', 'ge', 'gt', 'is', 'isnot', 'le',
28 'like', 'lt', 'match', 'ne', 'nomatch', 'not', 'notcontains', 'notlike', 'or',
33 local parameter
= token(l
.KEYWORD
, '-' * word_match({
34 'Confirm', 'Debug', 'ErrorAction', 'ErrorVariable', 'OutBuffer',
35 'OutVariable', 'Verbose', 'WhatIf'
39 local property
= token(l
.KEYWORD
, '.' * word_match({
40 'day', 'dayofweek', 'dayofyear', 'hour', 'millisecond', 'minute', 'month',
41 'second', 'timeofday', 'year'
45 local type = token(l
.KEYWORD
, '[' * word_match({
46 'array', 'boolean', 'byte', 'char', 'datetime', 'decimal', 'double',
47 'hashtable', 'int', 'long', 'single', 'string', 'xml'
51 local variable
= token(l
.VARIABLE
, '$' * (l
.digit^
1 + l
.word
+
52 l
.delimited_range('{}', true, true)))
55 local string = token(l
.STRING
, l
.delimited_range('"', true))
58 local number = token(l
.NUMBER
, l
.float
+ l
.integer
)
61 local operator
= token(l
.OPERATOR
, S('=!<>+-/*^&|~.,:;?()[]{}%`'))
67 {'comparison', comparison
},
68 {'parameter', parameter
},
69 {'property', property
},
71 {'variable', variable
},
74 {'operator', operator
},
79 [l
.OPERATOR
] = {['{'] = 1, ['}'] = -1}