fortran.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * @param {string} value
  3. * @returns {RegExp}
  4. * */
  5. /**
  6. * @param {RegExp | string } re
  7. * @returns {string}
  8. */
  9. function source(re) {
  10. if (!re) return null;
  11. if (typeof re === "string") return re;
  12. return re.source;
  13. }
  14. /**
  15. * @param {...(RegExp | string) } args
  16. * @returns {string}
  17. */
  18. function concat(...args) {
  19. const joined = args.map((x) => source(x)).join("");
  20. return joined;
  21. }
  22. /*
  23. Language: Fortran
  24. Author: Anthony Scemama <scemama@irsamc.ups-tlse.fr>
  25. Website: https://en.wikipedia.org/wiki/Fortran
  26. Category: scientific
  27. */
  28. /** @type LanguageFn */
  29. function fortran(hljs) {
  30. const PARAMS = {
  31. className: 'params',
  32. begin: '\\(',
  33. end: '\\)'
  34. };
  35. const COMMENT = {
  36. variants: [
  37. hljs.COMMENT('!', '$', {
  38. relevance: 0
  39. }),
  40. // allow FORTRAN 77 style comments
  41. hljs.COMMENT('^C[ ]', '$', {
  42. relevance: 0
  43. }),
  44. hljs.COMMENT('^C$', '$', {
  45. relevance: 0
  46. })
  47. ]
  48. };
  49. // regex in both fortran and irpf90 should match
  50. const OPTIONAL_NUMBER_SUFFIX = /(_[a-z_\d]+)?/;
  51. const OPTIONAL_NUMBER_EXP = /([de][+-]?\d+)?/;
  52. const NUMBER = {
  53. className: 'number',
  54. variants: [
  55. {
  56. begin: concat(/\b\d+/, /\.(\d*)/, OPTIONAL_NUMBER_EXP, OPTIONAL_NUMBER_SUFFIX)
  57. },
  58. {
  59. begin: concat(/\b\d+/, OPTIONAL_NUMBER_EXP, OPTIONAL_NUMBER_SUFFIX)
  60. },
  61. {
  62. begin: concat(/\.\d+/, OPTIONAL_NUMBER_EXP, OPTIONAL_NUMBER_SUFFIX)
  63. }
  64. ],
  65. relevance: 0
  66. };
  67. const FUNCTION_DEF = {
  68. className: 'function',
  69. beginKeywords: 'subroutine function program',
  70. illegal: '[${=\\n]',
  71. contains: [
  72. hljs.UNDERSCORE_TITLE_MODE,
  73. PARAMS
  74. ]
  75. };
  76. const STRING = {
  77. className: 'string',
  78. relevance: 0,
  79. variants: [
  80. hljs.APOS_STRING_MODE,
  81. hljs.QUOTE_STRING_MODE
  82. ]
  83. };
  84. const KEYWORDS = {
  85. literal: '.False. .True.',
  86. keyword: 'kind do concurrent local shared while private call intrinsic where elsewhere ' +
  87. 'type endtype endmodule endselect endinterface end enddo endif if forall endforall only contains default return stop then block endblock endassociate ' +
  88. 'public subroutine|10 function program .and. .or. .not. .le. .eq. .ge. .gt. .lt. ' +
  89. 'goto save else use module select case ' +
  90. 'access blank direct exist file fmt form formatted iostat name named nextrec number opened rec recl sequential status unformatted unit ' +
  91. 'continue format pause cycle exit ' +
  92. 'c_null_char c_alert c_backspace c_form_feed flush wait decimal round iomsg ' +
  93. 'synchronous nopass non_overridable pass protected volatile abstract extends import ' +
  94. 'non_intrinsic value deferred generic final enumerator class associate bind enum ' +
  95. 'c_int c_short c_long c_long_long c_signed_char c_size_t c_int8_t c_int16_t c_int32_t c_int64_t c_int_least8_t c_int_least16_t ' +
  96. 'c_int_least32_t c_int_least64_t c_int_fast8_t c_int_fast16_t c_int_fast32_t c_int_fast64_t c_intmax_t C_intptr_t c_float c_double ' +
  97. 'c_long_double c_float_complex c_double_complex c_long_double_complex c_bool c_char c_null_ptr c_null_funptr ' +
  98. 'c_new_line c_carriage_return c_horizontal_tab c_vertical_tab iso_c_binding c_loc c_funloc c_associated c_f_pointer ' +
  99. 'c_ptr c_funptr iso_fortran_env character_storage_size error_unit file_storage_size input_unit iostat_end iostat_eor ' +
  100. 'numeric_storage_size output_unit c_f_procpointer ieee_arithmetic ieee_support_underflow_control ' +
  101. 'ieee_get_underflow_mode ieee_set_underflow_mode newunit contiguous recursive ' +
  102. 'pad position action delim readwrite eor advance nml interface procedure namelist include sequence elemental pure impure ' +
  103. 'integer real character complex logical codimension dimension allocatable|10 parameter ' +
  104. 'external implicit|10 none double precision assign intent optional pointer ' +
  105. 'target in out common equivalence data',
  106. built_in: 'alog alog10 amax0 amax1 amin0 amin1 amod cabs ccos cexp clog csin csqrt dabs dacos dasin datan datan2 dcos dcosh ddim dexp dint ' +
  107. 'dlog dlog10 dmax1 dmin1 dmod dnint dsign dsin dsinh dsqrt dtan dtanh float iabs idim idint idnint ifix isign max0 max1 min0 min1 sngl ' +
  108. 'algama cdabs cdcos cdexp cdlog cdsin cdsqrt cqabs cqcos cqexp cqlog cqsin cqsqrt dcmplx dconjg derf derfc dfloat dgamma dimag dlgama ' +
  109. 'iqint qabs qacos qasin qatan qatan2 qcmplx qconjg qcos qcosh qdim qerf qerfc qexp qgamma qimag qlgama qlog qlog10 qmax1 qmin1 qmod ' +
  110. 'qnint qsign qsin qsinh qsqrt qtan qtanh abs acos aimag aint anint asin atan atan2 char cmplx conjg cos cosh exp ichar index int log ' +
  111. 'log10 max min nint sign sin sinh sqrt tan tanh print write dim lge lgt lle llt mod nullify allocate deallocate ' +
  112. 'adjustl adjustr all allocated any associated bit_size btest ceiling count cshift date_and_time digits dot_product ' +
  113. 'eoshift epsilon exponent floor fraction huge iand ibclr ibits ibset ieor ior ishft ishftc lbound len_trim matmul ' +
  114. 'maxexponent maxloc maxval merge minexponent minloc minval modulo mvbits nearest pack present product ' +
  115. 'radix random_number random_seed range repeat reshape rrspacing scale scan selected_int_kind selected_real_kind ' +
  116. 'set_exponent shape size spacing spread sum system_clock tiny transpose trim ubound unpack verify achar iachar transfer ' +
  117. 'dble entry dprod cpu_time command_argument_count get_command get_command_argument get_environment_variable is_iostat_end ' +
  118. 'ieee_arithmetic ieee_support_underflow_control ieee_get_underflow_mode ieee_set_underflow_mode ' +
  119. 'is_iostat_eor move_alloc new_line selected_char_kind same_type_as extends_type_of ' +
  120. 'acosh asinh atanh bessel_j0 bessel_j1 bessel_jn bessel_y0 bessel_y1 bessel_yn erf erfc erfc_scaled gamma log_gamma hypot norm2 ' +
  121. 'atomic_define atomic_ref execute_command_line leadz trailz storage_size merge_bits ' +
  122. 'bge bgt ble blt dshiftl dshiftr findloc iall iany iparity image_index lcobound ucobound maskl maskr ' +
  123. 'num_images parity popcnt poppar shifta shiftl shiftr this_image sync change team co_broadcast co_max co_min co_sum co_reduce'
  124. };
  125. return {
  126. name: 'Fortran',
  127. case_insensitive: true,
  128. aliases: [
  129. 'f90',
  130. 'f95'
  131. ],
  132. keywords: KEYWORDS,
  133. illegal: /\/\*/,
  134. contains: [
  135. STRING,
  136. FUNCTION_DEF,
  137. // allow `C = value` for assignments so they aren't misdetected
  138. // as Fortran 77 style comments
  139. {
  140. begin: /^C\s*=(?!=)/,
  141. relevance: 0
  142. },
  143. COMMENT,
  144. NUMBER
  145. ]
  146. };
  147. }
  148. module.exports = fortran;