mpw/bin/template_loader.rl

254 lines
5.7 KiB
Plaintext
Raw Normal View History

2014-12-29 05:12:47 +00:00
#include <string>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include "template_parser.h"
#include "template.h"
2014-12-29 20:15:34 +00:00
#include "intern.h"
2014-12-29 05:12:47 +00:00
namespace {
int tox(char c)
{
c |= 0x20; // lowercase it.
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
return 0;
}
uint32_t scan10(const char *begin, const char *end)
{
return std::accumulate(begin, end, 0,
[](uint32_t value, char c){
return value * 10 + c - '0';
});
}
uint32_t scan16(const char *begin, const char *end)
{
return std::accumulate(begin, end, 0,
[](uint32_t value, char c){
return (value << 4) + tox(c);
});
}
}
void *TemplateParseAlloc(void *(*mallocProc)(size_t));
void TemplateParseFree(void *p, void (*freeProc)(void*));
2014-12-29 20:15:34 +00:00
void TemplateParse(void *yyp, int yymajor, void *yyminor, Debug::TemplateParseInfo *);
void TemplateParse(void *yyp, int yymajor, int yyminor, Debug::TemplateParseInfo *info)
2014-12-29 05:12:47 +00:00
{
2014-12-29 21:58:01 +00:00
TemplateParse(yyp, yymajor, (void *)(ptrdiff_t)yyminor, info);
2014-12-29 05:12:47 +00:00
}
2014-12-29 20:15:34 +00:00
void TemplateParse(void *yyp, int yymajor, const std::string *yyminor, Debug::TemplateParseInfo *info)
2014-12-29 05:12:47 +00:00
{
2014-12-29 20:15:34 +00:00
TemplateParse(yyp, yymajor, (void *)yyminor, info);
2014-12-29 05:12:47 +00:00
}
2014-12-29 20:15:34 +00:00
#define TemplateParse(a,b,c) TemplateParse(a,b,c, &info)
2014-12-29 05:12:47 +00:00
%%{
machine lexer;
# this exits with cs == lexer_en_error.
error := any* ${ fbreak; };
block_comment := |*
2014-12-29 20:15:34 +00:00
[\n\r] { info.LineNumber++; };
2014-12-29 05:12:47 +00:00
'*/' { fgoto main; };
any ;
*|;
main := |*
2014-12-29 20:15:34 +00:00
[\n\r] { info.LineNumber++; };
2014-12-29 05:12:47 +00:00
[ \t]+;
'//' [^\r\n]* ;
'/*' { fgoto block_comment; };
';' { TemplateParse(parser, tkSEMI, 0); };
'{' { TemplateParse(parser, tkLBRACE, 0); };
'}' { TemplateParse(parser, tkRBRACE, 0); };
'[' { TemplateParse(parser, tkLBRACKET, 0); };
']' { TemplateParse(parser, tkRBRACKET, 0); };
'*' { TemplateParse(parser, tkSTAR, 0); };
'struct' { TemplateParse(parser, tkSTRUCT, 0); };
'typedef' { TemplateParse(parser, tkTYPEDEF, 0); };
'int' { TemplateParse(parser, tkINT, 0); };
'long' { TemplateParse(parser, tkLONG, 0); };
'short' { TemplateParse(parser, tkSHORT, 0); };
'volatile' { TemplateParse(parser, tkVOLATILE, 0); };
#'const' { TemplateParse(parser, tkCONST, 0); };
'char' { TemplateParse(parser, tkCHAR, 0); };
#'bool' { TemplateParse(parser, tkBOOL, 0); };
'void' { TemplateParse(parser, tkVOID, 0); };
'signed' { TemplateParse(parser, tkSIGNED, 0); };
'unsigned' { TemplateParse(parser, tkUNSIGNED, 0); };
2014-12-29 20:15:34 +00:00
'int64_t' { TemplateParse(parser, tkTYPECODE, kSInt64); };
'uint64_t' { TemplateParse(parser, tkTYPECODE, kUInt64); };
2014-12-29 05:12:47 +00:00
2014-12-29 20:15:34 +00:00
'int32_t' { TemplateParse(parser, tkTYPECODE, kSInt32); };
'uint32_t' { TemplateParse(parser, tkTYPECODE, kUInt32); };
2014-12-29 05:12:47 +00:00
2014-12-29 20:15:34 +00:00
'int16_t' { TemplateParse(parser, tkTYPECODE, kSInt16); };
'uint16_t' { TemplateParse(parser, tkTYPECODE, kUInt16); };
2014-12-29 05:12:47 +00:00
2014-12-29 20:15:34 +00:00
'int8_t' { TemplateParse(parser, tkTYPECODE, kSInt8); };
'uint8_t' { TemplateParse(parser, tkTYPECODE, kUInt8); };
2014-12-29 05:12:47 +00:00
2014-12-29 20:15:34 +00:00
'StringPtr' { TemplateParse(parser, tkTYPECODE, kPStringPtr); };
2014-12-29 05:12:47 +00:00
'CStringPtr' { TemplateParse(parser, tkTYPECODE, kCStringPtr); };
2014-12-29 20:15:34 +00:00
'Ptr' { TemplateParse(parser, tkTYPECODE, kVoidPtr); };
2014-12-29 05:12:47 +00:00
'OSType' { TemplateParse(parser, tkTYPECODE, kOSType); };
2014-12-29 20:15:34 +00:00
'OSErr' { TemplateParse(parser, tkTYPECODE, kOSErr); };
2014-12-29 05:12:47 +00:00
'Boolean' { TemplateParse(parser, tkTYPECODE, kBoolean); };
2014-12-29 20:15:34 +00:00
'Handle' { TemplateParse(parser, tkTYPECODE, kHandle); };
2014-12-29 05:12:47 +00:00
# numbers. negative numbers are not allowed.
'0x'i xdigit+ {
// hexadecimal
uint32_t value = scan16(ts + 2, te);
TemplateParse(parser, tkINTEGER, value);
};
digit+ {
uint32_t value = scan10(ts, te);
TemplateParse(parser, tkINTEGER, value);
};
# identifier ... but also need to check if it's a type.
[A-Za-z_][A-Za-z0-9_]* {
2014-12-29 05:12:47 +00:00
// intern the string.
2014-12-29 20:15:34 +00:00
const std::string *name = Intern::String(ts, te);
bool ok = false;
2014-12-29 05:12:47 +00:00
2014-12-29 20:15:34 +00:00
if (!ok) {
auto iter = Types.find(*name);
if (iter != Types.end())
{
TemplateParse(parser, tkTYPECODE, iter->second);
ok = true;
}
}
if (!ok) {
auto iter = Templates.find(*name);
if (iter != Templates.end())
{
TemplateParse(parser, tkTEMPLATE, iter->second);
ok = true;
}
2014-12-29 05:12:47 +00:00
}
2014-12-29 20:15:34 +00:00
if (!ok)
2014-12-29 05:12:47 +00:00
{
TemplateParse(parser, tkIDENTIFIER, name);
}
};
*|;
}%%
namespace Debug {
2014-12-29 20:15:34 +00:00
2014-12-29 05:12:47 +00:00
2014-12-29 20:15:34 +00:00
bool LoadTemplateFile(const std::string &filename, std::unordered_map<std::string, Template> &Templates)
2014-12-29 05:12:47 +00:00
{
%% write data;
void *parser;
int fd;
struct stat st;
char *buffer;
2014-12-29 20:15:34 +00:00
std::unordered_map<std::string, unsigned> Types;
TemplateParseInfo info;
info.LineNumber = 1;
info.templates = &Templates;
info.types = &Types;
// simple types are handled via the lexer so no need to populate them here.
2014-12-29 05:12:47 +00:00
if (stat(filename.c_str(), &st) < 0) return false;
if (st.st_size == 0) return false;
fd = open(filename.c_str(), O_RDONLY);
if (fd < 0) {
perror("Error opening template file: ");
return false;
}
buffer = (char *)mmap(nullptr, st.st_size, PROT_READ, MAP_FILE | MAP_SHARED, fd, 0);
if (buffer == MAP_FAILED) {
perror("Error mapping template file: ");
close(fd);
return false;
}
close(fd);
parser = TemplateParseAlloc(malloc);
const char *p = buffer;
const char *pe = buffer + st.st_size;
const char *eof = pe;
const char *ts;
const char *te;
int cs, act;
2014-12-31 23:46:38 +00:00
%% write init;
%% write exec;
2014-12-29 05:12:47 +00:00
2014-12-31 23:46:38 +00:00
if (cs == lexer_error || cs < lexer_first_final)
{
if (p == eof)
fprintf(stderr, "Template error: line %d - unexpected EOF\n", info.LineNumber);
else
fprintf(stderr, "Template error: line %d - illegal character: `%c'\n", info.LineNumber, *p);
2014-12-31 23:46:38 +00:00
TemplateParseFree(parser, free);
munmap(buffer, st.st_size);
return false;
2014-12-29 05:12:47 +00:00
}
2014-12-31 23:46:38 +00:00
2014-12-29 05:12:47 +00:00
TemplateParse(parser, 0, 0);
TemplateParseFree(parser, free);
munmap(buffer, st.st_size);
return true;
}
}