capnproto.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.capnproto
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~
  5. Lexers for the Cap'n Proto schema language.
  6. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. import re
  10. from pygments.lexer import RegexLexer, default
  11. from pygments.token import Text, Comment, Keyword, Name, Literal
  12. __all__ = ['CapnProtoLexer']
  13. class CapnProtoLexer(RegexLexer):
  14. """
  15. For `Cap'n Proto <https://capnproto.org>`_ source.
  16. .. versionadded:: 2.2
  17. """
  18. name = 'Cap\'n Proto'
  19. filenames = ['*.capnp']
  20. aliases = ['capnp']
  21. flags = re.MULTILINE | re.UNICODE
  22. tokens = {
  23. 'root': [
  24. (r'#.*?$', Comment.Single),
  25. (r'@[0-9a-zA-Z]*', Name.Decorator),
  26. (r'=', Literal, 'expression'),
  27. (r':', Name.Class, 'type'),
  28. (r'\$', Name.Attribute, 'annotation'),
  29. (r'(struct|enum|interface|union|import|using|const|annotation|'
  30. r'extends|in|of|on|as|with|from|fixed)\b',
  31. Keyword),
  32. (r'[\w.]+', Name),
  33. (r'[^#@=:$\w]+', Text),
  34. ],
  35. 'type': [
  36. (r'[^][=;,(){}$]+', Name.Class),
  37. (r'[\[(]', Name.Class, 'parentype'),
  38. default('#pop'),
  39. ],
  40. 'parentype': [
  41. (r'[^][;()]+', Name.Class),
  42. (r'[\[(]', Name.Class, '#push'),
  43. (r'[])]', Name.Class, '#pop'),
  44. default('#pop'),
  45. ],
  46. 'expression': [
  47. (r'[^][;,(){}$]+', Literal),
  48. (r'[\[(]', Literal, 'parenexp'),
  49. default('#pop'),
  50. ],
  51. 'parenexp': [
  52. (r'[^][;()]+', Literal),
  53. (r'[\[(]', Literal, '#push'),
  54. (r'[])]', Literal, '#pop'),
  55. default('#pop'),
  56. ],
  57. 'annotation': [
  58. (r'[^][;,(){}=:]+', Name.Attribute),
  59. (r'[\[(]', Name.Attribute, 'annexp'),
  60. default('#pop'),
  61. ],
  62. 'annexp': [
  63. (r'[^][;()]+', Name.Attribute),
  64. (r'[\[(]', Name.Attribute, '#push'),
  65. (r'[])]', Name.Attribute, '#pop'),
  66. default('#pop'),
  67. ],
  68. }