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
162
163
164
165
166
|
/* SPDX-License-Identifier: copyleft-next-0.3.1 */
/* Copyright 2024 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */
%option reentrant noyywrap nounput noinput nodefault
%{
#define FROM_LEXER
#include <fwd/parser.h>
#include <fwd/debug.h>
static void update_yylloc(struct parser *parser, YYLTYPE *lloc, const char *text)
{
(void)parser;
lloc->first_line = lloc->last_line;
lloc->first_column = lloc->last_column;
for (size_t i = 0; text[i] != 0; ++i) {
if (text[i] == '\n') {
lloc->last_line++;
/* flex uses 1 based indexing */
lloc->last_column = 1;
} else {
lloc->last_column++;
}
}
}
#define YY_USER_ACTION update_yylloc(parser, yylloc, yytext);
%}
HEX 0[xX][0-9a-fA-F]+
DEC -?[0-9]+
OCT 0[0-8]+
BIN 0b[0-1]+
INT {HEX}|{DEC}|{OCT}|{BIN}
HEXF [+-]?0[xX][0-9a-fA-F]+([pP][+-]?[0-9]+)
DECF [+-]?[0-9]+[.]([eE]?[+-]?[0-9]+)?[fF]?
ID [_a-zA-Z][_a-zA-Z0-9]*
APPLY {ID}!
STRING \"(\\.|[^"\\])*\"
%x SC_COMMENT
%%
"//".* {/* skip line comments */}
"/*" {BEGIN(SC_COMMENT);}
<SC_COMMENT>{
"/*" {++parser->comment_nesting;}
"*"+"/" {
if (parser->comment_nesting)
--parser->comment_nesting;
else
BEGIN(INITIAL);
}
"*"+ {}
[^/*\n]+ {}
[/] {}
\n {}
}
"::" {return SCOPE;}
"(" {return LPAREN;}
")" {return RPAREN;}
"{" {return LBRACE;}
"}" {return RBRACE;}
"[" {return LBRACKET;}
"]" {return RBRACKET;}
"." {return DOT;}
"," {return COMMA;}
";" {return SEMICOLON;}
":" {return COLON;}
"!" {return BANG;}
"+" {return PLUS;}
"-" {return MINUS;}
"*" {return STAR;}
"/" {return DIV;}
"%" {return REM;}
"^" {return XOR;}
"true" {
yylval->integer = 1;
return BOOL;
}
"false" {
yylval->integer = 0;
return BOOL;
}
'[^'\\]' {
/* regular character constant, 'a' */
yylval->integer = yytext[1];
return CHAR;
}
'\\.' {
/* escaped character constant */
yylval->integer = match_escape(yytext[2]);
return CHAR;
}
"?" {return QUESTION;}
"'" {return SQUOTE;}
"&" {return AND;}
"~" {return TILDE;}
"=" {return TO;}
"<" {return LT;}
">" {return GT;}
"<=" {return LE;}
">=" {return GE;}
"!=" {return NE;}
"==" {return EQ;}
"=>" {return FATARROW;}
"<<" {return LSHIFT;}
">>" {return RSHIFT;}
{STRING} {
/* seems risky, I know, but letting the parser choose when to allocate a
* new string seems to help with syntax error cleanup */
yylval->str = strdup(yytext);
return STRING;
}
{INT} {
yylval->integer = strtoll(yytext, 0, 0);
return INT;
}
{ID} {
yylval->str = strdup(yytext);
return ID;
}
{APPLY} {
/* strip trailing '!' */
char *s = yytext + strlen(yytext);
s[-1] = '\0';
yylval->str = strdup(yytext);
return APPLY;
}
[[:space:]]+ {/* skip whitespace */}
. {
struct src_issue issue;
issue.level = SRC_ERROR;
issue.loc = src_loc(*yylloc);
issue.fctx.fbuf = parser->buf;
issue.fctx.fname = parser->fname;
src_issue(issue, "Unexpected token: %s", yytext);
parser->failed = true;
}
%%
|