Code

d769e914a8535b9271bc6438029dcd1719fcf9f5
[lm2latex.git] / src / l2l_scanner.l
1 /*
2  * lm2latex - src/l2l_scanner.l
3  * Copyright (C) 2010 Sebastian Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 /*
29  * A Linux-Magazin markup to LaTeX converter -- LM markup scanner.
30  */
32 %{
33 #if HAVE_CONFIG_H
34 #       include "config.h"
35 #endif
37 #include <assert.h>
39 #include <ctype.h>
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
46 typedef struct {
47         char  *data;
48         size_t size;
49         size_t len;
50 } l2l_strbuf_t;
52 #define L2L_STRBUF_INIT { NULL, 0, 0 }
53 #define L2L_STRBUF_FREE(buf) ((buf)->size - (buf)->len)
54 #define L2L_STRBUF_END(buf) ((buf)->data + (buf)->len)
55 #define L2L_STRBUF_CLEAR(buf) ((buf)->len = 0)
57 static int
58 l2l_strbuf_append(l2l_strbuf_t *buf, const char *fmt, ...);
60 static void
61 l2l_strbuf_chomp(l2l_strbuf_t *buf);
63 static void
64 l2l_strbuf_destroy(l2l_strbuf_t *buf);
66 #define L2L_YY_APPEND_OR_ECHO(strbuf, ...) \
67         do { \
68                 if ((strbuf)) { \
69                         if (l2l_strbuf_append((strbuf), __VA_ARGS__)) \
70                                 YY_FATAL_ERROR("ERROR: internal error"); \
71                 } \
72                 else \
73                         fprintf(yyout, __VA_ARGS__); \
74         } while (0)
76 #define L2L_STATE_HEADER_OR_ABORT \
77         do { \
78                 if (l2l_scanner_state != L2L_HEADER) \
79                         YY_FATAL_ERROR("ERROR: unexpected header definition"); \
80         } while (0)
82 #define L2L_PATTERN_IS(str) (! strncmp(yytext, (str), strlen(str)))
83 #define L2L_PATTERN_IS_HEADER \
84         (L2L_PATTERN_IS("@R:") || L2L_PATTERN_IS("@SW:") \
85                 || L2L_PATTERN_IS("@D:") || L2L_PATTERN_IS("@T:") \
86                 || L2L_PATTERN_IS("@A:") || L2L_PATTERN_IS("@V:"))
87 %}
89 %option yylineno
90 %option noyywrap
92 /* optimize for "fast scanning"; create an 8-bit scanner */
93 %option fast 8bit
95 /* generate a reentrant scanner in its own namespace */
96 %option reentrant
97 %option prefix="l2l_yy" outfile="lex.yy.c"
99 %option verbose warn
101 %s box
103 %x caption box_caption
104 %x command
105 %x headertxt
106 %x itemize
107 %x listing
108 %x title
109 %x url
111 COMMENT @#:.*
112 WHITESPACE [ \t]
114 ANY_COMMAND @(R|SW|D|T|V|A|L|ZT|LI|B|Bi|IT|IL|IE|KT|KL|KE|TT|TH|TL|TE):
116 %%
118 %{
119         enum {
120                 L2L_HEADER,
121                 L2L_BODY,
122                 L2L_FIGURE,
124                 L2L_UNQUOTED_TEXT,
125                 L2L_QUOTED_TEXT,
126         };
128         int l2l_scanner_state = L2L_HEADER;
129         int l2l_quote_state   = L2L_UNQUOTED_TEXT;
131         /* text formatting */
132         _Bool l2l_italic      = 0;
133         _Bool l2l_bold        = 0;
134         _Bool l2l_code        = 0;
135         _Bool l2l_sup         = 0;
136         _Bool l2l_sub         = 0;
137         _Bool l2l_li_italic   = 0;
139         /* title page information */
140         l2l_strbuf_t l2l_category = L2L_STRBUF_INIT;
141         l2l_strbuf_t l2l_keyword  = L2L_STRBUF_INIT;
142         l2l_strbuf_t l2l_title    = L2L_STRBUF_INIT;
143         l2l_strbuf_t l2l_headline = L2L_STRBUF_INIT;
144         l2l_strbuf_t l2l_author   = L2L_STRBUF_INIT;
145         l2l_strbuf_t l2l_abstract = L2L_STRBUF_INIT;
147         /* temp. buffers */
148         l2l_strbuf_t l2l_caption  = L2L_STRBUF_INIT;
149         l2l_strbuf_t l2l_ititle   = L2L_STRBUF_INIT;
151         l2l_strbuf_t *l2l_buf_ptr = NULL;
153         int l2l_last_yystart = INITIAL;
154 %}
156 {COMMENT} { fprintf(yyout, "%%%s", yytext + strlen("@#:")); }
158 <INITIAL,box,caption,box_caption,command,headertxt,itemize,title>{
159         /* LaTeX reserved characters */
160         [#$%&_{}] { L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\\%s", yytext); }
161         [\^~]     { L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\\%s{}", yytext); }
162         [\\]      { L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "$\\backslash$"); }
164         /* special characters */
165         /* '---' and '--' are handled by LaTeX */
166         \*\* L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "~");
168         :\* L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\\,");
170         ["] {
171                 if (l2l_quote_state == L2L_UNQUOTED_TEXT) {
172                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\"`");
173                         l2l_quote_state = L2L_QUOTED_TEXT;
174                 }
175                 else {
176                         assert(l2l_quote_state == L2L_QUOTED_TEXT);
177                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\"'");
178                         l2l_quote_state = L2L_UNQUOTED_TEXT;
179                 }
180         }
182         /* text formatting */
183         \<[iI]> {
184                 if (l2l_italic) {
185                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "}");
186                         l2l_italic = 0;
187                 }
188                 else {
189                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "{\\itshape ");
190                         l2l_italic = 1;
191                 }
192         }
194         \<[bB]> {
195                 if (l2l_bold) {
196                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "}");
197                         l2l_bold = 0;
198                 }
199                 else {
200                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "{\\bfseries ");
201                         l2l_bold = 1;
202                 }
203         }
205         \<[cC]> {
206                 if (l2l_code) {
207                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "}");
208                         l2l_code = 0;
209                 }
210                 else {
211                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "{\\ttfamily ");
212                         l2l_code = 1;
213                 }
214         }
216         \<[\+]> {
217                 if (l2l_sup) {
218                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "}}");
219                         l2l_sup = 0;
220                 }
221                 else {
222                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr,
223                                         "\\ensuremath{^{\rmfamily ");
224                         l2l_sup = 1;
225                 }
226         }
228         \<[\-]> {
229                 if (l2l_sub) {
230                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "}}");
231                         l2l_sub = 0;
232                 }
233                 else {
234                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr,
235                                         "\\ensuremath{_{\rmfamily ");
236                         l2l_sub = 1;
237                 }
238         }
241 \<[uU]> {
242         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\\url{");
243         BEGIN(url);
246 <url>\<[uU]> {
247         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "}");
248         BEGIN(l2l_last_yystart);
251 <url>.|\n {
252         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "%s", yytext);
255         /*
256          * header
257          */
259 @R:{WHITESPACE}* {
260         L2L_STATE_HEADER_OR_ABORT;
261         l2l_buf_ptr = &l2l_category;
262         BEGIN(headertxt);
265 @SW:{WHITESPACE}* {
266         L2L_STATE_HEADER_OR_ABORT;
267         l2l_buf_ptr = &l2l_keyword;
268         BEGIN(headertxt);
271 @D:{WHITESPACE}* {
272         L2L_STATE_HEADER_OR_ABORT;
273         l2l_buf_ptr = &l2l_title;
274         BEGIN(headertxt);
277 @T:{WHITESPACE}* {
278         L2L_STATE_HEADER_OR_ABORT;
279         l2l_buf_ptr = &l2l_headline;
280         BEGIN(headertxt);
283 @A:{WHITESPACE}* {
284         L2L_STATE_HEADER_OR_ABORT;
285         l2l_buf_ptr = &l2l_author;
286         BEGIN(headertxt);
289 @V:{WHITESPACE}* {
290         L2L_STATE_HEADER_OR_ABORT;
291         l2l_buf_ptr = &l2l_abstract;
292         BEGIN(headertxt);
295 <headertxt>{ANY_COMMAND} {
296         if (l2l_buf_ptr) {
297                 l2l_strbuf_chomp(l2l_buf_ptr);
298                 l2l_buf_ptr = NULL;
299         }
301         if (! L2L_PATTERN_IS_HEADER) {
302                 assert(l2l_scanner_state == L2L_HEADER);
304                 if (l2l_category.len || l2l_keyword.len
305                                 || l2l_title.len || l2l_headline.len) {
306                         fprintf(yyout, "\\title{");
308                         if (l2l_category.len || l2l_keyword.len) {
309                                 fprintf(yyout, "{\\scriptsize [");
310                                 if (l2l_category.len) {
311                                         fprintf(yyout, "%s", l2l_category.data);
312                                         if (l2l_keyword.len)
313                                                 fprintf(yyout, " $\\rightarrow$ ");
314                                 }
315                                 if (l2l_keyword.len)
316                                         fprintf(yyout, "%s", l2l_keyword.data);
317                                 fprintf(yyout, "]}\\\\\n\\vspace{5mm}\n");
318                         }
320                         if (l2l_headline.len) {
321                                 if (l2l_title.len)
322                                         fprintf(yyout, "\\normalsize %s\\\\\n\\LARGE ",
323                                                         l2l_title.data);
324                                 fprintf(yyout, "%s", l2l_headline.data);
325                         }
326                         else if (l2l_title.len)
327                                 fprintf(yyout, "%s", l2l_title.data);
329                         fprintf(yyout, "}\n");
330                 }
332                 if (l2l_author.len)
333                         fprintf(yyout, "\\author{%s}\n", l2l_author.data);
334                 fprintf(yyout, "\\date{\\today}\n\n");
336                 fprintf(yyout, "\\begin{document}\n\n");
337                 l2l_scanner_state = L2L_BODY;
339                 fprintf(yyout, "\\maketitle\n\n");
341                 if (l2l_abstract.len)
342                         fprintf(yyout, "\\begin{abstract}\n%s\n"
343                                         "\\end{abstract}\n\n", l2l_abstract.data);
345                 fprintf(yyout, "\\twocolumn\n\n");
346         }
348         yyless(0);
349         BEGIN(l2l_last_yystart);
352 <headertxt>{COMMENT} { /* ignore */ }
354 <headertxt>.|\n {
355         if (l2l_buf_ptr)
356                 if (l2l_strbuf_append(l2l_buf_ptr, "%s", yytext))
357                         YY_FATAL_ERROR("ERROR: internal error");
358         /* else ignore */
361         /*
362          * body
363          */
365 \n\n{WHITESPACE}*\*{WHITESPACE}* {
366         fprintf(yyout, "\n\n\\begin{itemize}\n\\item ");
367         BEGIN(itemize);
370 <itemize>({WHITESPACE}|\n)*\n\n |
371 <itemize>({WHITESPACE}|\n)*{ANY_COMMAND} {
372         fprintf(yyout, "\n\\end{itemize}");
373         yyless(0);
374         BEGIN(l2l_last_yystart);
377         /* this will produce a longer match and, thus, take precedence over
378          * the pattern above matching an empty line after the itemize */
379 <itemize>({WHITESPACE}|\n)*\n{WHITESPACE}*\*{WHITESPACE}* {
380         fprintf(yyout, "\n\\item ");
382 <itemize>({WHITESPACE}|\n)*\n\n({WHITESPACE}{2,}|\t) ECHO;
384 <itemize>. ECHO;
386 @ZT:{WHITESPACE}* {
387         fprintf(yyout, "\\subsection*{");
388         BEGIN(command);
391 <command>({WHITESPACE}|\n)+{ANY_COMMAND} {
392         fprintf(yyout, "}");
393         yyless(0);
394         BEGIN(l2l_last_yystart);
397 <command>. ECHO;
399 @LI:{WHITESPACE}*\n? {
400         /* \xb7 = MIDDLE DOT (iso-8859-1 and iso-8859-15) */
401         fprintf(yyout, "\\begin{lstlisting}[escapechar=\xb7]\n");
402         BEGIN(listing);
405 <listing>\n*{ANY_COMMAND} {
406         fprintf(yyout, "\n\\end{lstlisting}");
407         yyless(0);
408         BEGIN(l2l_last_yystart);
411 <listing>\<§§I> {
412         if (l2l_li_italic) {
413                 L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\xb7");
414                 l2l_li_italic = 0;
415         }
416         else {
417                 L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\xb7\\itshape ");
418                 l2l_li_italic = 1;
419         }
422 <listing>§§\n /* ignore => lstlisting will insert appropriate arrows */;
424 <listing>. ECHO;
426         /*
427          * images/boxes
428          */
430 @(B|KT):{WHITESPACE}* {
431         if (l2l_scanner_state != L2L_FIGURE) {
432                 fprintf(yyout, "\\begin{figure}\n");
433                 l2l_scanner_state = L2L_FIGURE;
434         }
435         else {
436                 l2l_scanner_state = L2L_BODY;
437         }
439         l2l_buf_ptr = &l2l_caption;
440         if (! strncmp(yytext, "@B:", 3))
441                 BEGIN(caption);
442         else if (! strncmp(yytext, "@KT:", 4))
443                 BEGIN(box_caption);
444         else
445                 assert(0);
448 @Bi:.*\n {
449         char *filename;
451         yytext[yyleng - 1] = '\0';
453         filename = yytext + strlen("@Bi:");
454         while (isspace((int)*filename))
455                 ++filename;
457         if (l2l_scanner_state != L2L_FIGURE) {
458                 fprintf(yyout, "\\begin{figure}\n");
459                 l2l_scanner_state = L2L_FIGURE;
460         }
461         else {
462                 l2l_scanner_state = L2L_BODY;
463         }
465         fprintf(yyout, "\\includegraphics[width=\\columnwidth]{%s}\n", filename);
467         if (l2l_scanner_state == L2L_BODY)
468                 fprintf(yyout, "\\end{figure}\n");
471 @KL: {
472         if (l2l_scanner_state != L2L_FIGURE) {
473                 fprintf(yyout, "\\begin{figure}\n");
474                 l2l_scanner_state = L2L_FIGURE;
475         }
476         else {
477                 l2l_scanner_state = L2L_BODY;
478         }
480         l2l_last_yystart = box;
481         BEGIN(box);
484 <box>@(KE|L):{WHITESPACE}* {
485         fprintf(yyout, "\\end{figure}");
486         l2l_scanner_state = L2L_BODY;
487         l2l_last_yystart = INITIAL;
488         BEGIN(l2l_last_yystart);
491 <caption,box_caption>{ANY_COMMAND} {
492         if (l2l_buf_ptr) {
493                 l2l_strbuf_chomp(l2l_buf_ptr);
494                 l2l_buf_ptr = NULL;
495         }
497         if (l2l_caption.len) {
498                 fprintf(yyout, "\\caption*{%s}\n", l2l_caption.data);
499                 L2L_STRBUF_CLEAR(&l2l_caption);
500         }
502         if ((l2l_scanner_state == L2L_BODY) && (YY_START != box_caption))
503                 fprintf(yyout, "\\end{figure}\n\n");
505         yyless(0);
506         if (YY_START == caption)
507                 BEGIN(l2l_last_yystart);
508         else if (YY_START == box_caption) {
509                 l2l_last_yystart = box;
510                 BEGIN(box);
511         }
512         else
513                 assert(0);
516 <caption,box_caption>{COMMENT} { /* ignore */ }
518 <caption,box_caption>.|\n {
519         if (l2l_buf_ptr)
520                 if (l2l_strbuf_append(l2l_buf_ptr, "%s", yytext))
521                         YY_FATAL_ERROR("ERROR: internal error");
522         /* else ignore */
525         /*
526          * infos
527          */
529 @IT:{WHITESPACE}* {
530         l2l_buf_ptr = &l2l_ititle;
531         BEGIN(title);
534 <title>{ANY_COMMAND} {
535         if (l2l_buf_ptr) {
536                 l2l_strbuf_chomp(l2l_buf_ptr);
537                 l2l_buf_ptr = NULL;
538         }
540         if (l2l_ititle.len) {
541                 fprintf(yyout, "\\subsection*{\\rule{\\columnwidth}{1pt}"
542                                 "\\newline %s}\n", l2l_ititle.data);
543                 L2L_STRBUF_CLEAR(&l2l_ititle);
544         }
546         yyless(0);
547         BEGIN(l2l_last_yystart);
550 <title>{COMMENT} { /* ignore */ }
552 <title>.|\n {
553         if (l2l_buf_ptr)
554                 if (l2l_strbuf_append(l2l_buf_ptr, "%s", yytext))
555                         YY_FATAL_ERROR("ERROR: internal error");
556         /* else ignore */
559 @IL: { /* ignore */ }
561 @IE: { /* ignore */ }
563         /*
564          * tables
565          */
567 @TT: /* TODO */;
569 @TH: /* TODO */;
571 @TL: /* TODO */;
573 @TE: /* TODO */;
575         /* let all other rules that match "@L:" have a higher preceedence */
576 @L:{WHITESPACE}* { /* nothing to do */ }
578 <<EOF>> {
579         if (l2l_scanner_state == L2L_BODY) {
580                 fprintf(yyout, "\\end{document}\n");
581         }
582         else
583                 YY_FATAL_ERROR("ERROR: incomplete document");
585         l2l_strbuf_destroy(&l2l_category);
586         l2l_strbuf_destroy(&l2l_keyword);
587         l2l_strbuf_destroy(&l2l_title);
588         l2l_strbuf_destroy(&l2l_headline);
589         l2l_strbuf_destroy(&l2l_author);
590         l2l_strbuf_destroy(&l2l_abstract);
592         l2l_strbuf_destroy(&l2l_caption);
593         l2l_strbuf_destroy(&l2l_title);
595         yyterminate();
598 %%
600 static int
601 l2l_strbuf_append(l2l_strbuf_t *buf, const char *fmt, ...)
603         size_t  str_len;
604         va_list ap;
606         int status;
608         if ((! buf) || (! fmt))
609                 return 0;
611         va_start(ap, fmt);
612         status = vsnprintf(NULL, 0, fmt, ap);
613         va_end(ap);
615         if (status <= 0)
616                 return -1;
618         str_len = (size_t)status;
620         if (str_len > L2L_STRBUF_FREE(buf)) {
621                 char  *new_data;
622                 size_t new_size = 2 * buf->size;
624                 if (str_len > buf->size)
625                         new_size += str_len;
627                 new_data = realloc(buf->data, new_size + 1);
628                 if (! new_data) {
629                         l2l_strbuf_destroy(buf);
630                         return -1;
631                 }
633                 buf->data = new_data;
634                 buf->size = new_size;
635         }
636         assert(buf->len + str_len <= buf->size);
638         va_start(ap, fmt);
639         status = vsnprintf(L2L_STRBUF_END(buf), L2L_STRBUF_FREE(buf) + 1, fmt, ap);
640         va_end(ap);
642         if (status <= 0)
643                 return -1;
645         assert((size_t)status == str_len);
646         buf->len += str_len;
647         buf->data[buf->len] = '\0';
648         return 0;
649 } /* l2l_strbuf_append */
651 static void
652 l2l_strbuf_chomp(l2l_strbuf_t *buf)
654         if (! buf)
655                 return;
657         while (buf->len && isspace((int)buf->data[buf->len - 1])) {
658                 buf->data[buf->len - 1] = '\0';
659                 --buf->len;
660         }
661 } /* l2l_strbuf_chomp */
663 static void
664 l2l_strbuf_destroy(l2l_strbuf_t *buf)
666         if (! buf)
667                 return;
669         if (buf->data)
670                 free(buf->data);
671         memset(buf, 0, sizeof(*buf));
672 } /* l2l_strbuf_destroy */
674 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */