-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript-lexer.l
147 lines (109 loc) · 2.88 KB
/
script-lexer.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
%{
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <ctype.h>
#include <string.h>
#include "array.h"
#include "arena.h"
#include "script.h"
#include "script-parser.h"
unsigned int character;
unsigned int line = 0;
static int
stringliteral(yyscan_t yyscanner);
static int
binaryliteral(yyscan_t yyscanner);
%}
%option reentrant
%option noyywrap
%option bison-bridge
%option bison-locations
%option never-interactive
%%
0x[A-Fa-f0-9]+
-?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)? { yylval->p = arena_strdup(yyextra, yytext); character += yyleng; return Numeric; }
inf { return INFINITY; }
\" { return stringliteral(yyscanner); }
data\( { return binaryliteral(yyscanner); }
− { return MINUS; }
× { return MUL; }
÷ { return DIV; }
° { return DEGREES; }
[ \t\r\026]+ { character += yyleng; }
\n { ++line; character = 1; }
\357\273\277 { ; }
[A-Za-z][A-za-z0-9\x80-\xFF.-]* { yylval->p = arena_strdup(yyextra, yytext); character += yyleng; return Identifier; }
. { ++character; return *yytext; }
<<EOF>> { return EOF_; }
%%
static int
stringliteral(yyscan_t yyscanner)
{
struct yyguts_t *yyg = (struct yyguts_t *) yyscanner;
int ch;
ARRAY(char) result;
ARRAY_INIT(&result);
while(EOF != (ch = input(yyscanner)))
{
if(ch == '\"')
break;
if(ch == '\n')
{
++line;
character = 1;
}
ARRAY_ADD(&result, ch);
}
yylval->p = arena_strndup(yyextra, &ARRAY_GET(&result, 0),
ARRAY_COUNT(&result));
ARRAY_FREE(&result);
return StringLiteral;
}
static int
binaryliteral(yyscan_t yyscanner)
{
struct yyguts_t *yyg = (struct yyguts_t *) yyscanner;
int ch;
ARRAY(char) result;
ARRAY_INIT(&result);
while(EOF != (ch = input(yyscanner)))
{
if(ch == ')')
break;
if(ch == '\n')
{
++line;
character = 1;
}
if (!isspace (ch))
ARRAY_ADD(&result, ch);
}
yylval->p = arena_strndup(yyextra, &ARRAY_GET(&result, 0),
ARRAY_COUNT(&result));
ARRAY_FREE(&result);
return BinaryLiteral;
}
int
script_parse_file(struct script_parse_context *context, FILE *file)
{
YY_BUFFER_STATE buf;
int result = -1;
memset(context, 0, sizeof(*context));
yylex_init(&context->scanner);
arena_init(&context->statement_arena);
context->error = 0;
if(0 != (buf = yy_create_buffer(file, YY_BUF_SIZE, context->scanner)))
{
character = 1;
line = 1;
yy_switch_to_buffer(buf, context->scanner);
yyset_extra(&context->statement_arena, context->scanner);
result = yyparse(context);
if (context->error)
result = -1;
yy_delete_buffer(buf, context->scanner);
}
yylex_destroy(context->scanner);
return result;
}