zig.py 4.0 KB

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