julia.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. Language: Julia
  3. Description: Julia is a high-level, high-performance, dynamic programming language.
  4. Author: Kenta Sato <bicycle1885@gmail.com>
  5. Contributors: Alex Arslan <ararslan@comcast.net>, Fredrik Ekre <ekrefredrik@gmail.com>
  6. Website: https://julialang.org
  7. Category: scientific
  8. */
  9. function julia(hljs) {
  10. // Since there are numerous special names in Julia, it is too much trouble
  11. // to maintain them by hand. Hence these names (i.e. keywords, literals and
  12. // built-ins) are automatically generated from Julia 1.5.2 itself through
  13. // the following scripts for each.
  14. // ref: https://docs.julialang.org/en/v1/manual/variables/#Allowed-Variable-Names
  15. const VARIABLE_NAME_RE = '[A-Za-z_\\u00A1-\\uFFFF][A-Za-z_0-9\\u00A1-\\uFFFF]*';
  16. // # keyword generator, multi-word keywords handled manually below (Julia 1.5.2)
  17. // import REPL.REPLCompletions
  18. // res = String["in", "isa", "where"]
  19. // for kw in collect(x.keyword for x in REPLCompletions.complete_keyword(""))
  20. // if !(contains(kw, " ") || kw == "struct")
  21. // push!(res, kw)
  22. // end
  23. // end
  24. // sort!(unique!(res))
  25. // foreach(x -> println("\'", x, "\',"), res)
  26. const KEYWORD_LIST = [
  27. 'baremodule',
  28. 'begin',
  29. 'break',
  30. 'catch',
  31. 'ccall',
  32. 'const',
  33. 'continue',
  34. 'do',
  35. 'else',
  36. 'elseif',
  37. 'end',
  38. 'export',
  39. 'false',
  40. 'finally',
  41. 'for',
  42. 'function',
  43. 'global',
  44. 'if',
  45. 'import',
  46. 'in',
  47. 'isa',
  48. 'let',
  49. 'local',
  50. 'macro',
  51. 'module',
  52. 'quote',
  53. 'return',
  54. 'true',
  55. 'try',
  56. 'using',
  57. 'where',
  58. 'while',
  59. ];
  60. // # literal generator (Julia 1.5.2)
  61. // import REPL.REPLCompletions
  62. // res = String["true", "false"]
  63. // for compl in filter!(x -> isa(x, REPLCompletions.ModuleCompletion) && (x.parent === Base || x.parent === Core),
  64. // REPLCompletions.completions("", 0)[1])
  65. // try
  66. // v = eval(Symbol(compl.mod))
  67. // if !(v isa Function || v isa Type || v isa TypeVar || v isa Module || v isa Colon)
  68. // push!(res, compl.mod)
  69. // end
  70. // catch e
  71. // end
  72. // end
  73. // sort!(unique!(res))
  74. // foreach(x -> println("\'", x, "\',"), res)
  75. const LITERAL_LIST = [
  76. 'ARGS',
  77. 'C_NULL',
  78. 'DEPOT_PATH',
  79. 'ENDIAN_BOM',
  80. 'ENV',
  81. 'Inf',
  82. 'Inf16',
  83. 'Inf32',
  84. 'Inf64',
  85. 'InsertionSort',
  86. 'LOAD_PATH',
  87. 'MergeSort',
  88. 'NaN',
  89. 'NaN16',
  90. 'NaN32',
  91. 'NaN64',
  92. 'PROGRAM_FILE',
  93. 'QuickSort',
  94. 'RoundDown',
  95. 'RoundFromZero',
  96. 'RoundNearest',
  97. 'RoundNearestTiesAway',
  98. 'RoundNearestTiesUp',
  99. 'RoundToZero',
  100. 'RoundUp',
  101. 'VERSION|0',
  102. 'devnull',
  103. 'false',
  104. 'im',
  105. 'missing',
  106. 'nothing',
  107. 'pi',
  108. 'stderr',
  109. 'stdin',
  110. 'stdout',
  111. 'true',
  112. 'undef',
  113. 'π',
  114. 'ℯ',
  115. ];
  116. // # built_in generator (Julia 1.5.2)
  117. // import REPL.REPLCompletions
  118. // res = String[]
  119. // for compl in filter!(x -> isa(x, REPLCompletions.ModuleCompletion) && (x.parent === Base || x.parent === Core),
  120. // REPLCompletions.completions("", 0)[1])
  121. // try
  122. // v = eval(Symbol(compl.mod))
  123. // if (v isa Type || v isa TypeVar) && (compl.mod != "=>")
  124. // push!(res, compl.mod)
  125. // end
  126. // catch e
  127. // end
  128. // end
  129. // sort!(unique!(res))
  130. // foreach(x -> println("\'", x, "\',"), res)
  131. const BUILT_IN_LIST = [
  132. 'AbstractArray',
  133. 'AbstractChannel',
  134. 'AbstractChar',
  135. 'AbstractDict',
  136. 'AbstractDisplay',
  137. 'AbstractFloat',
  138. 'AbstractIrrational',
  139. 'AbstractMatrix',
  140. 'AbstractRange',
  141. 'AbstractSet',
  142. 'AbstractString',
  143. 'AbstractUnitRange',
  144. 'AbstractVecOrMat',
  145. 'AbstractVector',
  146. 'Any',
  147. 'ArgumentError',
  148. 'Array',
  149. 'AssertionError',
  150. 'BigFloat',
  151. 'BigInt',
  152. 'BitArray',
  153. 'BitMatrix',
  154. 'BitSet',
  155. 'BitVector',
  156. 'Bool',
  157. 'BoundsError',
  158. 'CapturedException',
  159. 'CartesianIndex',
  160. 'CartesianIndices',
  161. 'Cchar',
  162. 'Cdouble',
  163. 'Cfloat',
  164. 'Channel',
  165. 'Char',
  166. 'Cint',
  167. 'Cintmax_t',
  168. 'Clong',
  169. 'Clonglong',
  170. 'Cmd',
  171. 'Colon',
  172. 'Complex',
  173. 'ComplexF16',
  174. 'ComplexF32',
  175. 'ComplexF64',
  176. 'CompositeException',
  177. 'Condition',
  178. 'Cptrdiff_t',
  179. 'Cshort',
  180. 'Csize_t',
  181. 'Cssize_t',
  182. 'Cstring',
  183. 'Cuchar',
  184. 'Cuint',
  185. 'Cuintmax_t',
  186. 'Culong',
  187. 'Culonglong',
  188. 'Cushort',
  189. 'Cvoid',
  190. 'Cwchar_t',
  191. 'Cwstring',
  192. 'DataType',
  193. 'DenseArray',
  194. 'DenseMatrix',
  195. 'DenseVecOrMat',
  196. 'DenseVector',
  197. 'Dict',
  198. 'DimensionMismatch',
  199. 'Dims',
  200. 'DivideError',
  201. 'DomainError',
  202. 'EOFError',
  203. 'Enum',
  204. 'ErrorException',
  205. 'Exception',
  206. 'ExponentialBackOff',
  207. 'Expr',
  208. 'Float16',
  209. 'Float32',
  210. 'Float64',
  211. 'Function',
  212. 'GlobalRef',
  213. 'HTML',
  214. 'IO',
  215. 'IOBuffer',
  216. 'IOContext',
  217. 'IOStream',
  218. 'IdDict',
  219. 'IndexCartesian',
  220. 'IndexLinear',
  221. 'IndexStyle',
  222. 'InexactError',
  223. 'InitError',
  224. 'Int',
  225. 'Int128',
  226. 'Int16',
  227. 'Int32',
  228. 'Int64',
  229. 'Int8',
  230. 'Integer',
  231. 'InterruptException',
  232. 'InvalidStateException',
  233. 'Irrational',
  234. 'KeyError',
  235. 'LinRange',
  236. 'LineNumberNode',
  237. 'LinearIndices',
  238. 'LoadError',
  239. 'MIME',
  240. 'Matrix',
  241. 'Method',
  242. 'MethodError',
  243. 'Missing',
  244. 'MissingException',
  245. 'Module',
  246. 'NTuple',
  247. 'NamedTuple',
  248. 'Nothing',
  249. 'Number',
  250. 'OrdinalRange',
  251. 'OutOfMemoryError',
  252. 'OverflowError',
  253. 'Pair',
  254. 'PartialQuickSort',
  255. 'PermutedDimsArray',
  256. 'Pipe',
  257. 'ProcessFailedException',
  258. 'Ptr',
  259. 'QuoteNode',
  260. 'Rational',
  261. 'RawFD',
  262. 'ReadOnlyMemoryError',
  263. 'Real',
  264. 'ReentrantLock',
  265. 'Ref',
  266. 'Regex',
  267. 'RegexMatch',
  268. 'RoundingMode',
  269. 'SegmentationFault',
  270. 'Set',
  271. 'Signed',
  272. 'Some',
  273. 'StackOverflowError',
  274. 'StepRange',
  275. 'StepRangeLen',
  276. 'StridedArray',
  277. 'StridedMatrix',
  278. 'StridedVecOrMat',
  279. 'StridedVector',
  280. 'String',
  281. 'StringIndexError',
  282. 'SubArray',
  283. 'SubString',
  284. 'SubstitutionString',
  285. 'Symbol',
  286. 'SystemError',
  287. 'Task',
  288. 'TaskFailedException',
  289. 'Text',
  290. 'TextDisplay',
  291. 'Timer',
  292. 'Tuple',
  293. 'Type',
  294. 'TypeError',
  295. 'TypeVar',
  296. 'UInt',
  297. 'UInt128',
  298. 'UInt16',
  299. 'UInt32',
  300. 'UInt64',
  301. 'UInt8',
  302. 'UndefInitializer',
  303. 'UndefKeywordError',
  304. 'UndefRefError',
  305. 'UndefVarError',
  306. 'Union',
  307. 'UnionAll',
  308. 'UnitRange',
  309. 'Unsigned',
  310. 'Val',
  311. 'Vararg',
  312. 'VecElement',
  313. 'VecOrMat',
  314. 'Vector',
  315. 'VersionNumber',
  316. 'WeakKeyDict',
  317. 'WeakRef',
  318. ];
  319. const KEYWORDS = {
  320. $pattern: VARIABLE_NAME_RE,
  321. keyword: KEYWORD_LIST,
  322. literal: LITERAL_LIST,
  323. built_in: BUILT_IN_LIST,
  324. };
  325. // placeholder for recursive self-reference
  326. const DEFAULT = {
  327. keywords: KEYWORDS,
  328. illegal: /<\//
  329. };
  330. // ref: https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/
  331. const NUMBER = {
  332. className: 'number',
  333. // supported numeric literals:
  334. // * binary literal (e.g. 0x10)
  335. // * octal literal (e.g. 0o76543210)
  336. // * hexadecimal literal (e.g. 0xfedcba876543210)
  337. // * hexadecimal floating point literal (e.g. 0x1p0, 0x1.2p2)
  338. // * decimal literal (e.g. 9876543210, 100_000_000)
  339. // * floating pointe literal (e.g. 1.2, 1.2f, .2, 1., 1.2e10, 1.2e-10)
  340. begin: /(\b0x[\d_]*(\.[\d_]*)?|0x\.\d[\d_]*)p[-+]?\d+|\b0[box][a-fA-F0-9][a-fA-F0-9_]*|(\b\d[\d_]*(\.[\d_]*)?|\.\d[\d_]*)([eEfF][-+]?\d+)?/,
  341. relevance: 0
  342. };
  343. const CHAR = {
  344. className: 'string',
  345. begin: /'(.|\\[xXuU][a-zA-Z0-9]+)'/
  346. };
  347. const INTERPOLATION = {
  348. className: 'subst',
  349. begin: /\$\(/,
  350. end: /\)/,
  351. keywords: KEYWORDS
  352. };
  353. const INTERPOLATED_VARIABLE = {
  354. className: 'variable',
  355. begin: '\\$' + VARIABLE_NAME_RE
  356. };
  357. // TODO: neatly escape normal code in string literal
  358. const STRING = {
  359. className: 'string',
  360. contains: [
  361. hljs.BACKSLASH_ESCAPE,
  362. INTERPOLATION,
  363. INTERPOLATED_VARIABLE
  364. ],
  365. variants: [
  366. {
  367. begin: /\w*"""/,
  368. end: /"""\w*/,
  369. relevance: 10
  370. },
  371. {
  372. begin: /\w*"/,
  373. end: /"\w*/
  374. }
  375. ]
  376. };
  377. const COMMAND = {
  378. className: 'string',
  379. contains: [
  380. hljs.BACKSLASH_ESCAPE,
  381. INTERPOLATION,
  382. INTERPOLATED_VARIABLE
  383. ],
  384. begin: '`',
  385. end: '`'
  386. };
  387. const MACROCALL = {
  388. className: 'meta',
  389. begin: '@' + VARIABLE_NAME_RE
  390. };
  391. const COMMENT = {
  392. className: 'comment',
  393. variants: [
  394. {
  395. begin: '#=',
  396. end: '=#',
  397. relevance: 10
  398. },
  399. {
  400. begin: '#',
  401. end: '$'
  402. }
  403. ]
  404. };
  405. DEFAULT.name = 'Julia';
  406. DEFAULT.contains = [
  407. NUMBER,
  408. CHAR,
  409. STRING,
  410. COMMAND,
  411. MACROCALL,
  412. COMMENT,
  413. hljs.HASH_COMMENT_MODE,
  414. {
  415. className: 'keyword',
  416. begin:
  417. '\\b(((abstract|primitive)\\s+)type|(mutable\\s+)?struct)\\b'
  418. },
  419. { begin: /<:/ } // relevance booster
  420. ];
  421. INTERPOLATION.contains = DEFAULT.contains;
  422. return DEFAULT;
  423. }
  424. export { julia as default };