1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
%option noinput nounput
%option noyywrap
%option nodefault
/* %option reentrant bison-bridge bison-locations */
%{
#include <string.h>
#include "sexpr.h"
#include "sinc.h"
#include "parse.h"
#include "error.h"
#include "debug.h"
void yyerror(const char *s, ...);
char *str_value(char *s);
void update_yylloc() {
yylloc.first_line = yylloc.last_line;
yylloc.first_column = yylloc.last_column;
for (int i = 0; yytext[i]; i++) {
if (yytext[i] == '\n') {
yylloc.last_line++;
yylloc.last_column = 0;
} else {
yylloc.last_column++;
}
}
}
#define YY_USER_ACTION update_yylloc();
/**
* Calculate the size of a string value from a literal, by skipping backslashes
* and stopping at the first unescaped double quote encountered.
*/
size_t string_literal_size(char *s) {
size_t result = 0;
for (char *c = s; *c != '"'; result++, c++) {
if (*c == '\\') {
c++;
}
}
return result;
}
/**
* Copy a string literal into a new buffer, removing things like escape
* sequences.
*
* n is the size of the literal, NOT the size of the buffer (the buffer should
* be one char larger for the null terminator).
*/
void deliteral_string(char *literal, char *buf, size_t n) {
char *c = literal;
for (size_t i = 0; i < n; c++, i++) {
if (*c == '\\') {
c++;
switch (*c) {
case 'n':
buf[i] = '\n';
break;
case 't':
buf[i] = '\t';
break;
case '\\':
buf[i] = '\\';
break;
default:
warn("unrecognised escape sequence \\%c", *c);
buf[i] = *c;
}
} else {
buf[i] = *c;
}
}
buf[n] = '\0';
}
char *str_value(char *s) {
/*
* This function is given a string literal between quotes, for example:
*
* "hello world\n"
*
* We need to do a few things here:
* 1. drop the quotes;
* 2. replace escape sequences with the character they represent.
*/
s++;
size_t size = string_literal_size(s);
char *result = malloc(size + 1);
deliteral_string(s, result, size);
debug("in: %s\tout: %s (size %zu)\n", s-1, result, size);
return result;
}
%}
%option yylineno
ID [-A-Za-z_+*^!:/%.=<>][-A-Za-z0-9_+*^!:/%.=<>]*
STRING ["][^"]*["]|['][^']*[']
FLOAT [+-]?([0-9]+[.][0-9]*([Ee][+-]?[0-9]+)?|[.][0-9]+([Ee][+-]?[0-9]+)?)
INT [+-]?[0-9]+
%%
"(" |
")" |
"[" |
"]" |
";" { return yytext[0]; }
"()" { return NIL; }
{STRING} { yylval.s = str_value(yytext); return STRING; }
{ID} { /* FIXME: strdup allocates memory! */
yylval.s = strdup(yytext); return ID; }
{INT} { yylval.i = atoi(yytext); return INT; }
{FLOAT} { yylval.f = atof(yytext); return FLOAT; }
[ \t\n] /* ignore whitespace */
. { yyerror("Unexpected character %c\n", yytext[0]); }
|