aboutsummaryrefslogtreecommitdiffhomepage
path: root/lexlua/mumps.lua
blob: 8a7d7d8f11ae670503518efabf25142c13bd91f0 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
-- Copyright 2015-2018 Mitchell mitchell.att.foicica.com. See License.txt.
-- MUMPS (M) LPeg lexer.

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 = 'mumps'}

-- Whitespace.
local ws = token(l.WHITESPACE, l.space^1)

-- Comments.
local comment = token(l.COMMENT, ';' * l.nonnewline_esc^0)

-- Strings.
local string = token(l.STRING, l.delimited_range('"', true))

-- Numbers.
local number = token(l.NUMBER, l.float + l.integer) -- TODO: float?

-- Keywords.
local keyword = token(l.KEYWORD, word_match({
  -- Abbreviations.
  'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'q',
  'r', 's', 'u', 'v', 'w', 'x',
  -- Full.
  'break', 'close', 'do', 'else', 'for', 'goto', 'halt', 'hang', 'if', 'job',
  'kill', 'lock', 'merge', 'new', 'open', 'quit', 'read', 'set', 'use', 'view',
  'write', 'xecute',
  -- Cache- or GTM-specific.
  'catch', 'continue', 'elseif', 'tcommit', 'throw', 'trollback', 'try',
  'tstart', 'while',
}, nil, true))

-- Functions.
local func = token(l.FUNCTION, '$' * word_match({
  -- Abbreviations.
  'a', 'c', 'd', 'e', 'f', 'fn', 'g', 'j', 'l', 'n', 'na', 'o', 'p', 'q', 'ql',
  'qs', 'r', 're', 's', 'st', 't', 'tr', 'v',
  -- Full.
  'ascii', 'char', 'data', 'extract', 'find', 'fnumber', 'get', 'justify',
  'length', 'next', 'name', 'order', 'piece', 'query', 'qlength', 'qsubscript',
  'random', 'reverse', 'select', 'stack', 'text', 'translate', 'view',
  -- Z function abbreviations.
  'zd', 'zdh', 'zdt', 'zdth', 'zh', 'zt', 'zth', 'zu', 'zp',
  -- Z functions.
  'zabs', 'zarccos', 'zarcsin', 'zarctan', 'zcos', 'zcot', 'zcsc', 'zdate',
  'zdateh', 'zdatetime', 'zdatetimeh', 'zexp', 'zhex', 'zln', 'zlog', 'zpower',
  'zsec', 'zsin', 'zsqr', 'ztan', 'ztime', 'ztimeh', 'zutil', 'zf', 'zprevious',
  -- Cache- or GTM-specific.
  'bit', 'bitcount', 'bitfind', 'bitlogic', 'case', 'classmethod', 'classname',
  'decimal', 'double', 'factor', 'i', 'increment', 'inumber', 'isobject',
  'isvaliddouble', 'isvalidnum', 'li', 'list', 'lb', 'listbuild', 'ld',
  'listdata', 'lf', 'listfind', 'lfs', 'listfromstring', 'lg', 'listget', 'll',
  'listlength', 'listnext', 'ls', 'listsame', 'lts', 'listtostring', 'lv',
  'listvalid', 'locate', 'match', 'method', 'nc', 'nconvert', 'normalize',
  'now', 'num', 'number', 'parameter', 'prefetchoff', 'prefetchon', 'property',
  'replace', 'sc', 'sconvert', 'sortbegin', 'sortend', 'wa', 'wascii', 'wc',
  'wchar', 'we', 'wextract', 'wf', 'wfind', 'wiswide', 'wl', 'wlength', 'wre',
  'wreverse', 'xecute'
}, nil, true))

-- Variables.
local variable = token(l.VARIABLE, '$' * l.word_match({
  -- Abbreviations.
  'ec', 'es', 'et', 'h', 'i', 'j', 'k', 'p', 'q', 's', 'st', 't', 'tl',
  -- Full.
  'device', 'ecode', 'estack', 'etrap', 'halt', 'horolog', 'io', 'job',
  'namespace', 'principal', 'quit', 'roles', 'storage', 'stack', 'system',
  'test', 'this', 'tlevel', 'username', 'x', 'y',
  -- Z variable abbreviations.
  'za', 'zb', 'zc', 'ze', 'zh', 'zi', 'zj', 'zm', 'zn', 'zo', 'zp', 'zr', 'zs',
  'zt', 'zts', 'ztz', 'zv',
  -- Z variables.
  'zchild', 'zeof', 'zerror', 'zhorolog', 'zio', 'zjob', 'zmode', 'zname',
  'znspace', 'zorder', 'zparent', 'zpi', 'zpos', 'zreference', 'zstorage',
  'ztimestamp', 'ztimezone', 'ztrap', 'zversion',
}, nil, true))

-- Function entity.
local entity = token(l.LABEL, l.starts_line(('%' + l.alpha) * l.alnum^0))

-- Support functions.
local support_function = '$$' * ('%' + l.alpha) * l.alnum^0 *
                         (('%' + l.alpha) * l.alnum^0)^-1

-- Identifiers.
local identifier = token(l.IDENTIFIER, l.alpha * l.alnum^0)

-- Operators.
local operator = token(l.OPERATOR, S('+-/*<>!=_@#&|?:\\\',()[]'))

M._rules = {
  {'whitespace', ws},
  {'keyword', keyword},
  {'variable', variable},
  {'identifier', identifier},
  {'string', string},
  {'comment', comment},
  {'number', number},
  {'operator', operator},
}

M._foldsymbols = {
  _patterns = {'%l+', '[{}]', '/%*', '%*/', '//'},
  [l.PREPROCESSOR] = {['if'] = 1, ifdef = 1, ifndef = 1, endif = -1},
  [l.OPERATOR] = {['{'] = 1, ['}'] = -1},
  [l.COMMENT] = {['/*'] = 1, ['*/'] = -1, ['//'] = l.fold_line_comments('//')}
}

return M