mirror of
https://github.com/ksherlock/mpw.git
synced 2024-11-22 15:31:50 +00:00
171 lines
2.4 KiB
Ragel
171 lines
2.4 KiB
Ragel
|
|
#include <unordered_map>
|
|
#include <string>
|
|
#include <cstdio>
|
|
|
|
%%{
|
|
|
|
machine lexer;
|
|
|
|
action error {
|
|
fprintf(stderr, "Invalid line: %d %.*s", line, (int)length, cp);
|
|
fprintf(stderr, "%s - %x\n", name.c_str(), trap);
|
|
}
|
|
|
|
action addx {
|
|
trap = (trap << 4) + digittoint(fc);
|
|
}
|
|
|
|
action emplace {
|
|
|
|
auto iter = map.find(name);
|
|
|
|
if (iter == map.end())
|
|
{
|
|
map[std::move(name)] = trap;
|
|
}
|
|
else
|
|
{
|
|
if (iter->second != trap)
|
|
{
|
|
fprintf(stderr, "Warning: redefining %s ($%04x -> $%04x)\n",
|
|
name.c_str(),
|
|
iter->second,
|
|
trap
|
|
);
|
|
iter->second = trap;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
ws = [ \t];
|
|
|
|
|
|
value =
|
|
'0x' >{trap = 0; } xdigit+ @addx
|
|
|
|
|
'$' >{trap = 0; } xdigit+ @addx
|
|
# todo -- add identifiers?
|
|
;
|
|
|
|
|
|
line :=
|
|
[_A-Za-z][_A-Za-z0-9]* @{
|
|
name.push_back(fc);
|
|
}
|
|
|
|
ws*
|
|
'='
|
|
ws*
|
|
value
|
|
'\n' @emplace
|
|
;
|
|
|
|
comment := any* ${ fbreak; };
|
|
|
|
main := |*
|
|
ws; # leading space
|
|
'\n'; # blank line.
|
|
'#' => { fgoto comment; };
|
|
[A-Za-z_] => { fhold; fgoto line; };
|
|
*|;
|
|
|
|
}%%
|
|
|
|
namespace Debug {
|
|
std::unordered_map<std::string, uint32_t> LoadTrapFile(const std::string &path)
|
|
{
|
|
%% write data;
|
|
|
|
FILE *fp;
|
|
|
|
std::unordered_map<std::string, uint32_t> map;
|
|
|
|
fp = fopen(path.c_str(), "r");
|
|
if (!fp)
|
|
{
|
|
fprintf(stderr, "Unable to open file %s\n", path.c_str());
|
|
return map;
|
|
}
|
|
|
|
// todo -- consider using the debugger parser? That would allow
|
|
// full mathematical expressions.
|
|
|
|
int line = 0;
|
|
for(;;)
|
|
{
|
|
char *cp;
|
|
|
|
size_t length;
|
|
cp = fgetln(fp, &length);
|
|
if (!cp) break;
|
|
++line;
|
|
|
|
#if 0
|
|
while (isspace(*cp) && length)
|
|
{
|
|
++cp;
|
|
length--;
|
|
}
|
|
if (!length) continue;
|
|
#endif
|
|
|
|
while (length && isspace(cp[length - 1]))
|
|
length--;
|
|
if (!length) continue;
|
|
|
|
std::string buffer(cp, cp + length);
|
|
buffer.push_back('\n');
|
|
|
|
const char *p = buffer.c_str();
|
|
const char *pe = p + buffer.length();
|
|
//const char *eof = pe;
|
|
const char *ts;
|
|
const char *te;
|
|
|
|
int cs;
|
|
int act;
|
|
|
|
std::string name;
|
|
uint32_t trap = 0;
|
|
|
|
%% write init;
|
|
/* ... */
|
|
%% write exec;
|
|
|
|
if (cs == lexer_error)
|
|
{
|
|
fprintf(stderr, "Bad line: %.*s\n", (int)length, cp);
|
|
}
|
|
}
|
|
|
|
fclose(fp);
|
|
return map;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
#ifdef TEST
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
for (int i = 1; i < argc; ++i)
|
|
{
|
|
std::string f(argv[i]);
|
|
|
|
auto map = Debug::LoadTrapFile(f);
|
|
|
|
for(const auto kv : map)
|
|
{
|
|
printf("%s -> %04x\n", kv.first.c_str(), kv.second);
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
#endif
|
|
|