julia.js 8.7 KB

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