octave.js 4.4 KB

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