summaryrefslogtreecommitdiffstats
path: root/Parser/pegen.c
diff options
context:
space:
mode:
Diffstat (limited to 'Parser/pegen.c')
-rw-r--r--Parser/pegen.c57
1 files changed, 56 insertions, 1 deletions
diff --git a/Parser/pegen.c b/Parser/pegen.c
index ed989f5..4d6e69e 100644
--- a/Parser/pegen.c
+++ b/Parser/pegen.c
@@ -1,5 +1,5 @@
#include <Python.h>
-#include "pycore_ast.h" // _PyAST_Validate()
+#include "pycore_ast.h" // _PyAST_Validate(),
#include <errcode.h>
#include "tokenizer.h"
@@ -1818,6 +1818,51 @@ _PyPegen_get_values(Parser *p, asdl_seq *seq)
return new_seq;
}
+/* Constructs a KeyPatternPair that is used when parsing mapping & class patterns */
+KeyPatternPair *
+_PyPegen_key_pattern_pair(Parser *p, expr_ty key, pattern_ty pattern)
+{
+ KeyPatternPair *a = _PyArena_Malloc(p->arena, sizeof(KeyPatternPair));
+ if (!a) {
+ return NULL;
+ }
+ a->key = key;
+ a->pattern = pattern;
+ return a;
+}
+
+/* Extracts all keys from an asdl_seq* of KeyPatternPair*'s */
+asdl_expr_seq *
+_PyPegen_get_pattern_keys(Parser *p, asdl_seq *seq)
+{
+ Py_ssize_t len = asdl_seq_LEN(seq);
+ asdl_expr_seq *new_seq = _Py_asdl_expr_seq_new(len, p->arena);
+ if (!new_seq) {
+ return NULL;
+ }
+ for (Py_ssize_t i = 0; i < len; i++) {
+ KeyPatternPair *pair = asdl_seq_GET_UNTYPED(seq, i);
+ asdl_seq_SET(new_seq, i, pair->key);
+ }
+ return new_seq;
+}
+
+/* Extracts all patterns from an asdl_seq* of KeyPatternPair*'s */
+asdl_pattern_seq *
+_PyPegen_get_patterns(Parser *p, asdl_seq *seq)
+{
+ Py_ssize_t len = asdl_seq_LEN(seq);
+ asdl_pattern_seq *new_seq = _Py_asdl_pattern_seq_new(len, p->arena);
+ if (!new_seq) {
+ return NULL;
+ }
+ for (Py_ssize_t i = 0; i < len; i++) {
+ KeyPatternPair *pair = asdl_seq_GET_UNTYPED(seq, i);
+ asdl_seq_SET(new_seq, i, pair->pattern);
+ }
+ return new_seq;
+}
+
/* Constructs a NameDefaultPair */
NameDefaultPair *
_PyPegen_name_default_pair(Parser *p, arg_ty arg, expr_ty value, Token *tc)
@@ -2303,6 +2348,16 @@ error:
return NULL;
}
+expr_ty
+_PyPegen_ensure_imaginary(Parser *p, expr_ty exp)
+{
+ if (exp->kind != Constant_kind || !PyComplex_CheckExact(exp->v.Constant.value)) {
+ RAISE_SYNTAX_ERROR_KNOWN_LOCATION(exp, "Imaginary number required in complex literal");
+ return NULL;
+ }
+ return exp;
+}
+
mod_ty
_PyPegen_make_module(Parser *p, asdl_stmt_seq *a) {
asdl_type_ignore_seq *type_ignores = NULL;