zig.py 3.8 KB

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