Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50672 views
1
CodeMirror.defineMode("octave", function() {
2
function wordRegexp(words) {
3
return new RegExp("^((" + words.join(")|(") + "))\\b");
4
}
5
6
var singleOperators = new RegExp("^[\\+\\-\\*/&|\\^~<>!@'\\\\]");
7
var singleDelimiters = new RegExp('^[\\(\\[\\{\\},:=;]');
8
var doubleOperators = new RegExp("^((==)|(~=)|(<=)|(>=)|(<<)|(>>)|(\\.[\\+\\-\\*/\\^\\\\]))");
9
var doubleDelimiters = new RegExp("^((!=)|(\\+=)|(\\-=)|(\\*=)|(/=)|(&=)|(\\|=)|(\\^=))");
10
var tripleDelimiters = new RegExp("^((>>=)|(<<=))");
11
var expressionEnd = new RegExp("^[\\]\\)]");
12
var identifiers = new RegExp("^[_A-Za-z][_A-Za-z0-9]*");
13
14
var builtins = wordRegexp([
15
'error', 'eval', 'function', 'abs', 'acos', 'atan', 'asin', 'cos',
16
'cosh', 'exp', 'log', 'prod', 'sum', 'log10', 'max', 'min', 'sign', 'sin', 'sinh',
17
'sqrt', 'tan', 'reshape', 'break', 'zeros', 'default', 'margin', 'round', 'ones',
18
'rand', 'syn', 'ceil', 'floor', 'size', 'clear', 'zeros', 'eye', 'mean', 'std', 'cov',
19
'det', 'eig', 'inv', 'norm', 'rank', 'trace', 'expm', 'logm', 'sqrtm', 'linspace', 'plot',
20
'title', 'xlabel', 'ylabel', 'legend', 'text', 'grid', 'meshgrid', 'mesh', 'num2str',
21
'fft', 'ifft', 'arrayfun', 'cellfun', 'input', 'fliplr', 'flipud', 'ismember'
22
]);
23
24
var keywords = wordRegexp([
25
'return', 'case', 'switch', 'else', 'elseif', 'end', 'endif', 'endfunction',
26
'if', 'otherwise', 'do', 'for', 'while', 'try', 'catch', 'classdef', 'properties', 'events',
27
'methods', 'global', 'persistent', 'endfor', 'endwhile', 'printf', 'sprintf', 'disp', 'until',
28
'continue', 'pkg'
29
]);
30
31
32
// tokenizers
33
function tokenTranspose(stream, state) {
34
if (!stream.sol() && stream.peek() === '\'') {
35
stream.next();
36
state.tokenize = tokenBase;
37
return 'operator';
38
}
39
state.tokenize = tokenBase;
40
return tokenBase(stream, state);
41
}
42
43
44
function tokenComment(stream, state) {
45
if (stream.match(/^.*%}/)) {
46
state.tokenize = tokenBase;
47
return 'comment';
48
};
49
stream.skipToEnd();
50
return 'comment';
51
}
52
53
function tokenBase(stream, state) {
54
// whitespaces
55
if (stream.eatSpace()) return null;
56
57
// Handle one line Comments
58
if (stream.match('%{')){
59
state.tokenize = tokenComment;
60
stream.skipToEnd();
61
return 'comment';
62
}
63
64
if (stream.match(/^[%#]/)){
65
stream.skipToEnd();
66
return 'comment';
67
}
68
69
// Handle Number Literals
70
if (stream.match(/^[0-9\.+-]/, false)) {
71
if (stream.match(/^[+-]?0x[0-9a-fA-F]+[ij]?/)) {
72
stream.tokenize = tokenBase;
73
return 'number'; };
74
if (stream.match(/^[+-]?\d*\.\d+([EeDd][+-]?\d+)?[ij]?/)) { return 'number'; };
75
if (stream.match(/^[+-]?\d+([EeDd][+-]?\d+)?[ij]?/)) { return 'number'; };
76
}
77
if (stream.match(wordRegexp(['nan','NaN','inf','Inf']))) { return 'number'; };
78
79
// Handle Strings
80
if (stream.match(/^"([^"]|(""))*"/)) { return 'string'; } ;
81
if (stream.match(/^'([^']|(''))*'/)) { return 'string'; } ;
82
83
// Handle words
84
if (stream.match(keywords)) { return 'keyword'; } ;
85
if (stream.match(builtins)) { return 'builtin'; } ;
86
if (stream.match(identifiers)) { return 'variable'; } ;
87
88
if (stream.match(singleOperators) || stream.match(doubleOperators)) { return 'operator'; };
89
if (stream.match(singleDelimiters) || stream.match(doubleDelimiters) || stream.match(tripleDelimiters)) { return null; };
90
91
if (stream.match(expressionEnd)) {
92
state.tokenize = tokenTranspose;
93
return null;
94
};
95
96
97
// Handle non-detected items
98
stream.next();
99
return 'error';
100
};
101
102
103
return {
104
startState: function() {
105
return {
106
tokenize: tokenBase
107
};
108
},
109
110
token: function(stream, state) {
111
var style = state.tokenize(stream, state);
112
if (style === 'number' || style === 'variable'){
113
state.tokenize = tokenTranspose;
114
}
115
return style;
116
}
117
};
118
});
119
120
CodeMirror.defineMIME("text/x-octave", "octave");
121
122