Generate a parser c extension for the given grammar in the given path Returns a module object with a parse_string() method. TODO: express that using a Protocol.
(
grammar: Grammar,
path: pathlib.PurePath,
debug: bool = False,
library_dir: str | None = None,
)
| 83 | |
| 84 | |
| 85 | def generate_parser_c_extension( |
| 86 | grammar: Grammar, |
| 87 | path: pathlib.PurePath, |
| 88 | debug: bool = False, |
| 89 | library_dir: str | None = None, |
| 90 | ) -> Any: |
| 91 | """Generate a parser c extension for the given grammar in the given path |
| 92 | |
| 93 | Returns a module object with a parse_string() method. |
| 94 | TODO: express that using a Protocol. |
| 95 | """ |
| 96 | # Make sure that the working directory is empty: reusing non-empty temporary |
| 97 | # directories when generating extensions can lead to segmentation faults. |
| 98 | # Check issue #95 (https://github.com/gvanrossum/pegen/issues/95) for more |
| 99 | # context. |
| 100 | assert not os.listdir(path) |
| 101 | source = path / "parse.c" |
| 102 | with open(source, "w", encoding="utf-8") as file: |
| 103 | genr = CParserGenerator( |
| 104 | grammar, ALL_TOKENS, EXACT_TOKENS, NON_EXACT_TOKENS, file, debug=debug |
| 105 | ) |
| 106 | genr.generate("parse.c") |
| 107 | compile_c_extension( |
| 108 | str(source), |
| 109 | build_dir=str(path), |
| 110 | # Significant test_peg_generator speedups |
| 111 | disable_optimization=True, |
| 112 | library_dir=library_dir, |
| 113 | ) |
| 114 | |
| 115 | |
| 116 | def print_memstats() -> bool: |
no test coverage detected
searching dependent graphs…