]>
luflow.net public git repositories - flow-web.git/blob - static/highlight/languages/python.js
1 /*! `python` grammar compiled for Highlight.js 11.11.1 */
3 var hljsGrammar
= (function () {
8 Description: Python is an interpreted, object-oriented, high-level programming language with dynamic semantics.
9 Website: https://www.python.org
13 function python(hljs
) {
14 const regex
= hljs
.regex
;
15 const IDENT_RE
= /[\p{XID_Start}_]\p{XID_Continue}*/u;
16 const RESERVED_WORDS
= [
134 // https://docs.python.org/3/library/typing.html
135 // TODO: Could these be supplemented by a CamelCase matcher in certain
136 // contexts, leaving these remaining only for relevance hinting?
154 $pattern: /[A-Za-z]\w+|__\w+__/,
155 keyword: RESERVED_WORDS
,
163 begin: /^(>>>|\.\.\.) /
174 const LITERAL_BRACKET
= {
181 contains: [ hljs
.BACKSLASH_ESCAPE
],
184 begin: /([uU]|[bB]|[rR]|[bB][rR]|[rR][bB])?'''/,
187 hljs
.BACKSLASH_ESCAPE
,
193 begin: /([uU]|[bB]|[rR]|[bB][rR]|[rR][bB])?"""/,
196 hljs
.BACKSLASH_ESCAPE
,
202 begin: /([fF][rR]|[rR][fF]|[fF])'''/,
205 hljs
.BACKSLASH_ESCAPE
,
212 begin: /([fF][rR]|[rR][fF]|[fF])"""/,
215 hljs
.BACKSLASH_ESCAPE
,
222 begin: /([uU]|[rR])'/,
227 begin: /([uU]|[rR])"/,
232 begin: /([bB]|[bB][rR]|[rR][bB])'/,
236 begin: /([bB]|[bB][rR]|[rR][bB])"/,
240 begin: /([fF][rR]|[rR][fF]|[fF])'/,
243 hljs.BACKSLASH_ESCAPE,
249 begin: /([fF][rR]|[rR][fF]|[fF])"/,
252 hljs.BACKSLASH_ESCAPE,
257 hljs.APOS_STRING_MODE,
258 hljs.QUOTE_STRING_MODE
262 // https://docs.python.org/3.9/reference/lexical_analysis.html#numeric-literals
263 const digitpart = '[0-9](_
?[0-9])*';
264 const pointfloat = `(\\b(${digitpart}))?\\.(${digitpart})|\\b(${digitpart})\\.`;
265 // Whitespace after a number (or any lexical token) is needed only if its absence
266 // would change the tokenization
267 // https://docs.python.org/3.9/reference/lexical_analysis.html#whitespace-between-tokens
268 // We deviate slightly, requiring a word boundary or a keyword
269 // to avoid accidentally recognizing *prefixes* (e.g., `0` in `0x41` or `08` or `0__1`)
270 const lookahead = `\\b|${RESERVED_WORDS.join('|')}`;
275 // exponentfloat, pointfloat
276 // https://docs.python.org/3.9/reference/lexical_analysis.html#floating-point-literals
277 // optionally imaginary
278 // https://docs.python.org/3.9/reference/lexical_analysis.html#imaginary-literals
279 // Note: no leading \b because floats can start with a decimal point
280 // and we don't want to mishandle e
.g
. `fn(.5)`,
281 // no trailing \b for pointfloat because it can end with a decimal point
282 // and we don't want to mishandle e.g. `0..hex()`; this should be safe
283 // because both MUST contain a decimal point and so cannot be confused with
284 // the interior part of an identifier
286 begin: `(\\b(${digitpart})|(${pointfloat}))[eE][+-]?(${digitpart})[jJ]?(?=${lookahead})`
289 begin: `(${pointfloat})[jJ]?`
292 // decinteger, bininteger, octinteger, hexinteger
293 // https://docs.python.org/3.9/reference/lexical_analysis.html#integer-literals
294 // optionally "long" in Python 2
295 // https://docs.python.org/2.7/reference/lexical_analysis.html#integer-and-long-integer-literals
296 // decinteger is optionally imaginary
297 // https://docs.python.org/3.9/reference/lexical_analysis.html#imaginary-literals
299 begin: `\\b([1-9](_?[0-9])*|0+(_?0)*)[lLjJ]?(?=${lookahead})`
302 begin: `\\b0[bB](_?[01])+[lL]?(?=${lookahead})`
305 begin: `\\b0[oO](_?[0-7])+[lL]?(?=${lookahead})`
308 begin: `\\b0[xX](_?[0-9a-fA-F])+[lL]?(?=${lookahead})`
311 // imagnumber (digitpart-based)
312 // https://docs.python.org/3.9/reference/lexical_analysis.html#imaginary-literals
314 begin: `\\b(${digitpart})[jJ](?=${lookahead})`
318 const COMMENT_TYPE
= {
319 className: "comment",
320 begin: regex
.lookahead(/# type:/),
324 { // prevent keywords from coloring `type`
327 // comment within a datatype comment includes no keywords
338 // Exclude params in functions without params
355 hljs
.HASH_COMMENT_MODE
375 illegal: /(<\/|\?)|=>/,
380 // very common convention
381 scope: 'variable.language',
385 // eat "if" prior to string so that it won't accidentally be
386 // labeled as an f-string
390 { match: /\bor\b/, scope: "keyword" },
393 hljs
.HASH_COMMENT_MODE
,
411 /\(\s*/, IDENT_RE
,/\s*\)/
424 6: "title.class.inherited",
445 hljs
.registerLanguage('python', hljsGrammar
);