"pegen.py python" should validate the output file.
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 77.2k
- Forks
- 36k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
I ran pegen python -o grammar_parser.py after making an incorrect change to metagrammar.gram. This was invoked by make regen-pegen-metaparser, or build.bat --regen on Windows.
The resulting output file was syntactically correct, but it could not correctly parse the metagrammar file. So when I ran the make again later, it failed. The call to GeneratedParser.start() raised an exception.
The previous (correctly working) version of grammar_parser.py is now lost, unless I could get it back from Git.
Desired behavior
pegen must be sure that the newly generated output file will work correctly, before writing it out to the filesystem.
Generation of the output file begins in pegen/build.py,
def build_python_generator(
grammar: Grammar,
grammar_file: str,
output_file: str,
skip_actions: bool = False,
) -> ParserGenerator:
with open(output_file, "w") as file:
gen: ParserGenerator = PythonParserGenerator(grammar, file) # TODO: skip_actions
gen.generate(grammar_file)
return gen
This creates a new output file. Even if the body of the with raises an exception, the output file is closed and remains in the filesystem, overwriting the previous version.
Instead,
- Use a
StringIOas the file, instead of a realopen()file. Call itresult. - Generate the file, which fills in
result. - Validate
result(see more below). - If validation succeeds, then write the
resultto a newopen(output_file, "w"). - Otherwise, do nothing more. The file designated by
output_filewill still be intact.
Validating the output file
To validate the output file result, go through the entire process of generating result, except using
result instead of the grammar_parser module. This process is the entire code of build_python_parser_and_generator, not just the build_python_generator function.
grammar_parser is used to import the class GeneratedParser. For validation purposes, exec(result) with an empty globals/locals dict, then use the ['GeneratedParser'] item. Note, in build.py, this class is imported as the name GrammarParser.
This process should generate a new_result StringIO, and it should be identical to result. Perhaps there may be some insignificant differences between the two, and if so, they shouldn't be compared.
If the generation of new_result raises an exception, then the validation has failed.
Linked PRs
- gh-101937
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Empieza en pegen/build.py, especialmente en build_python_generator y build_python_parser_and_generator. Sigue cómo se produce y valida la salida del parser generado; después, conserva el archivo de salida existente cuando falle la validación y confirma que la salida correcta se escriba solo después de que la validación se haya superado. Revisa el PR enlazado gh-101937 para consultar el trabajo que ya está en curso.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- build-system, compilers
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bien especificado
- Aptitud para principiantes
- 32/100