jmespath.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """
  2. pygments.lexers.jmespath
  3. ~~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for the JMESPath language
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, bygroups, include
  9. from pygments.token import String, Punctuation, Whitespace, Name, Operator, \
  10. Number, Literal, Keyword
  11. __all__ = ['JMESPathLexer']
  12. class JMESPathLexer(RegexLexer):
  13. """
  14. For JMESPath queries.
  15. """
  16. name = 'JMESPath'
  17. url = 'https://jmespath.org'
  18. filenames = ['*.jp']
  19. aliases = ['jmespath', 'jp']
  20. version_added = ''
  21. tokens = {
  22. 'string': [
  23. (r"'(\\(.|\n)|[^'\\])*'", String),
  24. ],
  25. 'punctuation': [
  26. (r'(\[\?|[\.\*\[\],:\(\)\{\}\|])', Punctuation),
  27. ],
  28. 'ws': [
  29. (r" |\t|\n|\r", Whitespace)
  30. ],
  31. "dq-identifier": [
  32. (r'[^\\"]+', Name.Variable),
  33. (r'\\"', Name.Variable),
  34. (r'.', Punctuation, '#pop'),
  35. ],
  36. 'identifier': [
  37. (r'(&)?(")', bygroups(Name.Variable, Punctuation), 'dq-identifier'),
  38. (r'(")?(&?[A-Za-z][A-Za-z0-9_-]*)(")?', bygroups(Punctuation, Name.Variable, Punctuation)),
  39. ],
  40. 'root': [
  41. include('ws'),
  42. include('string'),
  43. (r'(==|!=|<=|>=|<|>|&&|\|\||!)', Operator),
  44. include('punctuation'),
  45. (r'@', Name.Variable.Global),
  46. (r'(&?[A-Za-z][A-Za-z0-9_]*)(\()', bygroups(Name.Function, Punctuation)),
  47. (r'(&)(\()', bygroups(Name.Variable, Punctuation)),
  48. include('identifier'),
  49. (r'-?\d+', Number),
  50. (r'`', Literal, 'literal'),
  51. ],
  52. 'literal': [
  53. include('ws'),
  54. include('string'),
  55. include('punctuation'),
  56. (r'(false|true|null)\b', Keyword.Constant),
  57. include('identifier'),
  58. (r'-?\d+\.?\d*([eE][-+]\d+)?', Number),
  59. (r'\\`', Literal),
  60. (r'`', Literal, '#pop'),
  61. ]
  62. }