mpw/bin/template_parser.lemon

128 lines
2.8 KiB
Plaintext
Raw Normal View History

2014-12-29 05:12:47 +00:00
%token_prefix tk
%name TemplateParse
2014-12-29 20:15:34 +00:00
%extra_argument { Debug::TemplateParseInfo *info }
2014-12-29 05:12:47 +00:00
%include {
#include <string>
#include <stdlib.h>
#include "template.h"
using namespace Debug;
}
%type struct_fields { FieldEntry * }
%type struct_field { FieldEntry * }
2014-12-29 20:15:34 +00:00
start ::= templates.
2014-12-29 05:12:47 +00:00
templates ::= templates struct.
templates ::= templates typedef.
templates ::= .
2014-12-29 20:15:34 +00:00
// typedeffing arrays or pointers is not allowed.
2014-12-29 05:12:47 +00:00
typedef ::= TYPEDEF type(a) IDENTIFIER(b). {
2014-12-29 20:15:34 +00:00
CreateTypedef((std::string *)b, a, info);
2014-12-29 05:12:47 +00:00
}
struct ::= STRUCT IDENTIFIER(a) LBRACE struct_fields(b) RBRACE SEMI.
{
2014-12-29 20:15:34 +00:00
CreateTemplate((std::string *)a, b, info);
2014-12-29 05:12:47 +00:00
}
struct_fields(rhs) ::= struct_fields(a) struct_field(b). {
// reverse order?
b->next = a;
rhs = b;
}
struct_fields(rhs) ::= struct_field(a). {
rhs = a;
}
struct_field(rhs) ::= type(a) IDENTIFIER(b) array_count(c) SEMI.
{
FieldEntry *e = (FieldEntry *)calloc(sizeof(FieldEntry), 1);
e->name = (std::string *)b;
e->type = a;
e->count = c;
rhs = e;
}
struct_field(rhs) ::= opt_volatile TEMPLATE(a) opt_star(star) IDENTIFIER(b) array_count(c) SEMI. {
FieldEntry *e = (FieldEntry *)calloc(sizeof(FieldEntry), 1);
e->name = (std::string *)b;
2014-12-29 20:15:34 +00:00
e->type = star ? kStruct : kStructPtr;
e->tmpl = (Template)a;
2014-12-29 05:12:47 +00:00
e->count = c;
rhs = e;
}
2014-12-29 20:15:34 +00:00
%type array_count { int }
array_count(rhs) ::= . { rhs = 0; }
array_count(rhs) ::= LBRACKET INTEGER(a) RBRACKET. {
2014-12-29 20:51:57 +00:00
int i = (int)(ptrdiff_t)a;
2014-12-29 20:15:34 +00:00
if (i == 0) {
fprintf(stderr, "Template error: line %u: 0-sized arrays are not allowed.\n",
info->LineNumber);
i = 1;
}
rhs = i;
}
%type type { int }
type(rhs) ::= opt_volatile typecode(a). { rhs = a; }
2014-12-29 05:12:47 +00:00
// this is an expected error...
type(rhs) ::= opt_volatile IDENTIFIER(xxx). {
// ugh, Lemon will blindly replace text within a string.
fprintf(stderr, "Template error: line %u: %s is not a known type.\n",
2014-12-29 20:15:34 +00:00
info->LineNumber, ((std::string *)xxx)->c_str());
2014-12-29 05:12:47 +00:00
rhs = 'i';
}
opt_volatile ::= .
opt_volatile ::= VOLATILE.
2014-12-29 20:15:34 +00:00
%type typecode { int }
typecode(rhs) ::= SIGNED. { rhs = kSInt32; }
typecode(rhs) ::= UNSIGNED. {rhs = kUInt32; }
typecode(rhs) ::= opt_signed CHAR. { rhs = kSInt8; }
typecode(rhs) ::= UNSIGNED CHAR. { rhs = kUInt8; }
2014-12-29 05:12:47 +00:00
2014-12-29 20:15:34 +00:00
typecode(rhs) ::= opt_signed SHORT. { rhs = kSInt16; }
typecode(rhs) ::= UNSIGNED SHORT. { rhs = kUInt16; }
2014-12-29 05:12:47 +00:00
2014-12-29 20:15:34 +00:00
typecode(rhs) ::= opt_signed LONG opt_int. { rhs = kSInt32; }
typecode(rhs) ::= UNSIGNED LONG opt_int. { rhs = kUInt32; }
2014-12-29 05:12:47 +00:00
2014-12-29 20:15:34 +00:00
typecode(rhs) ::= opt_signed LONG LONG. { rhs = kSInt64; }
typecode(rhs) ::= UNSIGNED LONG LONG. { rhs = kUInt64; }
2014-12-29 05:12:47 +00:00
2014-12-29 20:51:57 +00:00
typecode(rhs) ::= TYPECODE(a). { rhs = (int)(ptrdiff_t)a; }
2014-12-29 05:12:47 +00:00
/* pointers are not fully supported yet */
2014-12-29 20:15:34 +00:00
typecode(rhs) ::= VOID STAR. { rhs = kVoidPtr; }
2014-12-29 05:12:47 +00:00
opt_signed ::= .
opt_signed ::= SIGNED.
opt_int ::= .
opt_int ::= INT.
2014-12-29 20:15:34 +00:00
%type opt_star { int }
2014-12-29 05:12:47 +00:00
opt_star(rhs) ::= . { rhs = 0; }
opt_star(rhs) ::= STAR. { rhs = 1; }