Skip to content

Instantly share code, notes, and snippets.

@oguzalb
Created July 21, 2023 14:08
Show Gist options
  • Save oguzalb/590d034cde3fae9c3bc1c278c687246c to your computer and use it in GitHub Desktop.
Save oguzalb/590d034cde3fae9c3bc1c278c687246c to your computer and use it in GitHub Desktop.
adding arrow functions to Python grammar
diff --git a/Grammar/Tokens b/Grammar/Tokens
index 618ae81..29bcfeb 100644
--- a/Grammar/Tokens
+++ b/Grammar/Tokens
@@ -11,6 +11,7 @@ RPAR ')'
LSQB '['
RSQB ']'
COLON ':'
+FUNC_SIGN '=>'
COMMA ','
SEMI ';'
PLUS '+'
diff --git a/Grammar/python.gram b/Grammar/python.gram
index c1863ae..337a6d0 100644
--- a/Grammar/python.gram
+++ b/Grammar/python.gram
@@ -674,6 +674,7 @@ expressions[expr_ty]:
expression[expr_ty] (memo):
| invalid_expression
| invalid_legacy_expression
+ | lambarrowdef
| a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) }
| disjunction
| lambdef
@@ -858,6 +859,9 @@ group[expr_ty]:
lambdef[expr_ty]:
| 'lambda' a=[lambda_params] ':' b=expression {
_PyAST_Lambda((a) ? a : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), b, EXTRA) }
+lambarrowdef[expr_ty]:
+ | '(' a=[params] ')' '=>' b=expression {
+ _PyAST_Lambda((a) ? a : CHECK(arguments_ty, _PyPegen_empty_arguments(p)), b, EXTRA) }
lambda_params[arguments_ty]:
| invalid_lambda_parameters
@angelhdzdev
Copy link

Thank you very much! Nice work. 🔥🤝

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment