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