json.js 1.5 KB

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