Skip to content

Instantly share code, notes, and snippets.

@isidentical
Last active August 17, 2021 21:29
Show Gist options
  • Save isidentical/c326d31a93add1b228f9dafa44f5827d to your computer and use it in GitHub Desktop.
Save isidentical/c326d31a93add1b228f9dafa44f5827d to your computer and use it in GitHub Desktop.
expr_ty _PyPegen_produce_string(Parser *p, expr_ty a) {
int start = -1, end = -1, length = 0, seen_walrus = 0, mark;
for (mark = p->mark; start == -1; --mark) {
Token* token = p->tokens[mark];
if (token->end_lineno == a->end_lineno
&& token->end_col_offset == a->end_col_offset)
{
end = mark;
}
if (token->lineno == a->lineno
&& token->col_offset == a->col_offset)
{
start = mark;
}
if (end != -1) {
length = length + PyBytes_GET_SIZE(token->bytes);
if (IS_KEYWORD(token) || token->type == NUMBER) {
length = length + 2;
} else if (token->type == COLONEQUAL) {
seen_walrus = 1;
}
}
}
assert(start != -1 || end != -1);
char* buffer = PyMem_Calloc(length + seen_walrus * 2, sizeof(char*));
for (mark = start; mark <= end; ++mark) {
int is_start = mark == start;
Token* token = p->tokens[mark];
char *value = PyBytes_AS_STRING(token->bytes);
if (IS_KEYWORD(token) || token->type == NUMBER) {
const char *format;
char new_value[strlen(value) + 2];
if (is_start) {
format = "%s ";
} else {
format = " %s ";
}
value = PyMem_Malloc(sprintf(new_value, format, value) + 1);
strcpy(value, new_value);
}
if (mark == start) {
if (seen_walrus) {
strcpy(buffer, "(");
strcat(buffer, value);
} else {
strcpy(buffer, value);
}
} else {
strcat(buffer, value);
}
}
if (seen_walrus) {
strcat(buffer, ")");
}
PyObject *res = PyUnicode_FromString(buffer);
PyMem_Free(buffer);
if (res == NULL) {
return NULL;
}
if (PyArena_AddPyObject(p->arena, res) < 0) {
Py_DECREF(res);
return NULL;
}
return Constant(res, NULL, a->lineno, a->col_offset, a->end_lineno, a->end_col_offset,
p->arena);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment