zig.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """
  2. pygments.lexers.zig
  3. ~~~~~~~~~~~~~~~~~~~
  4. Lexers for Zig.
  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
  9. from pygments.token import Comment, Operator, Keyword, Name, String, \
  10. Number, Punctuation, Whitespace
  11. __all__ = ['ZigLexer']
  12. class ZigLexer(RegexLexer):
  13. """
  14. Lexer for the Zig language.
  15. grammar: https://ziglang.org/documentation/master/#Grammar
  16. """
  17. name = 'Zig'
  18. url = 'http://www.ziglang.org'
  19. aliases = ['zig']
  20. filenames = ['*.zig']
  21. mimetypes = ['text/zig']
  22. version_added = ''
  23. type_keywords = (
  24. words(('bool', 'f16', 'f32', 'f64', 'f128', 'void', 'noreturn', 'type',
  25. 'anyerror', 'promise', 'i0', 'u0', 'isize', 'usize', 'comptime_int',
  26. 'comptime_float', 'c_short', 'c_ushort', 'c_int', 'c_uint', 'c_long',
  27. 'c_ulong', 'c_longlong', 'c_ulonglong', 'c_longdouble', 'c_void'
  28. 'i8', 'u8', 'i16', 'u16', 'i32', 'u32', 'i64', 'u64', 'i128',
  29. 'u128'), suffix=r'\b'),
  30. Keyword.Type)
  31. storage_keywords = (
  32. words(('const', 'var', 'extern', 'packed', 'export', 'pub', 'noalias',
  33. 'inline', 'comptime', 'nakedcc', 'stdcallcc', 'volatile', 'allowzero',
  34. 'align', 'linksection', 'threadlocal'), suffix=r'\b'),
  35. Keyword.Reserved)
  36. structure_keywords = (
  37. words(('struct', 'enum', 'union', 'error'), suffix=r'\b'),
  38. Keyword)
  39. statement_keywords = (
  40. words(('break', 'return', 'continue', 'asm', 'defer', 'errdefer',
  41. 'unreachable', 'try', 'catch', 'async', 'await', 'suspend',
  42. 'resume', 'cancel'), suffix=r'\b'),
  43. Keyword)
  44. conditional_keywords = (
  45. words(('if', 'else', 'switch', 'and', 'or', 'orelse'), suffix=r'\b'),
  46. Keyword)
  47. repeat_keywords = (
  48. words(('while', 'for'), suffix=r'\b'),
  49. Keyword)
  50. other_keywords = (
  51. words(('fn', 'usingnamespace', 'test'), suffix=r'\b'),
  52. Keyword)
  53. constant_keywords = (
  54. words(('true', 'false', 'null', 'undefined'), suffix=r'\b'),
  55. Keyword.Constant)
  56. tokens = {
  57. 'root': [
  58. (r'\n', Whitespace),
  59. (r'\s+', Whitespace),
  60. (r'//.*?\n', Comment.Single),
  61. # Keywords
  62. statement_keywords,
  63. storage_keywords,
  64. structure_keywords,
  65. repeat_keywords,
  66. type_keywords,
  67. constant_keywords,
  68. conditional_keywords,
  69. other_keywords,
  70. # Floats
  71. (r'0x[0-9a-fA-F]+\.[0-9a-fA-F]+([pP][\-+]?[0-9a-fA-F]+)?', Number.Float),
  72. (r'0x[0-9a-fA-F]+\.?[pP][\-+]?[0-9a-fA-F]+', Number.Float),
  73. (r'[0-9]+\.[0-9]+([eE][-+]?[0-9]+)?', Number.Float),
  74. (r'[0-9]+\.?[eE][-+]?[0-9]+', Number.Float),
  75. # Integers
  76. (r'0b[01]+', Number.Bin),
  77. (r'0o[0-7]+', Number.Oct),
  78. (r'0x[0-9a-fA-F]+', Number.Hex),
  79. (r'[0-9]+', Number.Integer),
  80. # Identifier
  81. (r'@[a-zA-Z_]\w*', Name.Builtin),
  82. (r'[a-zA-Z_]\w*', Name),
  83. # Characters
  84. (r'\'\\\'\'', String.Escape),
  85. (r'\'\\(x[a-fA-F0-9]{2}|u[a-fA-F0-9]{4}|U[a-fA-F0-9]{6}|[nr\\t\'"])\'',
  86. String.Escape),
  87. (r'\'[^\\\']\'', String),
  88. # Strings
  89. (r'\\\\[^\n]*', String.Heredoc),
  90. (r'c\\\\[^\n]*', String.Heredoc),
  91. (r'c?"', String, 'string'),
  92. # Operators, Punctuation
  93. (r'[+%=><|^!?/\-*&~:]', Operator),
  94. (r'[{}()\[\],.;]', Punctuation)
  95. ],
  96. 'string': [
  97. (r'\\(x[a-fA-F0-9]{2}|u[a-fA-F0-9]{4}|U[a-fA-F0-9]{6}|[nr\\t\'"])',
  98. String.Escape),
  99. (r'[^\\"\n]+', String),
  100. (r'"', String, '#pop')
  101. ]
  102. }