Code

d02479d7370525f4336ccf14dead9eded4cb5b78
[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         }
240         \[[0-9]+\] {
241                 L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr,
242                                 "{\\bfseries\\textcolor{DarkBlue}{%s}}", yytext);
243         }
246 \<[uU]> {
247         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\\url{");
248         BEGIN(url);
251 <url>\<[uU]> {
252         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "}");
253         BEGIN(l2l_last_yystart);
256 <url>.|\n {
257         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "%s", yytext);
260 (Abbildung|Listing)({WHITESPACE}|\n)+[0-9]+ {
261         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\\textcolor{DarkBlue}{%s}", yytext);
264         /*
265          * header
266          */
268 @R:{WHITESPACE}* {
269         L2L_STATE_HEADER_OR_ABORT;
270         l2l_buf_ptr = &l2l_category;
271         BEGIN(headertxt);
274 @SW:{WHITESPACE}* {
275         L2L_STATE_HEADER_OR_ABORT;
276         l2l_buf_ptr = &l2l_keyword;
277         BEGIN(headertxt);
280 @D:{WHITESPACE}* {
281         L2L_STATE_HEADER_OR_ABORT;
282         l2l_buf_ptr = &l2l_title;
283         BEGIN(headertxt);
286 @T:{WHITESPACE}* {
287         L2L_STATE_HEADER_OR_ABORT;
288         l2l_buf_ptr = &l2l_headline;
289         BEGIN(headertxt);
292 @A:{WHITESPACE}* {
293         L2L_STATE_HEADER_OR_ABORT;
294         l2l_buf_ptr = &l2l_author;
295         BEGIN(headertxt);
298 @V:{WHITESPACE}* {
299         L2L_STATE_HEADER_OR_ABORT;
300         l2l_buf_ptr = &l2l_abstract;
301         BEGIN(headertxt);
304 <headertxt>{ANY_COMMAND} {
305         if (l2l_buf_ptr) {
306                 l2l_strbuf_chomp(l2l_buf_ptr);
307                 l2l_buf_ptr = NULL;
308         }
310         if (! L2L_PATTERN_IS_HEADER) {
311                 assert(l2l_scanner_state == L2L_HEADER);
313                 if (l2l_category.len || l2l_keyword.len
314                                 || l2l_title.len || l2l_headline.len) {
315                         fprintf(yyout, "\\title{");
317                         if (l2l_category.len || l2l_keyword.len) {
318                                 fprintf(yyout, "{\\scriptsize [");
319                                 if (l2l_category.len) {
320                                         fprintf(yyout, "%s", l2l_category.data);
321                                         if (l2l_keyword.len)
322                                                 fprintf(yyout, " $\\rightarrow$ ");
323                                 }
324                                 if (l2l_keyword.len)
325                                         fprintf(yyout, "%s", l2l_keyword.data);
326                                 fprintf(yyout, "]}\\\\\n\\vspace{5mm}\n");
327                         }
329                         if (l2l_headline.len) {
330                                 if (l2l_title.len)
331                                         fprintf(yyout, "\\normalsize %s\\\\\n\\LARGE ",
332                                                         l2l_title.data);
333                                 fprintf(yyout, "%s", l2l_headline.data);
334                         }
335                         else if (l2l_title.len)
336                                 fprintf(yyout, "%s", l2l_title.data);
338                         fprintf(yyout, "}\n");
339                 }
341                 if (l2l_author.len)
342                         fprintf(yyout, "\\author{%s}\n", l2l_author.data);
343                 fprintf(yyout, "\\date{\\today}\n\n");
345                 fprintf(yyout, "\\begin{document}\n\n");
346                 l2l_scanner_state = L2L_BODY;
348                 fprintf(yyout, "\\maketitle\n\n");
350                 if (l2l_abstract.len)
351                         fprintf(yyout, "\\begin{abstract}\n%s\n"
352                                         "\\end{abstract}\n\n", l2l_abstract.data);
354                 fprintf(yyout, "\\twocolumn\n\n");
355         }
357         yyless(0);
358         BEGIN(l2l_last_yystart);
361 <headertxt>{COMMENT} { /* ignore */ }
363 <headertxt>.|\n {
364         if (l2l_buf_ptr)
365                 if (l2l_strbuf_append(l2l_buf_ptr, "%s", yytext))
366                         YY_FATAL_ERROR("ERROR: internal error");
367         /* else ignore */
370         /*
371          * body
372          */
374 \n\n{WHITESPACE}*\*{WHITESPACE}* {
375         fprintf(yyout, "\n\n\\begin{itemize}\n\\item ");
376         BEGIN(itemize);
379 <itemize>({WHITESPACE}|\n)*\n\n |
380 <itemize>({WHITESPACE}|\n)*{ANY_COMMAND} {
381         fprintf(yyout, "\n\\end{itemize}");
382         yyless(0);
383         BEGIN(l2l_last_yystart);
386         /* this will produce a longer match and, thus, take precedence over
387          * the pattern above matching an empty line after the itemize */
388 <itemize>({WHITESPACE}|\n)*\n{WHITESPACE}*\*{WHITESPACE}* {
389         fprintf(yyout, "\n\\item ");
391 <itemize>({WHITESPACE}|\n)*\n\n({WHITESPACE}{2,}|\t) ECHO;
393 <itemize>. ECHO;
395 @ZT:{WHITESPACE}* {
396         fprintf(yyout, "\\subsection*{");
397         BEGIN(command);
400 <command>({WHITESPACE}|\n)+{ANY_COMMAND} {
401         fprintf(yyout, "}");
402         yyless(0);
403         BEGIN(l2l_last_yystart);
406 <command>. ECHO;
408 @LI:{WHITESPACE}*\n? {
409         /* \xb7 = MIDDLE DOT (iso-8859-1 and iso-8859-15) */
410         fprintf(yyout, "\\begin{lstlisting}[escapechar=\xb7]\n");
411         BEGIN(listing);
414 <listing>\n*{ANY_COMMAND} {
415         fprintf(yyout, "\n\\end{lstlisting}");
416         yyless(0);
417         BEGIN(l2l_last_yystart);
420 <listing>\<§§I> {
421         if (l2l_li_italic) {
422                 L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\xb7");
423                 l2l_li_italic = 0;
424         }
425         else {
426                 L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\xb7\\itshape ");
427                 l2l_li_italic = 1;
428         }
431 <listing>§§\n /* ignore => lstlisting will insert appropriate arrows */;
433 <listing>. ECHO;
435         /*
436          * images/boxes
437          */
439 @(B|KT):{WHITESPACE}* {
440         _Bool is_image = 0;
441         _Bool is_box   = 0;
443         if (! strncmp(yytext, "@B:", 3))
444                 is_image = 1;
445         else if (! strncmp(yytext, "@KT:", 4))
446                 is_box = 1;
448         if (l2l_scanner_state != L2L_FIGURE) {
449                 if (is_box)
450                         fprintf(yyout, "\\begin{l2lbox}\n");
451                 else
452                         fprintf(yyout, "\\begin{figure}\n");
453                 l2l_scanner_state = L2L_FIGURE;
454         }
455         else {
456                 l2l_scanner_state = L2L_BODY;
457         }
459         l2l_buf_ptr = &l2l_caption;
460         if (is_image)
461                 BEGIN(caption);
462         else if (is_box)
463                 BEGIN(box_caption);
464         else
465                 assert(0);
468 @Bi:.*\n {
469         char *filename;
471         yytext[yyleng - 1] = '\0';
473         filename = yytext + strlen("@Bi:");
474         while (isspace((int)*filename))
475                 ++filename;
477         if (l2l_scanner_state != L2L_FIGURE) {
478                 fprintf(yyout, "\\begin{figure}\n");
479                 l2l_scanner_state = L2L_FIGURE;
480         }
481         else {
482                 l2l_scanner_state = L2L_BODY;
483         }
485         fprintf(yyout, "\\includegraphics[width=\\columnwidth]{%s}\n", filename);
487         if (l2l_scanner_state == L2L_BODY)
488                 fprintf(yyout, "\\end{figure}\n");
491 @KL: {
492         if (l2l_scanner_state != L2L_FIGURE) {
493                 fprintf(yyout, "\\begin{l2lbox}\n");
494                 l2l_scanner_state = L2L_FIGURE;
495         }
496         else {
497                 l2l_scanner_state = L2L_BODY;
498         }
500         l2l_last_yystart = box;
501         BEGIN(box);
504 <box>@(KE|L):{WHITESPACE}* {
505         fprintf(yyout, "\\end{l2lbox}");
506         l2l_scanner_state = L2L_BODY;
507         l2l_last_yystart = INITIAL;
508         BEGIN(l2l_last_yystart);
511 <caption,box_caption>{ANY_COMMAND} {
512         if (l2l_buf_ptr) {
513                 l2l_strbuf_chomp(l2l_buf_ptr);
514                 l2l_buf_ptr = NULL;
515         }
517         if (l2l_caption.len) {
518                 fprintf(yyout, "\\caption*{%s}\n", l2l_caption.data);
519                 L2L_STRBUF_CLEAR(&l2l_caption);
520         }
522         if ((l2l_scanner_state == L2L_BODY) && (YY_START != box_caption))
523                 fprintf(yyout, "\\end{figure}\n\n");
525         yyless(0);
526         if (YY_START == caption)
527                 BEGIN(l2l_last_yystart);
528         else if (YY_START == box_caption) {
529                 l2l_last_yystart = box;
530                 BEGIN(box);
531         }
532         else
533                 assert(0);
536 <caption,box_caption>{COMMENT} { /* ignore */ }
538 <caption,box_caption>.|\n {
539         if (l2l_buf_ptr)
540                 if (l2l_strbuf_append(l2l_buf_ptr, "%s", yytext))
541                         YY_FATAL_ERROR("ERROR: internal error");
542         /* else ignore */
545         /*
546          * infos
547          */
549 @IT:{WHITESPACE}* {
550         l2l_buf_ptr = &l2l_ititle;
551         BEGIN(title);
554 <title>{ANY_COMMAND} {
555         if (l2l_buf_ptr) {
556                 l2l_strbuf_chomp(l2l_buf_ptr);
557                 l2l_buf_ptr = NULL;
558         }
560         if (l2l_ititle.len) {
561                 fprintf(yyout, "\\subsection*{\\rule{\\columnwidth}{1pt}"
562                                 "\\newline %s}\n", l2l_ititle.data);
563                 L2L_STRBUF_CLEAR(&l2l_ititle);
564         }
566         yyless(0);
567         BEGIN(l2l_last_yystart);
570 <title>{COMMENT} { /* ignore */ }
572 <title>.|\n {
573         if (l2l_buf_ptr)
574                 if (l2l_strbuf_append(l2l_buf_ptr, "%s", yytext))
575                         YY_FATAL_ERROR("ERROR: internal error");
576         /* else ignore */
579 @IL: { /* ignore */ }
581 @IE: { /* ignore */ }
583         /*
584          * tables
585          */
587 @TT: /* TODO */;
589 @TH: /* TODO */;
591 @TL: /* TODO */;
593 @TE: /* TODO */;
595         /* let all other rules that match "@L:" have a higher preceedence */
596 @L:{WHITESPACE}* { /* nothing to do */ }
598 <<EOF>> {
599         if (l2l_scanner_state == L2L_BODY) {
600                 fprintf(yyout, "\\end{document}\n");
601         }
602         else
603                 YY_FATAL_ERROR("ERROR: incomplete document");
605         l2l_strbuf_destroy(&l2l_category);
606         l2l_strbuf_destroy(&l2l_keyword);
607         l2l_strbuf_destroy(&l2l_title);
608         l2l_strbuf_destroy(&l2l_headline);
609         l2l_strbuf_destroy(&l2l_author);
610         l2l_strbuf_destroy(&l2l_abstract);
612         l2l_strbuf_destroy(&l2l_caption);
613         l2l_strbuf_destroy(&l2l_title);
615         yyterminate();
618 %%
620 static int
621 l2l_strbuf_append(l2l_strbuf_t *buf, const char *fmt, ...)
623         size_t  str_len;
624         va_list ap;
626         int status;
628         if ((! buf) || (! fmt))
629                 return 0;
631         va_start(ap, fmt);
632         status = vsnprintf(NULL, 0, fmt, ap);
633         va_end(ap);
635         if (status <= 0)
636                 return -1;
638         str_len = (size_t)status;
640         if (str_len > L2L_STRBUF_FREE(buf)) {
641                 char  *new_data;
642                 size_t new_size = 2 * buf->size;
644                 if (str_len > buf->size)
645                         new_size += str_len;
647                 new_data = realloc(buf->data, new_size + 1);
648                 if (! new_data) {
649                         l2l_strbuf_destroy(buf);
650                         return -1;
651                 }
653                 buf->data = new_data;
654                 buf->size = new_size;
655         }
656         assert(buf->len + str_len <= buf->size);
658         va_start(ap, fmt);
659         status = vsnprintf(L2L_STRBUF_END(buf), L2L_STRBUF_FREE(buf) + 1, fmt, ap);
660         va_end(ap);
662         if (status <= 0)
663                 return -1;
665         assert((size_t)status == str_len);
666         buf->len += str_len;
667         buf->data[buf->len] = '\0';
668         return 0;
669 } /* l2l_strbuf_append */
671 static void
672 l2l_strbuf_chomp(l2l_strbuf_t *buf)
674         if (! buf)
675                 return;
677         while (buf->len && isspace((int)buf->data[buf->len - 1])) {
678                 buf->data[buf->len - 1] = '\0';
679                 --buf->len;
680         }
681 } /* l2l_strbuf_chomp */
683 static void
684 l2l_strbuf_destroy(l2l_strbuf_t *buf)
686         if (! buf)
687                 return;
689         if (buf->data)
690                 free(buf->data);
691         memset(buf, 0, sizeof(*buf));
692 } /* l2l_strbuf_destroy */
694 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */