Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/lexers/rust.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-01 06:54 +0000

1""" 

2 pygments.lexers.rust 

3 ~~~~~~~~~~~~~~~~~~~~ 

4 

5 Lexers for the Rust language. 

6 

7 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS. 

8 :license: BSD, see LICENSE for details. 

9""" 

10 

11from pygments.lexer import RegexLexer, include, bygroups, words, default 

12from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ 

13 Number, Punctuation, Whitespace 

14 

15__all__ = ['RustLexer'] 

16 

17 

18class RustLexer(RegexLexer): 

19 """ 

20 Lexer for the Rust programming language (version 1.47). 

21 

22 .. versionadded:: 1.6 

23 """ 

24 name = 'Rust' 

25 url = 'https://www.rust-lang.org/' 

26 filenames = ['*.rs', '*.rs.in'] 

27 aliases = ['rust', 'rs'] 

28 mimetypes = ['text/rust', 'text/x-rust'] 

29 

30 keyword_types = (words(( 

31 'u8', 'u16', 'u32', 'u64', 'u128', 'i8', 'i16', 'i32', 'i64', 'i128', 

32 'usize', 'isize', 'f32', 'f64', 'char', 'str', 'bool', 

33 ), suffix=r'\b'), Keyword.Type) 

34 

35 builtin_funcs_types = (words(( 

36 'Copy', 'Send', 'Sized', 'Sync', 'Unpin', 

37 'Drop', 'Fn', 'FnMut', 'FnOnce', 'drop', 

38 'Box', 'ToOwned', 'Clone', 

39 'PartialEq', 'PartialOrd', 'Eq', 'Ord', 

40 'AsRef', 'AsMut', 'Into', 'From', 'Default', 

41 'Iterator', 'Extend', 'IntoIterator', 'DoubleEndedIterator', 

42 'ExactSizeIterator', 

43 'Option', 'Some', 'None', 

44 'Result', 'Ok', 'Err', 

45 'String', 'ToString', 'Vec', 

46 ), suffix=r'\b'), Name.Builtin) 

47 

48 builtin_macros = (words(( 

49 'asm', 'assert', 'assert_eq', 'assert_ne', 'cfg', 'column', 

50 'compile_error', 'concat', 'concat_idents', 'dbg', 'debug_assert', 

51 'debug_assert_eq', 'debug_assert_ne', 'env', 'eprint', 'eprintln', 

52 'file', 'format', 'format_args', 'format_args_nl', 'global_asm', 

53 'include', 'include_bytes', 'include_str', 

54 'is_aarch64_feature_detected', 

55 'is_arm_feature_detected', 

56 'is_mips64_feature_detected', 

57 'is_mips_feature_detected', 

58 'is_powerpc64_feature_detected', 

59 'is_powerpc_feature_detected', 

60 'is_x86_feature_detected', 

61 'line', 'llvm_asm', 'log_syntax', 'macro_rules', 'matches', 

62 'module_path', 'option_env', 'panic', 'print', 'println', 'stringify', 

63 'thread_local', 'todo', 'trace_macros', 'unimplemented', 'unreachable', 

64 'vec', 'write', 'writeln', 

65 ), suffix=r'!'), Name.Function.Magic) 

66 

