Skip to content

Commit

Permalink
OCaml backend: render case ranges compactly for char type.
Browse files Browse the repository at this point in the history
OCaml supports character ranges like 'a'..'z', but it does not support
numeric ranges like 0..9, therefore previously all ranges were rendered
in expanded form, like 'a'|'b'|'c'|...|'z'. This results in sometimes
very long lines with a lot of alternatives in match expressions.

This commit fixes it by adding a conditional into syntax file
definition. It checks if the range is of char type. If so, then the
range is rendered compactly.
  • Loading branch information
skvadrik committed Mar 3, 2024
1 parent fa85dac commit be1cc4e
Show file tree
Hide file tree
Showing 17 changed files with 1,424 additions and 1,353 deletions.
11 changes: 8 additions & 3 deletions bootstrap/src/default_syntax_ocaml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,14 @@ const char* DEFAULT_SYNTAX_OCAML =
" [case{0:-2}: topindent \"| \" case nl]\n"
" [case{-1}: topindent \"| \" case \" -> \" [stmt: stmt] nl];\n"
"\n"
"// Note: we cannot use `\"c when \" [val{0}: val] \" <= c && c <= \" [val{-1}: val]`, as it causes\n"
"// `Error: Variable c must occur on both sides of this | pattern` when there are multiple cases.\n"
"code:switch_case_range = [val{0}: val] [val{1:-1}: \"|\" val];\n"
"// In OCaml only literals of type `char` support case ranges.\n"
"// For `int`, we have to exlicitly list all range values. We cannot use\n"
"// `\"c when \" [val{0}: val] \" <= c && c <= \" [val{-1}: val]`\n"
"// as it causes error when there are multiple cases:\n"
"// `Error: Variable c must occur on both sides of this | pattern`.\n"
"code:switch_case_range = (char_literals\n"
" ? [val{0}: val] (multival ? \"..\" [val{-1}: val])\n"
" : [val{0}: val] [val{1:-1}: \"|\" val]);\n"
"\n"
"code:switch_case_default = \"_\";\n"
"\n"
Expand Down
Loading

0 comments on commit be1cc4e

Please sign in to comment.