forked from aboutcode-org/scancode-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbashlex.py
More file actions
66 lines (58 loc) · 1.57 KB
/
bashlex.py
File metadata and controls
66 lines (58 loc) · 1.57 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Minimal lexer for POSIX and Bash shells.
Derived from pygments.lexers.shell and significantly modified
copyright: Copyright 2006-2021 by the Pygments team, see bashlex.py.AUTHORS.
SPDX-License-Identifier: BSD-2-Clause
"""
from pygments.lexer import (
RegexLexer,
bygroups,
include
)
from pygments.token import (
Token,
Text,
Comment,
Operator,
Name,
String,
)
# White spaces
WS = Text.WS # NOQA
# new lines
LF = WS.LF
ArrayStart = Token.ArraySep.Start
ArrayEnd = Token.ArraySep.End
Equal = Operator.Equal # NOQA
class BashShellLexer(RegexLexer):
"""
A Pygments shell lexer to lex shell scripts used as package build scripts
and package manifests such as autotools, ebuild, PKGBUILD or APKBUILD.
This used as an input to further parse and extract metadata.
"""
name = 'Shell'
aliases = ['shell']
# for info, as this is not used here otherwise
filenames = ['*.ebuild', 'PKGBUILD', 'APKBUILD', ]
mimetypes = []
tokens = {
'root': [
include('basic'),
include('data'),
],
'basic': [
(r'#.*$', Comment),
# variable declaration and assignment
(r'^(\b\w+)(\+?=)', bygroups(Name.Variable, Equal)),
],
'data': [
(r'\(', ArrayStart),
(r'\)', ArrayEnd),
# (?s) is re.DOTALL e.g. across lines
(r'(?s)"(\\.|[^"])*"', String.Double),
(r"(?s)'(\\.|[^'])*'", String.Single),
(r'\n+', LF),
(r'\s+', WS),
(r'\S+', Text),
],
}