vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
boo.lua
(2442B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- Boo LPeg lexer.
3
4 local lexer = require('lexer')
5 local token, word_match = lexer.token, lexer.word_match
6 local P, S = lpeg.P, lpeg.S
7
8 local lex = lexer.new('boo')
9
10 -- Whitespace.
11 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
12
13 -- Keywords.
14 lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
15 'and', 'break', 'cast', 'continue', 'elif', 'else', 'ensure', 'except', 'for', 'given', 'goto',
16 'if', 'in', 'isa', 'is', 'not', 'or', 'otherwise', 'pass', 'raise', 'ref', 'try', 'unless',
17 'when', 'while',
18 -- Definitions.
19 'abstract', 'callable', 'class', 'constructor', 'def', 'destructor', 'do', 'enum', 'event',
20 'final', 'get', 'interface', 'internal', 'of', 'override', 'partial', 'private', 'protected',
21 'public', 'return', 'set', 'static', 'struct', 'transient', 'virtual', 'yield',
22 -- Namespaces.
23 'as', 'from', 'import', 'namespace',
24 -- Other.
25 'self', 'super', 'null', 'true', 'false'
26 }))
27
28 -- Types.
29 lex:add_rule('type', token(lexer.TYPE, word_match{
30 'bool', 'byte', 'char', 'date', 'decimal', 'double', 'duck', 'float', 'int', 'long', 'object',
31 'operator', 'regex', 'sbyte', 'short', 'single', 'string', 'timespan', 'uint', 'ulong', 'ushort'
32 }))
33
34 -- Functions.
35 lex:add_rule('function', token(lexer.FUNCTION, word_match{
36 'array', 'assert', 'checked', 'enumerate', '__eval__', 'filter', 'getter', 'len', 'lock', 'map',
37 'matrix', 'max', 'min', 'normalArrayIndexing', 'print', 'property', 'range', 'rawArrayIndexing',
38 'required', '__switch__', 'typeof', 'unchecked', 'using', 'yieldAll', 'zip'
39 }))
40
41 -- Identifiers.
42 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
43
44 -- Strings.
45 local sq_str = lexer.range("'", true)
46 local dq_str = lexer.range('"', true)
47 local tq_str = lexer.range('"""')
48 local string = token(lexer.STRING, tq_str + sq_str + dq_str)
49 local regex_str = lexer.after_set('!%^&*([{-=+|:;,?<>~', lexer.range('/', true))
50 local regex = token(lexer.REGEX, regex_str)
51 lex:add_rule('string', string + regex)
52
53 -- Comments.
54 local line_comment = lexer.to_eol('#', true)
55 local block_comment = lexer.range('/*', '*/')
56 lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
57
58 -- Numbers.
59 lex:add_rule('number', token(lexer.NUMBER, lexer.number * (S('msdhsfFlL') + 'ms')^-1))
60
61 -- Operators.
62 lex:add_rule('operator', token(lexer.OPERATOR, S('!%^&*()[]{}-=+/|:;.,?<>~`')))
63
64 lexer.property['scintillua.comment'] = '#'
65
66 return lex