1
Jianw
9 天以前 70f29da38121b9a467841253e3268feb5df02902
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
-- Copyright 2014-2018 Joshua Krämer. See License.txt.
-- Tcl LPeg lexer.
-- This lexer follows the TCL dodekalogue (http://wiki.tcl.tk/10259).
-- It is based on the previous lexer by Mitchell.
 
local lexer = require('lexer')
local token, word_match = lexer.token, lexer.word_match
local P, R, S = lpeg.P, lpeg.R, lpeg.S
 
local lex = lexer.new('tcl')
 
-- Whitespace.
lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
 
-- Comment.
lex:add_rule('comment', token(lexer.COMMENT, '#' * P(function(input, index)
  local i = index - 2
  while i > 0 and input:find('^[ \t]', i) do i = i - 1 end
  if i < 1 or input:find('^[\r\n;]', i) then return index end
end) * lexer.nonnewline^0))
 
-- Separator (semicolon).
lex:add_rule('separator', token(lexer.CLASS, P(';')))
 
-- Argument expander.
lex:add_rule('expander', token(lexer.LABEL, P('{*}')))
 
-- Delimiters.
lex:add_rule('braces', token(lexer.KEYWORD, S('{}')))
lex:add_rule('quotes', token(lexer.FUNCTION, '"'))
lex:add_rule('brackets', token(lexer.VARIABLE, S('[]')))
 
-- Variable substitution.
lex:add_rule('variable', token(lexer.STRING, '$' *
                                             (lexer.alnum + '_' + P(':')^2)^0))
 
-- Backslash substitution.
lex:add_rule('backslash', token(lexer.TYPE,
                                '\\' * (lexer.digit * lexer.digit^-2 +
                                        'x' * lexer.xdigit^1 +
                                        'u' * lexer.xdigit * lexer.xdigit^-3 +
                                        'U' * lexer.xdigit * lexer.xdigit^-7 +
                                        1)))
 
-- Fold points.
lex:add_fold_point(lexer.KEYWORD, '{', '}')
lex:add_fold_point(lexer.COMMENT, '#', lexer.fold_line_comments('#'))
 
return lex