-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.l
161 lines (142 loc) · 5.42 KB
/
parser.l
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
%{
/*
Copyright © 2008 Sam Chapin
This file is part of Gospel.
Gospel is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License
as published by the Free Software Foundation.
Gospel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Gospel. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h> // Flex needs this, but on some systems fails to generate the inclusion.
#include <stdlib.h>
#include "death.h"
#include "core.h"
#include "y.tab.h" // This needs to be read after core.h or else the default for YYSTYPE gets used.
%}
%option noyywrap
%option reentrant
%x NEWLINES
%x ENDING
ID [a-zA-Z_0-9]+
OP [!%&*\-+=<>?/]+
%%
<ENDING><<EOF>> return ENDOFFILE;
<INITIAL>{
<<EOF>> BEGIN(ENDING); return '\n';
\"([^"]|\\\")*\" { char *s = yytext + 1;
int remainder = strlen(s);
location->last_line = location->first_line;
while (*s != '"') {
--remainder;
if (*s == '\n') {
location->last_line++;
s++;
continue;
}
if (*s++ != '\\') continue;
switch (*s) {
case 'n': s[-1] = '\n'; break;
case 't': s[-1] = '\t'; break;
default: s[-1] = *s;
}
memmove(s, s + 1, --remainder);
}
*s = 0;
*value = string(yyget_text(yyscanner) + 1);
return STRING;
}
\$[^ \[\]\(\)\{\}\n.,;]* { *value = symbol(yyget_text(yyscanner) + 1);
return SYMBOL;
}
-?[0-9]+ { *value = call(string(yyget_text(yyscanner)), sAsDecimalInteger, emptyVector);
return INTEGER;
}
= { BEGIN(NEWLINES);
return ADDSLOT;
}
:= { BEGIN(NEWLINES);
return SETSLOT;
}
{ID} { *value = symbol(yyget_text(yyscanner));
return NAME;
}
{ID}: { *value = symbol(yyget_text(yyscanner));
BEGIN(NEWLINES);
return KEYWORD;
}
:{ID}: { *value = symbol(yyget_text(yyscanner) + 1);
BEGIN(NEWLINES);
return OPERATOR;
}
({ID}:){2,} { *value = symbol(yyget_text(yyscanner));
BEGIN(NEWLINES);
return KEYWORDS;
}
[\(\[\{] { ++*nesting;
return *yyget_text(yyscanner);
}
[\)\]\}] { --*nesting;
return *yyget_text(yyscanner);
}
{OP} { *value = symbol(yyget_text(yyscanner));
BEGIN(NEWLINES);
return OPERATOR;
}
(#[^\n]*)?\n { BEGIN(NEWLINES);
location->last_line = ++location->first_line;
if (*nesting) return '\n';
return 0;
}
\ /* discard */
. return *yyget_text(yyscanner);
}
<NEWLINES>{
<<EOF>> BEGIN(ENDING); return '\n';
\ *(#[^\n]*)?\n location->last_line = ++location->first_line;
. yyless(0); BEGIN(INITIAL);
}
%%
// TODO: Find a better way than this to get rid of the compiler warning.
int yyparse(int *, int *, void **, yyscan_t);
typedef struct {
FILE *file;
int line;
YY_BUFFER_STATE buffer;
yyscan_t scanner;
} scannerData;
scannerData *newScanner() {
scannerData *sd = malloc(sizeof(scannerData));
if (!sd) die("Unable to allocate memory for a new scanner.");
sd->line = 0;
if (yylex_init(&sd->scanner)) die("Unable to initialize scanner.");
return sd;
}
scannerData *beginParsing(FILE *input) {
scannerData *sd = newScanner();
yy_switch_to_buffer(sd->buffer = yy_create_buffer(sd->file = input, YY_BUF_SIZE, sd->scanner),
sd->scanner);
return sd;
}
void *parse(scannerData *sd) {
void *result;
int nesting = 0;
yyparse(&sd->line, &nesting, &result, sd->scanner);
return result;
}
void endParsing(scannerData *sd) {
yy_delete_buffer(sd->buffer, sd->scanner);
yylex_destroy(sd->scanner);
free(sd);
}
void *parseString(char *string) {
scannerData *sd = newScanner();
sd->buffer = yy_scan_string(string, sd->scanner);
void *result = parse(sd);
endParsing(sd);
return result;
}