Debian Codegraph @

codemirror-legacy-modes/mode/octave.js

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