Skip to content

Commit

Permalink
Split syntax configurations code:switch_cases and `code:switch_case…
Browse files Browse the repository at this point in the history
…s_oneline`.

This is so that syntax files for languages that don't need special
handling for oneline statements (like Go) do not have to handle
`oneline` conditional (which may be confusing and unexpected).
  • Loading branch information
skvadrik committed Nov 26, 2023
1 parent 9a0fdfa commit af41fd9
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 43 deletions.
12 changes: 7 additions & 5 deletions bootstrap/src/default_syntax_c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ const char* DEFAULT_SYNTAX_C =
" indent [case: case] dedent\n"
" topindent \"}\" nl;\n"
"\n"
"code:switch_cases = (oneline\n"
" ? [case{0:-2}: case nl]\n"
" [case{-1}: case \" \" [stmt: stmt]]\n"
" : [case: case nl]\n"
" indent [stmt: stmt] dedent);\n"
"code:switch_cases =\n"
" [case: case nl]\n"
" indent [stmt: stmt] dedent;\n"
"\n"
"code:switch_cases_oneline =\n"
" [case{0:-2}: case nl]\n"
" [case{-1}: case \" \" [stmt: stmt]];\n"
"\n"
"code:switch_case_range = (case_ranges\n"
" ? topindent \"case \" [val{0}: val] (multival ? \" ... \" [val{-1}: val]) \":\"\n"
Expand Down
3 changes: 1 addition & 2 deletions bootstrap/src/default_syntax_go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ const char* DEFAULT_SYNTAX_GO =
" [case{0:-2}: topindent case nl\n"
" indent topindent \"fallthrough\" dedent nl]\n"
" [case{-1}: topindent case nl]\n"
" // `topindent` is needed in `oneline` case because re2c renders it in a special way\n"
" indent (oneline ? topindent) [stmt: stmt] dedent;\n"
" indent [stmt: stmt] dedent;\n"
"\n"
"code:switch_case_range = \"case \" [val{0}: val] [val{1:-1}: \",\" val] \":\";\n"
"\n"
Expand Down
13 changes: 8 additions & 5 deletions bootstrap/src/default_syntax_rust.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ const char* DEFAULT_SYNTAX_RUST =
" topindent \"}\" nl;\n"
"\n"
"code:switch_cases =\n"
" [case{0:-2}: topindent case \" |\" nl] (oneline\n"
" ? [case{-1}: topindent case \" => \" [stmt: stmt]]\n"
" : [case{-1}: topindent case \" => {\" nl\n"
" indent [stmt: stmt] dedent\n"
" topindent \"}\" nl]);\n"
" [case{0:-2}: topindent case \" |\" nl]\n"
" [case{-1}: topindent case \" => {\" nl\n"
" indent [stmt: stmt] dedent\n"
" topindent \"}\" nl];\n"
"\n"
"code:switch_cases_oneline =\n"
" [case{0:-2}: topindent case \" |\" nl]\n"
" [case{-1}: topindent case \" => \" [stmt: stmt]];\n"
"\n"
"code:switch_case_range = [val{0}: val] (multival ? \" ..= \" [val{-1}: val]);\n"
"\n"
Expand Down
12 changes: 7 additions & 5 deletions include/syntax/c
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ code:switch =
indent [case: case] dedent
topindent "}" nl;

code:switch_cases = (oneline
? [case{0:-2}: case nl]
[case{-1}: case " " [stmt: stmt]]
: [case: case nl]
indent [stmt: stmt] dedent);
code:switch_cases =
[case: case nl]
indent [stmt: stmt] dedent;

code:switch_cases_oneline =
[case{0:-2}: case nl]
[case{-1}: case " " [stmt: stmt]];

