Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 7cd249768d
Fetching contributors…

Cannot retrieve contributors at this time

465 lines (421 sloc) 12.153 kB
/* A recursive-descent parser generated by greg 0.3.0 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct _GREG;
#define YYRULECOUNT 7
#include "ruby.h"
#include "parser.h"
#define YY_INPUT(buf, result, max) { \
if (N->pos < N->size) { \
result = max; \
if(N->pos + max > N->size) \
result = N->size - N->pos; \
memcpy(buf, N->bytes, result+1); \
N->pos += max; \
} else { \
result = 0; \
} \
}
#define YYSTYPE VALUE
#define YY_XTYPE Newspeak*
#define YY_XVAR N
#define YY_NAME(N) newspeak_code_##N
#define NS_VAL(n) rb_funcall(N->parser, rb_intern(n), 0)
#define NS_AST(n, v) rb_funcall(N->parser, rb_intern(n), 1, v)
#define NS_AST2(n, x, y) rb_funcall(N->parser, rb_intern(n), 2, x, y)
#define NS_OP(n, l, r) rb_funcall(N->parser, rb_intern(n), 2, l, r)
#ifndef YY_ALLOC
#define YY_ALLOC(N, D) malloc(N)
#endif
#ifndef YY_CALLOC
#define YY_CALLOC(N, S, D) calloc(N, S)
#endif
#ifndef YY_REALLOC
#define YY_REALLOC(B, N, D) realloc(B, N)
#endif
#ifndef YY_FREE
#define YY_FREE free
#endif
#ifndef YY_LOCAL
#define YY_LOCAL(T) static T
#endif
#ifndef YY_ACTION
#define YY_ACTION(T) static T
#endif
#ifndef YY_RULE
#define YY_RULE(T) static T
#endif
#ifndef YY_PARSE
#define YY_PARSE(T) T
#endif
#ifndef YY_NAME
#define YY_NAME(N) yy##N
#endif
#ifndef YY_INPUT
#define YY_INPUT(buf, result, max_size) \
{ \
int yyc= getchar(); \
result= (EOF == yyc) ? 0 : (*(buf)= yyc, 1); \
yyprintf((stderr, "<%c>", yyc)); \
}
#endif
#ifndef YY_BEGIN
#define YY_BEGIN ( G->begin= G->pos, 1)
#endif
#ifndef YY_END
#define YY_END ( G->end= G->pos, 1)
#endif
#ifdef YY_DEBUG
# define yyprintf(args) fprintf args
#else
# define yyprintf(args)
#endif
#ifndef YYSTYPE
#define YYSTYPE int
#endif
#ifndef YY_XTYPE
#define YY_XTYPE void *
#endif
#ifndef YY_XVAR
#define YY_XVAR yydata
#endif
#ifndef YY_PART
#define yy G->ss
typedef void (*yyaction)(struct _GREG *G, char *yytext, int yyleng, YY_XTYPE YY_XVAR);
typedef struct _yythunk { int begin, end; yyaction action; struct _yythunk *next; } yythunk;
typedef struct _GREG {
char *buf;
int buflen;
int pos;
int limit;
char *text;
int textlen;
int begin;
int end;
yythunk *thunks;
int thunkslen;
int thunkpos;
YYSTYPE ss;
YYSTYPE *val;
YYSTYPE *vals;
int valslen;
YY_XTYPE data;
} GREG;
YY_LOCAL(int) yyrefill(GREG *G)
{
int yyn;
YY_XTYPE YY_XVAR = (YY_XTYPE)G->data;
while (G->buflen - G->pos < 512)
{
G->buflen *= 2;
G->buf= YY_REALLOC(G->buf, G->buflen, G->data);
}
YY_INPUT((G->buf + G->pos), yyn, (G->buflen - G->pos));
if (!yyn) return 0;
G->limit += yyn;
return 1;
}
YY_LOCAL(int) yymatchDot(GREG *G)
{
if (G->pos >= G->limit && !yyrefill(G)) return 0;
++G->pos;
return 1;
}
YY_LOCAL(int) yymatchChar(GREG *G, int c)
{
if (G->pos >= G->limit && !yyrefill(G)) return 0;
if ((unsigned char)G->buf[G->pos] == c)
{
++G->pos;
yyprintf((stderr, " ok yymatchChar(%c) @ %s\n", c, G->buf+G->pos));
return 1;
}
yyprintf((stderr, " fail yymatchChar(%c) @ %s\n", c, G->buf+G->pos));
return 0;
}
YY_LOCAL(int) yymatchString(GREG *G, char *s)
{
int yysav= G->pos;
while (*s)
{
if (G->pos >= G->limit && !yyrefill(G)) return 0;
if (G->buf[G->pos] != *s)
{
G->pos= yysav;
return 0;
}
++s;
++G->pos;
}
return 1;
}
YY_LOCAL(int) yymatchClass(GREG *G, unsigned char *bits)
{
int c;
if (G->pos >= G->limit && !yyrefill(G)) return 0;
c= (unsigned char)G->buf[G->pos];
if (bits[c >> 3] & (1 << (c & 7)))
{
++G->pos;
yyprintf((stderr, " ok yymatchClass @ %s\n", G->buf+G->pos));
return 1;
}
yyprintf((stderr, " fail yymatchClass @ %s\n", G->buf+G->pos));
return 0;
}
YY_LOCAL(void) yyDo(GREG *G, yyaction action, int begin, int end)
{
while (G->thunkpos >= G->thunkslen)
{
G->thunkslen *= 2;
G->thunks= YY_REALLOC(G->thunks, sizeof(yythunk) * G->thunkslen, G->data);
}
G->thunks[G->thunkpos].begin= begin;
G->thunks[G->thunkpos].end= end;
G->thunks[G->thunkpos].action= action;
++G->thunkpos;
}
YY_LOCAL(int) yyText(GREG *G, int begin, int end)
{
int yyleng= end - begin;
if (yyleng <= 0)
yyleng= 0;
else
{
while (G->textlen < (yyleng - 1))
{
G->textlen *= 2;
G->text= YY_REALLOC(G->text, G->textlen, G->data);
}
memcpy(G->text, G->buf + begin, yyleng);
}
G->text[yyleng]= '\0';
return yyleng;
}
YY_LOCAL(void) yyDone(GREG *G)
{
int pos;
for (pos= 0; pos < G->thunkpos; ++pos)
{
yythunk *thunk= &G->thunks[pos];
int yyleng= thunk->end ? yyText(G, thunk->begin, thunk->end) : thunk->begin;
yyprintf((stderr, "DO [%d] %p %s\n", pos, thunk->action, G->text));
thunk->action(G, G->text, yyleng, G->data);
}
G->thunkpos= 0;
}
YY_LOCAL(void) yyCommit(GREG *G)
{
if ((G->limit -= G->pos))
{
memmove(G->buf, G->buf + G->pos, G->limit);
}
G->begin -= G->pos;
G->end -= G->pos;
G->pos= G->thunkpos= 0;
}
YY_LOCAL(int) yyAccept(GREG *G, int tp0)
{
if (tp0)
{
fprintf(stderr, "accept denied at %d\n", tp0);
return 0;
}
else
{
yyDone(G);
yyCommit(G);
}
return 1;
}
YY_LOCAL(void) yyPush(GREG *G, char *text, int count, YY_XTYPE YY_XVAR) {
size_t off = (G->val - G->vals) + count;
if (off > G->valslen) {
while (G->valslen < off + 1)
G->valslen *= 2;
G->vals= YY_REALLOC((void *)G->vals, sizeof(YYSTYPE) * G->valslen, G->data);
G->val= G->vals + off;
} else {
G->val += count;
}
}
YY_LOCAL(void) yyPop(GREG *G, char *text, int count, YY_XTYPE YY_XVAR) { G->val -= count; }
YY_LOCAL(void) yySet(GREG *G, char *text, int count, YY_XTYPE YY_XVAR) { G->val[count]= G->ss; }
#endif /* YY_PART */
#define YYACCEPT yyAccept(G, yythunkpos0)
YY_RULE(int) yy_end_of_file(GREG *G); /* 7 */
YY_RULE(int) yy_end_of_line(GREG *G); /* 6 */
YY_RULE(int) yy_comment(GREG *G); /* 5 */
YY_RULE(int) yy_space(GREG *G); /* 4 */
YY_RULE(int) yy__(GREG *G); /* 3 */
YY_RULE(int) yy___(GREG *G); /* 2 */
YY_RULE(int) yy_newspeak(GREG *G); /* 1 */
YY_RULE(int) yy_end_of_file(GREG *G)
{ int yypos0= G->pos, yythunkpos0= G->thunkpos;
yyprintf((stderr, "%s\n", "end_of_file"));
{ int yypos2= G->pos, yythunkpos2= G->thunkpos; if (!yymatchDot(G)) goto l2; goto l1;
l2:; G->pos= yypos2; G->thunkpos= yythunkpos2;
}
yyprintf((stderr, " ok %s @ %s\n", "end_of_file", G->buf+G->pos));
return 1;
l1:; G->pos= yypos0; G->thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "end_of_file", G->buf+G->pos));
return 0;
}
YY_RULE(int) yy_end_of_line(GREG *G)
{ int yypos0= G->pos, yythunkpos0= G->thunkpos;
yyprintf((stderr, "%s\n", "end_of_line"));
{ int yypos4= G->pos, yythunkpos4= G->thunkpos; if (!yymatchString(G, "\r\n")) goto l5; goto l4;
l5:; G->pos= yypos4; G->thunkpos= yythunkpos4; if (!yymatchChar(G, '\n')) goto l6; goto l4;
l6:; G->pos= yypos4; G->thunkpos= yythunkpos4; if (!yymatchChar(G, '\r')) goto l3;
}
l4:;
yyprintf((stderr, " ok %s @ %s\n", "end_of_line", G->buf+G->pos));
return 1;
l3:; G->pos= yypos0; G->thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "end_of_line", G->buf+G->pos));
return 0;
}
YY_RULE(int) yy_comment(GREG *G)
{ int yypos0= G->pos, yythunkpos0= G->thunkpos;
yyprintf((stderr, "%s\n", "comment")); if (!yymatchChar(G, '#')) goto l7;
l8:;
{ int yypos9= G->pos, yythunkpos9= G->thunkpos;
{ int yypos10= G->pos, yythunkpos10= G->thunkpos; if (!yy_end_of_line(G)) goto l10; goto l9;
l10:; G->pos= yypos10; G->thunkpos= yythunkpos10;
} goto l8;
l9:; G->pos= yypos9; G->thunkpos= yythunkpos9;
}
yyprintf((stderr, " ok %s @ %s\n", "comment", G->buf+G->pos));
return 1;
l7:; G->pos= yypos0; G->thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "comment", G->buf+G->pos));
return 0;
}
YY_RULE(int) yy_space(GREG *G)
{ int yypos0= G->pos, yythunkpos0= G->thunkpos;
yyprintf((stderr, "%s\n", "space"));
{ int yypos12= G->pos, yythunkpos12= G->thunkpos; if (!yymatchChar(G, ' ')) goto l13; goto l12;
l13:; G->pos= yypos12; G->thunkpos= yythunkpos12; if (!yymatchChar(G, '\f')) goto l14; goto l12;
l14:; G->pos= yypos12; G->thunkpos= yythunkpos12; if (!yymatchChar(G, '\v')) goto l15; goto l12;
l15:; G->pos= yypos12; G->thunkpos= yythunkpos12; if (!yymatchChar(G, '\t')) goto l11;
}
l12:;
yyprintf((stderr, " ok %s @ %s\n", "space", G->buf+G->pos));
return 1;
l11:; G->pos= yypos0; G->thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "space", G->buf+G->pos));
return 0;
}
YY_RULE(int) yy__(GREG *G)
{
yyprintf((stderr, "%s\n", "_"));
l17:;
{ int yypos18= G->pos, yythunkpos18= G->thunkpos;
{ int yypos19= G->pos, yythunkpos19= G->thunkpos; if (!yy_space(G)) goto l20; goto l19;
l20:; G->pos= yypos19; G->thunkpos= yythunkpos19; if (!yy_comment(G)) goto l18;
}
l19:; goto l17;
l18:; G->pos= yypos18; G->thunkpos= yythunkpos18;
}
yyprintf((stderr, " ok %s @ %s\n", "_", G->buf+G->pos));
return 1;
}
YY_RULE(int) yy___(GREG *G)
{
yyprintf((stderr, "%s\n", "__"));
l22:;
{ int yypos23= G->pos, yythunkpos23= G->thunkpos;
{ int yypos24= G->pos, yythunkpos24= G->thunkpos; if (!yy_space(G)) goto l25; goto l24;
l25:; G->pos= yypos24; G->thunkpos= yythunkpos24; if (!yy_comment(G)) goto l26; goto l24;
l26:; G->pos= yypos24; G->thunkpos= yythunkpos24; if (!yy_end_of_line(G)) goto l23;
}
l24:; goto l22;
l23:; G->pos= yypos23; G->thunkpos= yythunkpos23;
}
yyprintf((stderr, " ok %s @ %s\n", "__", G->buf+G->pos));
return 1;
}
YY_RULE(int) yy_newspeak(GREG *G)
{ int yypos0= G->pos, yythunkpos0= G->thunkpos;
yyprintf((stderr, "%s\n", "newspeak")); if (!yy___(G)) goto l27;
yyprintf((stderr, " ok %s @ %s\n", "newspeak", G->buf+G->pos));
return 1;
l27:; G->pos= yypos0; G->thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "newspeak", G->buf+G->pos));
return 0;
}
#ifndef YY_PART
typedef int (*yyrule)(GREG *G);
YY_PARSE(int) YY_NAME(parse_from)(GREG *G, yyrule yystart)
{
int yyok;
if (!G->buflen)
{
G->buflen= 1024;
G->buf= YY_ALLOC(G->buflen, G->data);
G->textlen= 1024;
G->text= YY_ALLOC(G->textlen, G->data);
G->thunkslen= 32;
G->thunks= YY_ALLOC(sizeof(yythunk) * G->thunkslen, G->data);
G->valslen= 32;
G->vals= YY_ALLOC(sizeof(YYSTYPE) * G->valslen, G->data);
G->begin= G->end= G->pos= G->limit= G->thunkpos= 0;
}
G->begin= G->end= G->pos;
G->thunkpos= 0;
G->val= G->vals;
yyok= yystart(G);
if (yyok) yyDone(G);
yyCommit(G);
return yyok;
(void)yyrefill;
(void)yymatchDot;
(void)yymatchChar;
(void)yymatchString;
(void)yymatchClass;
(void)yyDo;
(void)yyText;
(void)yyDone;
(void)yyCommit;
(void)yyAccept;
(void)yyPush;
(void)yyPop;
(void)yySet;
}
YY_PARSE(int) YY_NAME(parse)(GREG *G)
{
return YY_NAME(parse_from)(G, yy_newspeak);
}
YY_PARSE(GREG *) YY_NAME(parse_new)(YY_XTYPE data)
{
GREG *G = (GREG *)YY_CALLOC(1, sizeof(GREG), G->data);
G->data = data;
return G;
}
YY_PARSE(void) YY_NAME(parse_free)(GREG *G)
{
YY_FREE(G);
}
#endif
VALUE newspeak_parse_string(VALUE self, VALUE string) {
Newspeak N;
N.parser = self;
N.pos = 0;
N.size = RSTRING_LEN(string);
N.bytes = RSTRING_PTR(string);
GREG *G = newspeak_code_parse_new(&N);
G->pos = G->limit = 0;
if (!newspeak_code_parse(G)) {
rb_funcall(N.parser, rb_intern("syntax_error"), 1, INT2FIX(G->end));
return N.ast = Qfalse;
}
newspeak_code_parse_free(G);
return N.ast;
}
void Init_parser(void) {
VALUE rb_mSyme = rb_const_get(rb_cObject, rb_intern("Syme"));
VALUE rb_cParser = rb_const_get(rb_mSyme, rb_intern("Parser"));
rb_define_method(rb_cParser, "parse_string", RUBY_METHOD_FUNC(newspeak_parse_string), 1);
}
Jump to Line
Something went wrong with that request. Please try again.