Skip to content

Commit

Permalink
lambda variants
Browse files Browse the repository at this point in the history
  • Loading branch information
thatbirdguythatuknownot committed May 2, 2024
1 parent 17dffb3 commit 2260c0e
Show file tree
Hide file tree
Showing 7 changed files with 14,843 additions and 11,289 deletions.
4 changes: 4 additions & 0 deletions Doc/library/token-list.inc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Grammar/Tokens
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ PIPEGTEQUAL '|>='
QMARK '?'
DOUBLEQMARK '??'
DOUBLEQMARKEQUAL '??='
EQUALARROW '=>'

OP
TYPE_IGNORE
Expand Down
69 changes: 69 additions & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -1029,11 +1029,19 @@ class_or_func_expr[expr_ty]:
lambdef[expr_ty]:
| 'lambda' a=[lambda_params] ':' b=expression {
_PyAST_Lambda((a) ? a : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), b, EXTRA) }
| '|' a=[lambda_sum_params] '|' b=expression {
_PyAST_Lambda((a) ? a : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), b, EXTRA) }
| '(' a=[params] ')' '=>' b=expression {
_PyAST_Lambda((a) ? a : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), b, EXTRA) }

lambda_params[arguments_ty]:
| invalid_lambda_parameters
| lambda_parameters

lambda_sum_params[arguments_ty]:
| invalid_lambda_sum_parameters
| lambda_sum_parameters

# lambda_parameters etc. duplicates parameters but without annotations
# or type comments, and if there's no comma after a parameter, we expect
# a colon, not a close parenthesis. (For more, see parameters above.)
Expand Down Expand Up @@ -1079,6 +1087,46 @@ lambda_param_maybe_default[NameDefaultPair*]:
| a=lambda_param c=default? &':' { _PyPegen_name_default_pair(p, a, c, NULL) }
lambda_param[arg_ty]: a=NAME { _PyAST_arg(a->v.Name.id, NULL, NULL, EXTRA) }

lambda_sum_parameters[arguments_ty]:
| a=lambda_sum_slash_no_default b[asdl_arg_seq*]=lambda_sum_param_no_default* c=lambda_sum_param_with_default* d=[lambda_star_etc] {
CHECK_VERSION(arguments_ty, 8, "Positional-only parameters are", _PyPegen_make_arguments(p, a, NULL, b, c, d)) }
| a=lambda_sum_slash_with_default b=lambda_sum_param_with_default* c=[lambda_star_etc] {
CHECK_VERSION(arguments_ty, 8, "Positional-only parameters are", _PyPegen_make_arguments(p, NULL, a, NULL, b, c)) }
| a[asdl_arg_seq*]=lambda_sum_param_no_default+ b=lambda_sum_param_with_default* c=[lambda_star_etc] {
_PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
| a=lambda_sum_param_with_default+ b=[lambda_sum_star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}
| a=lambda_sum_star_etc { _PyPegen_make_arguments(p, NULL, NULL, NULL, NULL, a) }

lambda_sum_slash_no_default[asdl_arg_seq*]:
| a[asdl_arg_seq*]=lambda_sum_param_no_default+ '/' ',' { a }
| a[asdl_arg_seq*]=lambda_sum_param_no_default+ '/' &'|' { a }

lambda_sum_slash_with_default[SlashWithDefault*]:
| a=lambda_sum_param_no_default* b=lambda_sum_param_with_default+ '/' ',' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }
| a=lambda_sum_param_no_default* b=lambda_sum_param_with_default+ '/' &'|' { _PyPegen_slash_with_default(p, (asdl_arg_seq *)a, b) }

lambda_sum_star_etc[StarEtc*]:
| invalid_lambda_sum_star_etc
| '*' a=lambda_sum_param_no_default b=lambda_sum_param_maybe_default* c=[lambda_sum_kwds] {
_PyPegen_star_etc(p, a, b, c) }
| '*' ',' b=lambda_sum_param_maybe_default+ c=[lambda_sum_kwds] {
_PyPegen_star_etc(p, NULL, b, c) }
| a=lambda_sum_kwds { _PyPegen_star_etc(p, NULL, NULL, a) }

