]>
luflow.net public git repositories - flow-web.git/blob - static/highlight/es/languages/python.js
1 /*! `python` grammar compiled for Highlight.js 11.11.1 */
2 var hljsGrammar
= (function () {
7 Description: Python is an interpreted, object-oriented, high-level programming language with dynamic semantics.
8 Website: https://www.python.org
12 function python(hljs
) {
13 const regex
= hljs
.regex
;
14 const IDENT_RE
= /[\p{XID_Start}_]\p{XID_Continue}*/u;
15 const RESERVED_WORDS
= [
133 // https://docs.python.org/3/library/typing.html
134 // TODO: Could these be supplemented by a CamelCase matcher in certain
135 // contexts, leaving these remaining only for relevance hinting?
153 $pattern: /[A-Za-z]\w+|__\w+__/,
154 keyword: RESERVED_WORDS
,
162 begin: /^(>>>|\.\.\.) /
173 const LITERAL_BRACKET
= {
180 contains: [ hljs
.BACKSLASH_ESCAPE
],
183 begin: /([uU]|[bB]|[rR]|[bB][rR]|[rR][bB])?'''/,
186 hljs
.BACKSLASH_ESCAPE
,
192 begin: /([uU]|[bB]|[rR]|[bB][rR]|[rR][bB])?"""/,
195 hljs
.BACKSLASH_ESCAPE
,
201 begin: /([fF][rR]|[rR][fF]|[fF])'''/,
204 hljs
.BACKSLASH_ESCAPE
,
211 begin: /([fF][rR]|[rR][fF]|[fF])"""/,
214 hljs
.BACKSLASH_ESCAPE
,
221 begin: /([uU]|[rR])'/,
226 begin: /([uU]|[rR])"/,
231 begin: /([bB]|[bB][rR]|[rR][bB])'/,
235 begin: /([bB]|[bB][rR]|[rR][bB])"/,
239 begin: /([fF][rR]|[rR][fF]|[fF])'/,
242 hljs.BACKSLASH_ESCAPE,
248 begin: /([fF][rR]|[rR][fF]|[fF])"/,
251 hljs.BACKSLASH_ESCAPE,
256 hljs.APOS_STRING_MODE,
257 hljs.QUOTE_STRING_MODE
261 // https://docs.python.org/3.9/reference/lexical_analysis.html#numeric-literals
262 const digitpart = '[0-9](_
?[0-9])*';
263 const pointfloat = `(\\b(${digitpart}))?\\.(${digitpart})|\\b(${digitpart})\\.`;
264 // Whitespace after a number (or any lexical token) is needed only if its absence
265 // would change the tokenization
266 // https://docs.python.org/3.9/reference/lexical_analysis.html#whitespace-between-tokens
267 // We deviate slightly, requiring a word boundary or a keyword
268 // to avoid accidentally recognizing *prefixes* (e.g., `0` in `0x41` or `08` or `0__1`)
269 const lookahead = `\\b|${RESERVED_WORDS.join('|')}`;
274 // exponentfloat, pointfloat
275 // https://docs.python.org/3.9/reference/lexical_analysis.html#floating-point-literals
276 // optionally imaginary
277 // https://docs.python.org/3.9/reference/lexical_analysis.html#imaginary-literals
278 // Note: no leading \b because floats can start with a decimal point
279 // and we don't want to mishandle e
.g
. `fn(.5)`,
280 // no trailing \b for pointfloat because it can end with a decimal point
281 // and we don't want to mishandle e.g. `0..hex()`; this should be safe
282 // because both MUST contain a decimal point and so cannot be confused with
283 // the interior part of an identifier
285 begin: `(\\b(${digitpart})|(${pointfloat}))[eE][+-]?(${digitpart})[jJ]?(?=${lookahead})`
288 begin: `(${pointfloat})[jJ]?`
291 // decinteger, bininteger, octinteger, hexinteger
292 // https://docs.python.org/3.9/reference/lexical_analysis.html#integer-literals
293 // optionally "long" in Python 2
294 // https://docs.python.org/2.7/reference/lexical_analysis.html#integer-and-long-integer-literals
295 // decinteger is optionally imaginary
296 // https://docs.python.org/3.9/reference/lexical_analysis.html#imaginary-literals
298 begin: `\\b([1-9](_?[0-9])*|0+(_?0)*)[lLjJ]?(?=${lookahead})`
301 begin: `\\b0[bB](_?[01])+[lL]?(?=${lookahead})`
304 begin: `\\b0[oO](_?[0-7])+[lL]?(?=${lookahead})`
307 begin: `\\b0[xX](_?[0-9a-fA-F])+[lL]?(?=${lookahead})`
310 // imagnumber (digitpart-based)
311 // https://docs.python.org/3.9/reference/lexical_analysis.html#imaginary-literals
313 begin: `\\b(${digitpart})[jJ](?=${lookahead})`
317 const COMMENT_TYPE
= {
318 className: "comment",
319 begin: regex
.lookahead(/# type:/),
323 { // prevent keywords from coloring `type`
326 // comment within a datatype comment includes no keywords
337 // Exclude params in functions without params
354 hljs
.HASH_COMMENT_MODE
374 illegal: /(<\/|\?)|=>/,
379 // very common convention
380 scope: 'variable.language',
384 // eat "if" prior to string so that it won't accidentally be
385 // labeled as an f-string
389 { match: /\bor\b/, scope: "keyword" },
392 hljs
.HASH_COMMENT_MODE
,
410 /\(\s*/, IDENT_RE
,/\s*\)/
423 6: "title.class.inherited",
444 export default hljsGrammar
;