|
1 | 1 | -- Copyright 2006-2025 Mitchell. See LICENSE.
|
2 | 2 | -- Ini LPeg lexer.
|
3 | 3 |
|
4 |
| -local lexer = require('lexer') |
5 |
| -local token, word_match = lexer.token, lexer.word_match |
| 4 | +local lexer = lexer |
6 | 5 | local P, S = lpeg.P, lpeg.S
|
7 | 6 |
|
8 |
| -local lex = lexer.new('ini') |
9 |
| - |
10 |
| --- Whitespace. |
11 |
| -lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1)) |
| 7 | +local lex = lexer.new(...) |
12 | 8 |
|
13 | 9 | -- Keywords.
|
14 |
| -lex:add_rule('keyword', token(lexer.KEYWORD, word_match('true false on off yes no'))) |
| 10 | +lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD))) |
15 | 11 |
|
16 | 12 | -- Identifiers.
|
17 |
| -lex:add_rule('identifier', token(lexer.IDENTIFIER, (lexer.alpha + '_') * (lexer.alnum + S('_.'))^0)) |
| 13 | +-- Allows periods within identifiers for dotted keys (e.g., 'foo.bar'). |
| 14 | +lex:add_rule('identifier', |
| 15 | + lex:tag(lexer.IDENTIFIER, (lexer.alpha + '_') * (lexer.alnum + S('_. '))^0)) |
18 | 16 |
|
19 | 17 | -- Strings.
|
20 | 18 | local sq_str = lexer.range("'")
|
21 | 19 | local dq_str = lexer.range('"')
|
22 |
| -lex:add_rule('string', token(lexer.STRING, sq_str + dq_str)) |
| 20 | +lex:add_rule('string', lex:tag(lexer.STRING, sq_str + dq_str)) |
23 | 21 |
|
24 |
| --- Labels. |
25 |
| -lex:add_rule('label', token(lexer.LABEL, lexer.range('[', ']', true))) |
| 22 | +-- Labels (Section Headers). |
| 23 | +lex:add_rule('label', lex:tag(lexer.LABEL, lexer.range('[', ']', true))) |
26 | 24 |
|
27 | 25 | -- Comments.
|
28 |
| -lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol(lexer.starts_line(S(';#'))))) |
| 26 | +lex:add_rule('comment', |
| 27 | + lex:tag(lexer.COMMENT, lexer.to_eol(lexer.starts_line(S(';#'))))) |
29 | 28 |
|
30 | 29 | -- Numbers.
|
31 |
| -local integer = S('+-')^-1 * (lexer.hex_num + lexer.oct_num_('_') + lexer.dec_num_('_')) |
32 |
| -lex:add_rule('number', token(lexer.NUMBER, lexer.float + integer)) |
| 30 | +lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.float + lexer.integer)) |
33 | 31 |
|
34 | 32 | -- Operators.
|
35 |
| -lex:add_rule('operator', token(lexer.OPERATOR, '=')) |
| 33 | +lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('=:'))) |
| 34 | + |
| 35 | +-- Word lists |
| 36 | +lex:set_word_list(lexer.KEYWORD, {'true', 'false', 'on', 'off', 'yes', 'no'}) |
36 | 37 |
|
37 | 38 | lexer.property['scintillua.comment'] = '#'
|
38 | 39 |
|
|
0 commit comments