ThirdParty/Pygments/pygments/lexers/rust.py

changeset 4172
4f20dba37ab6
child 4697
c2e9bf425554
equal deleted inserted replaced
4170:8bc578136279 4172:4f20dba37ab6
1 # -*- coding: utf-8 -*-
2 """
3 pygments.lexers.rust
4 ~~~~~~~~~~~~~~~~~~~~
5
6 Lexers for the Rust language.
7
8 :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details.
10 """
11
12 from pygments.lexer import RegexLexer, include, bygroups, words, default
13 from pygments.token import Comment, Operator, Keyword, Name, String, \
14 Number, Punctuation, Whitespace
15
16 __all__ = ['RustLexer']
17
18
19 class RustLexer(RegexLexer):
20 """
21 Lexer for the Rust programming language (version 0.9).
22
23 .. versionadded:: 1.6
24 """
25 name = 'Rust'
26 filenames = ['*.rs']
27 aliases = ['rust']
28 mimetypes = ['text/x-rustsrc']
29
30 tokens = {
31 'root': [
32 # Whitespace and Comments
33 (r'\n', Whitespace),
34 (r'\s+', Whitespace),
35 (r'//[/!](.*?)\n', Comment.Doc),
36 (r'//(.*?)\n', Comment.Single),
37 (r'/\*', Comment.Multiline, 'comment'),
38
39 # Lifetime
40 (r"""'[a-zA-Z_]\w*""", Name.Label),
41 # Macro parameters
42 (r"""\$([a-zA-Z_]\w*|\(,?|\),?|,?)""", Comment.Preproc),
43 # Keywords
44 (words((
45 'as', 'box', 'break', 'continue', 'do', 'else', 'enum', 'extern',
46 'fn', 'for', 'if', 'impl', 'in', 'loop', 'match', 'mut', 'priv',
47 'proc', 'pub', 'ref', 'return', 'static', '\'static', 'struct',
48 'trait', 'true', 'type', 'unsafe', 'while'), suffix=r'\b'),
49 Keyword),
50 (words(('alignof', 'be', 'const', 'offsetof', 'pure', 'sizeof',
51 'typeof', 'once', 'unsized', 'yield'), suffix=r'\b'),
52 Keyword.Reserved),
53 (r'(mod|use)\b', Keyword.Namespace),
54 (r'(true|false)\b', Keyword.Constant),
55 (r'let\b', Keyword.Declaration),
56 (words(('u8', 'u16', 'u32', 'u64', 'i8', 'i16', 'i32', 'i64', 'uint',
57 'int', 'f32', 'f64', 'str', 'bool'), suffix=r'\b'),
58 Keyword.Type),
59 (r'self\b', Name.Builtin.Pseudo),
60 # Prelude
61 (words((
62 'Freeze', 'Pod', 'Send', 'Sized', 'Add', 'Sub', 'Mul', 'Div', 'Rem', 'Neg', 'Not', 'BitAnd',
63 'BitOr', 'BitXor', 'Drop', 'Shl', 'Shr', 'Index', 'Option', 'Some', 'None', 'Result',
64 'Ok', 'Err', 'from_str', 'range', 'print', 'println', 'Any', 'AnyOwnExt', 'AnyRefExt',
65 'AnyMutRefExt', 'Ascii', 'AsciiCast', 'OnwedAsciiCast', 'AsciiStr',
66 'IntoBytes', 'Bool', 'ToCStr', 'Char', 'Clone', 'DeepClone', 'Eq', 'ApproxEq',
67 'Ord', 'TotalEq', 'Ordering', 'Less', 'Equal', 'Greater', 'Equiv', 'Container',
68 'Mutable', 'Map', 'MutableMap', 'Set', 'MutableSet', 'Default', 'FromStr',
69 'Hash', 'FromIterator', 'Extendable', 'Iterator', 'DoubleEndedIterator',
70 'RandomAccessIterator', 'CloneableIterator', 'OrdIterator',
71 'MutableDoubleEndedIterator', 'ExactSize', 'Times', 'Algebraic',
72 'Trigonometric', 'Exponential', 'Hyperbolic', 'Bitwise', 'BitCount',
73 'Bounded', 'Integer', 'Fractional', 'Real', 'RealExt', 'Num', 'NumCast',
74 'CheckedAdd', 'CheckedSub', 'CheckedMul', 'Orderable', 'Signed',
75 'Unsigned', 'Round', 'Primitive', 'Int', 'Float', 'ToStrRadix',
76 'ToPrimitive', 'FromPrimitive', 'GenericPath', 'Path', 'PosixPath',
77 'WindowsPath', 'RawPtr', 'Buffer', 'Writer', 'Reader', 'Seek',
78 'SendStr', 'SendStrOwned', 'SendStrStatic', 'IntoSendStr', 'Str',
79 'StrVector', 'StrSlice', 'OwnedStr', 'IterBytes', 'ToStr', 'IntoStr',
80 'CopyableTuple', 'ImmutableTuple', 'ImmutableEqVector', 'ImmutableTotalOrdVector',
81 'ImmutableCopyableVector', 'OwnedVector', 'OwnedCopyableVector',
82 'OwnedEqVector', 'MutableVector', 'MutableTotalOrdVector',
83 'Vector', 'VectorVector', 'CopyableVector', 'ImmutableVector',
84 'Port', 'Chan', 'SharedChan', 'spawn', 'drop'), suffix=r'\b'),
85 Name.Builtin),
86 (r'(ImmutableTuple\d+|Tuple\d+)\b', Name.Builtin),
87 # Borrowed pointer
88 (r'(&)(\'[A-Za-z_]\w*)?', bygroups(Operator, Name)),
89 # Labels
90 (r'\'[A-Za-z_]\w*:', Name.Label),
91 # Character Literal
92 (r"""'(\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}"""
93 r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|.)'""",
94 String.Char),
95 # Binary Literal
96 (r'0b[01_]+', Number.Bin, 'number_lit'),
97 # Octal Literal
98 (r'0o[0-7_]+', Number.Oct, 'number_lit'),
99 # Hexadecimal Literal
100 (r'0[xX][0-9a-fA-F_]+', Number.Hex, 'number_lit'),
101 # Decimal Literal
102 (r'[0-9][0-9_]*(\.[0-9_]+[eE][+\-]?[0-9_]+|'
103 r'\.[0-9_]*|[eE][+\-]?[0-9_]+)', Number.Float, 'number_lit'),
104 (r'[0-9][0-9_]*', Number.Integer, 'number_lit'),
105 # String Literal
106 (r'"', String, 'string'),
107 (r'r(#*)".*?"\1', String.Raw),
108
109 # Operators and Punctuation
110 (r'[{}()\[\],.;]', Punctuation),
111 (r'[+\-*/%&|<>^!~@=:?]', Operator),
112
113 # Identifier
114 (r'[a-zA-Z_]\w*', Name),
115
116 # Attributes
117 (r'#!?\[', Comment.Preproc, 'attribute['),
118 # Macros
119 (r'([A-Za-z_]\w*)(!)(\s*)([A-Za-z_]\w*)?(\s*)(\{)',
120 bygroups(Comment.Preproc, Punctuation, Whitespace, Name,
121 Whitespace, Punctuation), 'macro{'),
122 (r'([A-Za-z_]\w*)(!)(\s*)([A-Za-z_]\w*)?(\()',
123 bygroups(Comment.Preproc, Punctuation, Whitespace, Name,
124 Punctuation), 'macro('),
125 ],
126 'comment': [
127 (r'[^*/]+', Comment.Multiline),
128 (r'/\*', Comment.Multiline, '#push'),
129 (r'\*/', Comment.Multiline, '#pop'),
130 (r'[*/]', Comment.Multiline),
131 ],
132 'number_lit': [
133 (r'[ui](8|16|32|64)', Keyword, '#pop'),
134 (r'f(32|64)', Keyword, '#pop'),
135 default('#pop'),
136 ],
137 'string': [
138 (r'"', String, '#pop'),
139 (r"""\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}"""
140 r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}""", String.Escape),
141 (r'[^\\"]+', String),
142 (r'\\', String),
143 ],
144 'macro{': [
145 (r'\{', Operator, '#push'),
146 (r'\}', Operator, '#pop'),
147 ],
148 'macro(': [
149 (r'\(', Operator, '#push'),
150 (r'\)', Operator, '#pop'),
151 ],
152 'attribute_common': [
153 (r'"', String, 'string'),
154 (r'\[', Comment.Preproc, 'attribute['),
155 (r'\(', Comment.Preproc, 'attribute('),
156 ],
157 'attribute[': [
158 include('attribute_common'),
159 (r'\];?', Comment.Preproc, '#pop'),
160 (r'[^"\]]+', Comment.Preproc),
161 ],
162 'attribute(': [
163 include('attribute_common'),
164 (r'\);?', Comment.Preproc, '#pop'),
165 (r'[^")]+', Comment.Preproc),
166 ],
167 }

eric ide

mercurial