67 tokens = { 

68 'root': [ 

69 # rust allows a file to start with a shebang, but if the first line 

70 # starts with #![ then it's not a shebang but a crate attribute. 

71 (r'#![^[\r\n].*$', Comment.Preproc), 

72 default('base'), 

73 ], 

74 'base': [ 

75 # Whitespace and Comments 

76 (r'\n', Whitespace), 

77 (r'\s+', Whitespace), 

78 (r'//!.*?\n', String.Doc), 

79 (r'///(\n|[^/].*?\n)', String.Doc), 

80 (r'//(.*?)\n', Comment.Single), 

81 (r'/\*\*(\n|[^/*])', String.Doc, 'doccomment'), 

82 (r'/\*!', String.Doc, 'doccomment'), 

83 (r'/\*', Comment.Multiline, 'comment'), 

84 

85 # Macro parameters 

86 (r"""\$([a-zA-Z_]\w*|\(,?|\),?|,?)""", Comment.Preproc), 

87 # Keywords 

88 (words(('as', 'async', 'await', 'box', 'const', 'crate', 'dyn', 

89 'else', 'extern', 'for', 'if', 'impl', 'in', 'loop', 

90 'match', 'move', 'mut', 'pub', 'ref', 'return', 'static', 

91 'super', 'trait', 'unsafe', 'use', 'where', 'while'), 

92 suffix=r'\b'), Keyword), 

93 (words(('abstract', 'become', 'do', 'final', 'macro', 'override', 

94 'priv', 'typeof', 'try', 'unsized', 'virtual', 'yield'), 

95 suffix=r'\b'), Keyword.Reserved), 

96 (r'(true|false)\b', Keyword.Constant), 

97 (r'self\b', Name.Builtin.Pseudo), 

98 (r'mod\b', Keyword, 'modname'), 

99 (r'let\b', Keyword.Declaration), 

100 (r'fn\b', Keyword, 'funcname'), 

101 (r'(struct|enum|type|union)\b', Keyword, 'typename'), 

102 (r'(default)(\s+)(type|fn)\b', bygroups(Keyword, Text, Keyword)), 

103 keyword_types, 

104 (r'[sS]elf\b', Name.Builtin.Pseudo), 

105 # Prelude (taken from Rust's src/libstd/prelude.rs) 

106 builtin_funcs_types, 

107 builtin_macros, 

108 # Path separators, so types don't catch them. 

109 (r'::\b', Text), 

110 # Types in positions. 

111 (r'(?::|->)', Text, 'typename'), 

112 # Labels 

113 (r'(break|continue)(\b\s*)(\'[A-Za-z_]\w*)?', 

114 bygroups(Keyword, Text.Whitespace, Name.Label)), 

115 

116 # Character literals 

117 (r"""'(\\['"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0""" 

118 r"""|\\u\{[0-9a-fA-F]{1,6}\}|.)'""", 

119 String.Char), 

120 (r"""b'(\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\0""" 

121 r"""|\\u\{[0-9a-fA-F]{1,6}\}|.)'""", 

122 String.Char), 

123 

124 # Binary literals 

125 (r'0b[01_]+', Number.Bin, 'number_lit'), 

126 # Octal literals 

127 (r'0o[0-7_]+', Number.Oct, 'number_lit'), 

128 # Hexadecimal literals 

129 (r'0[xX][0-9a-fA-F_]+', Number.Hex, 'number_lit'), 

130 # Decimal literals 

131 (r'[0-9][0-9_]*(\.[0-9_]+[eE][+\-]?[0-9_]+|' 

132 r'\.[0-9_]*(?!\.)|[eE][+\-]?[0-9_]+)', Number.Float, 

133 'number_lit'), 

134 (r'[0-9][0-9_]*', Number.Integer, 'number_lit'), 

135 

136 # String literals 

137 (r'b"', String, 'bytestring'), 

138 (r'"', String, 'string'), 

139 (r'(?s)b?r(#*)".*?"\1', String), 

140 

141 # Lifetime names 

142 (r"'", Operator, 'lifetime'), 

143 

144 # Operators and Punctuation 

145 (r'\.\.=?', Operator), 

146 (r'[{}()\[\],.;]', Punctuation), 

147 (r'[+\-*/%&|<>^!~@=:?]', Operator), 

148 

149 # Identifiers 

150 (r'[a-zA-Z_]\w*', Name), 

151 # Raw identifiers 

152 (r'r#[a-zA-Z_]\w*', Name), 

153 

154 # Attributes 

155 (r'#!?\[', Comment.Preproc, 'attribute['), 

156 

157 # Misc 

158 # Lone hashes: not used in Rust syntax, but allowed in macro 

159 # arguments, most famously for quote::quote!() 

160 (r'#', Text), 

161 ], 

162 'comment': [ 

163 (r'[^*/]+', Comment.Multiline), 

164 (r'/\*', Comment.Multiline, '#push'), 

165 (r'\*/', Comment.Multiline, '#pop'), 

166 (r'[*/]', Comment.Multiline), 

167 ], 

168 'doccomment': [ 

169 (r'[^*/]+', String.Doc), 

170 (r'/\*', String.Doc, '#push'), 

171 (r'\*/', String.Doc, '#pop'), 

172 (r'[*/]', String.Doc), 

173 ], 

174 'modname': [ 

175 (r'\s+', Text), 

176 (r'[a-zA-Z_]\w*', Name.Namespace, '#pop'), 

177 default('#pop'), 

178 ], 

179 'funcname': [ 

180 (r'\s+', Text), 

181 (r'[a-zA-Z_]\w*', Name.Function, '#pop'), 

182 default('#pop'), 

183 ], 

184 'typename': [ 

185 (r'\s+', Text), 

186 (r'&', Keyword.Pseudo), 

187 (r"'", Operator, 'lifetime'), 

188 builtin_funcs_types, 

189 keyword_types, 

190 (r'[a-zA-Z_]\w*', Name.Class, '#pop'), 

191 default('#pop'), 

192 ], 

193 'lifetime': [ 

194 (r"(static|_)", Name.Builtin), 

195 (r"[a-zA-Z_]+\w*", Name.Attribute), 

196 default('#pop'), 

197 ], 

198 'number_lit': [ 

199 (r'[ui](8|16|32|64|size)', Keyword, '#pop'), 

200 (r'f(32|64)', Keyword, '#pop'), 

201 default('#pop'), 

202 ], 

203 'string': [ 

204 (r'"', String, '#pop'), 

205 (r"""\\['"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0""" 

206 r"""|\\u\{[0-9a-fA-F]{1,6}\}""", String.Escape), 

207 (r'[^\\"]+', String), 

208 (r'\\', String), 

209 ], 

210 'bytestring': [ 

211 (r"""\\x[89a-fA-F][0-9a-fA-F]""", String.Escape), 

212 include('string'), 

213 ], 

214 'attribute_common': [ 

215 (r'"', String, 'string'), 

216 (r'\[', Comment.Preproc, 'attribute['), 

217 ], 

218 'attribute[': [ 

219 include('attribute_common'), 

220 (r'\]', Comment.Preproc, '#pop'), 

221 (r'[^"\]\[]+', Comment.Preproc), 

222 ], 

223 }