bare.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """
  2. pygments.lexers.bare
  3. ~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the BARE schema.
  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, words, bygroups
  10. from pygments.token import Text, Comment, Keyword, Name, Literal
  11. __all__ = ['BareLexer']
  12. class BareLexer(RegexLexer):
  13. """
  14. For `BARE schema <https://baremessages.org>`_ schema source.
  15. .. versionadded:: 2.7
  16. """
  17. name = 'BARE'
  18. filenames = ['*.bare']
  19. aliases = ['bare']
  20. flags = re.MULTILINE | re.UNICODE
  21. keywords = [
  22. 'type',
  23. 'enum',
  24. 'u8',
  25. 'u16',
  26. 'u32',
  27. 'u64',
  28. 'uint',
  29. 'i8',
  30. 'i16',
  31. 'i32',
  32. 'i64',
  33. 'int',
  34. 'f32',
  35. 'f64',
  36. 'bool',
  37. 'void',
  38. 'data',
  39. 'string',
  40. 'optional',
  41. 'map',
  42. ]
  43. tokens = {
  44. 'root': [
  45. (r'(type)(\s+)([A-Z][a-zA-Z0-9]+)(\s+\{)',
  46. bygroups(Keyword, Text, Name.Class, Text), 'struct'),
  47. (r'(type)(\s+)([A-Z][a-zA-Z0-9]+)(\s+\()',
  48. bygroups(Keyword, Text, Name.Class, Text), 'union'),
  49. (r'(type)(\s+)([A-Z][a-zA-Z0-9]+)(\s+)',
  50. bygroups(Keyword, Text, Name, Text), 'typedef'),
  51. (r'(enum)(\s+)([A-Z][a-zA-Z0-9]+)(\s+\{)',
  52. bygroups(Keyword, Text, Name.Class, Text), 'enum'),
  53. (r'#.*?$', Comment),
  54. (r'\s+', Text),
  55. ],
  56. 'struct': [
  57. (r'\{', Text, '#push'),
  58. (r'\}', Text, '#pop'),
  59. (r'([a-zA-Z0-9]+)(:\s*)', bygroups(Name.Attribute, Text), 'typedef'),
  60. (r'\s+', Text),
  61. ],
  62. 'union': [
  63. (r'\)', Text, '#pop'),
  64. (r'\s*\|\s*', Text),
  65. (r'[A-Z][a-zA-Z0-9]+', Name.Class),
  66. (words(keywords), Keyword),
  67. (r'\s+', Text),
  68. ],
  69. 'typedef': [
  70. (r'\[\]', Text),
  71. (r'#.*?$', Comment, '#pop'),
  72. (r'(\[)(\d+)(\])', bygroups(Text, Literal, Text)),
  73. (r'<|>', Text),
  74. (r'\(', Text, 'union'),
  75. (r'(\[)([a-z][a-z-A-Z0-9]+)(\])', bygroups(Text, Keyword, Text)),
  76. (r'(\[)([A-Z][a-z-A-Z0-9]+)(\])', bygroups(Text, Name.Class, Text)),
  77. (r'([A-Z][a-z-A-Z0-9]+)', Name.Class),
  78. (words(keywords), Keyword),
  79. (r'\n', Text, '#pop'),
  80. (r'\{', Text, 'struct'),
  81. (r'\s+', Text),
  82. (r'\d+', Literal),
  83. ],
  84. 'enum': [
  85. (r'\{', Text, '#push'),
  86. (r'\}', Text, '#pop'),
  87. (r'([A-Z][A-Z0-9_]*)(\s*=\s*)(\d+)', bygroups(Name.Attribute, Text, Literal)),
  88. (r'([A-Z][A-Z0-9_]*)', bygroups(Name.Attribute)),
  89. (r'#.*?$', Comment),
  90. (r'\s+', Text),
  91. ],
  92. }