Repository navigation
Expand file tree
/
Copy pathpython.ts
More file actions
427 lines (406 loc) · 23.8 KB
/
Copy pathpython.ts
File metadata and controls
427 lines (406 loc) · 23.8 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// Python 3.10 grammar for Monogram: the second-language proof (docs/SECOND-LANGUAGE.md).
//
// The definition is CPython 3.10's Grammar/python.gram (the PEG the reference parser is generated
// from), its tokenizer (Parser/tokenizer.c) and the language reference's lexical analysis; the
// oracle is `ast.parse` of the box's python3 (3.10.12). The `invalid_*` rules of python.gram are
// error reporting, not syntax, and are not modelled. Rules here keep python.gram's names (in
// PascalCase) so a disagreement can be traced to the production it came from.
//
// Indentation is the engine's opt-in indent mode (IndentConfig): the lexer emits Indent when a
// line's column exceeds the stack top, Dedent per popped level, and Newline between two lines at
// the same column (YAML's sibling separator). That carries the same information as CPython's
// NEWLINE / INDENT / DEDENT: a suite is `Indent Stmt (Newline Stmt)* Dedent` and the statement after
// a compound statement's suite is reached through `Dedent Newline`.
//
// f-strings are lexed as plain strings: their replacement fields are not parsed yet (the engine's
// template tokens cannot spell them; mechanism 2 of docs/SECOND-LANGUAGE.md). The corpus check
// records the inputs that differ for that reason as their own class.
import {
token, rule, defineGrammar,
left, right, op, prefix, cappedPrefix, atLevel, capExpr, arm, assignTarget, ctx,
opt, many, many1, alt, not,
altPattern, optPattern, seq, oneOf, noneOf, range, anyChar, star, plus, never, notFollowedBy,
type Element,
} from './src/api.ts';
import type { IndentConfig, TokenPattern } from './src/grammar/types.ts';
// ── Structural tokens: emitted by the lexer's indent mode, never matched by a pattern ──
const Indent = token(never(), {});
const Dedent = token(never(), {});
const Newline = token(never(), {});
// ── Lexical tokens (declaration order = lexer precedence: earlier wins) ──
const digit = range('0', '9');
const nonZero = range('1', '9');
const hexDigit = oneOf(digit, range('a', 'f'), range('A', 'F'));
const lineBreak = altPattern(seq('\r', optPattern('\n')), '\n');
// A comment runs to the end of the line. Comment-only lines are skipped by the indent mode itself
// (IndentConfig.comment) so they never move the column stack.
const Comment = token(seq('#', star(noneOf('\n', '\r'))), { skip: true, scope: 'comment.line.number-sign' });
// Explicit line joining (2.1.5): a backslash right before a line break joins the two lines.
const LineJoin = token(seq('\\', lineBreak), { skip: true });
// Strings (2.4.1). A prefix is any case of r, u, b, br/rb, f, fr/rf; `ur` is not a prefix. Raw or
// not, a backslash always takes the next character for the literal's END (r'\'' is one literal);
// what differs is which escapes are well formed: a non-raw str rejects a truncated \x / \u / \U /
// \N{…} (the decoder's SyntaxError), a non-raw bytes literal \x only, and bytes hold ASCII only.
const ci = (s: string) => seq(...[...s].map((c) => oneOf(c.toLowerCase(), c.toUpperCase())));
const strEscape = seq('\\', altPattern(
seq('x', hexDigit, hexDigit),
seq('u', hexDigit, hexDigit, hexDigit, hexDigit),
seq('U', hexDigit, hexDigit, hexDigit, hexDigit, hexDigit, hexDigit, hexDigit, hexDigit),
seq('N{', plus(noneOf('}', '\n', '\r')), '}'),
noneOf('x', 'u', 'U', 'N'),
));
const bytesEscape = seq('\\', altPattern(seq('x', hexDigit, hexDigit), noneOf('x')));
const rawEscape = seq('\\', anyChar());
// The four quote forms of one body: `chars(q)` is what a body may hold besides an escape and,
// in a long form, a quote not starting the closer.
const quoted = (esc: TokenPattern, chars: (q: string, long: boolean) => TokenPattern) => altPattern(
seq("'''", star(altPattern(chars("'", true), esc, seq("'", notFollowedBy("''")))), "'''"),
seq('"""', star(altPattern(chars('"', true), esc, seq('"', notFollowedBy('""')))), '"""'),
seq("'", star(altPattern(chars("'", false), esc)), "'"),
seq('"', star(altPattern(chars('"', false), esc)), '"'),
);
const strChars = (q: string, long: boolean) => long ? noneOf(q, '\\') : noneOf(q, '\\', '\n', '\r');
// A bytes body is ASCII: the class is spelled as the ASCII range minus the quote, `\` and (short
// forms) line breaks.
const bytesChars = (q: string, long: boolean) => noneOf(q, '\\', ...(long ? [] : ['\n', '\r']), range('\u0080', ''));
const String_ = token(seq(optPattern(altPattern(ci('u'), ci('f'))), quoted(strEscape, strChars)), { string: true });
const RawString = token(seq(altPattern(ci('rf'), ci('fr'), ci('r')), quoted(rawEscape, strChars)), { string: true });
const Bytes = token(seq(ci('b'), quoted(bytesEscape, bytesChars)), { string: true });
const RawBytes = token(seq(altPattern(ci('rb'), ci('br')), quoted(seq('\\', noneOf(range('\u0080', ''))), bytesChars)), { string: true });
// Numbers (2.4.5 - 2.4.7). Underscores only between digits. A decimal integer other than zero
// has no leading zero (`0777` lexes as `0` then `777`, two atoms: rejected by the grammar).
const digitPart = seq(digit, star(seq(optPattern('_'), digit)));
const pointFloat = altPattern(seq(optPattern(digitPart), '.', digitPart), seq(digitPart, '.'));
const exponent = seq(oneOf('e', 'E'), optPattern(oneOf('+', '-')), digitPart);
const floatNumber = altPattern(seq(altPattern(pointFloat, digitPart), exponent), pointFloat);
const Imaginary = token(seq(altPattern(floatNumber, digitPart), oneOf('j', 'J')), { scope: 'constant.numeric' });
const Float = token(floatNumber, { scope: 'constant.numeric' });
const Integer = token(altPattern(
seq('0', oneOf('x', 'X'), plus(seq(optPattern('_'), hexDigit))),
seq('0', oneOf('o', 'O'), plus(seq(optPattern('_'), range('0', '7')))),
seq('0', oneOf('b', 'B'), plus(seq(optPattern('_'), oneOf('0', '1')))),
seq(nonZero, star(seq(optPattern('_'), digit))),
seq(plus('0'), star(seq(optPattern('_'), '0'))),
), { scope: 'constant.numeric' });
// NAME (2.3): ASCII here; a character beyond ASCII goes through the lexer's Unicode fallback, which
// reads Python's classes as declared: `_` + XID_Start, then XID_Continue (no `$`, no ZWNJ / ZWJ).
const Name = token(seq(oneOf(range('a', 'z'), range('A', 'Z'), '_'), star(oneOf(range('a', 'z'), range('A', 'Z'), digit, '_'))), {
identifier: true, identifierClasses: { start: '[_\\p{XID_Start}]', continue: '[\\p{XID_Continue}]' },
});
// ── Keywords (2.3.1). Soft keywords (`match`, `case`, `_`) are names everywhere but where an arm
// spells them. ──
export const keywords = [
'False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue',
'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in',
'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield',
];
const notKw = not(alt(...keywords));
const NAME = () => [notKw, Name] as const;
// ── Expressions (python.gram "EXPRESSIONS"): ONE Pratt rule from `lambda` down to `await`.
// python.gram's levels (expression, disjunction, conjunction, inversion, comparison, bitwise_or …)
// are binding powers here. `not` is a CAPPED prefix (cappedPrefix): it begins an operand only at
// its own level or a looser one, so `a and not b` parses and `a == not b`, `-not x` do not. A
// context that python.gram writes with a lower level's rule reads this rule at that level
// (atLevel): `'*' bitwise_or` is `['*', atLevel('|', Expression)]`, a comprehension's iterable
// and condition are disjunctions (`atLevel('or', …)`). The conditional is a mixfix led below `or`
// whose condition is a disjunction; `lambda` is a capped nud (capExpr) below the conditional: no
// operator takes it as an operand and nothing continues it. The comparisons are left-nested
// binary operators (python.gram's `comparison` is one n-ary node; the verdict is the same). The
// two-word operators are led arms at the comparison level (ledPrec). `**` binds tighter than a
// unary prefix on its left (`-x ** y` is `-(x ** y)`) and takes a unary on its right (`2 ** -1`);
// `await` takes a primary only. ──
const Expression = rule(($) => [
Primary,
['await', Primary],
[$, 'not', 'in', $],
[$, 'in', $],
[$, 'is', 'not', $],
[$, 'is', $],
[$, op, $],
[prefix, $],
[$, 'if', atLevel('or', Expression), 'else', $],
capExpr('if', 'lambda', opt(LambdaParameters), ':', $),
]);
const Disjunction = () => atLevel('or', Expression);
const BitwiseOr = () => atLevel('|', Expression);
const Expressions = rule(() => [[Expression, many(',', Expression), opt(',')]]);
const AssignmentExpression = rule(() => [[...NAME(), ':=', Expression]]);
const NamedExpression = rule(() => [AssignmentExpression, [Expression, not(':=')]]);
const StarExpression = rule(() => [arm('starred', '*', BitwiseOr()), Expression]);
const StarExpressions = rule(() => [arm('exprList', StarExpression, many(',', StarExpression), opt(','))]);
const StarNamedExpression = rule(() => [arm('starred', '*', BitwiseOr()), NamedExpression]);
const StarNamedExpressions = rule(() => [arm('starNamedList', StarNamedExpression, many(',', StarNamedExpression), opt(','))]);
const YieldExpr = rule(() => [['yield', 'from', Expression], ['yield', opt(StarExpressions)]]);
// What brackets hold reads a comparison `in` again (`for a[b in c] in d:`): the context parameter
// `in`, which a for head's targets clear (ForTargets), is set inside every bracket pair.
const inBrackets = (...items: Element[]) => ctx({ in: true }, ...items);
// primary: atom and its trailers.
const Primary = rule(($) => [
Atom,
arm('attr', $, '.', ...NAME()),
[$, inBrackets(Genexp)],
[$, '(', inBrackets(opt(Arguments)), ')'],
arm('subscript', $, '[', inBrackets(Slices), ']'),
]);
const Slice = rule(() => [
[opt(Expression), ':', opt(Expression), opt(':', opt(Expression))],
NamedExpression,
]);
const Slices = rule(() => [[Slice, many(',', Slice), opt(',')]]);
const Strings = rule(() => [[many1(alt(String_, RawString, Bytes, RawBytes))]]);
const Atom = rule(() => [
arm('atomName', ...NAME()),
'True', 'False', 'None', '...',
Strings,
Imaginary, Float, Integer,
inBrackets(Tuple), inBrackets(Group), inBrackets(Genexp),
inBrackets(List), inBrackets(Listcomp),
inBrackets(Dict), inBrackets(Set), inBrackets(Dictcomp), inBrackets(Setcomp),
]);
const Group = rule(() => [arm('parenExpr', '(', alt(YieldExpr, NamedExpression), ')')]);
const Tuple = rule(() => [arm('parenTuple', '(', opt(StarNamedExpression, ',', opt(StarNamedExpressions)), ')')]);
const List = rule(() => [arm('listDisplay', '[', opt(StarNamedExpressions), ']')]);
const Set = rule(() => [['{', StarNamedExpressions, '}']]);
const Kvpair = rule(() => [[Expression, ':', Expression]]);
const DoubleStarredKvpair = rule(() => [['**', BitwiseOr()], Kvpair]);
const Dict = rule(() => [['{', opt(DoubleStarredKvpair, many(',', DoubleStarredKvpair), opt(',')), '}']]);
// A for head's targets: star expressions none of which reads a top-level `in` (it is the
// head's), each read with the context parameter `in` cleared (a primary sets it again).
const ForTargets = rule(() => [arm('exprList', ctx({ in: false }, StarExpression), many(',', ctx({ in: false }, StarExpression)), opt(','))]);
const ForIfClause = rule(() => [[opt('async'), 'for', ForTargets, assignTarget('star'), 'in', Disjunction(), many('if', Disjunction())]]);
const ForIfClauses = rule(() => [[many1(ForIfClause)]]);
const Listcomp = rule(() => [['[', NamedExpression, ForIfClauses, ']']]);
const Setcomp = rule(() => [['{', NamedExpression, ForIfClauses, '}']]);
const Dictcomp = rule(() => [['{', Kvpair, ForIfClauses, '}']]);
const Genexp = rule(() => [['(', alt(AssignmentExpression, [Expression, not(':=')]), ForIfClauses, ')']]);
// arguments: positional (starred or named, never `name=`), then keywords: `*x` may mix with
// `k=v`, `**m` comes after every `*x`.
const KwargOrStarred = rule(() => [[...NAME(), '=', Expression], ['*', Expression]]);
const KwargOrDoubleStarred = rule(() => [[...NAME(), '=', Expression], ['**', Expression]]);
const Kwargs = rule(() => [
[KwargOrStarred, many(',', KwargOrStarred), ',', KwargOrDoubleStarred, many(',', KwargOrDoubleStarred)],
[KwargOrStarred, many(',', KwargOrStarred)],
[KwargOrDoubleStarred, many(',', KwargOrDoubleStarred)],
]);
const PositionalArg = rule(() => [['*', Expression], [alt(AssignmentExpression, [Expression, not(':=')]), not('=')]]);
const Args = rule(() => [
[PositionalArg, many(',', PositionalArg), opt(',', Kwargs)],
Kwargs,
]);
const Arguments = rule(() => [[Args, opt(','), not(not(')'))]]);
// lambda: python.gram's lambda_parameters, the parameter forms without annotations, ended by `:`.
// parameters: the same with annotations, ended by `)`. One builder serves both (closer, param).
function paramForms(param: () => Element[], close: string) {
const end = alt(',', not(not(close)));
const noDefault = () => [...param(), end];
const withDefault = () => [...param(), '=', Expression, end];
const maybeDefault = () => [...param(), opt('=', Expression), end];
const slash = alt(['/', ','], ['/', not(not(close))]);
const kwds = () => ['**', ...noDefault()];
const starEtc = () => alt(
['*', ...noDefault(), many(...maybeDefault()), opt(...kwds())],
['*', ',', many1(...maybeDefault()), opt(...kwds())],
kwds(),
);
return [
[many1(...noDefault()), slash, many(...noDefault()), many(...withDefault()), opt(starEtc())],
[many(...noDefault()), many1(...withDefault()), slash, many(...withDefault()), opt(starEtc())],
[many1(...noDefault()), many(...withDefault()), opt(starEtc())],
[many1(...withDefault()), opt(starEtc())],
[starEtc()],
];
}
const Parameters = rule(() => paramForms(() => [...NAME(), opt(':', Expression)], ')'));
const LambdaParameters = rule(() => paramForms(() => [...NAME()], ':'));
// ── Targets (python.gram "TARGETS"). A target is parsed as an expression and checked where a
// target is taken (`assignTarget(ctx)`, the contexts declared in `assignTargets` below, docs/GRAMMAR.md
// "Declared assignment-target contexts"): the head of a statement is parsed once. python.gram's
// t_primary arms end in `&t_lookahead`, so the LAST `.name` / `[…]` of a chain is the target's:
// here the node a chain builds is its last trailer's arm, `attr` and `subscript` are targets and a
// call is not (`f().x = 1` assigns, `f() = 1` has no derivation). ──
// ── Simple statements ──
const AugAssign = () => alt('+=', '-=', '*=', '@=', '/=', '%=', '&=', '|=', '^=', '<<=', '>>=', '**=', '//=');
const AnnotatedRhs = () => alt(YieldExpr, StarExpressions);
// The value of an assignment, and the next `=` of a chain (`a = b = c`): a value followed by `=`
// is a target (python.gram's `(star_targets '=')+ (yield_expr | star_expressions)`).
const AssignValue = rule(($) => [
YieldExpr,
[StarExpressions, opt('=', assignTarget('star'), $)],
]);
const DottedName = rule(() => [[...NAME(), many('.', ...NAME())]]);
const DottedAsName = rule(() => [[DottedName, opt('as', ...NAME())]]);
const ImportFromAsName = rule(() => [[...NAME(), opt('as', ...NAME())]]);
const ImportFromAsNames = () => [ImportFromAsName, many(',', ImportFromAsName)];
const ImportFromTargets = () => alt(['(', ...ImportFromAsNames(), opt(','), ')'], [...ImportFromAsNames(), not(',')], '*');
const ImportStmt = rule(() => [
['import', DottedAsName, many(',', DottedAsName)],
['from', many(alt('.', '...')), DottedName, 'import', ImportFromTargets()],
['from', many1(alt('.', '...')), 'import', ImportFromTargets()],
]);
// An expression statement and the assignments share their head, parsed once: `=` takes star
// targets, an augmented assignment and an annotation a single target.
const SimpleStmt = rule(() => [
[StarExpressions, opt(alt(
['=', assignTarget('star'), AssignValue],
[AugAssign(), assignTarget('single'), AnnotatedRhs()],
[':', assignTarget('single'), Expression, opt('=', AnnotatedRhs())],
))],
['return', opt(StarExpressions)],
ImportStmt,
['raise', Expression, opt('from', Expression)],
'raise',
'pass',
['del', StarExpressions, assignTarget('del')],
YieldExpr,
['assert', Expression, opt(',', Expression)],
'break',
'continue',
['global', ...NAME(), many(',', ...NAME())],
['nonlocal', ...NAME(), many(',', ...NAME())],
]);
const SimpleStmts = rule(() => [[SimpleStmt, many(';', SimpleStmt), opt(';')]]);
// ── Compound statements. A suite is a simple-statement list on the header's line or an indented
// block; a continuation clause (elif / else / except / finally) sits on a later line at the
// header's column, so a Newline comes before it. ──
const Block = rule(() => [[Indent, Statements, Dedent], SimpleStmts]);
const ElseBlock = () => [Newline, 'else', ':', Block];
const ElifStmt = rule(($) => [[Newline, 'elif', NamedExpression, ':', Block, opt(alt($, ElseBlock()))]]);
const IfStmt = rule(() => [['if', NamedExpression, ':', Block, opt(alt(ElifStmt, ElseBlock()))]]);
const WhileStmt = rule(() => [['while', NamedExpression, ':', Block, opt(...ElseBlock())]]);
const ForStmt = rule(() => [[opt('async'), 'for', ForTargets, assignTarget('star'), 'in', StarExpressions, ':', Block, opt(...ElseBlock())]]);
const WithItem = rule(() => [[Expression, opt('as', StarExpression, assignTarget('star'), not(not(alt(',', ')', ':'))))]]);
const WithStmt = rule(() => [
[opt('async'), 'with', '(', WithItem, many(',', WithItem), opt(','), ')', ':', Block],
[opt('async'), 'with', WithItem, many(',', WithItem), ':', Block],
]);
const ExceptBlock = rule(() => [[Newline, 'except', opt(Expression, opt('as', ...NAME())), ':', Block]]);
const FinallyBlock = () => [Newline, 'finally', ':', Block];
const TryStmt = rule(() => [
['try', ':', Block, ...FinallyBlock()],
['try', ':', Block, many1(ExceptBlock), opt(...ElseBlock()), opt(...FinallyBlock())],
]);
const Decorator = () => ['@', NamedExpression, Newline];
const FunctionDef = rule(() => [[many(...Decorator()), opt('async'), 'def', ...NAME(), '(', opt(Parameters), ')', opt('->', Expression), ':', Block]]);
const ClassDef = rule(() => [[many(...Decorator()), 'class', ...NAME(), opt('(', opt(Arguments), ')'), ':', Block]]);
// ── match (PEP 634). `match`, `case` and `_` are soft keywords: names elsewhere. ──
const SignedNumber = () => alt([opt('-'), alt(Integer, Float, Imaginary)]);
const ComplexNumber = () => [opt('-'), alt(Integer, Float), alt('+', '-'), Imaginary];
const LiteralPattern = rule(() => [
[SignedNumber(), not(alt('+', '-'))],
ComplexNumber(),
Strings, 'None', 'True', 'False',
]);
const Attr = rule(() => [[...NAME(), many1('.', ...NAME())]]);
const NameOrAttr = () => [...NAME(), many('.', ...NAME())];
const PatternCaptureTarget = rule(() => [[not('_'), ...NAME(), not(alt('.', '(', '='))]]);
const ClosedPattern = rule(() => [
LiteralPattern,
PatternCaptureTarget,
'_',
[Attr, not(alt('.', '(', '='))],
['(', Pattern, ')'],
['[', opt(MaybeSequencePattern), ']'],
['(', opt(OpenSequencePattern), ')'],
MappingPattern,
ClassPattern,
]);
const OrPattern = rule(() => [[ClosedPattern, many('|', ClosedPattern)]]);
const Pattern = rule(() => [[OrPattern, 'as', PatternCaptureTarget], OrPattern]);
const StarPattern = rule(() => [['*', alt(PatternCaptureTarget, '_')]]);
const MaybeStarPattern = () => alt(StarPattern, Pattern);
const MaybeSequencePattern = rule(() => [[MaybeStarPattern(), many(',', MaybeStarPattern()), opt(',')]]);
const OpenSequencePattern = rule(() => [[MaybeStarPattern(), ',', opt(MaybeSequencePattern)]]);
const KeyValuePattern = rule(() => [[alt(LiteralPattern, Attr), ':', Pattern]]);
const DoubleStarPattern = () => ['**', PatternCaptureTarget];
const MappingPattern = rule(() => [
['{', '}'],
['{', ...DoubleStarPattern(), opt(','), '}'],
['{', KeyValuePattern, many(',', KeyValuePattern), ',', ...DoubleStarPattern(), opt(','), '}'],
['{', KeyValuePattern, many(',', KeyValuePattern), opt(','), '}'],
]);
const KeywordPattern = rule(() => [[...NAME(), '=', Pattern]]);
const ClassPattern = rule(() => [
[...NameOrAttr(), '(', ')'],
[...NameOrAttr(), '(', Pattern, many(',', Pattern), opt(','), ')'],
[...NameOrAttr(), '(', KeywordPattern, many(',', KeywordPattern), opt(','), ')'],
[...NameOrAttr(), '(', Pattern, many(',', Pattern), ',', KeywordPattern, many(',', KeywordPattern), opt(','), ')'],
]);
const Patterns = rule(() => [OpenSequencePattern, Pattern]);
const CaseBlock = rule(() => [['case', Patterns, opt('if', NamedExpression), ':', Block]]);
const SubjectExpr = rule(() => [[StarNamedExpression, ',', opt(StarNamedExpressions)], NamedExpression]);
const MatchStmt = rule(() => [['match', SubjectExpr, ':', Indent, CaseBlock, many(Newline, CaseBlock), Dedent]]);
const CompoundStmt = rule(() => [FunctionDef, IfStmt, ClassDef, WithStmt, ForStmt, TryStmt, WhileStmt, MatchStmt]);
const Statement = rule(() => [CompoundStmt, SimpleStmts]);
const Statements = rule(() => [[Statement, many(Newline, Statement)]]);
const File = rule(() => [[opt(Statements)]]);
const indent: IndentConfig = {
indentToken: 'Indent',
dedentToken: 'Dedent',
newlineToken: 'Newline',
flowOpen: ['(', '[', '{'],
flowClose: [')', ']', '}'],
comment: '#',
};
export default defineGrammar({
name: 'python',
scopeName: 'source.python',
tokens: { Comment, LineJoin, RawBytes, Bytes, RawString, String: String_, Imaginary, Float, Integer, Name, Indent, Dedent, Newline },
prec: [
left('or'),
left('and'),
right(cappedPrefix('not')),
left('==', '!=', '<', '<=', '>', '>='),
left('|'),
left('^'),
left('&'),
left('<<', '>>'),
left('+', '-'),
left('*', '/', '//', '%', '@'),
right(prefix('+', '-', '~')),
right('**'),
],
rules: {
Expression, Expressions,
AssignmentExpression, NamedExpression, StarExpression, StarExpressions, StarNamedExpression, StarNamedExpressions, YieldExpr,
Primary, Slice, Slices, Strings, Atom, Group, Tuple, List, Set, Kvpair, DoubleStarredKvpair, Dict,
ForTargets, ForIfClause, ForIfClauses, Listcomp, Setcomp, Dictcomp, Genexp,
KwargOrStarred, KwargOrDoubleStarred, Kwargs, PositionalArg, Args, Arguments,
Parameters, LambdaParameters,
AssignValue, DottedName, DottedAsName, ImportFromAsName, ImportStmt, SimpleStmt, SimpleStmts,
Block, ElifStmt, IfStmt, WhileStmt, ForStmt, WithItem, WithStmt, ExceptBlock, TryStmt, FunctionDef, ClassDef,
LiteralPattern, Attr, PatternCaptureTarget, ClosedPattern, OrPattern, Pattern, StarPattern, MaybeSequencePattern, OpenSequencePattern,
KeyValuePattern, MappingPattern, KeywordPattern, ClassPattern, Patterns, CaseBlock, SubjectExpr, MatchStmt,
CompoundStmt, Statement, Statements, File,
},
// The context parameters (docs/GRAMMAR.md "Contexts"): `in`, whether the comparison `in` is read.
params: { in: true },
ledPrec: [
{ connector: 'if', below: 'or' },
{ connector: 'not', sameAs: '<', chainRhs: true },
{ connector: 'in', sameAs: '<', chainRhs: true, when: '+in' }, // not in a for target (ForTargets)
{ connector: 'is', sameAs: '<', chainRhs: true },
],
entry: File,
indent,
// python.gram's three target contexts. star_targets (`=`, `for`, `with … as`, comprehensions): a
// name, an attribute, a subscript, and parenthesized, bracketed and bare tuples, a parenthesis
// and a starred target (`*a`) of them. single_target (augmented assignment, annotation): a name,
// an attribute, a subscript or a parenthesis of one: no tuple, so a list of exprList's is a
// single target only as its one child with no ',' after it (each leaf and the parenthesis are
// `last`). del_targets: star_targets without the star.
assignTargets: {
star: {
leaves: ['atomName', 'attr', 'subscript'],
containers: { exprList: 'all', starNamedList: 'all', parenTuple: 'all', parenExpr: 'all', listDisplay: 'all', starred: 'all' },
},
single: {
leaves: ['atomName', 'attr', 'subscript'],
containers: { exprList: 'all', parenExpr: 'all' },
last: ['atomName', 'attr', 'subscript', 'parenExpr'],
},
del: {
leaves: ['atomName', 'attr', 'subscript'],
containers: { exprList: 'all', starNamedList: 'all', parenTuple: 'all', parenExpr: 'all', listDisplay: 'all' },
},
},
});