aboutsummaryrefslogtreecommitdiff
path: root/lightroff.lex
blob: 93264157cdfbc438e09a147c5f0606f92e32d1ce (plain)
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
%option noyywrap
%x NEW
%x REQ
%x RAW
%x ESC
%x RES

%%

\\		BEGIN(ESC);
^\n*		/* Ignore newlines at beginning of file. */
\n		BEGIN(NEW);
\<!\[		BEGIN(RAW);
\<!--.*-->\n*	/* Ignore comment. */
^<		printf("."); BEGIN(REQ);	/* Beginning of file. */
\<		printf("\\c\n."); BEGIN(REQ);
.		ECHO;

	/* Handle text following newlines. */

<NEW>\n		/* Ignore consecutive newlines. */
<NEW>\\		printf("\n"); BEGIN(ESC);
<NEW><!\[\n?	printf("\n"); BEGIN(RAW);
<NEW><!--.*-->	/* Ignore comment. */
<NEW><		printf("\n."); BEGIN(REQ);
<NEW>\.		printf("\n\\&."); BEGIN(0);
<NEW>.		printf("\n"); BEGIN(0); REJECT;

	/* Translate request. */

<REQ>\\		BEGIN(RES);
<REQ>>		BEGIN(NEW);

	/* Pass through raw troff code. */

<RAW>\n\]>	BEGIN(NEW);
<RAW>\]>	BEGIN(0);
<RAW>.		ECHO;

	/* Escape next character. */

<ESC>\\		printf("\\\\"); BEGIN(0);
<ESC><		ECHO; BEGIN(0);
<ESC>.		printf("\\"); ECHO; BEGIN(0);

	/* Escape next character within request. */

<RES>\\		printf("\\\\"); BEGIN(REQ);
<RES>>		ECHO; BEGIN(REQ);
<RES>.		printf("\\"); ECHO; BEGIN(REQ);

%%

int
main()
{
	char *st;

	yylex();

	switch (YYSTATE) {
	case 0: return 0;
	case NEW: printf("\n"); return 0;
	case REQ: st = "request"; break;
	case RAW: st = "raw section"; break;
	case ESC: st = "escape"; break;
	case RES: st = "escape (within request)"; break;
	}

	fprintf(stderr, "%s unfinished\n", st);
	return 1;
}