octave.js 4.2 KB

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