CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/codemirror/mode/makefile.js
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
// CodeMirror, copyright (c) by Marijn Haverbeke and others
7
// Distributed under an MIT license: http://codemirror.net/LICENSE
8
// derived from https://github.com/notepadqq/CodeMirror/blob/d790fc39c1a5f06aa66415110b8ebe3026df665a/mode/makefile/makefile.js
9
10
import * as CodeMirror from "codemirror";
11
12
CodeMirror.defineMode('makefile', function() {
13
14
var words = {};
15
16
function tokenBase(stream, state) {
17
if (stream.eatSpace()) return null;
18
19
var sol = stream.sol();
20
var ch = stream.next();
21
var cur = stream.current();
22
23
if (ch === '@') { return "atom"; }
24
25
if (sol) {
26
27
if (ch === '#') {
28
if (stream.eat('!')) {
29
stream.skipToEnd();
30
return 'meta';
31
}
32
stream.skipToEnd();
33
return 'tag';
34
}
35
36
// if, ifdef, ifndef, ifeq, ifneq
37
if (ch === 'i' && (stream.match('f ') || stream.match('fdef ') || stream.match('fndef ') || stream.match('feq (') || stream.match('fneq ('))) {
38
stream.skipToEnd();
39
return "bracket";
40
}
41
// else, endif
42
if (ch === 'e' && (stream.match('lse') || stream.match('ndif'))) { return "bracket"; }
43
// include
44
if (ch === 'i' && stream.match('nclude ')) { return "string"; }
45
// -include
46
if (ch === '-' && stream.match('include ')) { return "string"; }
47
// vpath
48
if (ch === 'v' && stream.match('path ')) { return "string"; }
49
50
// makros
51
if ((stream.match(/^[\w]+[\s]+/) || stream.match(/^[\w]+/)) &&
52
(stream.peek() === '=' ||
53
((stream.match('?') || stream.match('+') || stream.match('-')) && stream.peek() === '='))
54
) { return "variable-2"; }
55
56
// Makefile targets
57
if ( stream.eat(':') || stream.match(/^[\w]+:/) || stream.match(/^(.)+[\w]+:/) ||
58
(ch === '$' && stream.match(/^\(+[\w]+\)+/) && stream.eat(':'))
59
) {
60
if (stream.peek() === '=') {
61
return "variable-2";
62
} else {
63
return "header";
64
}
65
}
66
67
} else {
68
69
if (ch === '*') { return "quote"; }
70
if (ch === '%') { return 'atom'; }
71
if (ch === '\\' && stream.eol()) { return "comment"; }
72
if (ch === '#') {
73
stream.skipToEnd();
74
return 'tag';
75
}
76
77
if ((stream.eat(':') || stream.eat('+')) && stream.peek('=')) { return "variable-2"; }
78
79
if (ch === '$' && stream.eat('$') && stream.match(/^[\w]+/)) { return "variable-2"; }
80
if (ch === '$' && stream.eat('(')) {
81
state.tokens.unshift(tokenDollar);
82
return tokenize(stream, state);
83
}
84
if (ch === '$' && stream.eat('{')) {
85
state.tokens.unshift(tokenDollarB);
86
return tokenize(stream, state);
87
}
88
if (ch === '$' && (stream.eat('@') || stream.eat('<') || stream.eat('^'))) { return "quote"; }
89
90
if (ch === '\'' || ch === '"' || ch === '`') {
91
state.tokens.unshift(tokenString(ch));
92
return tokenize(stream, state);
93
}
94
}
95
96
stream.eatWhile(/[\w-]/);
97
return words.hasOwnProperty(cur) ? words[cur] : null;
98
}
99
100
function tokenString(quote) {
101
return function(stream, state) {
102
var next, end = false, escaped = false;
103
while ((next = stream.next()) != null) {
104
if (next === quote && !escaped) {
105
end = true;
106
break;
107
}
108
}
109
if (end || !escaped) {
110
state.tokens.shift();
111
}
112
return ((quote === ')' || quote === '}') ? 'variable-2' : 'string');
113
};
114
};
115
116
var tokenDollar = function(stream, state) {
117
if (state.tokens.length > 1) stream.eat('$');
118
state.tokens[0] = tokenString(')');
119
return tokenize(stream, state);
120
};
121
var tokenDollarB = function(stream, state) {
122
if (state.tokens.length > 1) stream.eat('$');
123
state.tokens[0] = tokenString('}');
124
return tokenize(stream, state);
125
};
126
127
function tokenize(stream, state) {
128
return (state.tokens[0] || tokenBase) (stream, state);
129
};
130
131
return {
132
startState: function() {return {tokens:[]};},
133
token: function(stream, state) {
134
return tokenize(stream, state);
135
},
136
lineComment: "#"
137
};
138
});
139
140
CodeMirror.defineMIME('text/x-makefile', 'makefile');
141
142
143