jftt/l2/3.lex

102 lines
1.5 KiB
Text
Raw Permalink Normal View History

2024-11-03 22:05:19 +01:00
%{
#include <stdbool.h>
bool leave_docs = false;
2024-11-09 16:01:47 +01:00
bool in_docs = false;
2024-11-03 22:05:19 +01:00
%}
%x SINGLE_LINE
%x MULTI_LINE
2024-11-09 15:48:43 +01:00
%x STRING
%x INCLUDE
2024-11-03 22:05:19 +01:00
%%
2024-11-09 15:48:43 +01:00
<INITIAL>"#include <" { ECHO; BEGIN(INCLUDE); }
<INCLUDE>">" { ECHO; BEGIN(INITIAL); }
<INITIAL>\" { ECHO; BEGIN(STRING); }
<STRING>\\\" { ECHO; }
<STRING>\" { ECHO; BEGIN(INITIAL); }
<STRING>. { ECHO; }
2024-11-03 22:05:19 +01:00
<INITIAL>"///"|"//!" {
2024-11-09 16:01:47 +01:00
in_docs = true;
2024-11-03 22:05:19 +01:00
if (leave_docs) {
ECHO;
}
2024-11-09 16:01:47 +01:00
BEGIN(SINGLE_LINE);
2024-11-03 22:05:19 +01:00
}
2024-11-09 15:48:43 +01:00
<INITIAL>"/**/" { putchar(' '); }
2024-11-03 22:05:19 +01:00
<INITIAL>"/**"|"/*!" {
2024-11-09 16:01:47 +01:00
in_docs = true;
2024-11-03 22:05:19 +01:00
if (leave_docs) {
ECHO;
}
2024-11-09 16:01:47 +01:00
BEGIN(MULTI_LINE);
2024-11-03 22:05:19 +01:00
}
<INITIAL>"//" { BEGIN(SINGLE_LINE); }
<INITIAL>. { ECHO; }
2024-11-09 15:48:43 +01:00
<SINGLE_LINE>"\\\n" { }
2024-11-09 16:01:47 +01:00
<SINGLE_LINE>\n {
if (leave_docs && in_docs) {
ECHO;
in_docs = false;
}
BEGIN(INITIAL);
}
<SINGLE_LINE>. {
if (leave_docs && in_docs) {
ECHO;
}
}
2024-11-03 22:05:19 +01:00
<INITIAL>"/*" { BEGIN(MULTI_LINE); }
2024-11-09 16:01:47 +01:00
<MULTI_LINE>"*/" {
if (leave_docs && in_docs) {
ECHO;
in_docs = false;
} else {
putchar(' ');
}
BEGIN(INITIAL);
}
<MULTI_LINE>. {
if (leave_docs && in_docs) {
ECHO;
}
}
2024-11-03 22:05:19 +01:00
%%
// single line comment
/*
* multi line comment
*/
/// single line doc 1
//! single line doc 2
/**
* multi line doc 1
*/
/*!
* multi line doc 2
*/
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc > 1) {
leave_docs = true;
}
yylex();
return EXIT_SUCCESS;
}