build: set version to 0.5
[vis.git] / lua / lexers / powershell.lua
blob6ee33fdd6707017d3989141ebefca1b44f7b4467
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'}
11 -- Whitespace.
12 local ws = token(l.WHITESPACE, l.space^1)
14 -- Comments.
15 local comment = token(l.COMMENT, '#' * l.nonnewline^0)
17 -- Keywords.
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',
22 'Write-Host'
23 }, '-', true))
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',
29 'replace'
30 }, nil, true))
32 -- Parameters.
33 local parameter = token(l.KEYWORD, '-' * word_match({
34 'Confirm', 'Debug', 'ErrorAction', 'ErrorVariable', 'OutBuffer',
35 'OutVariable', 'Verbose', 'WhatIf'
36 }, nil, true))
38 -- Properties.
39 local property = token(l.KEYWORD, '.' * word_match({
40 'day', 'dayofweek', 'dayofyear', 'hour', 'millisecond', 'minute', 'month',
41 'second', 'timeofday', 'year'
42 }, nil, true))
44 -- Types.
45 local type = token(l.KEYWORD, '[' * word_match({
46 'array', 'boolean', 'byte', 'char', 'datetime', 'decimal', 'double',
47 'hashtable', 'int', 'long', 'single', 'string', 'xml'
48 }, nil, true) * ']')
50 -- Variables.
51 local variable = token(l.VARIABLE, '$' * (l.digit^1 + l.word +
52 l.delimited_range('{}', true, true)))
54 -- Strings.
55 local string = token(l.STRING, l.delimited_range('"', true))
57 -- Numbers.
58 local number = token(l.NUMBER, l.float + l.integer)
60 -- Operators.
61 local operator = token(l.OPERATOR, S('=!<>+-/*^&|~.,:;?()[]{}%`'))
63 M._rules = {
64 {'whitespace', ws},
65 {'comment', comment},
66 {'keyword', keyword},
67 {'comparison', comparison},
68 {'parameter', parameter},
69 {'property', property},
70 {'type', type},
71 {'variable', variable},
72 {'string', string},
73 {'number', number},
74 {'operator', operator},
77 M._foldsymbols = {
78 _patterns = {'[{}]'},
79 [l.OPERATOR] = {['{'] = 1, ['}'] = -1}
82 return M