capnproto.py 2.1 KB

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