json.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. var LITERALS = {literal: 'true false null'};
  10. var ALLOWED_COMMENTS = [
  11. hljs.C_LINE_COMMENT_MODE,
  12. hljs.C_BLOCK_COMMENT_MODE
  13. ];
  14. var TYPES = [
  15. hljs.QUOTE_STRING_MODE,
  16. hljs.C_NUMBER_MODE
  17. ];
  18. var VALUE_CONTAINER = {
  19. end: ',', endsWithParent: true, excludeEnd: true,
  20. contains: TYPES,
  21. keywords: LITERALS
  22. };
  23. var OBJECT = {
  24. begin: '{', end: '}',
  25. contains: [
  26. {
  27. className: 'attr',
  28. begin: /"/, end: /"/,
  29. contains: [hljs.BACKSLASH_ESCAPE],
  30. illegal: '\\n',
  31. },
  32. hljs.inherit(VALUE_CONTAINER, {begin: /:/})
  33. ].concat(ALLOWED_COMMENTS),
  34. illegal: '\\S'
  35. };
  36. var ARRAY = {
  37. begin: '\\[', end: '\\]',
  38. 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
  39. illegal: '\\S'
  40. };
  41. TYPES.push(OBJECT, ARRAY);
  42. ALLOWED_COMMENTS.forEach(function(rule) {
  43. TYPES.push(rule);
  44. });
  45. return {
  46. name: 'JSON',
  47. contains: TYPES,
  48. keywords: LITERALS,
  49. illegal: '\\S'
  50. };
  51. }
  52. module.exports = json;