jftt/l2/3.lex

101 lines
1.5 KiB
Text

%{
#include <stdbool.h>
bool leave_docs = false;
bool in_docs = false;
%}
%x SINGLE_LINE
%x MULTI_LINE
%x STRING
%x INCLUDE
%%
<INITIAL>"#include <" { ECHO; BEGIN(INCLUDE); }
<INCLUDE>">" { ECHO; BEGIN(INITIAL); }
<INITIAL>\" { ECHO; BEGIN(STRING); }
<STRING>\\\" { ECHO; }
<STRING>\" { ECHO; BEGIN(INITIAL); }
<STRING>. { ECHO; }
<INITIAL>"///"|"//!" {
in_docs = true;
if (leave_docs) {
ECHO;
}
BEGIN(SINGLE_LINE);
}
<INITIAL>"/**/" { putchar(' '); }
<INITIAL>"/**"|"/*!" {
in_docs = true;
if (leave_docs) {
ECHO;
}
BEGIN(MULTI_LINE);
}
<INITIAL>"//" { BEGIN(SINGLE_LINE); }
<INITIAL>. { ECHO; }
<SINGLE_LINE>"\\\n" { }
<SINGLE_LINE>\n {
if (leave_docs && in_docs) {
ECHO;
in_docs = false;
}
BEGIN(INITIAL);
}
<SINGLE_LINE>. {
if (leave_docs && in_docs) {
ECHO;
}
}
<INITIAL>"/*" { BEGIN(MULTI_LINE); }
<MULTI_LINE>"*/" {
if (leave_docs && in_docs) {
ECHO;
in_docs = false;
} else {
putchar(' ');
}
BEGIN(INITIAL);
}
<MULTI_LINE>. {
if (leave_docs && in_docs) {
ECHO;
}
}
%%
// 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;
}