smithy.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """
  2. pygments.lexers.smithy
  3. ~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for the Smithy IDL.
  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, words
  9. from pygments.token import Text, Comment, Keyword, Name, String, \
  10. Number, Whitespace, Punctuation
  11. __all__ = ['SmithyLexer']
  12. class SmithyLexer(RegexLexer):
  13. """
  14. For Smithy IDL
  15. .. versionadded:: 2.10
  16. """
  17. name = 'Smithy'
  18. url = 'https://awslabs.github.io/smithy/'
  19. filenames = ['*.smithy']
  20. aliases = ['smithy']
  21. unquoted = r'[A-Za-z0-9_\.#$-]+'
  22. identifier = r"[A-Za-z0-9_\.#$-]+"
  23. simple_shapes = (
  24. 'use', 'byte', 'short', 'integer', 'long', 'float', 'document',
  25. 'double', 'bigInteger', 'bigDecimal', 'boolean', 'blob', 'string',
  26. 'timestamp',
  27. )
  28. aggregate_shapes = (
  29. 'apply', 'list', 'map', 'set', 'structure', 'union', 'resource',
  30. 'operation', 'service', 'trait'
  31. )
  32. tokens = {
  33. 'root': [
  34. (r'///.*$', Comment.Multiline),
  35. (r'//.*$', Comment),
  36. (r'@[0-9a-zA-Z\.#-]*', Name.Decorator),
  37. (r'(=)', Name.Decorator),
  38. (r'^(\$version)(:)(.+)',
  39. bygroups(Keyword.Declaration, Name.Decorator, Name.Class)),
  40. (r'^(namespace)(\s+' + identifier + r')\b',
  41. bygroups(Keyword.Declaration, Name.Class)),
  42. (words(simple_shapes,
  43. prefix=r'^', suffix=r'(\s+' + identifier + r')\b'),
  44. bygroups(Keyword.Declaration, Name.Class)),
  45. (words(aggregate_shapes,
  46. prefix=r'^', suffix=r'(\s+' + identifier + r')'),
  47. bygroups(Keyword.Declaration, Name.Class)),
  48. (r'^(metadata)(\s+)((?:\S+)|(?:\"[^"]+\"))(\s*)(=)',
  49. bygroups(Keyword.Declaration, Whitespace, Name.Class,
  50. Whitespace, Name.Decorator)),
  51. (r"(true|false|null)", Keyword.Constant),
  52. (r"(-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?)", Number),
  53. (identifier + ":", Name.Label),
  54. (identifier, Name.Variable.Class),
  55. (r'\[', Text, "#push"),
  56. (r'\]', Text, "#pop"),
  57. (r'\(', Text, "#push"),
  58. (r'\)', Text, "#pop"),
  59. (r'\{', Text, "#push"),
  60. (r'\}', Text, "#pop"),
  61. (r'"{3}(\\\\|\n|\\")*"{3}', String.Doc),
  62. (r'"(\\\\|\n|\\"|[^"])*"', String.Double),
  63. (r"'(\\\\|\n|\\'|[^'])*'", String.Single),
  64. (r'[:,]+', Punctuation),
  65. (r'\s+', Whitespace),
  66. ]
  67. }