Skip to content

Commit

Permalink
Use embedded syntax files if no --syntax option has been provided.
Browse files Browse the repository at this point in the history
This allows one to run re2c/re2go/re2rust without --syntax option and
get the same behaviour as before.
  • Loading branch information
skvadrik committed Nov 18, 2023
1 parent aeb4f7a commit 8a8c031
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
59 changes: 41 additions & 18 deletions src/codegen/syntax.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<size_t>(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<size_t>(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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/codegen/syntax.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<uint32_t>(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_
2 changes: 1 addition & 1 deletion src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 8a8c031

Please sign in to comment.