fortran.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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')
  5. // CommonJS
  6. mod(require('../../lib/codemirror'))
  7. else if (typeof define == 'function' && define.amd)
  8. // AMD
  9. define(['../../lib/codemirror'], mod)
  10. // Plain browser env
  11. else mod(CodeMirror)
  12. })(function (CodeMirror) {
  13. 'use strict'
  14. CodeMirror.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. var keywords = words([
  23. 'abstract',
  24. 'accept',
  25. 'allocatable',
  26. 'allocate',
  27. 'array',
  28. 'assign',
  29. 'asynchronous',
  30. 'backspace',
  31. 'bind',
  32. 'block',
  33. 'byte',
  34. 'call',
  35. 'case',
  36. 'class',
  37. 'close',
  38. 'common',
  39. 'contains',
  40. 'continue',
  41. 'cycle',
  42. 'data',
  43. 'deallocate',
  44. 'decode',
  45. 'deferred',
  46. 'dimension',
  47. 'do',
  48. 'elemental',
  49. 'else',
  50. 'encode',
  51. 'end',
  52. 'endif',
  53. 'entry',
  54. 'enumerator',
  55. 'equivalence',
  56. 'exit',
  57. 'external',
  58. 'extrinsic',
  59. 'final',
  60. 'forall',
  61. 'format',
  62. 'function',
  63. 'generic',
  64. 'go',
  65. 'goto',
  66. 'if',
  67. 'implicit',
  68. 'import',
  69. 'include',
  70. 'inquire',
  71. 'intent',
  72. 'interface',
  73. 'intrinsic',
  74. 'module',
  75. 'namelist',
  76. 'non_intrinsic',
  77. 'non_overridable',
  78. 'none',
  79. 'nopass',
  80. 'nullify',
  81. 'open',
  82. 'optional',
  83. 'options',
  84. 'parameter',
  85. 'pass',
  86. 'pause',
  87. 'pointer',
  88. 'print',
  89. 'private',
  90. 'program',
  91. 'protected',
  92. 'public',
  93. 'pure',
  94. 'read',
  95. 'recursive',
  96. 'result',
  97. 'return',
  98. 'rewind',
  99. 'save',
  100. 'select',
  101. 'sequence',
  102. 'stop',
  103. 'subroutine',
  104. 'target',
  105. 'then',
  106. 'to',
  107. 'type',
  108. 'use',
  109. 'value',
  110. 'volatile',
  111. 'where',
  112. 'while',
  113. 'write',
  114. ])
  115. var builtins = words([
  116. 'abort',
  117. 'abs',
  118. 'access',
  119. 'achar',
  120. 'acos',
  121. 'adjustl',
  122. 'adjustr',
  123. 'aimag',
  124. 'aint',
  125. 'alarm',
  126. 'all',
  127. 'allocated',
  128. 'alog',
  129. 'amax',
  130. 'amin',
  131. 'amod',
  132. 'and',
  133. 'anint',
  134. 'any',
  135. 'asin',
  136. 'associated',
  137. 'atan',
  138. 'besj',
  139. 'besjn',
  140. 'besy',
  141. 'besyn',
  142. 'bit_size',
  143. 'btest',
  144. 'cabs',
  145. 'ccos',
  146. 'ceiling',
  147. 'cexp',
  148. 'char',
  149. 'chdir',
  150. 'chmod',
  151. 'clog',
  152. 'cmplx',
  153. 'command_argument_count',
  154. 'complex',
  155. 'conjg',
  156. 'cos',
  157. 'cosh',
  158. 'count',
  159. 'cpu_time',
  160. 'cshift',
  161. 'csin',
  162. 'csqrt',
  163. 'ctime',
  164. 'c_funloc',
  165. 'c_loc',
  166. 'c_associated',
  167. 'c_null_ptr',
  168. 'c_null_funptr',
  169. 'c_f_pointer',
  170. 'c_null_char',
  171. 'c_alert',
  172. 'c_backspace',
  173. 'c_form_feed',
  174. 'c_new_line',
  175. 'c_carriage_return',
  176. 'c_horizontal_tab',
  177. 'c_vertical_tab',
  178. 'dabs',
  179. 'dacos',
  180. 'dasin',
  181. 'datan',
  182. 'date_and_time',
  183. 'dbesj',
  184. 'dbesj',
  185. 'dbesjn',
  186. 'dbesy',
  187. 'dbesy',
  188. 'dbesyn',
  189. 'dble',
  190. 'dcos',
  191. 'dcosh',
  192. 'ddim',
  193. 'derf',
  194. 'derfc',
  195. 'dexp',
  196. 'digits',
  197. 'dim',
  198. 'dint',
  199. 'dlog',
  200. 'dlog',
  201. 'dmax',
  202. 'dmin',
  203. 'dmod',
  204. 'dnint',
  205. 'dot_product',
  206. 'dprod',
  207. 'dsign',
  208. 'dsinh',
  209. 'dsin',
  210. 'dsqrt',
  211. 'dtanh',
  212. 'dtan',
  213. 'dtime',
  214. 'eoshift',
  215. 'epsilon',
  216. 'erf',
  217. 'erfc',
  218. 'etime',
  219. 'exit',
  220. 'exp',
  221. 'exponent',
  222. 'extends_type_of',
  223. 'fdate',
  224. 'fget',
  225. 'fgetc',
  226. 'float',
  227. 'floor',
  228. 'flush',
  229. 'fnum',
  230. 'fputc',
  231. 'fput',
  232. 'fraction',
  233. 'fseek',
  234. 'fstat',
  235. 'ftell',
  236. 'gerror',
  237. 'getarg',
  238. 'get_command',
  239. 'get_command_argument',
  240. 'get_environment_variable',
  241. 'getcwd',
  242. 'getenv',
  243. 'getgid',
  244. 'getlog',
  245. 'getpid',
  246. 'getuid',
  247. 'gmtime',
  248. 'hostnm',
  249. 'huge',
  250. 'iabs',
  251. 'iachar',
  252. 'iand',
  253. 'iargc',
  254. 'ibclr',
  255. 'ibits',
  256. 'ibset',
  257. 'ichar',
  258. 'idate',
  259. 'idim',
  260. 'idint',
  261. 'idnint',
  262. 'ieor',
  263. 'ierrno',
  264. 'ifix',
  265. 'imag',
  266. 'imagpart',
  267. 'index',
  268. 'int',
  269. 'ior',
  270. 'irand',
  271. 'isatty',
  272. 'ishft',
  273. 'ishftc',
  274. 'isign',
  275. 'iso_c_binding',
  276. 'is_iostat_end',
  277. 'is_iostat_eor',
  278. 'itime',
  279. 'kill',
  280. 'kind',
  281. 'lbound',
  282. 'len',
  283. 'len_trim',
  284. 'lge',
  285. 'lgt',
  286. 'link',
  287. 'lle',
  288. 'llt',
  289. 'lnblnk',
  290. 'loc',
  291. 'log',
  292. 'logical',
  293. 'long',
  294. 'lshift',
  295. 'lstat',
  296. 'ltime',
  297. 'matmul',
  298. 'max',
  299. 'maxexponent',
  300. 'maxloc',
  301. 'maxval',
  302. 'mclock',
  303. 'merge',
  304. 'move_alloc',
  305. 'min',
  306. 'minexponent',
  307. 'minloc',
  308. 'minval',
  309. 'mod',
  310. 'modulo',
  311. 'mvbits',
  312. 'nearest',
  313. 'new_line',
  314. 'nint',
  315. 'not',
  316. 'or',
  317. 'pack',
  318. 'perror',
  319. 'precision',
  320. 'present',
  321. 'product',
  322. 'radix',
  323. 'rand',
  324. 'random_number',
  325. 'random_seed',
  326. 'range',
  327. 'real',
  328. 'realpart',
  329. 'rename',
  330. 'repeat',
  331. 'reshape',
  332. 'rrspacing',
  333. 'rshift',
  334. 'same_type_as',
  335. 'scale',
  336. 'scan',
  337. 'second',
  338. 'selected_int_kind',
  339. 'selected_real_kind',
  340. 'set_exponent',
  341. 'shape',
  342. 'short',
  343. 'sign',
  344. 'signal',
  345. 'sinh',
  346. 'sin',
  347. 'sleep',
  348. 'sngl',
  349. 'spacing',
  350. 'spread',
  351. 'sqrt',
  352. 'srand',
  353. 'stat',
  354. 'sum',
  355. 'symlnk',
  356. 'system',
  357. 'system_clock',
  358. 'tan',
  359. 'tanh',
  360. 'time',
  361. 'tiny',
  362. 'transfer',
  363. 'transpose',
  364. 'trim',
  365. 'ttynam',
  366. 'ubound',
  367. 'umask',
  368. 'unlink',
  369. 'unpack',
  370. 'verify',
  371. 'xor',
  372. 'zabs',
  373. 'zcos',
  374. 'zexp',
  375. 'zlog',
  376. 'zsin',
  377. 'zsqrt',
  378. ])
  379. var dataTypes = words([
  380. 'c_bool',
  381. 'c_char',
  382. 'c_double',
  383. 'c_double_complex',
  384. 'c_float',
  385. 'c_float_complex',
  386. 'c_funptr',
  387. 'c_int',
  388. 'c_int16_t',
  389. 'c_int32_t',
  390. 'c_int64_t',
  391. 'c_int8_t',
  392. 'c_int_fast16_t',
  393. 'c_int_fast32_t',
  394. 'c_int_fast64_t',
  395. 'c_int_fast8_t',
  396. 'c_int_least16_t',
  397. 'c_int_least32_t',
  398. 'c_int_least64_t',
  399. 'c_int_least8_t',
  400. 'c_intmax_t',
  401. 'c_intptr_t',
  402. 'c_long',
  403. 'c_long_double',
  404. 'c_long_double_complex',
  405. 'c_long_long',
  406. 'c_ptr',
  407. 'c_short',
  408. 'c_signed_char',
  409. 'c_size_t',
  410. 'character',
  411. 'complex',
  412. 'double',
  413. 'integer',
  414. 'logical',
  415. 'real',
  416. ])
  417. var isOperatorChar = /[+\-*&=<>\/\:]/
  418. var litOperator = /^\.(and|or|eq|lt|le|gt|ge|ne|not|eqv|neqv)\./i
  419. function tokenBase(stream, state) {
  420. if (stream.match(litOperator)) {
  421. return 'operator'
  422. }
  423. var ch = stream.next()
  424. if (ch == '!') {
  425. stream.skipToEnd()
  426. return 'comment'
  427. }
  428. if (ch == '"' || ch == "'") {
  429. state.tokenize = tokenString(ch)
  430. return state.tokenize(stream, state)
  431. }
  432. if (/[\[\]\(\),]/.test(ch)) {
  433. return null
  434. }
  435. if (/\d/.test(ch)) {
  436. stream.eatWhile(/[\w\.]/)
  437. return 'number'
  438. }
  439. if (isOperatorChar.test(ch)) {
  440. stream.eatWhile(isOperatorChar)
  441. return 'operator'
  442. }
  443. stream.eatWhile(/[\w\$_]/)
  444. var word = stream.current().toLowerCase()
  445. if (keywords.hasOwnProperty(word)) {
  446. return 'keyword'
  447. }
  448. if (builtins.hasOwnProperty(word) || dataTypes.hasOwnProperty(word)) {
  449. return 'builtin'
  450. }
  451. return 'variable'
  452. }
  453. function tokenString(quote) {
  454. return function (stream, state) {
  455. var escaped = false,
  456. next,
  457. end = false
  458. while ((next = stream.next()) != null) {
  459. if (next == quote && !escaped) {
  460. end = true
  461. break
  462. }
  463. escaped = !escaped && next == '\\'
  464. }
  465. if (end || !escaped) state.tokenize = null
  466. return 'string'
  467. }
  468. }
  469. // Interface
  470. return {
  471. startState: function () {
  472. return { tokenize: null }
  473. },
  474. token: function (stream, state) {
  475. if (stream.eatSpace()) return null
  476. var style = (state.tokenize || tokenBase)(stream, state)
  477. if (style == 'comment' || style == 'meta') return style
  478. return style
  479. },
  480. }
  481. })
  482. CodeMirror.defineMIME('text/x-fortran', 'fortran')
  483. })