lambda_sum_kwds[arg_ty]:
| invalid_lambda_kwds
| '**' a=lambda_sum_param_no_default { a }

lambda_sum_param_no_default[arg_ty]:
| a=lambda_param ',' { a }
| a=lambda_param &'|' { a }
lambda_sum_param_with_default[NameDefaultPair*]:
| a=lambda_param c=default ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
| a=lambda_param c=default &'|' { _PyPegen_name_default_pair(p, a, c, NULL) }
lambda_sum_param_maybe_default[NameDefaultPair*]:
| a=lambda_param c=default? ',' { _PyPegen_name_default_pair(p, a, c, NULL) }
| a=lambda_param c=default? &'|' { _PyPegen_name_default_pair(p, a, c, NULL) }

# LITERALS
# ========

Expand Down Expand Up @@ -1456,6 +1504,27 @@ invalid_lambda_kwds:
| '**' lambda_param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-keyword argument cannot have default value") }
| '**' lambda_param ',' a=lambda_param { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "arguments cannot follow var-keyword argument") }
| '**' lambda_param ',' a[Token*]=('*'|'**'|'/') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "arguments cannot follow var-keyword argument") }
invalid_lambda_sum_parameters:
| a="/" ',' {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "at least one argument must precede /") }
| (lambda_sum_slash_no_default | lambda_sum_slash_with_default) lambda_sum_param_maybe_default* a='/' {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "/ may appear only once") }
| lambda_sum_slash_no_default? lambda_sum_param_no_default* invalid_lambda_sum_parameters_helper a=lambda_sum_param_no_default {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "parameter without a default follows parameter with a default") }
| lambda_sum_param_no_default* a='(' ','.lambda_param+ ','? b=')' {
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "Lambda expression parameters cannot be parenthesized") }
| (lambda_sum_slash_no_default | lambda_sum_slash_with_default)? lambda_sum_param_maybe_default* '*' (',' | lambda_sum_param_no_default) lambda_sum_param_maybe_default* a='/' {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "/ must be ahead of *") }
| lambda_sum_param_maybe_default+ '/' a='*' {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expected comma between / and *") }
invalid_lambda_sum_parameters_helper:
| a=lambda_sum_slash_with_default { _PyPegen_singleton_seq(p, a) }
| lambda_sum_param_with_default+
invalid_lambda_sum_star_etc:
| '*' ('|' | ',' ('|' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
| '*' lambda_param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-positional argument cannot have default value") }
| '*' (lambda_sum_param_no_default | ',') lambda_sum_param_maybe_default* a='*' (lambda_sum_param_no_default | ',') {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "* argument may appear only once") }
invalid_double_type_comments:
| TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT {
RAISE_SYNTAX_ERROR("Cannot have two type comments on def") }
Expand Down
23 changes: 12 additions & 11 deletions Include/internal/pycore_token.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,18 @@ extern "C" {
#define QMARK 58
#define DOUBLEQMARK 59
#define DOUBLEQMARKEQUAL 60
#define OP 61
#define TYPE_IGNORE 62
#define TYPE_COMMENT 63
#define SOFT_KEYWORD 64
#define FSTRING_START 65
#define FSTRING_MIDDLE 66
#define FSTRING_END 67
#define COMMENT 68
#define NL 69
#define ERRORTOKEN 70
#define N_TOKENS 72
#define EQUALARROW 61
#define OP 62
#define TYPE_IGNORE 63
#define TYPE_COMMENT 64
#define SOFT_KEYWORD 65
#define FSTRING_START 66
#define FSTRING_MIDDLE 67
#define FSTRING_END 68
#define COMMENT 69
#define NL 70
#define ERRORTOKEN 71
#define N_TOKENS 73
#define NT_OFFSET 256

/* Special definitions for cooperation with parser */
Expand Down
26 changes: 14 additions & 12 deletions Lib/token.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2260c0e

Please sign in to comment.