diff --git a/src/codegen/syntax.cc b/src/codegen/syntax.cc index cb90199a2..77641b85d 100644 --- a/src/codegen/syntax.cc +++ b/src/codegen/syntax.cc @@ -4,6 +4,10 @@ #include "src/codegen/output.h" #include "src/codegen/syntax.h" +extern const char* DEFAULT_SYNTAX_C; +extern const char* DEFAULT_SYNTAX_GO; +extern const char* DEFAULT_SYNTAX_RUST; + namespace re2c { Stx::Stx(OutAllocator& alc) @@ -334,33 +338,52 @@ StxFile::~StxFile() { if (file) fclose(file); } -Ret StxFile::read() { +Ret StxFile::read(Lang lang) { msg.filenames.push_back(fname); - file = fopen(fname.c_str(), "rb"); - if (!file) RET_FAIL(error("cannot open syntax file '%s'", fname.c_str())); - - // get file size - fseek(file, 0, SEEK_END); - flen = static_cast(ftell(file)); - fseek(file, 0, SEEK_SET); - - // allocate buffer - buf = new uint8_t[flen + 1]; - - // read file contents into buffer and append terminating zero at the end - if (fread(buf, 1, flen, file) != flen) { - RET_FAIL(error("cannot read syntax file '%s'", fname.c_str())); + if (fname.empty()) { + // use the default syntax config that is provided as a string + const char* src = nullptr; + switch (lang) { + case Lang::C: src = DEFAULT_SYNTAX_C; break; + case Lang::GO: src = DEFAULT_SYNTAX_GO; break; + case Lang::RUST: src = DEFAULT_SYNTAX_RUST; break; + } + flen = strlen(src); + + // allocate buffer + buf = new uint8_t[flen + 1]; + + // fill in buffer from the config string + memcpy(buf, src, flen); + buf[flen] = 0; + } else { + // use the provided syntax file + file = fopen(fname.c_str(), "rb"); + if (!file) RET_FAIL(error("cannot open syntax file '%s'", fname.c_str())); + + // get file size + fseek(file, 0, SEEK_END); + flen = static_cast(ftell(file)); + fseek(file, 0, SEEK_SET); + + // allocate buffer + buf = new uint8_t[flen + 1]; + + // read file contents into buffer and append terminating zero at the end + if (fread(buf, 1, flen, file) != flen) { + RET_FAIL(error("cannot read syntax file '%s'", fname.c_str())); + } + buf[flen] = 0; } - buf[flen] = 0; cur = tok = pos = buf; return Ret::OK; } -Ret load_syntax_config(const std::string& fname, Msg& msg, OutAllocator& alc, Stx& stx) { +Ret load_syntax_config(const std::string& fname, Msg& msg, OutAllocator& alc, Stx& stx, Lang lang) { StxFile sf(fname, msg, alc); - CHECK_RET(sf.read()); + CHECK_RET(sf.read(lang)); CHECK_RET(sf.parse(stx)); return Ret::OK; } diff --git a/src/codegen/syntax.h b/src/codegen/syntax.h index 1f4d16af0..b51cdcd93 100644 --- a/src/codegen/syntax.h +++ b/src/codegen/syntax.h @@ -171,7 +171,7 @@ class StxFile { public: StxFile(const std::string& fname, Msg& msg, OutAllocator& alc); ~StxFile(); - Ret read(); + Ret read(Lang lang); Ret parse(Stx& stx); int lex_token(YYSTYPE* yylval); bool check_conf_name(const char* name) const; @@ -277,13 +277,13 @@ inline StxCode* Stx::make_code_list( return x; } -Ret load_syntax_config(const std::string& fname, Msg& msg, OutAllocator& alc, Stx& stx); - loc_t StxFile::tok_loc() const { DCHECK(pos <= tok); return {loc.line, static_cast(tok - pos), loc.file}; } +Ret load_syntax_config(const std::string& fname, Msg& msg, OutAllocator& alc, Stx& stx, Lang lang); + } // namespace re2c #endif // _RE2C_CODEGEN_SYNTAX_ diff --git a/src/main.cc b/src/main.cc index 53785320e..632048539 100644 --- a/src/main.cc +++ b/src/main.cc @@ -146,7 +146,7 @@ LOCAL_NODISCARD(Ret compile(int, char* argv[])) { // Load syntax file before opening source files, as it must have file index 0. Stx stx(out_alc); - CHECK_RET(load_syntax_config(globopts.syntax_file, msg, out_alc, stx)); + CHECK_RET(load_syntax_config(globopts.syntax_file, msg, out_alc, stx, globopts.lang)); Input input(&globopts, msg); CHECK_RET(input.open(globopts.source_file, nullptr));