vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
fennel.lua
(1692B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- Fennel LPeg lexer.
3 -- Contributed by Momohime Honda.
4
5 local lexer = require('lexer')
6 local token, word_match = lexer.token, lexer.word_match
7 local P, S = lpeg.P, lpeg.S
8
9 local lex = lexer.new('fennel', {inherit = lexer.load('lua')})
10
11 -- Whitespace.
12 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
13
14 -- Keywords.
15 lex:modify_rule('keyword', token(lexer.KEYWORD, word_match{
16 '#', '%', '*', '+', '-', '->>', '->', '-?>>', '-?>', '..', '.', '//', '/', ':', '<=', '<', '=',
17 '>=', '>', '?.', '^', '~=', 'λ', 'accumulate', 'and', 'band', 'bnot', 'bor', 'bxor', 'collect',
18 'comment', 'do', 'doto', 'each', 'eval-compiler', 'fn', 'for', 'global', 'hashfn', 'icollect',
19 'if', 'import-macros', 'include', 'lambda', 'length', 'let', 'local', 'lshift', 'lua', 'macro',
20 'macrodebug', 'macros', 'match', 'not', 'not=', 'or', 'partial', 'pick-args', 'pick-values',
21 'quote', 'require-macros', 'rshift', 'set', 'set-forcibly!', 'tset', 'values', 'var', 'when',
22 'while', 'with-open'
23 }))
24
25 -- Identifiers.
26 local initial = lexer.alpha + S('|$%&#*+-/<=>?~^_λ!')
27 local subsequent = initial + lexer.digit
28 lex:modify_rule('identifier', token(lexer.IDENTIFIER, initial * subsequent^0 * P('#')^-1))
29
30 -- Strings.
31 local dq_str = lexer.range('"')
32 local kw_str = lpeg.B(1 - subsequent) * ':' * subsequent^1
33 lex:modify_rule('string', token(lexer.STRING, dq_str + kw_str))
34
35 -- Comments.
36 lex:modify_rule('comment', token(lexer.COMMENT, lexer.to_eol(';')))
37
38 -- Ignore these rules.
39 -- lex:modify_rule('longstring', P(false))
40 lex:modify_rule('label', P(false))
41 lex:modify_rule('operator', P(false))
42
43 lexer.property['scintillua.comment'] = ';'
44
45 return lex