You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
8.4KB

  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("fortran", function() {
  13. function words(array) {
  14. var keys = {};
  15. for (var i = 0; i < array.length; ++i) {
  16. keys[array[i]] = true;
  17. }
  18. return keys;
  19. }
  20. var keywords = words([
  21. "abstract", "accept", "allocatable", "allocate",
  22. "array", "assign", "asynchronous", "backspace",
  23. "bind", "block", "byte", "call", "case",
  24. "class", "close", "common", "contains",
  25. "continue", "cycle", "data", "deallocate",
  26. "decode", "deferred", "dimension", "do",
  27. "elemental", "else", "encode", "end",
  28. "endif", "entry", "enumerator", "equivalence",
  29. "exit", "external", "extrinsic", "final",
  30. "forall", "format", "function", "generic",
  31. "go", "goto", "if", "implicit", "import", "include",
  32. "inquire", "intent", "interface", "intrinsic",
  33. "module", "namelist", "non_intrinsic",
  34. "non_overridable", "none", "nopass",
  35. "nullify", "open", "optional", "options",
  36. "parameter", "pass", "pause", "pointer",
  37. "print", "private", "program", "protected",
  38. "public", "pure", "read", "recursive", "result",
  39. "return", "rewind", "save", "select", "sequence",
  40. "stop", "subroutine", "target", "then", "to", "type",
  41. "use", "value", "volatile", "where", "while",
  42. "write"]);
  43. var builtins = words(["abort", "abs", "access", "achar", "acos",
  44. "adjustl", "adjustr", "aimag", "aint", "alarm",
  45. "all", "allocated", "alog", "amax", "amin",
  46. "amod", "and", "anint", "any", "asin",
  47. "associated", "atan", "besj", "besjn", "besy",
  48. "besyn", "bit_size", "btest", "cabs", "ccos",
  49. "ceiling", "cexp", "char", "chdir", "chmod",
  50. "clog", "cmplx", "command_argument_count",
  51. "complex", "conjg", "cos", "cosh", "count",
  52. "cpu_time", "cshift", "csin", "csqrt", "ctime",
  53. "c_funloc", "c_loc", "c_associated", "c_null_ptr",
  54. "c_null_funptr", "c_f_pointer", "c_null_char",
  55. "c_alert", "c_backspace", "c_form_feed",
  56. "c_new_line", "c_carriage_return",
  57. "c_horizontal_tab", "c_vertical_tab", "dabs",
  58. "dacos", "dasin", "datan", "date_and_time",
  59. "dbesj", "dbesj", "dbesjn", "dbesy", "dbesy",
  60. "dbesyn", "dble", "dcos", "dcosh", "ddim", "derf",
  61. "derfc", "dexp", "digits", "dim", "dint", "dlog",
  62. "dlog", "dmax", "dmin", "dmod", "dnint",
  63. "dot_product", "dprod", "dsign", "dsinh",
  64. "dsin", "dsqrt", "dtanh", "dtan", "dtime",
  65. "eoshift", "epsilon", "erf", "erfc", "etime",
  66. "exit", "exp", "exponent", "extends_type_of",
  67. "fdate", "fget", "fgetc", "float", "floor",
  68. "flush", "fnum", "fputc", "fput", "fraction",
  69. "fseek", "fstat", "ftell", "gerror", "getarg",
  70. "get_command", "get_command_argument",
  71. "get_environment_variable", "getcwd",
  72. "getenv", "getgid", "getlog", "getpid",
  73. "getuid", "gmtime", "hostnm", "huge", "iabs",
  74. "iachar", "iand", "iargc", "ibclr", "ibits",
  75. "ibset", "ichar", "idate", "idim", "idint",
  76. "idnint", "ieor", "ierrno", "ifix", "imag",
  77. "imagpart", "index", "int", "ior", "irand",
  78. "isatty", "ishft", "ishftc", "isign",
  79. "iso_c_binding", "is_iostat_end", "is_iostat_eor",
  80. "itime", "kill", "kind", "lbound", "len", "len_trim",
  81. "lge", "lgt", "link", "lle", "llt", "lnblnk", "loc",
  82. "log", "logical", "long", "lshift", "lstat", "ltime",
  83. "matmul", "max", "maxexponent", "maxloc", "maxval",
  84. "mclock", "merge", "move_alloc", "min", "minexponent",
  85. "minloc", "minval", "mod", "modulo", "mvbits",
  86. "nearest", "new_line", "nint", "not", "or", "pack",
  87. "perror", "precision", "present", "product", "radix",
  88. "rand", "random_number", "random_seed", "range",
  89. "real", "realpart", "rename", "repeat", "reshape",
  90. "rrspacing", "rshift", "same_type_as", "scale",
  91. "scan", "second", "selected_int_kind",
  92. "selected_real_kind", "set_exponent", "shape",
  93. "short", "sign", "signal", "sinh", "sin", "sleep",
  94. "sngl", "spacing", "spread", "sqrt", "srand", "stat",
  95. "sum", "symlnk", "system", "system_clock", "tan",
  96. "tanh", "time", "tiny", "transfer", "transpose",
  97. "trim", "ttynam", "ubound", "umask", "unlink",
  98. "unpack", "verify", "xor", "zabs", "zcos", "zexp",
  99. "zlog", "zsin", "zsqrt"]);
  100. var dataTypes = words(["c_bool", "c_char", "c_double", "c_double_complex",
  101. "c_float", "c_float_complex", "c_funptr", "c_int",
  102. "c_int16_t", "c_int32_t", "c_int64_t", "c_int8_t",
  103. "c_int_fast16_t", "c_int_fast32_t", "c_int_fast64_t",
  104. "c_int_fast8_t", "c_int_least16_t", "c_int_least32_t",
  105. "c_int_least64_t", "c_int_least8_t", "c_intmax_t",
  106. "c_intptr_t", "c_long", "c_long_double",
  107. "c_long_double_complex", "c_long_long", "c_ptr",
  108. "c_short", "c_signed_char", "c_size_t", "character",
  109. "complex", "double", "integer", "logical", "real"]);
  110. var isOperatorChar = /[+\-*&=<>\/\:]/;
  111. var litOperator = /^\.(and|or|eq|lt|le|gt|ge|ne|not|eqv|neqv)\./i;
  112. function tokenBase(stream, state) {
  113. if (stream.match(litOperator)){
  114. return 'operator';
  115. }
  116. var ch = stream.next();
  117. if (ch == "!") {
  118. stream.skipToEnd();
  119. return "comment";
  120. }
  121. if (ch == '"' || ch == "'") {
  122. state.tokenize = tokenString(ch);
  123. return state.tokenize(stream, state);
  124. }
  125. if (/[\[\]\(\),]/.test(ch)) {
  126. return null;
  127. }
  128. if (/\d/.test(ch)) {
  129. stream.eatWhile(/[\w\.]/);
  130. return "number";
  131. }
  132. if (isOperatorChar.test(ch)) {
  133. stream.eatWhile(isOperatorChar);
  134. return "operator";
  135. }
  136. stream.eatWhile(/[\w\$_]/);
  137. var word = stream.current().toLowerCase();
  138. if (keywords.hasOwnProperty(word)){
  139. return 'keyword';
  140. }
  141. if (builtins.hasOwnProperty(word) || dataTypes.hasOwnProperty(word)) {
  142. return 'builtin';
  143. }
  144. return "variable";
  145. }
  146. function tokenString(quote) {
  147. return function(stream, state) {
  148. var escaped = false, next, end = false;
  149. while ((next = stream.next()) != null) {
  150. if (next == quote && !escaped) {
  151. end = true;
  152. break;
  153. }
  154. escaped = !escaped && next == "\\";
  155. }
  156. if (end || !escaped) state.tokenize = null;
  157. return "string";
  158. };
  159. }
  160. // Interface
  161. return {
  162. startState: function() {
  163. return {tokenize: null};
  164. },
  165. token: function(stream, state) {
  166. if (stream.eatSpace()) return null;
  167. var style = (state.tokenize || tokenBase)(stream, state);
  168. if (style == "comment" || style == "meta") return style;
  169. return style;
  170. }
  171. };
  172. });
  173. CodeMirror.defineMIME("text/x-fortran", "fortran");
  174. });