vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
icon.lua
(2246B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- LPeg lexer for the Icon programming language.
3 -- http://www.cs.arizona.edu/icon
4 -- Contributed by Carl Sturtivant.
5
6 local lexer = require('lexer')
7 local token, word_match = lexer.token, lexer.word_match
8 local P, S = lpeg.P, lpeg.S
9
10 local lex = lexer.new('icon')
11
12 -- Whitespace.
13 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
14
15 -- Keywords.
16 lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
17 'break', 'by', 'case', 'create', 'default', 'do', 'else', 'end', 'every', 'fail', 'global', 'if',
18 'initial', 'invocable', 'link', 'local', 'next', 'not', 'of', 'procedure', 'record', 'repeat',
19 'return', 'static', 'suspend', 'then', 'to', 'until', 'while'
20 }))
21
22 -- Icon Keywords: unique to Icon.
23 lex:add_rule('special_keyword', token('special_keyword', '&' * word_match{
24 'allocated', 'ascii', 'clock', 'collections', 'cset', 'current', 'date', 'dateline', 'digits',
25 'dump', 'e', 'error', 'errornumber', 'errortext', 'errorvalue', 'errout', 'fail', 'features',
26 'file', 'host', 'input', 'lcase', 'letters', 'level', 'line', 'main', 'null', 'output', 'phi',
27 'pi', 'pos', 'progname', 'random', 'regions', 'source', 'storage', 'subject', 'time', 'trace',
28 'ucase', 'version'
29 }))
30 lex:add_style('special_keyword', lexer.styles.type)
31
32 -- Identifiers.
33 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
34
35 -- Strings.
36 local sq_str = lexer.range("'")
37 local dq_str = lexer.range('"')
38 lex:add_rule('string', token(lexer.STRING, sq_str + dq_str))
39
40 -- Comments.
41 lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol('#', true)))
42
43 -- Numbers.
44 local radix_literal = P('-')^-1 * lexer.dec_num * S('rR') * lexer.alnum^1
45 lex:add_rule('number', token(lexer.NUMBER, radix_literal + lexer.number))
46
47 -- Preprocessor.
48 lex:add_rule('preproc', token(lexer.PREPROCESSOR, '$' *
49 word_match('define else endif error ifdef ifndef include line undef')))
50
51 -- Operators.
52 lex:add_rule('operator', token(lexer.OPERATOR, S('+-/*%<>~!=^&|?~@:;,.()[]{}')))
53
54 -- Fold points.
55 lex:add_fold_point(lexer.PREPROCESSOR, 'ifdef', 'endif')
56 lex:add_fold_point(lexer.PREPROCESSOR, 'ifndef', 'endif')
57 lex:add_fold_point(lexer.KEYWORD, 'procedure', 'end')
58
59 lexer.property['scintillua.comment'] = '#'
60
61 return lex