json.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. Language: JSON
  3. Description: JSON (JavaScript Object Notation) is a lightweight data-interchange format.
  4. Author: Ivan Sagalaev <maniac@softwaremaniacs.org>
  5. Website: http://www.json.org
  6. Category: common, protocols
  7. */
  8. function json(hljs) {
  9. const LITERALS = {
  10. literal: 'true false null'
  11. };
  12. const ALLOWED_COMMENTS = [
  13. hljs.C_LINE_COMMENT_MODE,
  14. hljs.C_BLOCK_COMMENT_MODE
  15. ];
  16. const TYPES = [
  17. hljs.QUOTE_STRING_MODE,
  18. hljs.C_NUMBER_MODE
  19. ];
  20. const VALUE_CONTAINER = {
  21. end: ',',
  22. endsWithParent: true,
  23. excludeEnd: true,
  24. contains: TYPES,
  25. keywords: LITERALS
  26. };
  27. const OBJECT = {
  28. begin: /\{/,
  29. end: /\}/,
  30. contains: [
  31. {
  32. className: 'attr',
  33. begin: /"/,
  34. end: /"/,
  35. contains: [hljs.BACKSLASH_ESCAPE],
  36. illegal: '\\n'
  37. },
  38. hljs.inherit(VALUE_CONTAINER, {
  39. begin: /:/
  40. })
  41. ].concat(ALLOWED_COMMENTS),
  42. illegal: '\\S'
  43. };
  44. const ARRAY = {
  45. begin: '\\[',
  46. end: '\\]',
  47. contains: [hljs.inherit(VALUE_CONTAINER)], // inherit is a workaround for a bug that makes shared modes with endsWithParent compile only the ending of one of the parents
  48. illegal: '\\S'
  49. };
  50. TYPES.push(OBJECT, ARRAY);
  51. ALLOWED_COMMENTS.forEach(function(rule) {
  52. TYPES.push(rule);
  53. });
  54. return {
  55. name: 'JSON',
  56. contains: TYPES,
  57. keywords: LITERALS,
  58. illegal: '\\S'
  59. };
  60. }
  61. module.exports = json;