build: set version to 0.5
[vis.git] / lua / lexers / pkgbuild.lua
blobf08f114da758ae1fd0497f9daa153d1c11634714
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'}
10 -- Whitespace.
11 local ws = token(l.WHITESPACE, l.space^1)
13 -- Comments.
14 local comment = token(l.COMMENT, '#' * l.nonnewline^0)
16 -- Strings.
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
26 end
27 end)
28 local string = token(l.STRING, sq_str + dq_str + ex_str + heredoc)
30 -- Numbers.
31 local number = token(l.NUMBER, l.float + l.integer)
33 -- Keywords.
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',
39 -- Operators.
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'
43 }, '-'))
45 -- Functions.
46 local func = token(l.FUNCTION, word_match{
47 'build',
48 'check',
49 'package',
50 'pkgver',
51 'prepare'
52 } * '()')
54 -- Constants.
55 local constants = {
56 -- We do *not* list pkgver, srcdir and startdir here.
57 -- These are defined by makepkg but user should not alter them.
58 'arch',
59 'backup',
60 'changelog',
61 'epoch',
62 'groups',
63 'install',
64 'license',
65 'noextract',
66 'options',
67 'pkgbase',
68 'pkgdesc',
69 'pkgname',
70 'pkgrel',
71 'pkgver',
72 'url',
73 'validpgpkeys'
75 local arch_specific = {
76 'checkdepends',
77 'conflicts',
78 'depends',
79 'makedepends',
80 'md5sums',
81 'optdepends',
82 'provides',
83 'replaces',
84 'sha1sums',
85 'sha256sums',
86 'sha384sums',
87 'sha512sums',
88 'source'
90 for _, field in ipairs(arch_specific) do
91 for _,arch in ipairs({ '', 'i686', 'x86_64' }) do
92 table.insert(constants, field..(arch ~= '' and '_'..arch or ''))
93 end
94 end
95 local constant = token(l.CONSTANT, word_match(constants))
97 -- Identifiers.
98 local identifier = token(l.IDENTIFIER, l.word)
100 -- Variables.
101 local variable = token(l.VARIABLE,
102 '$' * (S('!#?*@$') +
103 l.delimited_range('()', true, true) +
104 l.delimited_range('[]', true, true) +
105 l.delimited_range('{}', true, true) +
106 l.delimited_range('`', true, true) +
107 l.digit^1 + l.word))
109 -- Operators.
110 local operator = token(l.OPERATOR, S('=!<>+-/*^~.,:;?()[]{}'))
112 M._rules = {
113 {'whitespace', ws},
114 {'comment', comment},
115 {'string', string},
116 {'number', number},
117 {'keyword', keyword},
118 {'function', func},
119 {'constant', constant},
120 {'identifier', identifier},
121 {'variable', variable},
122 {'operator', operator},
125 M._foldsymbols = {
126 _patterns = {'[%(%){}]', '#'},
127 [l.OPERATOR] = {['('] = 1, [')'] = -1, ['{'] = 1, ['}'] = -1},
128 [l.COMMENT] = {['#'] = l.fold_line_comments('#')}
131 return M