fortran.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. Language: Fortran
  3. Author: Anthony Scemama <scemama@irsamc.ups-tlse.fr>
  4. Website: https://en.wikipedia.org/wiki/Fortran
  5. Category: scientific
  6. */
  7. /** @type LanguageFn */
  8. function fortran(hljs) {
  9. const regex = hljs.regex;
  10. const PARAMS = {
  11. className: 'params',
  12. begin: '\\(',
  13. end: '\\)'
  14. };
  15. const COMMENT = { variants: [
  16. hljs.COMMENT('!', '$', { relevance: 0 }),
  17. // allow FORTRAN 77 style comments
  18. hljs.COMMENT('^C[ ]', '$', { relevance: 0 }),
  19. hljs.COMMENT('^C$', '$', { relevance: 0 })
  20. ] };
  21. // regex in both fortran and irpf90 should match
  22. const OPTIONAL_NUMBER_SUFFIX = /(_[a-z_\d]+)?/;
  23. const OPTIONAL_NUMBER_EXP = /([de][+-]?\d+)?/;
  24. const NUMBER = {
  25. className: 'number',
  26. variants: [
  27. { begin: regex.concat(/\b\d+/, /\.(\d*)/, OPTIONAL_NUMBER_EXP, OPTIONAL_NUMBER_SUFFIX) },
  28. { begin: regex.concat(/\b\d+/, OPTIONAL_NUMBER_EXP, OPTIONAL_NUMBER_SUFFIX) },
  29. { begin: regex.concat(/\.\d+/, OPTIONAL_NUMBER_EXP, OPTIONAL_NUMBER_SUFFIX) }
  30. ],
  31. relevance: 0
  32. };
  33. const FUNCTION_DEF = {
  34. className: 'function',
  35. beginKeywords: 'subroutine function program',
  36. illegal: '[${=\\n]',
  37. contains: [
  38. hljs.UNDERSCORE_TITLE_MODE,
  39. PARAMS
  40. ]
  41. };
  42. const STRING = {
  43. className: 'string',
  44. relevance: 0,
  45. variants: [
  46. hljs.APOS_STRING_MODE,
  47. hljs.QUOTE_STRING_MODE
  48. ]
  49. };
  50. const KEYWORDS = [
  51. "kind",
  52. "do",
  53. "concurrent",
  54. "local",
  55. "shared",
  56. "while",
  57. "private",
  58. "call",
  59. "intrinsic",
  60. "where",
  61. "elsewhere",
  62. "type",
  63. "endtype",
  64. "endmodule",
  65. "endselect",
  66. "endinterface",
  67. "end",
  68. "enddo",
  69. "endif",
  70. "if",
  71. "forall",
  72. "endforall",
  73. "only",
  74. "contains",
  75. "default",
  76. "return",
  77. "stop",
  78. "then",
  79. "block",
  80. "endblock",
  81. "endassociate",
  82. "public",
  83. "subroutine|10",
  84. "function",
  85. "program",
  86. ".and.",
  87. ".or.",
  88. ".not.",
  89. ".le.",
  90. ".eq.",
  91. ".ge.",
  92. ".gt.",
  93. ".lt.",
  94. "goto",
  95. "save",
  96. "else",
  97. "use",
  98. "module",
  99. "select",
  100. "case",
  101. "access",
  102. "blank",
  103. "direct",
  104. "exist",
  105. "file",
  106. "fmt",
  107. "form",
  108. "formatted",
  109. "iostat",
  110. "name",
  111. "named",
  112. "nextrec",
  113. "number",
  114. "opened",
  115. "rec",
  116. "recl",
  117. "sequential",
  118. "status",
  119. "unformatted",
  120. "unit",
  121. "continue",
  122. "format",
  123. "pause",
  124. "cycle",
  125. "exit",
  126. "c_null_char",
  127. "c_alert",
  128. "c_backspace",
  129. "c_form_feed",
  130. "flush",
  131. "wait",
  132. "decimal",
  133. "round",
  134. "iomsg",
  135. "synchronous",
  136. "nopass",
  137. "non_overridable",
  138. "pass",
  139. "protected",
  140. "volatile",
  141. "abstract",
  142. "extends",
  143. "import",
  144. "non_intrinsic",
  145. "value",
  146. "deferred",
  147. "generic",
  148. "final",
  149. "enumerator",
  150. "class",
  151. "associate",
  152. "bind",
  153. "enum",
  154. "c_int",
  155. "c_short",
  156. "c_long",
  157. "c_long_long",
  158. "c_signed_char",
  159. "c_size_t",
  160. "c_int8_t",
  161. "c_int16_t",
  162. "c_int32_t",
  163. "c_int64_t",
  164. "c_int_least8_t",
  165. "c_int_least16_t",
  166. "c_int_least32_t",
  167. "c_int_least64_t",
  168. "c_int_fast8_t",
  169. "c_int_fast16_t",
  170. "c_int_fast32_t",
  171. "c_int_fast64_t",
  172. "c_intmax_t",
  173. "C_intptr_t",
  174. "c_float",
  175. "c_double",
  176. "c_long_double",
  177. "c_float_complex",
  178. "c_double_complex",
  179. "c_long_double_complex",
  180. "c_bool",
  181. "c_char",
  182. "c_null_ptr",
  183. "c_null_funptr",
  184. "c_new_line",
  185. "c_carriage_return",
  186. "c_horizontal_tab",
  187. "c_vertical_tab",
  188. "iso_c_binding",
  189. "c_loc",
  190. "c_funloc",
  191. "c_associated",
  192. "c_f_pointer",
  193. "c_ptr",
  194. "c_funptr",
  195. "iso_fortran_env",
  196. "character_storage_size",
  197. "error_unit",
  198. "file_storage_size",
  199. "input_unit",
  200. "iostat_end",
  201. "iostat_eor",
  202. "numeric_storage_size",
  203. "output_unit",
  204. "c_f_procpointer",
  205. "ieee_arithmetic",
  206. "ieee_support_underflow_control",
  207. "ieee_get_underflow_mode",
  208. "ieee_set_underflow_mode",
  209. "newunit",
  210. "contiguous",
  211. "recursive",
  212. "pad",
  213. "position",
  214. "action",
  215. "delim",
  216. "readwrite",
  217. "eor",
  218. "advance",
  219. "nml",
  220. "interface",
  221. "procedure",
  222. "namelist",
  223. "include",
  224. "sequence",
  225. "elemental",
  226. "pure",
  227. "impure",
  228. "integer",
  229. "real",
  230. "character",
  231. "complex",
  232. "logical",
  233. "codimension",
  234. "dimension",
  235. "allocatable|10",
  236. "parameter",
  237. "external",
  238. "implicit|10",
  239. "none",
  240. "double",
  241. "precision",
  242. "assign",
  243. "intent",
  244. "optional",
  245. "pointer",
  246. "target",
  247. "in",
  248. "out",
  249. "common",
  250. "equivalence",
  251. "data"
  252. ];
  253. const LITERALS = [
  254. ".False.",
  255. ".True."
  256. ];
  257. const BUILT_INS = [
  258. "alog",
  259. "alog10",
  260. "amax0",
  261. "amax1",
  262. "amin0",
  263. "amin1",
  264. "amod",
  265. "cabs",
  266. "ccos",
  267. "cexp",
  268. "clog",
  269. "csin",
  270. "csqrt",
  271. "dabs",
  272. "dacos",
  273. "dasin",
  274. "datan",
  275. "datan2",
  276. "dcos",
  277. "dcosh",
  278. "ddim",
  279. "dexp",
  280. "dint",
  281. "dlog",
  282. "dlog10",
  283. "dmax1",
  284. "dmin1",
  285. "dmod",
  286. "dnint",
  287. "dsign",
  288. "dsin",
  289. "dsinh",
  290. "dsqrt",
  291. "dtan",
  292. "dtanh",
  293. "float",
  294. "iabs",
  295. "idim",
  296. "idint",
  297. "idnint",
  298. "ifix",
  299. "isign",
  300. "max0",
  301. "max1",
  302. "min0",
  303. "min1",
  304. "sngl",
  305. "algama",
  306. "cdabs",
  307. "cdcos",
  308. "cdexp",
  309. "cdlog",
  310. "cdsin",
  311. "cdsqrt",
  312. "cqabs",
  313. "cqcos",
  314. "cqexp",
  315. "cqlog",
  316. "cqsin",
  317. "cqsqrt",
  318. "dcmplx",
  319. "dconjg",
  320. "derf",
  321. "derfc",
  322. "dfloat",
  323. "dgamma",
  324. "dimag",
  325. "dlgama",
  326. "iqint",
  327. "qabs",
  328. "qacos",
  329. "qasin",
  330. "qatan",
  331. "qatan2",
  332. "qcmplx",
  333. "qconjg",
  334. "qcos",
  335. "qcosh",
  336. "qdim",
  337. "qerf",
  338. "qerfc",
  339. "qexp",
  340. "qgamma",
  341. "qimag",
  342. "qlgama",
  343. "qlog",
  344. "qlog10",
  345. "qmax1",
  346. "qmin1",
  347. "qmod",
  348. "qnint",
  349. "qsign",
  350. "qsin",
  351. "qsinh",
  352. "qsqrt",
  353. "qtan",
  354. "qtanh",
  355. "abs",
  356. "acos",
  357. "aimag",
  358. "aint",
  359. "anint",
  360. "asin",
  361. "atan",
  362. "atan2",
  363. "char",
  364. "cmplx",
  365. "conjg",
  366. "cos",
  367. "cosh",
  368. "exp",
  369. "ichar",
  370. "index",
  371. "int",
  372. "log",
  373. "log10",
  374. "max",
  375. "min",
  376. "nint",
  377. "sign",
  378. "sin",
  379. "sinh",
  380. "sqrt",
  381. "tan",
  382. "tanh",
  383. "print",
  384. "write",
  385. "dim",
  386. "lge",
  387. "lgt",
  388. "lle",
  389. "llt",
  390. "mod",
  391. "nullify",
  392. "allocate",
  393. "deallocate",
  394. "adjustl",
  395. "adjustr",
  396. "all",
  397. "allocated",
  398. "any",
  399. "associated",
  400. "bit_size",
  401. "btest",
  402. "ceiling",
  403. "count",
  404. "cshift",
  405. "date_and_time",
  406. "digits",
  407. "dot_product",
  408. "eoshift",
  409. "epsilon",
  410. "exponent",
  411. "floor",
  412. "fraction",
  413. "huge",
  414. "iand",
  415. "ibclr",
  416. "ibits",
  417. "ibset",
  418. "ieor",
  419. "ior",
  420. "ishft",
  421. "ishftc",
  422. "lbound",
  423. "len_trim",
  424. "matmul",
  425. "maxexponent",
  426. "maxloc",
  427. "maxval",
  428. "merge",
  429. "minexponent",
  430. "minloc",
  431. "minval",
  432. "modulo",
  433. "mvbits",
  434. "nearest",
  435. "pack",
  436. "present",
  437. "product",
  438. "radix",
  439. "random_number",
  440. "random_seed",
  441. "range",
  442. "repeat",
  443. "reshape",
  444. "rrspacing",
  445. "scale",
  446. "scan",
  447. "selected_int_kind",
  448. "selected_real_kind",
  449. "set_exponent",
  450. "shape",
  451. "size",
  452. "spacing",
  453. "spread",
  454. "sum",
  455. "system_clock",
  456. "tiny",
  457. "transpose",
  458. "trim",
  459. "ubound",
  460. "unpack",
  461. "verify",
  462. "achar",
  463. "iachar",
  464. "transfer",
  465. "dble",
  466. "entry",
  467. "dprod",
  468. "cpu_time",
  469. "command_argument_count",
  470. "get_command",
  471. "get_command_argument",
  472. "get_environment_variable",
  473. "is_iostat_end",
  474. "ieee_arithmetic",
  475. "ieee_support_underflow_control",
  476. "ieee_get_underflow_mode",
  477. "ieee_set_underflow_mode",
  478. "is_iostat_eor",
  479. "move_alloc",
  480. "new_line",
  481. "selected_char_kind",
  482. "same_type_as",
  483. "extends_type_of",
  484. "acosh",
  485. "asinh",
  486. "atanh",
  487. "bessel_j0",
  488. "bessel_j1",
  489. "bessel_jn",
  490. "bessel_y0",
  491. "bessel_y1",
  492. "bessel_yn",
  493. "erf",
  494. "erfc",
  495. "erfc_scaled",
  496. "gamma",
  497. "log_gamma",
  498. "hypot",
  499. "norm2",
  500. "atomic_define",
  501. "atomic_ref",
  502. "execute_command_line",
  503. "leadz",
  504. "trailz",
  505. "storage_size",
  506. "merge_bits",
  507. "bge",
  508. "bgt",
  509. "ble",
  510. "blt",
  511. "dshiftl",
  512. "dshiftr",
  513. "findloc",
  514. "iall",
  515. "iany",
  516. "iparity",
  517. "image_index",
  518. "lcobound",
  519. "ucobound",
  520. "maskl",
  521. "maskr",
  522. "num_images",
  523. "parity",
  524. "popcnt",
  525. "poppar",
  526. "shifta",
  527. "shiftl",
  528. "shiftr",
  529. "this_image",
  530. "sync",
  531. "change",
  532. "team",
  533. "co_broadcast",
  534. "co_max",
  535. "co_min",
  536. "co_sum",
  537. "co_reduce"
  538. ];
  539. return {
  540. name: 'Fortran',
  541. case_insensitive: true,
  542. aliases: [
  543. 'f90',
  544. 'f95'
  545. ],
  546. keywords: {
  547. $pattern: /\b[a-z][a-z0-9_]+\b|\.[a-z][a-z0-9_]+\./,
  548. keyword: KEYWORDS,
  549. literal: LITERALS,
  550. built_in: BUILT_INS
  551. },
  552. illegal: /\/\*/,
  553. contains: [
  554. STRING,
  555. FUNCTION_DEF,
  556. // allow `C = value` for assignments so they aren't misdetected
  557. // as Fortran 77 style comments
  558. {
  559. begin: /^C\s*=(?!=)/,
  560. relevance: 0
  561. },
  562. COMMENT,
  563. NUMBER
  564. ]
  565. };
  566. }
  567. export { fortran as default };