vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
d.lua
(6541B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- D LPeg lexer.
3 -- Heavily modified by Brian Schott (@Hackerpilot on Github).
4
5 local lexer = lexer
6 local P, S = lpeg.P, lpeg.S
7
8 local lex = lexer.new(...)
9
10 -- Class names.
11 local ws = lex:get_rule('whitespace')
12 lex:add_rule('class',
13 lex:tag(lexer.TYPE, P('class') + 'struct') * ws^-1 * lex:tag(lexer.CLASS, lexer.word))
14
15 -- Versions.
16 local open_paren = lex:tag(lexer.OPERATOR, '(')
17 lex:add_rule('version', lex:tag(lexer.KEYWORD, 'version') * ws^-1 * open_paren * ws^-1 *
18 lex:tag(lexer.CONSTANT_BUILTIN .. '.version', lex:word_match('version')))
19
20 -- Scopes.
21 lex:add_rule('scope', lex:tag(lexer.KEYWORD, 'scope') * ws^-1 * open_paren * ws^-1 *
22 lex:tag(lexer.CONSTANT_BUILTIN .. '.scope', lexer.word_match('exit success failure')))
23
24 -- Traits.
25 lex:add_rule('trait', lex:tag(lexer.KEYWORD, '__traits') * ws^-1 * open_paren * ws^-1 *
26 lex:tag(lexer.VARIABLE_BUILTIN .. '.traits', lex:word_match('trait')))
27
28 -- Function names.
29 local func = lex:tag(lexer.FUNCTION, lexer.word)
30 local method = lpeg.B('.') * lex:tag(lexer.FUNCTION_METHOD, lexer.word)
31 lex:add_rule('function', (method + func) * #(ws^-1 * ('!' * lexer.word^-1 * ws^-1)^-1 * '('))
32
33 -- Keywords.
34 lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)))
35
36 -- Types.
37 lex:add_rule('type', lex:tag(lexer.TYPE, lex:word_match(lexer.TYPE)))
38
39 -- Constants.
40 lex:add_rule('constant', lex:tag(lexer.CONSTANT_BUILTIN, lex:word_match(lexer.CONSTANT_BUILTIN)))
41
42 -- Properties.
43 local dot = lex:tag(lexer.OPERATOR, '.')
44 lex:add_rule('property', lpeg.B(lexer.alnum + ')') * dot *
45 lex:tag(lexer.VARIABLE_BUILTIN, lex:word_match('property')))
46
47 -- Strings.
48 local sq_str = lexer.range("'", true) * S('cwd')^-1
49 local dq_str = lexer.range('"') * S('cwd')^-1
50 local lit_str = 'r' * lexer.range('"', false, false) * S('cwd')^-1
51 local bt_str = lexer.range('`', false, false) * S('cwd')^-1
52 local hex_str = 'x' * lexer.range('"') * S('cwd')^-1
53 local other_hex_str = '\\x' * (lexer.xdigit * lexer.xdigit)^1
54 local str = sq_str + dq_str + lit_str + bt_str + hex_str + other_hex_str
55 for left, right in pairs{['['] = ']', ['('] = ')', ['{'] = '}', ['<'] = '>'} do
56 str = str + lexer.range('q"' .. left, right .. '"', false, false, true) * S('cwd')^-1
57 end
58 lex:add_rule('string', lex:tag(lexer.STRING, str))
59
60 -- Identifiers.
61 lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, lexer.word))
62
63 -- Comments.
64 local line_comment = lexer.to_eol('//', true)
65 local block_comment = lexer.range('/*', '*/')
66 local nested_comment = lexer.range('/+', '+/', false, false, true)
67 lex:add_rule('comment', lex:tag(lexer.COMMENT, line_comment + block_comment + nested_comment))
68
69 -- Numbers.
70 lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.number_('_') * S('uULdDfFi')^-1))
71
72 -- Preprocessor.
73 lex:add_rule('annotation', lex:tag(lexer.ANNOTATION, '@' * lexer.word^1))
74 lex:add_rule('preprocessor', lex:tag(lexer.PREPROCESSOR, lexer.to_eol('#')))
75
76 -- Operators.
77 lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('?=!<>+-*$/%&|^~.,;:()[]{}')))
78
79 -- Fold points.
80 lex:add_fold_point(lexer.OPERATOR, '{', '}')
81 lex:add_fold_point(lexer.COMMENT, '/*', '*/')
82 lex:add_fold_point(lexer.COMMENT, '/+', '+/')
83
84 -- Word lists.
85 lex:set_word_list('version', {
86 'AArch64', 'AIX', 'all', 'Alpha', 'Alpha_HardFloat', 'Alpha_SoftFloat', 'Android', 'ARM',
87 'ARM_HardFloat', 'ARM_SoftFloat', 'ARM_SoftFP', 'ARM_Thumb', 'assert', 'BigEndian', 'BSD',
88 'Cygwin', 'D_Coverage', 'D_Ddoc', 'D_HardFloat', 'DigitalMars', 'D_InlineAsm_X86',
89 'D_InlineAsm_X86_64', 'D_LP64', 'D_NoBoundsChecks', 'D_PIC', 'DragonFlyBSD', 'D_SIMD',
90 'D_SoftFloat', 'D_Version2', 'D_X32', 'FreeBSD', 'GNU', 'Haiku', 'HPPA', 'HPPA64', 'Hurd', 'IA64',
91 'LDC', 'linux', 'LittleEndian', 'MIPS32', 'MIPS64', 'MIPS_EABI', 'MIPS_HardFloat', 'MIPS_N32',
92 'MIPS_N64', 'MIPS_O32', 'MIPS_O64', 'MIPS_SoftFloat', 'NetBSD', 'none', 'OpenBSD', 'OSX', 'Posix',
93 'PPC', 'PPC64', 'PPC_HardFloat', 'PPC_SoftFloat', 'S390', 'S390X', 'SDC', 'SH', 'SH64', 'SkyOS',
94 'Solaris', 'SPARC', 'SPARC64', 'SPARC_HardFloat', 'SPARC_SoftFloat', 'SPARC_V8Plus', 'SysV3',
95 'SysV4', 'unittest', 'Win32', 'Win64', 'Windows', 'X86', 'X86_64'
96 })
97
98 lex:set_word_list('trait', {
99 'allMembers', 'classInstanceSize', 'compiles', 'derivedMembers', 'getAttributes', 'getMember',
100 'getOverloads', 'getProtection', 'getUnitTests', 'getVirtualFunctions', 'getVirtualIndex',
101 'getVirtualMethods', 'hasMember', 'identifier', 'isAbstractClass', 'isAbstractFunction',
102 'isArithmetic', 'isAssociativeArray', 'isFinalClass', 'isFinalFunction', 'isFloating',
103 'isIntegral', 'isLazy', 'isNested', 'isOut', 'isOverrideFunction', 'isPOD', 'isRef', 'isSame',
104 'isScalar', 'isStaticArray', 'isStaticFunction', 'isUnsigned', 'isVirtualFunction',
105 'isVirtualMethod', 'parent'
106 })
107
108 lex:set_word_list(lexer.KEYWORD, {
109 'abstract', 'align', 'asm', 'assert', 'auto', 'body', 'break', 'case', 'cast', 'catch', 'const',
110 'continue', 'debug', 'default', 'delete', 'deprecated', 'do', 'else', 'extern', 'export', 'false',
111 'final', 'finally', 'for', 'foreach', 'foreach_reverse', 'goto', 'if', 'import', 'immutable',
112 'in', 'inout', 'invariant', 'is', 'lazy', 'macro', 'mixin', 'new', 'nothrow', 'null', 'out',
113 'override', 'pragma', 'private', 'protected', 'public', 'pure', 'ref', 'return', 'scope',
114 'shared', 'static', 'super', 'switch', 'synchronized', 'this', 'throwtrue', 'try', 'typeid',
115 'typeof', 'unittest', 'version', 'virtual', 'volatile', 'while', 'with', '__gshared', '__thread',
116 '__traits', '__vector', '__parameters'
117 })
118
119 lex:set_word_list(lexer.TYPE, {
120 'alias', 'bool', 'byte', 'cdouble', 'cent', 'cfloat', 'char', 'class', 'creal', 'dchar',
121 'delegate', 'double', 'enum', 'float', 'function', 'idouble', 'ifloat', 'int', 'interface',
122 'ireal', 'long', 'module', 'package', 'ptrdiff_t', 'real', 'short', 'size_t', 'struct',
123 'template', 'typedef', 'ubyte', 'ucent', 'uint', 'ulong', 'union', 'ushort', 'void', 'wchar',
124 'string', 'wstring', 'dstring', 'hash_t', 'equals_t'
125 })
126
127 lex:set_word_list(lexer.CONSTANT_BUILTIN, {
128 '__FILE__', '__LINE__', '__DATE__', '__EOF__', '__TIME__', '__TIMESTAMP__', '__VENDOR__',
129 '__VERSION__', '__FUNCTION__', '__PRETTY_FUNCTION__', '__MODULE__'
130 })
131
132 lex:set_word_list('property', {
133 'alignof', 'dig', 'dup', 'epsilon', 'idup', 'im', 'init', 'infinity', 'keys', 'length',
134 'mangleof', 'mant_dig', 'max', 'max_10_exp', 'max_exp', 'min', 'min_normal', 'min_10_exp',
135 'min_exp', 'nan', 'offsetof', 'ptr', 're', 'rehash', 'reverse', 'sizeof', 'sort', 'stringof',
136 'tupleof', 'values'
137 })
138
139 lexer.property['scintillua.comment'] = '//'
140
141 return lex