vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
pure.lua
(1660B)
1 -- Copyright 2015-2025 David B. Lamkins <david@lamkins.net>. See LICENSE.
2 -- pure LPeg lexer, see http://purelang.bitbucket.org/
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('pure')
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 'namespace', 'with', 'end', 'using', 'interface', 'extern', 'let', 'const', 'def', 'type',
16 'public', 'private', 'nonfix', 'outfix', 'infix', 'infixl', 'infixr', 'prefix', 'postfix', 'if',
17 'otherwise', 'when', 'case', 'of', 'then', 'else'
18 }))
19
20 -- Identifiers.
21 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
22
23 -- Strings.
24 lex:add_rule('string', token(lexer.STRING, lexer.range('"', true)))
25
26 -- Comments.
27 local line_comment = lexer.to_eol('//')
28 local block_comment = lexer.range('/*', '*/')
29 lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
30
31 -- Numbers.
32 local bin = '0' * S('Bb') * S('01')^1
33 local hex = lexer.hex_num
34 local dec = lexer.dec_num
35 local int = (bin + hex + dec) * P('L')^-1
36 local rad = P('.') - '..'
37 local exp = (S('Ee') * S('+-')^-1 * int)^-1
38 local flt = int * (rad * dec)^-1 * exp + int^-1 * rad * dec * exp
39 lex:add_rule('number', token(lexer.NUMBER, flt + int))
40
41 -- Pragmas.
42 local hashbang = lexer.starts_line('#!') * (lexer.nonnewline - '//')^0
43 lex:add_rule('pragma', token(lexer.PREPROCESSOR, hashbang))
44
45 -- Operators.
46 lex:add_rule('operator', token(lexer.OPERATOR, '..' + S('+-/*%<>~!=^&|?~:;,.()[]{}@#$`\\\'')))
47
48 lexer.property['scintillua.comment'] = '//'
49
50 return lex