Skip to content

Instantly share code, notes, and snippets.

@jpcima
Last active May 4, 2020 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpcima/1f84262dced7b062d5181fc5f63b28c4 to your computer and use it in GitHub Desktop.
Save jpcima/1f84262dced7b062d5181fc5f63b28c4 to your computer and use it in GitHub Desktop.
/* -*- mode: c++; -*- */
#include "absl/strings/string_view.h"
#include "absl/strings/str_cat.h"
#include <string>
#include <cstdint>
enum OpcodeFixScope {
kOpcodeFixGeneric,
kOpcodeFixRegion,
};
static std::string fixOpcode(absl::string_view rawOpcode, OpcodeFixScope scope)
{
std::string opcode { rawOpcode };
/*!re2c
re2c:flags:posix-captures = 1;
re2c:define:YYCTYPE = char;
re2c:yyfill:enable = 0;
*/
/*!maxnmatch:re2c*/
const char* YYCURSOR;
const char* YYMARKER;
const char* yypmatch[2 * YYMAXNMATCH];
uint32_t yynmatch;
/*!stags:re2c format = "const char* @@; "; */
auto group = [&yypmatch](size_t i) -> absl::string_view {
const char *beg = yypmatch[2 * i];
const char *end = yypmatch[2 * i + 1];
return absl::string_view(beg, end - beg);
};
/*!re2c
digit = [0-9];
number = digit+;
END = [\x00];
*/
//--------------------------------------------------------------------------
YYCURSOR = opcode.c_str();
/*!re2c
(.*) '_cc' (number) END {
opcode = absl::StrCat(group(1), "_oncc", group(2));
goto end_generic;
}
* {
goto end_generic;
}
*/
end_generic:
/* end */;
//--------------------------------------------------------------------------
if (scope == kOpcodeFixRegion) {
YYCURSOR = opcode.c_str();
/*!re2c
lfoV1 = 'amplfo'|'fillfo'|'pitchlfo';
egV1 = 'ampeg'|'fileg'|'pitcheg';
eqV1 = 'eq' number;
lfoV2 = 'lfo' number;
(lfoV1) '_' ('depth'|'freq'|'fade') 'cc' (number) END {
opcode = absl::StrCat(group(1), "_", group(2), "_oncc", group(3));
goto end_region;
}
(eqV1) '_' ('bw'|'freq'|'gain') 'cc' (number) END {
opcode = absl::StrCat(group(1), "_", group(2), "_oncc", group(3));
goto end_region;
}
(egV1) '_' ('delay'|'start'|'attack'|'hold'|'decay'|'sustain'|'release') 'cc' (number) END {
opcode = absl::StrCat(group(1), "_", group(2), "_oncc", group(3));
goto end_region;
}
(lfoV2) '_' ('wave'|'offset'|'ratio'|'scale') END {
opcode = absl::StrCat(group(1), "_", group(2), "1");
goto end_region;
}
'loop' ('mode'|'start'|'end') END {
opcode = absl::StrCat("loop_", group(1));
goto end_region;
}
'off' ('by'|'mode') END {
opcode = absl::StrCat("off_", group(1));
goto end_region;
}
'bend' ('up'|'down') END {
opcode = absl::StrCat("bend_", group(1));
goto end_region;
}
'filtype' END {
opcode = absl::StrCat("fil1_type");
goto end_region;
}
'fil' (number) 'type' END {
opcode = absl::StrCat("fil", group(1), "_type");
goto end_region;
}
'polyphony_group' END {
opcode = "group";
goto end_region;
}
'gain' ('_' .*)? END {
opcode = absl::StrCat("volume", group(1));
goto end_region;
}
'tune' ('_' .*)? END {
opcode = absl::StrCat("pitch", group(1));
goto end_region;
}
'fil_' (.*) END {
opcode = absl::StrCat("fil1_", group(1));
goto end_region;
}
'on_' ('hi'|'lo') 'cc' (number) END {
opcode = absl::StrCat("start_", group(1), "cc", group(2));
goto end_region;
}
* { goto end_region; }
*/
end_region:
/* end */;
} // scope == kOpcodeFixRegion
//--------------------------------------------------------------------------
#undef YYMAXNMATCH
return opcode;
}
#include <stdio.h>
int main(int argc, char *argv[])
{
if (argc != 2) return 1;
std::string r = fixOpcode(argv[1], kOpcodeFixRegion);
printf("Result: %s\n", r.c_str());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment