json5.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """
  2. pygments.lexers.json5
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for Json5 file format.
  5. :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import include, RegexLexer, words
  9. from pygments.token import Comment, Keyword, Name, Number, Punctuation, \
  10. String, Whitespace
  11. __all__ = ['Json5Lexer']
  12. def string_rules(quote_mark):
  13. return [
  14. (rf"[^{quote_mark}\\]+", String),
  15. (r"\\.", String.Escape),
  16. (r"\\", Punctuation),
  17. (quote_mark, String, '#pop'),
  18. ]
  19. def quoted_field_name(quote_mark):
  20. return [
  21. (rf'([^{quote_mark}\\]|\\.)*{quote_mark}',
  22. Name.Variable, ('#pop', 'object_value'))
  23. ]
  24. class Json5Lexer(RegexLexer):
  25. """Lexer for JSON5 data structures."""
  26. name = 'JSON5'
  27. aliases = ['json5']
  28. filenames = ['*.json5']
  29. url = "https://json5.org"
  30. version_added = '2.19'
  31. tokens = {
  32. '_comments': [
  33. (r'(//|#).*\n', Comment.Single),
  34. (r'/\*\*([^/]|/(?!\*))*\*/', String.Doc),
  35. (r'/\*([^/]|/(?!\*))*\*/', Comment),
  36. ],
  37. 'root': [
  38. include('_comments'),
  39. (r"'", String, 'singlestring'),
  40. (r'"', String, 'doublestring'),
  41. (r'[+-]?0[xX][0-9a-fA-F]+', Number.Hex),
  42. (r'[+-.]?[0-9]+[.]?[0-9]?([eE][-]?[0-9]+)?', Number.Float),
  43. (r'\{', Punctuation, 'object'),
  44. (r'\[', Punctuation, 'array'),
  45. (words(['false', 'Infinity', '+Infinity', '-Infinity', 'NaN',
  46. 'null', 'true',], suffix=r'\b'), Keyword),
  47. (r'\s+', Whitespace),
  48. (r':', Punctuation),
  49. ],
  50. 'singlestring': string_rules("'"),
  51. 'doublestring': string_rules('"'),
  52. 'array': [
  53. (r',', Punctuation),
  54. (r'\]', Punctuation, '#pop'),
  55. include('root'),
  56. ],
  57. 'object': [
  58. (r'\s+', Whitespace),
  59. (r'\}', Punctuation, '#pop'),
  60. (r'\b([^:]+)', Name.Variable, 'object_value'),
  61. (r'"', Name.Variable, 'double_field_name'),
  62. (r"'", Name.Variable, 'single_field_name'),
  63. include('_comments'),
  64. ],
  65. 'double_field_name': quoted_field_name('"'),
  66. 'single_field_name': quoted_field_name("'"),
  67. 'object_value': [
  68. (r',', Punctuation, '#pop'),
  69. (r'\}', Punctuation, '#pop:2'),
  70. include('root'),
  71. ],
  72. }