vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
batch.lua
(1949B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- Batch 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('batch', {case_insensitive_fold_points = true})
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 'cd', 'chdir', 'md', 'mkdir', 'cls', 'for', 'if', 'echo', 'echo.', 'move', 'copy', 'ren', 'del',
16 'set', 'call', 'exit', 'setlocal', 'shift', 'endlocal', 'pause', 'defined', 'exist', 'errorlevel',
17 'else', 'in', 'do', 'NUL', 'AUX', 'PRN', 'not', 'goto', 'pushd', 'popd'
18 }, true)))
19
20 -- Functions.
21 lex:add_rule('function', token(lexer.FUNCTION, word_match({
22 'APPEND', 'ATTRIB', 'CHKDSK', 'CHOICE', 'DEBUG', 'DEFRAG', 'DELTREE', 'DISKCOMP', 'DISKCOPY',
23 'DOSKEY', 'DRVSPACE', 'EMM386', 'EXPAND', 'FASTOPEN', 'FC', 'FDISK', 'FIND', 'FORMAT', 'GRAPHICS',
24 'KEYB', 'LABEL', 'LOADFIX', 'MEM', 'MODE', 'MORE', 'MOVE', 'MSCDEX', 'NLSFUNC', 'POWER', 'PRINT',
25 'RD', 'REPLACE', 'RESTORE', 'SETVER', 'SHARE', 'SORT', 'SUBST', 'SYS', 'TREE', 'UNDELETE',
26 'UNFORMAT', 'VSAFE', 'XCOPY'
27 }, true)))
28
29 -- Comments.
30 local rem = (P('REM') + 'rem') * #lexer.space
31 lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol(rem + '::')))
32
33 -- Identifiers.
34 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
35
36 -- Strings.
37 lex:add_rule('string', token(lexer.STRING, lexer.range('"', true)))
38
39 -- Variables.
40 local arg = '%' * lexer.digit + '%~' * lexer.alnum^1
41 local variable = lexer.range('%', true, false)
42 lex:add_rule('variable', token(lexer.VARIABLE, arg + variable))
43
44 -- Labels.
45 lex:add_rule('label', token(lexer.LABEL, ':' * lexer.word))
46
47 -- Operators.
48 lex:add_rule('operator', token(lexer.OPERATOR, S('+|&!<>=')))
49
50 -- Fold points.
51 lex:add_fold_point(lexer.KEYWORD, 'setlocal', 'endlocal')
52
53 lexer.property['scintillua.comment'] = 'REM '
54
55 return lex