2013-10-11 02:18:37 -04:00
|
|
|
%{
|
2013-10-11 23:07:28 -04:00
|
|
|
#include <stdlib.h>
|
2013-10-20 21:48:03 -04:00
|
|
|
#include <string.h>
|
2013-10-11 23:07:28 -04:00
|
|
|
|
2013-10-11 02:18:37 -04:00
|
|
|
#include "picrin.h"
|
|
|
|
#include "y.tab.h"
|
|
|
|
|
|
|
|
struct parser_control {
|
|
|
|
pic_state *pic;
|
2013-10-22 14:45:57 -04:00
|
|
|
void *yyscanner;
|
2013-10-11 02:18:37 -04:00
|
|
|
pic_value value;
|
2013-10-19 14:05:42 -04:00
|
|
|
bool incomp;
|
2013-10-22 14:13:10 -04:00
|
|
|
int yynerrs;
|
2013-10-11 02:18:37 -04:00
|
|
|
};
|
|
|
|
|
2013-10-20 22:12:34 -04:00
|
|
|
static pic_value new_escaped_string(pic_state *, const char *);
|
2013-10-20 21:48:03 -04:00
|
|
|
|
2013-10-22 14:45:57 -04:00
|
|
|
#define YY_DECL int yylex_ (YYSTYPE *yylvalp, yyscan_t yyscanner, struct parser_control *p)
|
2013-10-11 02:18:37 -04:00
|
|
|
%}
|
|
|
|
|
2013-10-22 14:45:57 -04:00
|
|
|
%option reentrant
|
2013-10-19 14:05:42 -04:00
|
|
|
%option noinput
|
2013-10-12 05:46:41 -04:00
|
|
|
%option nounput
|
|
|
|
|
2013-10-16 00:17:01 -04:00
|
|
|
/* boolean */
|
|
|
|
boolean #t|#f|#true|#false
|
|
|
|
|
2013-10-15 21:19:16 -04:00
|
|
|
/* symbol */
|
2013-10-17 05:14:18 -04:00
|
|
|
identifier [a-z0-9A-Z+-/*?<=>_.]+
|
2013-10-15 21:19:16 -04:00
|
|
|
|
|
|
|
/* number */
|
|
|
|
digit [0-9]
|
|
|
|
real {sign}{ureal}|{infnan}
|
|
|
|
ureal {digit}+|\.{digit}+|{digit}+\.{digit}*
|
|
|
|
sign [+-]?
|
2013-10-16 00:17:01 -04:00
|
|
|
infnan "+inf.0"|"-inf.0"|"+nan.0"|"-nan.0"
|
2013-10-15 21:19:16 -04:00
|
|
|
|
2013-10-20 21:48:03 -04:00
|
|
|
/* string */
|
|
|
|
string \"{string_elem}*\"
|
|
|
|
string_elem [^\"\\]|"\\\""|"\\\\"
|
|
|
|
|
2013-10-11 02:18:37 -04:00
|
|
|
%%
|
|
|
|
|
2013-10-12 05:46:11 -04:00
|
|
|
[ \t\n\r] /* skip whitespace */
|
2013-10-17 05:14:18 -04:00
|
|
|
"." return tDOT;
|
2013-10-15 10:25:31 -04:00
|
|
|
"(" return tLPAREN;
|
|
|
|
")" return tRPAREN;
|
2013-10-20 20:29:56 -04:00
|
|
|
"'" return tQUOTE;
|
2013-10-22 14:13:10 -04:00
|
|
|
{boolean} { yylvalp->datum = pic_bool_value(strcmp(yytext, "#t") == 0 || strcmp(yytext, "#true") == 0); return tBOOLEAN; }
|
|
|
|
{real} { yylvalp->datum = pic_float_value(atof(yytext)); return tNUMBER; }
|
|
|
|
{identifier} { yylvalp->datum = pic_intern_cstr(p->pic, yytext); return tSYMBOL; }
|
2013-10-20 21:48:03 -04:00
|
|
|
{string} {
|
2013-10-22 14:13:10 -04:00
|
|
|
yylvalp->datum = new_escaped_string(p->pic, yytext);
|
2013-10-20 21:48:03 -04:00
|
|
|
return tSTRING;
|
|
|
|
}
|
2013-10-11 02:18:37 -04:00
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
int
|
2013-10-22 14:45:57 -04:00
|
|
|
yywrap(yyscan_t yyscanner)
|
2013-10-11 02:18:37 -04:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2013-10-19 14:11:08 -04:00
|
|
|
|
2013-10-20 21:48:03 -04:00
|
|
|
static pic_value
|
2013-10-20 22:12:34 -04:00
|
|
|
new_escaped_string(pic_state *pic, const char *str)
|
2013-10-20 21:48:03 -04:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char *new_str;
|
2013-10-20 22:12:34 -04:00
|
|
|
int i,j;
|
2013-10-20 21:48:03 -04:00
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
len = strlen(str);
|
|
|
|
new_str = (char *)pic_alloc(pic, len + 1);
|
2013-10-20 22:12:34 -04:00
|
|
|
for (j = 0, i = 1; i < len - 1; ++i, ++j) {
|
|
|
|
if (str[i] == '\\') {
|
|
|
|
++i;
|
|
|
|
new_str[j] = str[i];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
new_str[j] = str[i];
|
2013-10-20 21:48:03 -04:00
|
|
|
}
|
2013-10-20 22:12:34 -04:00
|
|
|
new_str[j] = '\0';
|
2013-10-20 21:48:03 -04:00
|
|
|
|
|
|
|
v = pic_str_new_cstr(pic, new_str);
|
|
|
|
pic_free(pic, new_str);
|
|
|
|
return v;
|
|
|
|
}
|