summary refs log tree commit diff
path: root/bin/c11.l
blob: 21e7d44b79bbcf9a08da038cb60be7c81e9919f7 (plain) (blame)
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
/* Copyright (C) 2020  C. McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

%option prefix="c11"
%option noyywrap

%{
#include "hilex.h"
%}

%s MacroLine MacroInclude
%x CharLiteral StringLiteral

ident [_[:alpha:]][_[:alnum:]]*
width "*"|[0-9]+

%%
	static int pop = INITIAL;

[[:blank:]]+ { return Normal; }

^"%"[%{}]? {
	BEGIN(pop = MacroLine);
	return Macro;
}

([-+*/%&|^=!<>]|"<<"|">>")"="? |
[=~.?:]|"++"|"--"|"&&"|"||"|"->" |
sizeof|(_A|alignof) {
	return Operator;
}

([1-9][0-9]*|"0"[0-7]*|"0x"[[:xdigit:]]+)([ulUL]{0,3}) |
[0-9]*("."[0-9]*)?([eE][+-]?[0-9]+)?[flFL]? |
"0x"[[:xdigit:]]*("."[[:xdigit:]]*)?([pP][+-]?[0-9]+)[flFL]? {
	return Number;
}

auto|break|case|const|continue|default|do|else|enum|extern|for|goto|if|inline |
register|restrict|return|static|struct|switch|typedef|union|volatile|while |
(_A|a)lignas|_Atomic|_Generic|(_N|n)oreturn|(_S|s)tatic_assert |
(_T|t)hread_local {
	return Keyword;
}

^"#"[[:blank:]]*(include|import) {
	BEGIN(pop = MacroInclude);
	return Macro;
}
^"#"[[:blank:]]*{ident} {
	BEGIN(pop = MacroLine);
	return Macro;
}
<MacroInclude>"<"[^>]+">" {
	return String;
}
<MacroLine,MacroInclude>{
	"\n" {
		BEGIN(pop = INITIAL);
		return Normal;
	}
	"\\\n" { return Macro; }
	{ident} { return Macro; }
}

{ident} { return Identifier; }

"//"([^\n]|"\\\n")* |
"/*"([^*]|"*"[^/])*"*"+"/" {
	return Comment;
}

[LUu]?"'" {
	BEGIN(CharLiteral);
	return String;
}
([LU]|u8?)?"\"" {
	BEGIN(StringLiteral);
	return String;
}

<CharLiteral,StringLiteral>{
	"\\"[''""?\\abfnrtv] |
	"\\"([0-7]{1,3}) |
	"\\x"([[:xdigit:]]{2}) |
	"\\u"([[:xdigit:]]{4}) |
	"\\U"([[:xdigit:]]{8}) {
		return StringEscape;
	}
}
<StringLiteral>{
	"%%" |
	"%"[ #+-0]*{width}?("."{width})?([Lhjltz]|hh|ll)?[AEFGXacdefginopsux] {
		return StringFormat;
	}
}

<CharLiteral>{
	"'" {
		BEGIN(pop);
		return String;
	}
	[^\\'']+|. { return String; }
}
<StringLiteral>{
	"\"" {
		BEGIN(pop);
		return String;
	}
	[^%\\""]+|. { return String; }
}

<MacroLine,MacroInclude>. {
	return Macro;
}

.|\n { return Normal; }

%{
	(void)yyunput;
	(void)input;
%}

%%

const struct Lexer LexC = { yylex, &yyin, &yytext };