forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expose a C API for pegen #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7e13f96
WIP: Start the work towards exposing pegen as a C-API
pablogsal 1d695fa
Expose pegen both as a C API and as a Python-level module:
lysnikolaou 3d20a52
Remake of the design of the PyPegen C API and how it is generated:
lysnikolaou 836fb07
Don't rely on the Pegen C API just yet
lysnikolaou a5bb3a2
Add mode parameter to run_parser* functions again, so that we can gen…
lysnikolaou 0a409bf
Merge branch 'pegen' into c_api_lys
lysnikolaou b53b244
Minor bug fixes and black formatting
lysnikolaou 0004dec
Fix unintended change in pythonrun.c
lysnikolaou 95be5d0
Respond to review comments
lysnikolaou 46e774d
Merge branch 'pegen' into c_api_lys
lysnikolaou 68e2245
Fix nits and remove arena allocation from Parser_New
lysnikolaou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #ifndef Py_LIMITED_API | ||
| #ifndef Py_PEGENINTERFACE | ||
| #define Py_PEGENINTERFACE | ||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| #include "Python.h" | ||
| #include "Python-ast.h" | ||
|
|
||
| PyAPI_FUNC(mod_ty) PyPegen_ASTFromFile(const char *filename, PyArena *arena); | ||
| PyAPI_FUNC(mod_ty) PyPegen_ASTFromString(const char *str, PyArena *arena); | ||
| PyAPI_FUNC(PyCodeObject *) PyPegen_CodeObjectFromFile(const char *filename, PyArena *arena); | ||
| PyAPI_FUNC(PyCodeObject *) PyPegen_CodeObjectFromString(const char *str, PyArena *arena); | ||
lysnikolaou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
| #endif /* !Py_PEGENINTERFACE*/ | ||
| #endif /* !Py_LIMITED_API */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #include <Python.h> | ||
| #include <pegen_interface.h> | ||
|
|
||
| PyObject * | ||
| _Py_parse_file(PyObject *self, PyObject *args) | ||
| { | ||
| char *filename; | ||
|
|
||
| if (!PyArg_ParseTuple(args, "s", &filename)) { | ||
| return NULL; | ||
| } | ||
|
|
||
| PyArena *arena = PyArena_New(); | ||
| if (arena == NULL) { | ||
| return NULL; | ||
| } | ||
|
|
||
| mod_ty res = PyPegen_ASTFromFile(filename, arena); | ||
| if (res == NULL) { | ||
| PyArena_Free(arena); | ||
| return NULL; | ||
| } | ||
| PyObject *result = PyAST_mod2obj(res); | ||
|
|
||
| PyArena_Free(arena); | ||
| return result; | ||
| } | ||
|
|
||
| PyObject * | ||
| _Py_parse_string(PyObject *self, PyObject *args) | ||
| { | ||
| char *the_string; | ||
|
|
||
| if (!PyArg_ParseTuple(args, "s", &the_string)) { | ||
| return NULL; | ||
| } | ||
|
|
||
| PyArena *arena = PyArena_New(); | ||
| if (arena == NULL) { | ||
| return NULL; | ||
| } | ||
|
|
||
| mod_ty res = PyPegen_ASTFromString(the_string, arena); | ||
| if (res == NULL) { | ||
| PyArena_Free(arena); | ||
| return NULL; | ||
| } | ||
| PyObject *result = PyAST_mod2obj(res); | ||
|
|
||
| PyArena_Free(arena); | ||
| return result; | ||
| } | ||
|
|
||
| static PyMethodDef ParseMethods[] = { | ||
| {"parse_file", (PyCFunction)(void(*)(void))_Py_parse_file, METH_VARARGS, "Parse a file."}, | ||
| {"parse_string", (PyCFunction)(void(*)(void))_Py_parse_string, METH_VARARGS, "Parse a string."}, | ||
| {NULL, NULL, 0, NULL} /* Sentinel */ | ||
| }; | ||
|
|
||
| static struct PyModuleDef parsemodule = { | ||
| PyModuleDef_HEAD_INIT, | ||
| .m_name = "peg_parser", | ||
| .m_doc = "A parser.", | ||
| .m_methods = ParseMethods, | ||
| }; | ||
|
|
||
| PyMODINIT_FUNC | ||
| PyInit_peg_parser(void) | ||
| { | ||
| return PyModule_Create(&parsemodule); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Modules/peg_parser/parse_string.c → Parser/pegen/parse_string.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #include <pegen_interface.h> | ||
|
|
||
| #include "../tokenizer.h" | ||
| #include "pegen.h" | ||
|
|
||
| mod_ty | ||
| PyPegen_ASTFromString(const char *str, PyArena *arena) | ||
| { | ||
| return run_parser_from_string(str, parse_start, RAW_AST_OBJECT, arena); | ||
lysnikolaou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| mod_ty | ||
| PyPegen_ASTFromFile(const char *filename, PyArena *arena) | ||
| { | ||
| return run_parser_from_file(filename, parse_start, RAW_AST_OBJECT, arena); | ||
| } | ||
|
|
||
| PyCodeObject * | ||
| PyPegen_CodeObjectFromString(const char *str, PyArena *arena) | ||
| { | ||
| return run_parser_from_string(str, parse_start, CODE_OBJECT, arena); | ||
| } | ||
|
|
||
| PyCodeObject * | ||
| PyPegen_CodeObjectFromFile(const char *file, PyArena *arena) | ||
| { | ||
| return run_parser_from_file(file, parse_start, CODE_OBJECT, arena); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.