prism-v.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. (function (Prism) {
  2. var interpolationExpr = {
  3. pattern: /[\s\S]+/,
  4. inside: null
  5. };
  6. Prism.languages.v = Prism.languages.extend('clike', {
  7. 'string': {
  8. pattern: /r?(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
  9. alias: 'quoted-string',
  10. greedy: true,
  11. inside: {
  12. 'interpolation': {
  13. pattern: /((?:^|[^\\])(?:\\{2})*)\$(?:\{[^{}]*\}|\w+(?:\.\w+(?:\([^\(\)]*\))?|\[[^\[\]]+\])*)/,
  14. lookbehind: true,
  15. inside: {
  16. 'interpolation-variable': {
  17. pattern: /^\$\w[\s\S]*$/,
  18. alias: 'variable'
  19. },
  20. 'interpolation-punctuation': {
  21. pattern: /^\$\{|\}$/,
  22. alias: 'punctuation'
  23. },
  24. 'interpolation-expression': interpolationExpr
  25. }
  26. }
  27. }
  28. },
  29. 'class-name': {
  30. pattern: /(\b(?:enum|interface|struct|type)\s+)(?:C\.)?\w+/,
  31. lookbehind: true
  32. },
  33. 'keyword': /(?:\b(?:__global|as|asm|assert|atomic|break|chan|const|continue|defer|else|embed|enum|fn|for|go(?:to)?|if|import|in|interface|is|lock|match|module|mut|none|or|pub|return|rlock|select|shared|sizeof|static|struct|type(?:of)?|union|unsafe)|\$(?:else|for|if)|#(?:flag|include))\b/,
  34. 'number': /\b(?:0x[a-f\d]+(?:_[a-f\d]+)*|0b[01]+(?:_[01]+)*|0o[0-7]+(?:_[0-7]+)*|\d+(?:_\d+)*(?:\.\d+(?:_\d+)*)?)\b/i,
  35. 'operator': /~|\?|[*\/%^!=]=?|\+[=+]?|-[=-]?|\|[=|]?|&(?:=|&|\^=?)?|>(?:>=?|=)?|<(?:<=?|=|-)?|:=|\.\.\.?/,
  36. 'builtin': /\b(?:any(?:_float|_int)?|bool|byte(?:ptr)?|charptr|f(?:32|64)|i(?:8|16|64|128|nt)|rune|size_t|string|u(?:16|32|64|128)|voidptr)\b/
  37. });
  38. interpolationExpr.inside = Prism.languages.v;
  39. Prism.languages.insertBefore('v', 'string', {
  40. 'char': {
  41. pattern: /`(?:\\`|\\?[^`]{1,2})`/, // using {1,2} instead of `u` flag for compatibility
  42. alias: 'rune'
  43. }
  44. });
  45. Prism.languages.insertBefore('v', 'operator', {
  46. 'attribute': {
  47. pattern: /(^[\t ]*)\[(?:deprecated|direct_array_access|flag|inline|live|ref_only|typedef|unsafe_fn|windows_stdcall)\]/m,
  48. lookbehind: true,
  49. alias: 'annotation',
  50. inside: {
  51. 'punctuation': /[\[\]]/,
  52. 'keyword': /\w+/
  53. }
  54. },
  55. 'generic': {
  56. pattern: /<\w+>(?=\s*[\)\{])/,
  57. inside: {
  58. 'punctuation': /[<>]/,
  59. 'class-name': /\w+/
  60. }
  61. }
  62. });
  63. Prism.languages.insertBefore('v', 'function', {
  64. 'generic-function': {
  65. // e.g. foo<T>( ...
  66. pattern: /\b\w+\s*<\w+>(?=\()/,
  67. inside: {
  68. 'function': /^\w+/,
  69. 'generic': {
  70. pattern: /<\w+>/,
  71. inside: Prism.languages.v.generic.inside
  72. }
  73. }
  74. }
  75. });
  76. }(Prism));