json.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. export default function(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. }