AST_Module* parse_file(const char* fn) { STAT_TIMER(t0, "us_timer_cpyton_parsing"); Timer _t("parsing"); if (ENABLE_PYPA_PARSER) { AST_Module* rtn = pypa_parse(fn); assert(rtn); return rtn; } FILE* fp = popen(getParserCommandLine(fn).c_str(), "r"); BufferedReader* reader = new BufferedReader(fp); AST* rtn = readASTMisc(reader); reader->fill(); ASSERT(reader->bytesBuffered() == 0, "%d", reader->bytesBuffered()); delete reader; int code = pclose(fp); assert(code == 0); assert(rtn->type == AST_TYPE::Module); long us = _t.end(); static StatCounter us_parsing("us_parsing"); us_parsing.log(us); return ast_cast<AST_Module>(rtn); }
static void _reparse(const char* fn, const std::string& cache_fn) { FILE* parser = popen(getParserCommandLine(fn).c_str(), "r"); FILE* cache_fp = fopen(cache_fn.c_str(), "w"); assert(cache_fp); fwrite(MAGIC_STRING, 1, MAGIC_STRING_LENGTH, cache_fp); int checksum_start = ftell(cache_fp); int bytes_written = -1; // Currently just use the length as the checksum static_assert(sizeof(bytes_written) >= CHECKSUM_LENGTH, ""); fwrite(&bytes_written, 1, CHECKSUM_LENGTH, cache_fp); bytes_written = 0; char buf[80]; while (true) { int nread = fread(buf, 1, 80, parser); if (nread == 0) break; bytes_written += nread; fwrite(buf, 1, nread, cache_fp); } int code = pclose(parser); assert(code == 0); fseek(cache_fp, checksum_start, SEEK_SET); fwrite(&bytes_written, 1, CHECKSUM_LENGTH, cache_fp); fclose(cache_fp); }
static ParseResult _reparse(const char* fn, const std::string& cache_fn, AST_Module*& module) { FILE* cache_fp = fopen(cache_fn.c_str(), "w"); if (!cache_fp) return ParseResult::PYC_UNWRITABLE; fwrite(getMagic(), 1, MAGIC_STRING_LENGTH, cache_fp); int checksum_start = ftell(cache_fp); int bytes_written = -1; // Currently just use the length as the checksum static_assert(sizeof(bytes_written) >= CHECKSUM_LENGTH, ""); fwrite(&bytes_written, 1, CHECKSUM_LENGTH, cache_fp); bytes_written = 0; if (ENABLE_PYPA_PARSER) { module = pypa_parse(fn); if (!module) return ParseResult::FAILURE; bytes_written += serializeAST(module, cache_fp); } else { FILE* parser = popen(getParserCommandLine(fn).c_str(), "r"); char buf[80]; while (true) { int nread = fread(buf, 1, 80, parser); if (nread == 0) break; bytes_written += nread; fwrite(buf, 1, nread, cache_fp); } int code = pclose(parser); assert(code == 0); } fseek(cache_fp, checksum_start, SEEK_SET); fwrite(&bytes_written, 1, CHECKSUM_LENGTH, cache_fp); fclose(cache_fp); return ParseResult::SUCCESS; }