rust.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.rust
  4. ~~~~~~~~~~~~~~~~~~~~
  5. Lexers for the Rust language.
  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, include, bygroups, words, default
  10. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  11. Number, Punctuation, Whitespace
  12. __all__ = ['RustLexer']
  13. class RustLexer(RegexLexer):
  14. """
  15. Lexer for the Rust programming language (version 1.10).
  16. .. versionadded:: 1.6
  17. """
  18. name = 'Rust'
  19. filenames = ['*.rs', '*.rs.in']
  20. aliases = ['rust', 'rs']
  21. mimetypes = ['text/rust']
  22. keyword_types = (
  23. words(('u8', 'u16', 'u32', 'u64', 'i8', 'i16', 'i32', 'i64',
  24. 'i128', 'u128', 'usize', 'isize', 'f32', 'f64', 'str', 'bool'),
  25. suffix=r'\b'),
  26. Keyword.Type)
  27. builtin_types = (words((
  28. # Reexported core operators
  29. 'Copy', 'Send', 'Sized', 'Sync',
  30. 'Drop', 'Fn', 'FnMut', 'FnOnce',
  31. # Reexported types and traits
  32. 'Box',
  33. 'ToOwned',
  34. 'Clone',
  35. 'PartialEq', 'PartialOrd', 'Eq', 'Ord',
  36. 'AsRef', 'AsMut', 'Into', 'From',
  37. 'Default',
  38. 'Iterator', 'Extend', 'IntoIterator',
  39. 'DoubleEndedIterator', 'ExactSizeIterator',
  40. 'Option',
  41. 'Some', 'None',
  42. 'Result',
  43. 'Ok', 'Err',
  44. 'SliceConcatExt',
  45. 'String', 'ToString',
  46. 'Vec'), suffix=r'\b'),
  47. Name.Builtin)
  48. tokens = {
  49. 'root': [
  50. # rust allows a file to start with a shebang, but if the first line
  51. # starts with #![ then it's not a shebang but a crate attribute.
  52. (r'#![^[\r\n].*$', Comment.Preproc),
  53. default('base'),
  54. ],
  55. 'base': [
  56. # Whitespace and Comments
  57. (r'\n', Whitespace),
  58. (r'\s+', Whitespace),
  59. (r'//!.*?\n', String.Doc),
  60. (r'///(\n|[^/].*?\n)', String.Doc),
  61. (r'//(.*?)\n', Comment.Single),
  62. (r'/\*\*(\n|[^/*])', String.Doc, 'doccomment'),
  63. (r'/\*!', String.Doc, 'doccomment'),
  64. (r'/\*', Comment.Multiline, 'comment'),
  65. # Macro parameters
  66. (r"""\$([a-zA-Z_]\w*|\(,?|\),?|,?)""", Comment.Preproc),
  67. # Keywords
  68. (words((
  69. 'as', 'async', 'await', 'box', 'const', 'crate', 'else',
  70. 'extern', 'for', 'if', 'impl', 'in', 'loop', 'match', 'move',
  71. 'mut', 'pub', 'ref', 'return', 'static', 'super', 'trait',
  72. 'try', 'unsafe', 'use', 'where', 'while'), suffix=r'\b'),
  73. Keyword),
  74. (words(('abstract', 'alignof', 'become', 'do', 'final', 'macro',
  75. 'offsetof', 'override', 'priv', 'proc', 'pure', 'sizeof',
  76. 'typeof', 'unsized', 'virtual', 'yield'), suffix=r'\b'),
  77. Keyword.Reserved),
  78. (r'(true|false)\b', Keyword.Constant),
  79. (r'mod\b', Keyword, 'modname'),
  80. (r'let\b', Keyword.Declaration),
  81. (r'fn\b', Keyword, 'funcname'),
  82. (r'(struct|enum|type|union)\b', Keyword, 'typename'),
  83. (r'(default)(\s+)(type|fn)\b', bygroups(Keyword, Text, Keyword)),
  84. keyword_types,
  85. (r'self\b', Name.Builtin.Pseudo),
  86. # Prelude (taken from Rust's src/libstd/prelude.rs)
  87. builtin_types,
  88. # Path seperators, so types don't catch them.
  89. (r'::\b', Text),
  90. # Types in positions.
  91. (r'(?::|->)', Text, 'typename'),
  92. # Labels
  93. (r'(break|continue)(\s*)(\'[A-Za-z_]\w*)?',
  94. bygroups(Keyword, Text.Whitespace, Name.Label)),
  95. # Character Literal
  96. (r"""'(\\['"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0"""
  97. r"""|\\u\{[0-9a-fA-F]{1,6}\}|.)'""",
  98. String.Char),
  99. (r"""b'(\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\0"""
  100. r"""|\\u\{[0-9a-fA-F]{1,6}\}|.)'""",
  101. String.Char),
  102. # Binary Literal
  103. (r'0b[01_]+', Number.Bin, 'number_lit'),
  104. # Octal Literal
  105. (r'0o[0-7_]+', Number.Oct, 'number_lit'),
  106. # Hexadecimal Literal
  107. (r'0[xX][0-9a-fA-F_]+', Number.Hex, 'number_lit'),
  108. # Decimal Literal
  109. (r'[0-9][0-9_]*(\.[0-9_]+[eE][+\-]?[0-9_]+|'
  110. r'\.[0-9_]*(?!\.)|[eE][+\-]?[0-9_]+)', Number.Float,
  111. 'number_lit'),
  112. (r'[0-9][0-9_]*', Number.Integer, 'number_lit'),
  113. # String Literal
  114. (r'b"', String, 'bytestring'),
  115. (r'"', String, 'string'),
  116. (r'b?r(#*)".*?"\1', String),
  117. # Lifetime
  118. (r"""'static""", Name.Builtin),
  119. (r"""'[a-zA-Z_]\w*""", Name.Attribute),
  120. # Operators and Punctuation
  121. (r'[{}()\[\],.;]', Punctuation),
  122. (r'[+\-*/%&|<>^!~@=:?]', Operator),
  123. # Identifier
  124. (r'[a-zA-Z_]\w*', Name),
  125. # Attributes
  126. (r'#!?\[', Comment.Preproc, 'attribute['),
  127. # Macros
  128. (r'([A-Za-z_]\w*)(!)(\s*)([A-Za-z_]\w*)?(\s*)(\{)',
  129. bygroups(Comment.Preproc, Punctuation, Whitespace, Name,
  130. Whitespace, Punctuation), 'macro{'),
  131. (r'([A-Za-z_]\w*)(!)(\s*)([A-Za-z_]\w*)?(\()',
  132. bygroups(Comment.Preproc, Punctuation, Whitespace, Name,
  133. Punctuation), 'macro('),
  134. ],
  135. 'comment': [
  136. (r'[^*/]+', Comment.Multiline),
  137. (r'/\*', Comment.Multiline, '#push'),
  138. (r'\*/', Comment.Multiline, '#pop'),
  139. (r'[*/]', Comment.Multiline),
  140. ],
  141. 'doccomment': [
  142. (r'[^*/]+', String.Doc),
  143. (r'/\*', String.Doc, '#push'),
  144. (r'\*/', String.Doc, '#pop'),
  145. (r'[*/]', String.Doc),
  146. ],
  147. 'modname': [
  148. (r'\s+', Text),
  149. (r'[a-zA-Z_]\w*', Name.Namespace, '#pop'),
  150. default('#pop'),
  151. ],
  152. 'funcname': [
  153. (r'\s+', Text),
  154. (r'[a-zA-Z_]\w*', Name.Function, '#pop'),
  155. default('#pop'),
  156. ],
  157. 'typename': [
  158. (r'\s+', Text),
  159. (r'&', Keyword.Pseudo),
  160. builtin_types,
  161. keyword_types,
  162. (r'[a-zA-Z_]\w*', Name.Class, '#pop'),
  163. default('#pop'),
  164. ],
  165. 'number_lit': [
  166. (r'[ui](8|16|32|64|size)', Keyword, '#pop'),
  167. (r'f(32|64)', Keyword, '#pop'),
  168. default('#pop'),
  169. ],
  170. 'string': [
  171. (r'"', String, '#pop'),
  172. (r"""\\['"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0"""
  173. r"""|\\u\{[0-9a-fA-F]{1,6}\}""", String.Escape),
  174. (r'[^\\"]+', String),
  175. (r'\\', String),
  176. ],
  177. 'bytestring': [
  178. (r"""\\x[89a-fA-F][0-9a-fA-F]""", String.Escape),
  179. include('string'),
  180. ],
  181. 'macro{': [
  182. (r'\{', Operator, '#push'),
  183. (r'\}', Operator, '#pop'),
  184. ],
  185. 'macro(': [
  186. (r'\(', Operator, '#push'),
  187. (r'\)', Operator, '#pop'),
  188. ],
  189. 'attribute_common': [
  190. (r'"', String, 'string'),
  191. (r'\[', Comment.Preproc, 'attribute['),
  192. (r'\(', Comment.Preproc, 'attribute('),
  193. ],
  194. 'attribute[': [
  195. include('attribute_common'),
  196. (r'\];?', Comment.Preproc, '#pop'),
  197. (r'[^"\]]+', Comment.Preproc),
  198. ],
  199. 'attribute(': [
  200. include('attribute_common'),
  201. (r'\);?', Comment.Preproc, '#pop'),
  202. (r'[^")]+', Comment.Preproc),
  203. ],
  204. }