bare.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """
  2. pygments.lexers.bare
  3. ~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the BARE schema.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, words, bygroups
  9. from pygments.token import Text, Comment, Keyword, Name, Literal, Whitespace
  10. __all__ = ['BareLexer']
  11. class BareLexer(RegexLexer):
  12. """
  13. For BARE schema source.
  14. """
  15. name = 'BARE'
  16. url = 'https://baremessages.org'
  17. filenames = ['*.bare']
  18. aliases = ['bare']
  19. version_added = '2.7'
  20. keywords = [
  21. 'type',
  22. 'enum',
  23. 'u8',
  24. 'u16',
  25. 'u32',
  26. 'u64',
  27. 'uint',
  28. 'i8',
  29. 'i16',
  30. 'i32',
  31. 'i64',
  32. 'int',
  33. 'f32',
  34. 'f64',
  35. 'bool',
  36. 'void',
  37. 'data',
  38. 'string',
  39. 'optional',
  40. 'map',
  41. ]
  42. tokens = {
  43. 'root': [
  44. (r'(type)(\s+)([A-Z][a-zA-Z0-9]+)(\s+)(\{)',
  45. bygroups(Keyword, Whitespace, Name.Class, Whitespace, Text), 'struct'),
  46. (r'(type)(\s+)([A-Z][a-zA-Z0-9]+)(\s+)(\()',
  47. bygroups(Keyword, Whitespace, Name.Class, Whitespace, Text), 'union'),
  48. (r'(type)(\s+)([A-Z][a-zA-Z0-9]+)(\s+)',
  49. bygroups(Keyword, Whitespace, Name, Whitespace), 'typedef'),
  50. (r'(enum)(\s+)([A-Z][a-zA-Z0-9]+)(\s+\{)',
  51. bygroups(Keyword, Whitespace, Name.Class, Whitespace), 'enum'),
  52. (r'#.*?$', Comment),
  53. (r'\s+', Whitespace),
  54. ],
  55. 'struct': [
  56. (r'\{', Text, '#push'),
  57. (r'\}', Text, '#pop'),
  58. (r'([a-zA-Z0-9]+)(:)(\s*)',
  59. bygroups(Name.Attribute, Text, Whitespace), 'typedef'),
  60. (r'\s+', Whitespace),
  61. ],
  62. 'union': [
  63. (r'\)', Text, '#pop'),
  64. (r'(\s*)(\|)(\s*)', bygroups(Whitespace, Text, Whitespace)),
  65. (r'[A-Z][a-zA-Z0-9]+', Name.Class),
  66. (words(keywords), Keyword),
  67. (r'\s+', Whitespace),
  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+', Whitespace),
  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+)',
  88. bygroups(Name.Attribute, Text, Literal)),
  89. (r'([A-Z][A-Z0-9_]*)', bygroups(Name.Attribute)),
  90. (r'#.*?$', Comment),
  91. (r'\s+', Whitespace),
  92. ],
  93. }