Parser done implementing
This commit is contained in:
parent
828096de17
commit
fdb861b299
4 changed files with 31 additions and 42 deletions
|
|
@ -555,17 +555,13 @@ void obj::BCgen::deinit() {}
|
|||
|
||||
bool obj::BCgen::Compile(obj::MethodObject* method) {
|
||||
|
||||
Parser sParger;
|
||||
Parser parser;
|
||||
|
||||
auto script = method->mScript->mReadable->val;
|
||||
auto res = sParger.parse(script);
|
||||
auto res = parser.parse(script);
|
||||
|
||||
if (res.err) {
|
||||
if (res.isError) {
|
||||
// TODO : print parse error
|
||||
auto loc = res.err->getErrorLocation(script.read());
|
||||
printf("Parser Error (%i,%i): %s\n", loc.head, loc.tail, res.err->mDescription.read());
|
||||
// tp::gLogeer->write(tp::sfmt("Parser Error (%i,%i): \n", loc.head, loc.tail), true, tp::Logger::LogEntry::ERR);
|
||||
// tp::GLog->write(res.err->mDescr, true, tp::Logger::LogEntry::ERR);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,9 +6,26 @@
|
|||
|
||||
using namespace obj;
|
||||
|
||||
lalr::ErrorPolicy::~ErrorPolicy() = default;
|
||||
void lalr::ErrorPolicy::lalr_error(int line, int column, int error, const char* format, va_list args) {}
|
||||
void lalr::ErrorPolicy::lalr_vprintf(const char* format, va_list args){};
|
||||
|
||||
class CustomErrorPolicy : public lalr::ErrorPolicy {
|
||||
public:
|
||||
virtual ~CustomErrorPolicy() override {}
|
||||
|
||||
virtual void lalr_error(int line, int column, int error, const char* format, va_list args) override {
|
||||
printf("Parser Error: %i %i \n", line, column);
|
||||
vfprintf(stdout, format, args);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
virtual void lalr_vprintf(const char* format, va_list args) override { printf(format, args); }
|
||||
};
|
||||
|
||||
Parser::Result Parser::parse(const tp::String& stream) {
|
||||
|
||||
lalr::ErrorPolicy errorPolicy;
|
||||
CustomErrorPolicy errorPolicy;
|
||||
|
||||
LalrParser parser(oscript_parser_state_machine, &errorPolicy);
|
||||
|
||||
|
|
@ -16,14 +33,10 @@ Parser::Result Parser::parse(const tp::String& stream) {
|
|||
|
||||
std::string streamStd(stream.read());
|
||||
|
||||
LALR_ASSERT(parser.valid());
|
||||
ASSERT(parser.valid());
|
||||
|
||||
parser.parse(streamStd.begin(), streamStd.end());
|
||||
|
||||
parser.accepted();
|
||||
parser.full();
|
||||
|
||||
auto scope = (BCgen::StatementScope*) parser.user_data();
|
||||
|
||||
return { nullptr, scope };
|
||||
return { scope, parser.accepted() && parser.full() };
|
||||
}
|
||||
|
|
@ -9,35 +9,12 @@ namespace obj {
|
|||
public:
|
||||
Parser() = default;
|
||||
|
||||
struct Error {
|
||||
tp::String mDescription = "No Description";
|
||||
tp::alni mAdvancedIdx = 0;
|
||||
|
||||
Error() = default;
|
||||
Error(const tp::String& description, tp::alni idx) {
|
||||
mDescription = description;
|
||||
mAdvancedIdx = idx;
|
||||
}
|
||||
|
||||
tp::Pair<tp::alni, tp::alni> getErrorLocation(const char* stream) const {
|
||||
tp::alni line = 1;
|
||||
tp::alni column = 1;
|
||||
|
||||
for (auto i : tp::Range(mAdvancedIdx)) {
|
||||
if (stream[i] == '\n') {
|
||||
column = 0;
|
||||
line++;
|
||||
}
|
||||
column++;
|
||||
}
|
||||
|
||||
return { line, column };
|
||||
}
|
||||
};
|
||||
|
||||
struct Result {
|
||||
Error* err = nullptr;
|
||||
obj::BCgen::StatementScope* scope = nullptr;
|
||||
bool isError = false;
|
||||
tp::String description;
|
||||
tp::halni line = 0;
|
||||
tp::halni column = 0;
|
||||
};
|
||||
|
||||
Result parse(const tp::String& stream);
|
||||
|
|
|
|||
|
|
@ -11,6 +11,9 @@ TEST_DEF_STATIC(Basic) {
|
|||
|
||||
String stream = "var i = true; print (i + 1) * 10;";
|
||||
auto res = parser.parse(stream);
|
||||
|
||||
TEST(!res.isError);
|
||||
|
||||
delete res.scope;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue