Skip to content

Commit 5b3e0e6

Browse files
committed
makefile: improve support
Improve support for makefile syntax: - simple variables (eg `$(var)`) and special variables (eg `$@`) - variable inside strings (eg `"blabla $(var) blabla"`) - functions (eg `$(patsubst arg,arg,$(var))`) - variable assignement (eg `var=toto`, `var+=toto`, `var:=toto`) - targets (eg `all: dependency $(var)`) - language keywords (`define`, `endef`, `ifeq`, etc.) Signed-off-by: Joel Porquet <[email protected]>
1 parent d3219c5 commit 5b3e0e6

File tree

1 file changed

+69
-32
lines changed

1 file changed

+69
-32
lines changed

src/languages/makefile.js

+69-32
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,87 @@
11
/*
22
Language: Makefile
33
Author: Ivan Sagalaev <[email protected]>
4+
Contributors: Joël Porquet <[email protected]>
45
Category: common
56
*/
67

78
function(hljs) {
9+
/* Variables: simple (eg $(var)) and special (eg $@) */
810
var VARIABLE = {
911
className: 'variable',
10-
begin: /\$\(/, end: /\)/,
11-
contains: [hljs.BACKSLASH_ESCAPE]
12-
};
13-
return {
14-
aliases: ['mk', 'mak'],
15-
contains: [
16-
hljs.HASH_COMMENT_MODE,
17-
{
18-
begin: /^\w+\s*\W*=/, returnBegin: true,
19-
relevance: 0,
20-
starts: {
21-
end: /\s*\W*=/, excludeEnd: true,
22-
starts: {
23-
end: /$/,
24-
relevance: 0,
25-
contains: [
26-
VARIABLE
27-
]
28-
}
29-
}
30-
},
12+
variants: [
3113
{
32-
className: 'section',
33-
begin: /^[\w]+:\s*$/
14+
begin: '\\$\\(' + hljs.UNDERSCORE_IDENT_RE + '\\)',
15+
contains: [hljs.BACKSLASH_ESCAPE],
3416
},
3517
{
36-
className: 'meta',
37-
begin: /^\.PHONY:/, end: /$/,
38-
keywords: {'meta-keyword': '.PHONY'}, lexemes: /[\.\w]+/
18+
begin: /\$[@%<?\^\+\*]/
3919
},
20+
]
21+
};
22+
/* Quoted string with variables inside */
23+
var QUOTE_STRING = {
24+
className: 'string',
25+
begin: /"/, end: /"/,
26+
contains: [
27+
hljs.BACKSLASH_ESCAPE,
28+
VARIABLE,
29+
]
30+
};
31+
/* Function: $(func arg,...) */
32+
var FUNC = {
33+
className: 'variable',
34+
begin: /\$\([\w-]+\s/, end: /\)/,
35+
keywords: {
36+
built_in:
37+
'subst patsubst strip findstring filter filter-out sort ' +
38+
'word wordlist firstword lastword dir notdir suffix basename ' +
39+
'addsuffix addprefix join wildcard realpath abspath error warning ' +
40+
'shell origin flavor foreach if or and call eval file value',
41+
},
42+
contains: [
43+
VARIABLE,
44+
]
45+
};
46+
/* Variable assignment */
47+
var VAR_ASSIG = {
48+
begin: '^' + hljs.UNDERSCORE_IDENT_RE + '\\s*[:+?]?=',
49+
illegal: '\\n',
50+
returnBegin: true,
51+
contains: [
4052
{
41-
begin: /^\t+/, end: /$/,
42-
relevance: 0,
43-
contains: [
44-
hljs.QUOTE_STRING_MODE,
45-
VARIABLE
46-
]
53+
begin: '^' + hljs.UNDERSCORE_IDENT_RE, end: '[:+?]?=',
54+
excludeEnd: true,
4755
}
4856
]
4957
};
58+
/* Meta targets (.PHONY) */
59+
var META = {
60+
className: 'meta',
61+
begin: /^\.PHONY:/, end: /$/,
62+
keywords: {'meta-keyword': '.PHONY'},
63+
lexemes: /[\.\w]+/
64+
};
65+
/* Targets */
66+
var TARGET = {
67+
className: 'section',
68+
begin: /^[^\s]+:/, end: /$/,
69+
contains: [VARIABLE,]
70+
};
71+
return {
72+
aliases: ['mk', 'mak'],
73+
keywords:
74+
'define endef undefine ifdef ifndef ifeq ifneq else endif ' +
75+
'include -include sinclude override export unexport private vpath',
76+
lexemes: /[\w-]+/,
77+
contains: [
78+
hljs.HASH_COMMENT_MODE,
79+
VARIABLE,
80+
QUOTE_STRING,
81+
FUNC,
82+
VAR_ASSIG,
83+
META,
84+
TARGET,
85+
]
86+
};
5087
}

0 commit comments

Comments
 (0)