code:switch_case_range = (case_ranges
? topindent "case " [val{0}: val] (multival ? " ... " [val{-1}: val]) ":"
Expand Down
3 changes: 1 addition & 2 deletions include/syntax/go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ code:switch_cases =
[case{0:-2}: topindent case nl
indent topindent "fallthrough" dedent nl]
[case{-1}: topindent case nl]
// `topindent` is needed in `oneline` case because re2c renders it in a special way
indent (oneline ? topindent) [stmt: stmt] dedent;
indent [stmt: stmt] dedent;

code:switch_case_range = "case " [val{0}: val] [val{1:-1}: "," val] ":";

Expand Down
13 changes: 8 additions & 5 deletions include/syntax/rust
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ code:switch =
topindent "}" nl;

code:switch_cases =
[case{0:-2}: topindent case " |" nl] (oneline
? [case{-1}: topindent case " => " [stmt: stmt]]
: [case{-1}: topindent case " => {" nl
indent [stmt: stmt] dedent
topindent "}" nl]);
[case{0:-2}: topindent case " |" nl]
[case{-1}: topindent case " => {" nl
indent [stmt: stmt] dedent
topindent "}" nl];

code:switch_cases_oneline =
[case{0:-2}: topindent case " |" nl]
[case{-1}: topindent case " => " [stmt: stmt]];

code:switch_case_range = [val{0}: val] (multival ? " ..= " [val{-1}: val]);

Expand Down
36 changes: 18 additions & 18 deletions src/codegen/pass4_render.cc
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ class RenderSwitchCaseBlock : public OutputCallback {
bool oneline;

public:
RenderSwitchCaseBlock(RenderContext& rctx, const CodeCase* code)
RenderSwitchCaseBlock(RenderContext& rctx, const CodeCase* code, bool oneline)
: rctx(rctx)
, code(code)
, curr_stmt(nullptr)
Expand All @@ -345,12 +345,8 @@ class RenderSwitchCaseBlock : public OutputCallback {
, curr_range(0)
, last_range(0)
, nranges(code->kind == CodeCase::Kind::RANGES ? code->ranges->size : 1)
, oneline(false) {
const Code* head = code->body->head;
for (const Code* s = head; s; s = s->next) ++nstmt;
oneline = head != nullptr
&& head->next == nullptr
&& (head->kind == CodeKind::STMT || head->kind == CodeKind::TEXT);
, oneline(oneline) {
for (const Code* s = code->body->head; s; s = s->next) ++nstmt;
}

void render_var(const char* var) override {
Expand Down Expand Up @@ -416,14 +412,6 @@ class RenderSwitchCaseBlock : public OutputCallback {
return false;
}

bool eval_cond(const char* var) {
if (strcmp(var, "oneline") == 0) {
return oneline;
}
UNREACHABLE();
return false;
}

FORBID_COPY(RenderSwitchCaseBlock);
};

Expand All @@ -433,19 +421,31 @@ class RenderSwitch : public OutputCallback {
const CodeCase* curr_case;
const CodeCase* last_case;
size_t ncases;
const bool specialize_oneline;

public:
RenderSwitch(RenderContext& rctx, const CodeSwitch* code)
: rctx(rctx), code(code), curr_case(nullptr), last_case(nullptr), ncases(0) {
: rctx(rctx)
, code(code)
, curr_case(nullptr)
, last_case(nullptr)
, ncases(0)
, specialize_oneline(rctx.stx.have_conf("code:switch_cases_oneline")) {
for (const CodeCase* c = code->cases->head; c; c = c->next) ++ncases;
}

void render_var(const char* var) override {
if (strcmp(var, "expr") == 0) {
rctx.os << code->expr;
} else if (strcmp(var, "case") == 0) {
RenderSwitchCaseBlock callback(rctx, curr_case);
rctx.stx.gen_code(rctx.os, rctx.opts, "code:switch_cases", callback);
const Code* s = curr_case->body->head;
bool oneline = specialize_oneline
&& s != nullptr
&& s->next == nullptr
&& (s->kind == CodeKind::STMT || s->kind == CodeKind::TEXT);
const char* conf = oneline ? "code:switch_cases_oneline" : "code:switch_cases";
RenderSwitchCaseBlock callback(rctx, curr_case, oneline);
rctx.stx.gen_code(rctx.os, rctx.opts, conf, callback);
} else {
render_global_var(rctx, var);
}
Expand Down
9 changes: 8 additions & 1 deletion src/codegen/syntax.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ Stx::Stx(OutAllocator& alc)
{"expr"}, {"case"}, {}
};
allowed_code_confs["code:switch_cases"] = {
{}, {"case", "stmt"}, {"oneline"}
{}, {"case", "stmt"}, {}
};
allowed_code_confs["code:switch_cases_oneline"] = {
{}, {"case", "stmt"}, {}
};
allowed_code_confs["code:switch_case_range"] = {
{}, {"val"}, {"multival"}
Expand Down Expand Up @@ -257,6 +260,10 @@ Ret Stx::validate_conf_code(const StxConf* conf) {
return Ret::OK;
}

bool Stx::have_conf(const char* name) const {
return confs.find(name) != confs.end();
}

void Stx::push_list_on_stack(const StxCode* x) {
if (x == nullptr) return;
push_list_on_stack(x->next);
Expand Down
1 change: 1 addition & 0 deletions src/codegen/syntax.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class Stx {
Ret validate_conf_expr(const StxConf* conf);
Ret validate_conf_code(const StxConf* conf);

bool have_conf(const char* name) const;
void gen_code(std::ostream& os, const opt_t* opts, const char* name, OutputCallback& callback);
};

Expand Down

0 comments on commit af41fd9

Please sign in to comment.