vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
io_lang.lua
(1538B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- Io 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('io_lang')
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 'block', 'method', 'while', 'foreach', 'if', 'else', 'do', 'super', 'self', 'clone', 'proto',
16 'setSlot', 'hasSlot', 'type', 'write', 'print', 'forward'
17 }))
18
19 -- Types.
20 lex:add_rule('type', token(lexer.TYPE, word_match{
21 'Block', 'Buffer', 'CFunction', 'Date', 'Duration', 'File', 'Future', 'LinkedList', 'List', 'Map',
22 'Message', 'Nil', 'Nop', 'Number', 'Object', 'String', 'WeakLink'
23 }))
24
25 -- Identifiers.
26 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
27
28 -- Strings.
29 local sq_str = lexer.range("'")
30 local dq_str = lexer.range('"')
31 local tq_str = lexer.range('"""')
32 lex:add_rule('string', token(lexer.STRING, tq_str + sq_str + dq_str))
33
34 -- Comments.
35 local line_comment = lexer.to_eol(P('#') + '//')
36 local block_comment = lexer.range('/*', '*/')
37 lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
38
39 -- Numbers.
40 lex:add_rule('number', token(lexer.NUMBER, lexer.number))
41
42 -- Operators.
43 lex:add_rule('operator', token(lexer.OPERATOR, S('`~@$%^&*-+/=\\<>?.,:;()[]{}')))
44
45 -- Fold points.
46 lex:add_fold_point(lexer.OPERATOR, '(', ')')
47 lex:add_fold_point(lexer.COMMENT, '/*', '*/')
48
49 lexer.property['scintillua.comment'] = '#'
50
51 return lex