picrin/src/parse.y

144 lines
1.8 KiB
Plaintext
Raw Normal View History

2013-10-11 02:18:37 -04:00
%{
#include <stdlib.h>
#include <stdio.h>
#include "picrin.h"
2013-10-19 23:34:57 -04:00
#include "picrin/pair.h"
2013-10-11 02:18:37 -04:00
2013-10-17 04:57:12 -04:00
#define YYERROR_VERBOSE 1
2013-10-11 02:18:37 -04:00
struct parser_control {
pic_state *pic;
pic_value value;
2013-10-17 07:48:50 -04:00
bool incomp;
2013-10-11 02:18:37 -04:00
};
void init_scanner(const char *);
void final_scanner(void);
void yyerror(struct parser_control *, const char *);
int yylex(struct parser_control *);
2013-10-11 02:18:37 -04:00
%}
%parse-param {struct parser_control *p}
%lex-param {struct parser_control *p}
%union {
pic_value datum;
}
%token tLPAREN tRPAREN tDOT
2013-10-20 20:29:56 -04:00
%token tQUOTE
2013-10-20 21:48:03 -04:00
%token <datum> tSYMBOL tNUMBER tBOOLEAN tSTRING
2013-10-11 02:18:37 -04:00
2013-10-20 21:48:03 -04:00
%type <datum> datum simple_datum compound_datum abbrev
%type <datum> list list_data
2013-10-11 02:18:37 -04:00
%%
program
:
{
2013-10-17 07:48:50 -04:00
p->value = pic_undef_value();
2013-10-11 02:18:37 -04:00
}
| datum
{
p->value = $1;
}
2013-10-17 07:48:50 -04:00
| incomplete_datum
{
p->incomp = true;
p->value = pic_undef_value();
}
2013-10-11 02:18:37 -04:00
;
datum
: simple_datum
| compound_datum
;
simple_datum
: tSYMBOL
2013-10-20 21:48:03 -04:00
| tNUMBER
| tBOOLEAN
| tSTRING
2013-10-16 00:17:01 -04:00
;
2013-10-11 02:18:37 -04:00
compound_datum
: list
2013-10-20 20:29:56 -04:00
| abbrev
2013-10-11 02:18:37 -04:00
;
list
2013-10-17 05:14:18 -04:00
: tLPAREN list_data tRPAREN
2013-10-11 02:18:37 -04:00
{
$$ = $2;
}
;
2013-10-17 05:14:18 -04:00
list_data
: /* none */
2013-10-11 02:18:37 -04:00
{
$$ = pic_nil_value();
}
2013-10-17 05:14:18 -04:00
| datum tDOT datum
2013-10-11 02:18:37 -04:00
{
$$ = pic_cons(p->pic, $1, $3);
}
2013-10-17 05:14:18 -04:00
| datum list_data
2013-10-11 02:18:37 -04:00
{
$$ = pic_cons(p->pic, $1, $2);
}
;
2013-10-20 20:29:56 -04:00
abbrev
: tQUOTE datum
{
$$ = pic_cons(p->pic, p->pic->sQUOTE, pic_cons(p->pic, $2, pic_nil_value()));
}
;
2013-10-17 07:48:50 -04:00
incomplete_datum
: tLPAREN incomplete_data
2013-10-20 20:29:56 -04:00
| tQUOTE
| tQUOTE incomplete_datum
2013-10-17 07:48:50 -04:00
;
incomplete_data
: /* none */
| datum tDOT
| datum incomplete_datum
| datum tDOT incomplete_datum
| datum incomplete_data
;
2013-10-11 02:18:37 -04:00
%%
2013-10-19 14:05:42 -04:00
void
2013-10-11 02:18:37 -04:00
yyerror(struct parser_control *p, const char *msg)
{
puts(msg);
}
2013-10-17 07:48:50 -04:00
bool
pic_parse(pic_state *pic, const char *str, pic_value *v)
2013-10-11 02:18:37 -04:00
{
struct parser_control p;
p.pic = pic;
2013-10-17 07:48:50 -04:00
p.incomp = false;
2013-10-11 02:18:37 -04:00
init_scanner(str);
2013-10-11 02:18:37 -04:00
yyparse(&p);
final_scanner();
2013-10-11 02:18:37 -04:00
2013-10-17 04:57:12 -04:00
if (yynerrs > 0) {
p.value = pic_undef_value();
}
2013-10-17 07:48:50 -04:00
if (! p.incomp) {
*v = p.value;
}
return ! p.incomp;
2013-10-11 02:18:37 -04:00
}