Permalink
Browse files

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 <joel@porquet.org>
  • Loading branch information...
joel-porquet committed Apr 6, 2017
1 parent d3219c5 commit 5b3e0e68bfaae282faff6697d6a490567fa9d44b
Showing with 69 additions and 32 deletions.
  1. +69 −32 src/languages/makefile.js
View
@@ -1,50 +1,87 @@
/*
Language: Makefile
Author: Ivan Sagalaev <maniac@softwaremaniacs.org>
+Contributors: Joël Porquet <joel@porquet.org>
Category: common
*/
function(hljs) {
+ /* Variables: simple (eg $(var)) and special (eg $@) */
var VARIABLE = {
className: 'variable',
- begin: /\$\(/, end: /\)/,
- contains: [hljs.BACKSLASH_ESCAPE]
- };
- return {
- aliases: ['mk', 'mak'],
- contains: [
- hljs.HASH_COMMENT_MODE,
- {
- begin: /^\w+\s*\W*=/, returnBegin: true,
- relevance: 0,
- starts: {
- end: /\s*\W*=/, excludeEnd: true,
- starts: {
- end: /$/,
- relevance: 0,
- contains: [
- VARIABLE
- ]
- }
- }
- },
+ variants: [
{
- className: 'section',
- begin: /^[\w]+:\s*$/
+ begin: '\\$\\(' + hljs.UNDERSCORE_IDENT_RE + '\\)',
+ contains: [hljs.BACKSLASH_ESCAPE],
},
{
- className: 'meta',
- begin: /^\.PHONY:/, end: /$/,
- keywords: {'meta-keyword': '.PHONY'}, lexemes: /[\.\w]+/
+ begin: /\$[@%<?\^\+\*]/
},
+ ]
+ };
+ /* Quoted string with variables inside */
+ var QUOTE_STRING = {
+ className: 'string',
+ begin: /"/, end: /"/,
+ contains: [
+ hljs.BACKSLASH_ESCAPE,
+ VARIABLE,
+ ]
+ };
+ /* Function: $(func arg,...) */
+ var FUNC = {
+ className: 'variable',
+ begin: /\$\([\w-]+\s/, end: /\)/,
+ keywords: {
+ built_in:
+ 'subst patsubst strip findstring filter filter-out sort ' +
+ 'word wordlist firstword lastword dir notdir suffix basename ' +
+ 'addsuffix addprefix join wildcard realpath abspath error warning ' +
+ 'shell origin flavor foreach if or and call eval file value',
+ },
+ contains: [
+ VARIABLE,
+ ]
+ };
+ /* Variable assignment */
+ var VAR_ASSIG = {
+ begin: '^' + hljs.UNDERSCORE_IDENT_RE + '\\s*[:+?]?=',
+ illegal: '\\n',
+ returnBegin: true,
+ contains: [
{
- begin: /^\t+/, end: /$/,
- relevance: 0,
- contains: [
- hljs.QUOTE_STRING_MODE,
- VARIABLE
- ]
+ begin: '^' + hljs.UNDERSCORE_IDENT_RE, end: '[:+?]?=',
+ excludeEnd: true,
}
]
};
+ /* Meta targets (.PHONY) */
+ var META = {
+ className: 'meta',
+ begin: /^\.PHONY:/, end: /$/,
+ keywords: {'meta-keyword': '.PHONY'},
+ lexemes: /[\.\w]+/
+ };
+ /* Targets */
+ var TARGET = {
+ className: 'section',
+ begin: /^[^\s]+:/, end: /$/,
+ contains: [VARIABLE,]
+ };
+ return {
+ aliases: ['mk', 'mak'],
+ keywords:
+ 'define endef undefine ifdef ifndef ifeq ifneq else endif ' +
+ 'include -include sinclude override export unexport private vpath',
+ lexemes: /[\w-]+/,
+ contains: [
+ hljs.HASH_COMMENT_MODE,
+ VARIABLE,
+ QUOTE_STRING,
+ FUNC,
+ VAR_ASSIG,
+ META,
+ TARGET,
+ ]
+ };
}

0 comments on commit 5b3e0e6

Please sign in to comment.