This repository has been archived by the owner on Jul 29, 2022. It is now read-only.
generated from jasonkuhrt/template-typescript-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python.ts
80 lines (71 loc) · 2.33 KB
/
python.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import type { LexerConfig } from '../lexer/types';
import type { ParserConfig } from '../parser/types';
import type { LanguageConfig } from './types';
/**
* @see https://docs.python.org/3/reference/lexical_analysis.html#operators
*/
const operators =
/* prettier-ignore */ [
// Operators
'+', '-', '*', '**', '/', '//', '%', '@',
'<<', '>>', '&', '|', '^', '~', ':=',
'<', '>', '<=', '>=', '==', '!=',
// Delimiters
',', ':', '.', ';', '@', '=', '->',
'+=', '-=', '*=', '/=', '//=', '%=', '@=',
'&=', '|=', '^=', '>>=', '<<=', '**=',
];
/**
* @see https://docs.python.org/3/reference/lexical_analysis.html#numeric-literals
*/
const bindigit = '[01]';
const octdigit = '[0-7]';
const digit = '[0-9]';
const nonzerodigit = '[1-9]';
const hexdigit = `(?:${digit}|[a-fA-F])`;
const bininteger = `(?:0[bB](?:_?${bindigit})+)`;
const octinteger = `(?:0[oO](?:_?${octdigit})+)`;
const hexinteger = `(?:0[xX](?:_?${hexdigit})+)`;
const decinteger = `(?:${nonzerodigit}(?:_?${digit})*|0+(?:_?0)*)`;
const integer = `(?:${decinteger}|${bininteger}|${octinteger}|${hexinteger})`;
const digitpart = `(?:${digit}(?:_?${digit})*)`;
const fraction = `(?:\\.${digitpart})`;
const exponent = `(?:[eE][-+]?${digitpart})`;
const pointfloat = `(?:${digitpart}?${fraction}|${digitpart}\\.)`;
const exponentfloat = `(?:(?:${digitpart}|${pointfloat})${exponent})`;
const floatnumber = `(?:${pointfloat}|${exponentfloat})`;
const numbers = new RegExp(`(?:${integer}|${floatnumber})`);
export const lexer: LexerConfig = {
joinLines: '\\',
comments: [{ type: 'line-comment', startsWith: '#' }],
symbols: /[_a-zA-Z][_a-zA-Z0-9]*/,
numbers,
operators,
brackets: [
{ startsWith: '{', endsWith: '}' },
{ startsWith: '[', endsWith: ']' },
{ startsWith: '(', endsWith: ')' },
],
strings: [
{ startsWith: "'" },
{ startsWith: '"' },
{ startsWith: "'''" },
{ startsWith: '"""' },
{
startsWith: "f'",
endsWith: "'",
templates: [{ type: 'expr', startsWith: '{', endsWith: '}' }],
},
{
startsWith: 'f"',
endsWith: '"',
templates: [{ type: 'expr', startsWith: '{', endsWith: '}' }],
},
{ startsWith: "r'", endsWith: "'" },
{ startsWith: 'r"', endsWith: '"' },
],
};
export const parser: ParserConfig = {
useIndentBlocks: true,
};
export const lang: LanguageConfig = { lexer, parser };