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