forked from V-Lor/codemirror-mode-makefile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakefile.js
84 lines (74 loc) · 2.58 KB
/
makefile.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("codemirror"));
else if (typeof define == "function" && define.amd) // AMD
define(["codemirror"], mod);
else // Plain browser env
mod(CodeMirror);
})(function(CodeMirror) {
"use strict";
var keywords = ["define", "endef", "undefine", "ifdef", "ifndef", "ifeq",
"ifneq", "else", "endif", "include", "-include", "sinclude",
"override", "export", "unexport", "private", "vpath"];
var keywordsRegex = new RegExp("\\b((" + keywords.join(")|(") + "))");
var operators = ["=", ":=", "::=", "\\+=", "\\?=", ":"];
var operatorsRegex = new RegExp("((" + operators.join(")|(") + "))");
var automaticVariables = ["\\$@", "\\$%", "\\$<", "\\$\\?", "\\$\\^",
"\\$\\+", "\\$\\*"];
var automaticVariablesRegex = new RegExp(
"((" + automaticVariables.join(")|(") + "))");
// Any sequence of characters not containing '#', ':', '='
var variableNameRegex = /((?![#:=\s]).)*/;
var usingVariableRegex = new RegExp(
"\\$(\\((" + variableNameRegex.source + "(,\\s?)?)+" + "\\)|\\{(" +
variableNameRegex.source + "(, \\s?)?)+" + "\\})");
var definitionVariableRegex = new RegExp(
variableNameRegex.source + "\\s*((" + operators.join(")|(") + "))");
CodeMirror.registerHelper("hintWords", "makefile", keywords);
CodeMirror.defineMode("makefile", function() {
return {
token: function(stream, state) {
var ch = stream.peek();
state.escaped = false;
// Comments
if (ch === '#') {
stream.skipToEnd();
return "comment";
}
// Operators
if (stream.match(operatorsRegex))
return "operator";
// Keywords
if (stream.match(keywordsRegex))
return "keyword";
// Using variables
if (stream.match(usingVariableRegex) ||
stream.match(automaticVariablesRegex)) {
return 'variable-2';
}
// Variable definition
if (stream.match(definitionVariableRegex)) {
stream.backUp(1);
return "def";
}
// nothing found, continue
state.pairStart = false;
state.escaped = (ch === '\\');
stream.next();
return null;
},
startState: function () {
return {
pair: false,
pairStart: false,
inDefinition: false,
inInclude: false,
continueString: false,
pending: false,
};
},
lineComment: '#',
};
});
CodeMirror.defineMIME("text/x-makefile", "makefile");
});