prism-cue.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. (function (Prism) {
  2. // https://cuelang.org/docs/references/spec/
  3. // eslint-disable-next-line regexp/strict
  4. var stringEscape = /\\(?:(?!\2)|\2(?:[^()\r\n]|\([^()]*\)))/.source;
  5. // eslint-disable-next-line regexp/strict
  6. var stringTypes = /"""(?:[^\\"]|"(?!""\2)|<esc>)*"""/.source +
  7. // eslint-disable-next-line regexp/strict
  8. '|' + /'''(?:[^\\']|'(?!''\2)|<esc>)*'''/.source +
  9. // eslint-disable-next-line regexp/strict
  10. '|' + /"(?:[^\\\r\n"]|"(?!\2)|<esc>)*"/.source +
  11. // eslint-disable-next-line regexp/strict
  12. '|' + /'(?:[^\\\r\n']|'(?!\2)|<esc>)*'/.source;
  13. var stringLiteral = '(?:' + stringTypes.replace(/<esc>/g, stringEscape) + ')';
  14. Prism.languages.cue = {
  15. 'comment': {
  16. pattern: /\/\/.*/,
  17. greedy: true
  18. },
  19. 'string-literal': {
  20. // eslint-disable-next-line regexp/strict
  21. pattern: RegExp(/(^|[^#"'\\])(#*)/.source + stringLiteral + /(?!["'])\2/.source),
  22. lookbehind: true,
  23. greedy: true,
  24. inside: {
  25. // I'm using dirty hack here. We have to know the number hashes at the start of the string somehow,
  26. // but we can't look back. So instead, we will use a lookahead, go to the end of the string, and
  27. // capture the hashes at the end of the string.
  28. 'escape': {
  29. pattern: /(?=[\s\S]*["'](#*)$)\\\1(?:U[a-fA-F0-9]{1,8}|u[a-fA-F0-9]{1,4}|x[a-fA-F0-9]{1,2}|\d{2,3}|[^(])/,
  30. greedy: true,
  31. alias: 'string'
  32. },
  33. 'interpolation': {
  34. pattern: /(?=[\s\S]*["'](#*)$)\\\1\([^()]*\)/,
  35. greedy: true,
  36. inside: {
  37. 'punctuation': /^\\#*\(|\)$/,
  38. 'expression': {
  39. pattern: /[\s\S]+/,
  40. inside: null
  41. }
  42. }
  43. },
  44. 'string': /[\s\S]+/
  45. }
  46. },
  47. 'keyword': {
  48. pattern: /(^|[^\w$])(?:for|if|import|in|let|null|package)(?![\w$])/,
  49. lookbehind: true
  50. },
  51. 'boolean': {
  52. pattern: /(^|[^\w$])(?:false|true)(?![\w$])/,
  53. lookbehind: true
  54. },
  55. 'builtin': {
  56. pattern: /(^|[^\w$])(?:bool|bytes|float|float(?:32|64)|u?int(?:8|16|32|64|128)?|number|rune|string)(?![\w$])/,
  57. lookbehind: true
  58. },
  59. 'attribute': {
  60. pattern: /@[\w$]+(?=\s*\()/,
  61. alias: 'function'
  62. },
  63. 'function': {
  64. pattern: /(^|[^\w$])[a-z_$][\w$]*(?=\s*\()/i,
  65. lookbehind: true
  66. },
  67. 'number': {
  68. pattern: /(^|[^\w$.])(?:0b[01]+(?:_[01]+)*|0o[0-7]+(?:_[0-7]+)*|0[xX][0-9A-Fa-f]+(?:_[0-9A-Fa-f]+)*|(?:\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\.\d+(?:_\d+)*)(?:[eE][+-]?\d+(?:_\d+)*)?(?:[KMGTP]i?)?)(?![\w$])/,
  69. lookbehind: true
  70. },
  71. 'operator': /\.{3}|_\|_|&&?|\|\|?|[=!]~|[<>=!]=?|[+\-*/?]/,
  72. 'punctuation': /[()[\]{},.:]/
  73. };
  74. Prism.languages.cue['string-literal'].inside.interpolation.inside.expression.inside = Prism.languages.cue;
  75. }(Prism));