1 -- Copyright 2006-2013 gwash. See LICENSE.
2 -- Archlinux PKGBUILD LPeg lexer.
4 local l
= require('lexer')
5 local token
, word_match
= l
.token
, l
.word_match
6 local P
, R
, S
= lpeg
.P
, lpeg
.R
, lpeg
.S
8 local M
= {_NAME
= 'pkgbuild'}
11 local ws
= token(l
.WHITESPACE
, l
.space^
1)
14 local comment
= token(l
.COMMENT
, '#' * l
.nonnewline^
0)
17 local sq_str
= l
.delimited_range("'", false, true)
18 local dq_str
= l
.delimited_range('"')
19 local ex_str
= l
.delimited_range('`')
20 local heredoc
= '<<' * P(function(input
, index
)
21 local s
, e
, _
, delimiter
=
22 input
:find('(["\']?)([%a_][%w_]*)%1[\n\r\f;]+', index
)
23 if s
== index
and delimiter
then
24 local _
, e
= input
:find('[\n\r\f]+'..delimiter
, e
)
25 return e
and e
+ 1 or #input
+ 1
28 local string = token(l
.STRING
, sq_str
+ dq_str
+ ex_str
+ heredoc
)
31 local number = token(l
.NUMBER
, l
.float
+ l
.integer
)
34 local keyword
= token(l
.KEYWORD
, word_match({
35 'patch', 'cd', 'make', 'patch', 'mkdir', 'cp', 'sed', 'install', 'rm',
36 'if', 'then', 'elif', 'else', 'fi', 'case', 'in', 'esac', 'while', 'for',
37 'do', 'done', 'continue', 'local', 'return', 'git', 'svn', 'co', 'clone',
38 'gconf-merge-schema', 'msg', 'echo', 'ln',
40 '-a', '-b', '-c', '-d', '-e', '-f', '-g', '-h', '-k', '-p', '-r', '-s', '-t',
41 '-u', '-w', '-x', '-O', '-G', '-L', '-S', '-N', '-nt', '-ot', '-ef', '-o',
42 '-z', '-n', '-eq', '-ne', '-lt', '-le', '-gt', '-ge', '-Np', '-i'
46 local func
= token(l
.FUNCTION
, word_match
{
55 local constant
= token(l
.CONSTANT
, word_match
{
88 local identifier
= token(l
.IDENTIFIER
, l
.word
)
91 local variable
= token(l
.VARIABLE
,
93 l
.delimited_range('()', true, true) +
94 l
.delimited_range('[]', true, true) +
95 l
.delimited_range('{}', true, true) +
96 l
.delimited_range('`', true, true) +
100 local operator
= token(l
.OPERATOR
, S('=!<>+-/*^~.,:;?()[]{}'))
104 {'comment', comment
},
107 {'keyword', keyword
},
109 {'constant', constant
},
110 {'identifier', identifier
},
111 {'variable', variable
},
112 {'operator', operator
},
116 _patterns
= {'[%(%){}]', '#'},
117 [l
.OPERATOR
] = {['('] = 1, [')'] = -1, ['{'] = 1, ['}'] = -1},
118 [l
.COMMENT
] = {['#'] = l
.fold_line_comments('#')}