jmespath.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """
  2. pygments.lexers.jmespath
  3. ~~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for the JMESPath language
  5. :copyright: Copyright 2006-2023 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. tokens = {
  21. 'string': [
  22. (r"'(\\(.|\n)|[^'\\])*'", String),
  23. ],
  24. 'punctuation': [
  25. (r'(\[\?|[\.\*\[\],:\(\)\{\}\|])', Punctuation),
  26. ],
  27. 'ws': [
  28. (r" |\t|\n|\r", Whitespace)
  29. ],
  30. "dq-identifier": [
  31. (r'[^\\"]+', Name.Variable),
  32. (r'\\"', Name.Variable),
  33. (r'.', Punctuation, '#pop'),
  34. ],
  35. 'identifier': [
  36. (r'(&)?(")', bygroups(Name.Variable, Punctuation), 'dq-identifier'),
  37. (r'(")?(&?[A-Za-z][A-Za-z0-9_-]*)(")?', bygroups(Punctuation, Name.Variable, Punctuation)),
  38. ],
  39. 'root': [
  40. include('ws'),
  41. include('string'),
  42. (r'(==|!=|<=|>=|<|>|&&|\|\||!)', Operator),
  43. include('punctuation'),
  44. (r'@', Name.Variable.Global),
  45. (r'(&?[A-Za-z][A-Za-z0-9_]*)(\()', bygroups(Name.Function, Punctuation)),
  46. (r'(&)(\()', bygroups(Name.Variable, Punctuation)),
  47. include('identifier'),
  48. (r'-?\d+', Number),
  49. (r'`', Literal, 'literal'),
  50. ],
  51. 'literal': [
  52. include('ws'),
  53. include('string'),
  54. include('punctuation'),
  55. (r'(false|true|null)\b', Keyword.Constant),
  56. include('identifier'),
  57. (r'-?\d+\.?\d*([eE][-+]\d+)?', Number),
  58. (r'\\`', Literal),
  59. (r'`', Literal, '#pop'),
  60. ]
  61. }