Debian Codegraph @

doc/docs/tokens.rst

raw
1 .. -*- mode: rst -*-
2
3 ==============
4 Builtin Tokens
5 ==============
6
7 .. module:: pygments.token
8
9 In the :mod:`pygments.token` module, there is a special object called `Token`
10 that is used to create token types.
11
12 You can create a new token type by accessing an attribute of `Token` whose
13 name starts with an uppercase letter:
14
15 .. sourcecode:: pycon
16
17 >>> from pygments.token import Token
18 >>> Token.String
19 Token.String
20 >>> Token.String is Token.String
21 True
22
23 Note that tokens are singletons so you can use the ``is`` operator for comparing
24 token types.
25
26 You can also use the ``in`` operator to perform set tests:
27
28 .. sourcecode:: pycon
29
30 >>> from pygments.token import Comment
31 >>> Comment.Single in Comment
32 True
33 >>> Comment in Comment.Multi
34 False
35
36 This can be useful in :doc:`filters <filters>` and if you write lexers on your
37 own without using the base lexers.
38
39 You can also split a token type into a hierarchy, and get the parent of it:
40
41 .. sourcecode:: pycon
42
43 >>> String.split()
44 [Token, Token.Literal, Token.Literal.String]
45 >>> String.parent
46 Token.Literal
47
48 In principle, you can create an unlimited number of token types but nobody can
49 guarantee that a style would define style rules for a token type. Because of
50 that, Pygments proposes some global token types defined in the
51 `pygments.token.STANDARD_TYPES` dict.
52
53 For some tokens aliases are already defined:
54
55 .. sourcecode:: pycon
56
57 >>> from pygments.token import String
58 >>> String
59 Token.Literal.String
60
61 Inside the :mod:`pygments.token` module the following aliases are defined:
62
63 ============= ============================ ====================================
64 `Text` `Token.Text` for any type of text data
65 `Whitespace` `Token.Text.Whitespace` for whitespace
66 `Error` `Token.Error` represents lexer errors
67 `Other` `Token.Other` special token for data not
68 matched by a parser (e.g. HTML
69 markup in PHP code)
70 `Keyword` `Token.Keyword` any kind of keywords
71 `Name` `Token.Name` variable/function names
72 `Literal` `Token.Literal` Any literals
73 `String` `Token.Literal.String` string literals
74 `Number` `Token.Literal.Number` number literals
75 `Operator` `Token.Operator` operators (``+``, ``not``...)
76 `Punctuation` `Token.Punctuation` punctuation (``[``, ``(``...)
77 `Comment` `Token.Comment` any kind of comments
78 `Generic` `Token.Generic` generic tokens (have a look at
79 the explanation below)
80 ============= ============================ ====================================
81
82 Normally you just create token types using the already defined aliases. For each
83 of those token aliases, a number of subtypes exists (excluding the special tokens
84 `Token.Text`, `Token.Error` and `Token.Other`)
85
86 It's also possible to convert strings to token types (for example
87 if you want to supply a token from the command line):
88
89 .. sourcecode:: pycon
90
91 >>> from pygments.token import String, string_to_tokentype
92 >>> string_to_tokentype("String")
93 Token.Literal.String
94 >>> string_to_tokentype("Token.Literal.String")
95 Token.Literal.String
96 >>> string_to_tokentype(String)
97 Token.Literal.String
98
99
100 Keyword Tokens
101 ==============
102
103 `Keyword`
104 For any kind of keyword (especially if it doesn't match any of the
105 subtypes of course).
106
107 `Keyword.Constant`
108 For keywords that are constants (e.g. ``None`` in future Python versions).
109
110 `Keyword.Declaration`
111 For keywords used for variable declaration (e.g. ``var`` in some programming
112 languages like JavaScript).
113
114 `Keyword.Namespace`
115 For keywords used for namespace declarations (e.g. ``import`` in Python and
116 Java and ``package`` in Java).
117
118 `Keyword.Pseudo`
119 For keywords that aren't really keywords (e.g. ``None`` in old Python
120 versions).
121
122 `Keyword.Reserved`
123 For reserved keywords.
124
125 `Keyword.Type`
126 For builtin types that can't be used as identifiers (e.g. ``int``,
127 ``char`` etc. in C).
128
129
130 Name Tokens
131 ===========
132
133 `Name`
134 For any name (variable names, function names, classes).
135
136 `Name.Attribute`
137 For all attributes (e.g. in HTML tags).
138
139 `Name.Builtin`
140 Builtin names; names that are available in the global namespace.
141
142 `Name.Builtin.Pseudo`
143 Builtin names that are implicit (e.g. ``self`` in Ruby, ``this`` in Java).
144
145 `Name.Class`
146 Class names. Because no lexer can know if a name is a class or a function
147 or something else this token is meant for class declarations.
148
149 `Name.Constant`
150 Token type for constants. In some languages you can recognise a token by the
151 way it's defined (the value after a ``const`` keyword for example). In
152 other languages constants are uppercase by definition (Ruby).
153
154 `Name.Decorator`
155 Token type for decorators. Decorators are syntactic elements in the Python
156 language. Similar syntax elements exist in C# and Java.
157
158 `Name.Entity`
159 Token type for special entities. (e.g. ``&nbsp;`` in HTML).
160
161 `Name.Exception`
162 Token type for exception names (e.g. ``RuntimeError`` in Python). Some languages
163 define exceptions in the function signature (Java). You can highlight
164 the name of that exception using this token then.
165
166 `Name.Function`
167 Token type for function names.
168
169 `Name.Function.Magic`
170 same as `Name.Function` but for special function names that have an implicit use
171 in a language (e.g. ``__init__`` method in Python).
172
173 `Name.Label`
174 Token type for label names (e.g. in languages that support ``goto``).
175
176 `Name.Namespace`
177 Token type for namespaces. (e.g. import paths in Java/Python), names following
178 the ``module``/``namespace`` keyword in other languages.
179
180 `Name.Other`
181 Other names. Normally unused.
182
183 `Name.Property`
184 Additional token type occasionally used for class attributes.
185
186 `Name.Tag`
187 Tag names (in HTML/XML markup or configuration files).
188
189 `Name.Variable`
190 Token type for variables. Some languages have prefixes for variable names
191 (PHP, Ruby, Perl). You can highlight them using this token.
192
193 `Name.Variable.Class`
194 same as `Name.Variable` but for class variables (also static variables).
195
196 `Name.Variable.Global`
197 same as `Name.Variable` but for global variables (used in Ruby, for
198 example).
199
200 `Name.Variable.Instance`
201 same as `Name.Variable` but for instance variables.
202
203 `Name.Variable.Magic`
204 same as `Name.Variable` but for special variable names that have an implicit use
205 in a language (e.g. ``__doc__`` in Python).
206
207
208 Literals
209 ========
210
211 `Literal`
212 For any literal (if not further defined).
213
214 `Literal.Date`
215 for date literals (e.g. ``42d`` in Boo).
216
217
218 `String`
219 For any string literal.
220
221 `String.Affix`
222 Token type for affixes that further specify the type of the string they're
223 attached to (e.g. the prefixes ``r`` and ``u8`` in ``r"foo"`` and ``u8"foo"``).
224
225 `String.Backtick`
226 Token type for strings enclosed in backticks.
227
228 `String.Char`
229 Token type for single characters (e.g. Java, C).
230
231 `String.Delimiter`
232 Token type for delimiting identifiers in "heredoc", raw and other similar
233 strings (e.g. the word ``END`` in Perl code ``print <<'END';``).
234
235 `String.Doc`
236 Token type for documentation strings (for example Python).
237
238 `String.Double`
239 Double quoted strings.
240
241 `String.Escape`
242 Token type for escape sequences in strings.
243
244 `String.Heredoc`
245 Token type for "heredoc" strings (e.g. in Ruby or Perl).
246
247 `String.Interpol`
248 Token type for interpolated parts in strings (e.g. ``#{foo}`` in Ruby).
249
250 `String.Other`
251 Token type for any other strings (for example ``%q{foo}`` string constructs
252 in Ruby).
253
254 `String.Regex`
255 Token type for regular expression literals (e.g. ``/foo/`` in JavaScript).
256
257 `String.Single`
258 Token type for single quoted strings.
259
260 `String.Symbol`
261 Token type for symbols (e.g. ``:foo`` in LISP or Ruby).
262
263
264 `Number`
265 Token type for any number literal.
266
267 `Number.Bin`
268 Token type for binary literals (e.g. ``0b101010``).
269
270 `Number.Float`
271 Token type for float literals (e.g. ``42.0``).
272
273 `Number.Hex`
274 Token type for hexadecimal number literals (e.g. ``0xdeadbeef``).
275
276 `Number.Integer`
277 Token type for integer literals (e.g. ``42``).
278
279 `Number.Integer.Long`
280 Token type for long integer literals (e.g. ``42L`` in Python).
281
282 `Number.Oct`
283 Token type for octal literals.
284
285
286 Operators
287 =========
288
289 `Operator`
290 For any punctuation operator (e.g. ``+``, ``-``).
291
292 `Operator.Word`
293 For any operator that is a word (e.g. ``not``).
294
295
296 Punctuation
297 ===========
298
299 .. versionadded:: 0.7
300
301 `Punctuation`
302 For any punctuation which is not an operator (e.g. ``[``, ``(``...)
303
304 `Punctuation.Marker`
305 For markers that point to a location (e.g., carets in Python
306 tracebacks for syntax errors).
307
308 .. versionadded:: 2.10
309
310
311 Comments
312 ========
313
314 `Comment`
315 Token type for any comment.
316
317 `Comment.Hashbang`
318 Token type for hashbang comments (i.e. first lines of files that start with
319 ``#!``).
320
321 `Comment.Multiline`
322 Token type for multiline comments.
323
324 `Comment.Preproc`
325 Token type for preprocessor comments (also ``<?php``/``<%`` constructs).
326
327 `Comment.PreprocFile`
328 Token type for filenames in preprocessor comments, such as include files in C/C++.
329
330 `Comment.Single`
331 Token type for comments that end at the end of a line (e.g. ``# foo``).
332
333 `Comment.Special`
334 Special data in comments. For example code tags, author and license
335 information, etc.
336
337
338 Generic Tokens
339 ==============
340
341 Generic tokens are for special lexers like the `DiffLexer` that doesn't really
342 highlight a programming language but a patch file.
343
344
345 `Generic`
346 A generic, unstyled token. Normally you don't use this token type.
347
348 `Generic.Deleted`
349 Marks the token value as deleted.
350
351 `Generic.Emph`
352 Marks the token value as emphasized.
353
354 `Generic.Error`
355 Marks the token value as an error message.
356
357 `Generic.Heading`
358 Marks the token value as headline.
359
360 `Generic.Inserted`
361 Marks the token value as inserted.
362
363 `Generic.Output`
364 Marks the token value as program output (e.g. for python cli lexer).
365
366 `Generic.Prompt`
367 Marks the token value as command prompt (e.g. bash lexer).
368
369 `Generic.Strong`
370 Marks the token value as bold (e.g. for rst lexer).
371
372 `Generic.EmphStrong`
373 Marks the token value as bold and emphasized.
374
375 `Generic.Subheading`
376 Marks the token value as subheadline.
377
378 `Generic.Traceback`
379 Marks the token value as a part of an error traceback.