gams.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 } re
  16. * @returns {string}
  17. */
  18. function anyNumberOfTimes(re) {
  19. return concat('(', re, ')*');
  20. }
  21. /**
  22. * @param {...(RegExp | string) } args
  23. * @returns {string}
  24. */
  25. function concat(...args) {
  26. const joined = args.map((x) => source(x)).join("");
  27. return joined;
  28. }
  29. /** @type LanguageFn */
  30. function gams(hljs) {
  31. const KEYWORDS = {
  32. keyword:
  33. 'abort acronym acronyms alias all and assign binary card diag display ' +
  34. 'else eq file files for free ge gt if integer le loop lt maximizing ' +
  35. 'minimizing model models ne negative no not option options or ord ' +
  36. 'positive prod put putpage puttl repeat sameas semicont semiint smax ' +
  37. 'smin solve sos1 sos2 sum system table then until using while xor yes',
  38. literal:
  39. 'eps inf na',
  40. built_in:
  41. 'abs arccos arcsin arctan arctan2 Beta betaReg binomial ceil centropy ' +
  42. 'cos cosh cvPower div div0 eDist entropy errorf execSeed exp fact ' +
  43. 'floor frac gamma gammaReg log logBeta logGamma log10 log2 mapVal max ' +
  44. 'min mod ncpCM ncpF ncpVUpow ncpVUsin normal pi poly power ' +
  45. 'randBinomial randLinear randTriangle round rPower sigmoid sign ' +
  46. 'signPower sin sinh slexp sllog10 slrec sqexp sqlog10 sqr sqrec sqrt ' +
  47. 'tan tanh trunc uniform uniformInt vcPower bool_and bool_eqv bool_imp ' +
  48. 'bool_not bool_or bool_xor ifThen rel_eq rel_ge rel_gt rel_le rel_lt ' +
  49. 'rel_ne gday gdow ghour gleap gmillisec gminute gmonth gsecond gyear ' +
  50. 'jdate jnow jstart jtime errorLevel execError gamsRelease gamsVersion ' +
  51. 'handleCollect handleDelete handleStatus handleSubmit heapFree ' +
  52. 'heapLimit heapSize jobHandle jobKill jobStatus jobTerminate ' +
  53. 'licenseLevel licenseStatus maxExecError sleep timeClose timeComp ' +
  54. 'timeElapsed timeExec timeStart'
  55. };
  56. const PARAMS = {
  57. className: 'params',
  58. begin: /\(/,
  59. end: /\)/,
  60. excludeBegin: true,
  61. excludeEnd: true
  62. };
  63. const SYMBOLS = {
  64. className: 'symbol',
  65. variants: [
  66. {
  67. begin: /=[lgenxc]=/
  68. },
  69. {
  70. begin: /\$/
  71. }
  72. ]
  73. };
  74. const QSTR = { // One-line quoted comment string
  75. className: 'comment',
  76. variants: [
  77. {
  78. begin: '\'',
  79. end: '\''
  80. },
  81. {
  82. begin: '"',
  83. end: '"'
  84. }
  85. ],
  86. illegal: '\\n',
  87. contains: [hljs.BACKSLASH_ESCAPE]
  88. };
  89. const ASSIGNMENT = {
  90. begin: '/',
  91. end: '/',
  92. keywords: KEYWORDS,
  93. contains: [
  94. QSTR,
  95. hljs.C_LINE_COMMENT_MODE,
  96. hljs.C_BLOCK_COMMENT_MODE,
  97. hljs.QUOTE_STRING_MODE,
  98. hljs.APOS_STRING_MODE,
  99. hljs.C_NUMBER_MODE
  100. ]
  101. };
  102. const COMMENT_WORD = /[a-z0-9&#*=?@\\><:,()$[\]_.{}!+%^-]+/;
  103. const DESCTEXT = { // Parameter/set/variable description text
  104. begin: /[a-z][a-z0-9_]*(\([a-z0-9_, ]*\))?[ \t]+/,
  105. excludeBegin: true,
  106. end: '$',
  107. endsWithParent: true,
  108. contains: [
  109. QSTR,
  110. ASSIGNMENT,
  111. {
  112. className: 'comment',
  113. // one comment word, then possibly more
  114. begin: concat(
  115. COMMENT_WORD,
  116. // [ ] because \s would be too broad (matching newlines)
  117. anyNumberOfTimes(concat(/[ ]+/, COMMENT_WORD))
  118. ),
  119. relevance: 0
  120. }
  121. ]
  122. };
  123. return {
  124. name: 'GAMS',
  125. aliases: ['gms'],
  126. case_insensitive: true,
  127. keywords: KEYWORDS,
  128. contains: [
  129. hljs.COMMENT(/^\$ontext/, /^\$offtext/),
  130. {
  131. className: 'meta',
  132. begin: '^\\$[a-z0-9]+',
  133. end: '$',
  134. returnBegin: true,
  135. contains: [
  136. {
  137. className: 'meta-keyword',
  138. begin: '^\\$[a-z0-9]+'
  139. }
  140. ]
  141. },
  142. hljs.COMMENT('^\\*', '$'),
  143. hljs.C_LINE_COMMENT_MODE,
  144. hljs.C_BLOCK_COMMENT_MODE,
  145. hljs.QUOTE_STRING_MODE,
  146. hljs.APOS_STRING_MODE,
  147. // Declarations
  148. {
  149. beginKeywords:
  150. 'set sets parameter parameters variable variables ' +
  151. 'scalar scalars equation equations',
  152. end: ';',
  153. contains: [
  154. hljs.COMMENT('^\\*', '$'),
  155. hljs.C_LINE_COMMENT_MODE,
  156. hljs.C_BLOCK_COMMENT_MODE,
  157. hljs.QUOTE_STRING_MODE,
  158. hljs.APOS_STRING_MODE,
  159. ASSIGNMENT,
  160. DESCTEXT
  161. ]
  162. },
  163. { // table environment
  164. beginKeywords: 'table',
  165. end: ';',
  166. returnBegin: true,
  167. contains: [
  168. { // table header row
  169. beginKeywords: 'table',
  170. end: '$',
  171. contains: [DESCTEXT]
  172. },
  173. hljs.COMMENT('^\\*', '$'),
  174. hljs.C_LINE_COMMENT_MODE,
  175. hljs.C_BLOCK_COMMENT_MODE,
  176. hljs.QUOTE_STRING_MODE,
  177. hljs.APOS_STRING_MODE,
  178. hljs.C_NUMBER_MODE
  179. // Table does not contain DESCTEXT or ASSIGNMENT
  180. ]
  181. },
  182. // Function definitions
  183. {
  184. className: 'function',
  185. begin: /^[a-z][a-z0-9_,\-+' ()$]+\.{2}/,
  186. returnBegin: true,
  187. contains: [
  188. { // Function title
  189. className: 'title',
  190. begin: /^[a-z0-9_]+/
  191. },
  192. PARAMS,
  193. SYMBOLS
  194. ]
  195. },
  196. hljs.C_NUMBER_MODE,
  197. SYMBOLS
  198. ]
  199. };
  200. }
  201. module.exports = gams;