prism-dart.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. (function (Prism) {
  2. var keywords = [
  3. /\b(?:async|sync|yield)\*/,
  4. /\b(?:abstract|assert|async|await|break|case|catch|class|const|continue|covariant|default|deferred|do|dynamic|else|enum|export|extends|extension|external|factory|final|finally|for|get|hide|if|implements|import|in|interface|library|mixin|new|null|on|operator|part|rethrow|return|set|show|static|super|switch|sync|this|throw|try|typedef|var|void|while|with|yield)\b/
  5. ];
  6. // Handles named imports, such as http.Client
  7. var packagePrefix = /(^|[^\w.])(?:[a-z]\w*\s*\.\s*)*(?:[A-Z]\w*\s*\.\s*)*/.source;
  8. // based on the dart naming conventions
  9. var className = {
  10. pattern: RegExp(packagePrefix + /[A-Z](?:[\d_A-Z]*[a-z]\w*)?\b/.source),
  11. lookbehind: true,
  12. inside: {
  13. 'namespace': {
  14. pattern: /^[a-z]\w*(?:\s*\.\s*[a-z]\w*)*(?:\s*\.)?/,
  15. inside: {
  16. 'punctuation': /\./
  17. }
  18. },
  19. }
  20. };
  21. Prism.languages.dart = Prism.languages.extend('clike', {
  22. 'class-name': [
  23. className,
  24. {
  25. // variables and parameters
  26. // this to support class names (or generic parameters) which do not contain a lower case letter (also works for methods)
  27. pattern: RegExp(packagePrefix + /[A-Z]\w*(?=\s+\w+\s*[;,=()])/.source),
  28. lookbehind: true,
  29. inside: className.inside
  30. }
  31. ],
  32. 'keyword': keywords,
  33. 'operator': /\bis!|\b(?:as|is)\b|\+\+|--|&&|\|\||<<=?|>>=?|~(?:\/=?)?|[+\-*\/%&^|=!<>]=?|\?/
  34. });
  35. Prism.languages.insertBefore('dart', 'string', {
  36. 'string-literal': {
  37. pattern: /r?(?:("""|''')[\s\S]*?\1|(["'])(?:\\.|(?!\2)[^\\\r\n])*\2(?!\2))/,
  38. greedy: true,
  39. inside: {
  40. 'interpolation': {
  41. pattern: /((?:^|[^\\])(?:\\{2})*)\$(?:\w+|\{(?:[^{}]|\{[^{}]*\})*\})/,
  42. lookbehind: true,
  43. inside: {
  44. 'punctuation': /^\$\{?|\}$/,
  45. 'expression': {
  46. pattern: /[\s\S]+/,
  47. inside: Prism.languages.dart
  48. }
  49. }
  50. },
  51. 'string': /[\s\S]+/
  52. }
  53. },
  54. 'string': undefined
  55. });
  56. Prism.languages.insertBefore('dart', 'class-name', {
  57. 'metadata': {
  58. pattern: /@\w+/,
  59. alias: 'function'
  60. }
  61. });
  62. Prism.languages.insertBefore('dart', 'class-name', {
  63. 'generics': {
  64. pattern: /<(?:[\w\s,.&?]|<(?:[\w\s,.&?]|<(?:[\w\s,.&?]|<[\w\s,.&?]*>)*>)*>)*>/,
  65. inside: {
  66. 'class-name': className,
  67. 'keyword': keywords,
  68. 'punctuation': /[<>(),.:]/,
  69. 'operator': /[?&|]/
  70. }
  71. },
  72. });
  73. }(Prism));