all repos — NoPaste @ 3e3c433f026c49e04d0efa0f0f39fc688ed32bb1

Resurrected - The PussTheCat.org fork of NoPaste

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