Commit 93243be1 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #828 from kmod/parsing

Have pypa parse strings directly
parents 3796b9f7 b50324db
...@@ -1013,6 +1013,12 @@ static std::string getParserCommandLine(const char* fn) { ...@@ -1013,6 +1013,12 @@ static std::string getParserCommandLine(const char* fn) {
} }
AST_Module* parse_string(const char* code) { AST_Module* parse_string(const char* code) {
if (ENABLE_PYPA_PARSER) {
AST_Module* rtn = pypa_parse_string(code);
RELEASE_ASSERT(rtn, "unknown parse error (possibly: '%s'?)", strerror(errno));
return rtn;
}
int size = strlen(code); int size = strlen(code);
char buf[] = "pystontmp_XXXXXX"; char buf[] = "pystontmp_XXXXXX";
char* tmpdir = mkdtemp(buf); char* tmpdir = mkdtemp(buf);
......
...@@ -36,6 +36,10 @@ ...@@ -36,6 +36,10 @@
#include "runtime/objmodel.h" #include "runtime/objmodel.h"
#include "runtime/types.h" #include "runtime/types.h"
/* For cStringIO: */
#include "Python.h"
#include "cStringIO.h"
namespace pypa { namespace pypa {
bool string_to_double(String const& s, double& result); bool string_to_double(String const& s, double& result);
} }
...@@ -1009,66 +1013,37 @@ pypa::String pypaEscapeDecoder(const pypa::String& s, const pypa::String& encodi ...@@ -1009,66 +1013,37 @@ pypa::String pypaEscapeDecoder(const pypa::String& s, const pypa::String& encodi
} }
} }
class PystonSourceReader : public pypa::Reader { class PystonReader : public pypa::Reader {
public: public:
PystonSourceReader(); PystonReader();
~PystonSourceReader() override; ~PystonReader() override;
bool open_file(const std::string& file_path);
void close();
bool set_encoding(const std::string& coding) override; bool set_encoding(const std::string& coding) override;
std::string get_encoding() const { return encoding; }
std::string get_line() override; std::string get_line() override;
unsigned get_line_number() const override { return line_number; } unsigned get_line_number() const override { return line_number; }
std::string get_filename() const override { return file_path; }
virtual char next() = 0;
virtual PyObject* open_python_file() noexcept = 0;
bool eof() const override { return is_eof; } bool eof() const override { return is_eof; }
void set_eof() { is_eof = true; }
private: private:
char next();
std::string file_path;
bool is_eof; bool is_eof;
FILE* file;
unsigned line_number;
PyObject* readline; PyObject* readline;
std::string encoding; unsigned line_number;
}; };
PystonSourceReader::PystonSourceReader() : file(nullptr), readline(nullptr) { PystonReader::PystonReader() : is_eof(false), readline(nullptr), line_number(0) {
close();
} }
PystonSourceReader::~PystonSourceReader() { PystonReader::~PystonReader() {
close();
}
bool PystonSourceReader::open_file(const std::string& _file_path) {
file = fopen(_file_path.c_str(), "r");
if (!file)
return false;
file_path = _file_path;
is_eof = false;
line_number = 0;
readline = nullptr;
return true;
}
void PystonSourceReader::close() {
if (file)
fclose(file);
file = nullptr;
file_path.clear();
is_eof = true;
if (readline) if (readline)
gc::deregisterPermanentRoot(readline); gc::deregisterPermanentRoot(readline);
readline = nullptr; readline = nullptr;
line_number = 0;
} }
bool PystonSourceReader::set_encoding(const std::string& coding) { bool PystonReader::set_encoding(const std::string& coding) {
PyObject* stream = PyFile_FromFile(file, file_path.c_str(), "rb", NULL); PyObject* stream = open_python_file();
if (stream == NULL) if (stream == NULL)
return false; return false;
...@@ -1084,19 +1059,7 @@ bool PystonSourceReader::set_encoding(const std::string& coding) { ...@@ -1084,19 +1059,7 @@ bool PystonSourceReader::set_encoding(const std::string& coding) {
return true; return true;
} }
char PystonSourceReader::next() { std::string PystonReader::get_line() {
if (is_eof)
return 0;
int c = fgetc(file);
if (c == EOF) {
is_eof = true;
return 0;
}
return c;
}
std::string PystonSourceReader::get_line() {
if (eof()) if (eof())
return std::string(); return std::string();
...@@ -1134,11 +1097,74 @@ std::string PystonSourceReader::get_line() { ...@@ -1134,11 +1097,74 @@ std::string PystonSourceReader::get_line() {
return line->s(); return line->s();
} }
AST_Module* pypa_parse(char const* file_path) { class PystonFileReader : public PystonReader {
auto reader = llvm::make_unique<PystonSourceReader>(); public:
if (!reader->open_file(file_path)) PystonFileReader(FILE* file, std::string file_path);
~PystonFileReader() override;
PyObject* open_python_file() noexcept override;
std::string get_filename() const override { return file_path; }
static std::unique_ptr<PystonFileReader> create(const char* path);
private:
char next() override;
FILE* file;
std::string file_path;
};
PystonFileReader::PystonFileReader(FILE* file, std::string file_path) : file(file), file_path(std::move(file_path)) {
}
PystonFileReader::~PystonFileReader() {
if (file)
fclose(file);
file = nullptr;
file_path.clear();
set_eof();
}
std::unique_ptr<PystonFileReader> PystonFileReader::create(const char* path) {
FILE* f = fopen(path, "r");
if (!f)
return nullptr; return nullptr;
return llvm::make_unique<PystonFileReader>(f, path);
}
PyObject* PystonFileReader::open_python_file() noexcept {
return PyFile_FromFile(file, file_path.c_str(), "rb", NULL);
}
char PystonFileReader::next() {
if (eof())
return 0;
int c = fgetc(file);
if (c == EOF) {
set_eof();
return 0;
}
return c;
}
class PystonStringReader : public PystonReader {
public:
PystonStringReader(const char* str) : str(str), position(0) {}
~PystonStringReader() override {}
std::string get_filename() const override { return "<stdin>"; }
private:
char next() override;
PyObject* open_python_file() noexcept override;
const char* str;
int position;
};
static AST_Module* parse_with_reader(std::unique_ptr<pypa::Reader> reader) {
pypa::Lexer lexer(std::move(reader)); pypa::Lexer lexer(std::move(reader));
pypa::SymbolTablePtr symbols; pypa::SymbolTablePtr symbols;
pypa::AstModulePtr module; pypa::AstModulePtr module;
...@@ -1156,4 +1182,35 @@ AST_Module* pypa_parse(char const* file_path) { ...@@ -1156,4 +1182,35 @@ AST_Module* pypa_parse(char const* file_path) {
} }
return nullptr; return nullptr;
} }
char PystonStringReader::next() {
char c = str[position];
if (c)
position++;
else
set_eof();
return c;
}
PyObject* PystonStringReader::open_python_file() noexcept {
PycString_IMPORT;
PyObject* s = PyString_FromString(str + position);
if (!s)
return s;
return PycStringIO->NewInput(s);
}
AST_Module* pypa_parse(char const* file_path) {
auto reader = PystonFileReader::create(file_path);
if (!reader)
return nullptr;
return parse_with_reader(std::move(reader));
}
AST_Module* pypa_parse_string(char const* str) {
auto reader = llvm::make_unique<PystonStringReader>(str);
return parse_with_reader(std::move(reader));
}
} }
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
namespace pyston { namespace pyston {
class AST_Module; class AST_Module;
AST_Module* pypa_parse(char const* file_path); AST_Module* pypa_parse(char const* file_path);
AST_Module* pypa_parse_string(char const* str);
} }
#endif // PYSTON_CODEGEN_PYPAPARSER_H #endif // PYSTON_CODEGEN_PYPAPARSER_H
import os.path
s = open(os.path.join(os.path.dirname(__file__), "coding_cp1252.py"), 'r').read()
exec s
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment