Fixes recursion bug in disambiguate_in().
[ohcount] / src / parsers / metafont.rl
1 // metafont.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
2
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_METAFONT_PARSER_H
5 #define OHCOUNT_METAFONT_PARSER_H
6
7 #include "../parser_macros.h"
8
9 // the name of the language
10 const char *METAFONT_LANG = LANG_METAFONT;
11
12 // the languages entities
13 const char *metafont_entities[] = {
14   "space", "comment", "string", "any",
15 };
16
17 // constants associated with the entities
18 enum {
19   METAFONT_SPACE = 0, METAFONT_COMMENT, METAFONT_STRING, METAFONT_ANY
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine metafont;
26   write data;
27   include common "common.rl";
28
29   # Line counting machine
30
31   action metafont_ccallback {
32     switch(entity) {
33     case METAFONT_SPACE:
34       ls
35       break;
36     case METAFONT_ANY:
37       code
38       break;
39     case INTERNAL_NL:
40       std_internal_newline(METAFONT_LANG)
41       break;
42     case NEWLINE:
43       std_newline(METAFONT_LANG)
44     }
45   }
46
47   metafont_comment = '%' @{ fhold; } @comment nonnewline*;
48
49   metafont_string = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
50
51   metafont_line := |*
52     spaces            ${ entity = METAFONT_SPACE; } => metafont_ccallback;
53     metafont_comment;
54     metafont_string;
55     newline           ${ entity = NEWLINE;        } => metafont_ccallback;
56     ^space            ${ entity = METAFONT_ANY;   } => metafont_ccallback;
57   *|;
58
59   # Entity machine
60
61   action metafont_ecallback {
62     callback(METAFONT_LANG, metafont_entities[entity], cint(ts), cint(te),
63              userdata);
64   }
65
66   metafont_comment_entity = '%' nonnewline*;
67
68   metafont_entity := |*
69     space+                  ${ entity = METAFONT_SPACE;   } => metafont_ecallback;
70     metafont_comment_entity ${ entity = METAFONT_COMMENT; } => metafont_ecallback;
71     # TODO:
72     ^space;
73   *|;
74 }%%
75
76 /************************* Required for every parser *************************/
77
78 /* Parses a string buffer with Metafont code.
79  *
80  * @param *buffer The string to parse.
81  * @param length The length of the string to parse.
82  * @param count Integer flag specifying whether or not to count lines. If yes,
83  *   uses the Ragel machine optimized for counting. Otherwise uses the Ragel
84  *   machine optimized for returning entity positions.
85  * @param *callback Callback function. If count is set, callback is called for
86  *   every line of code, comment, or blank with 'lcode', 'lcomment', and
87  *   'lblank' respectively. Otherwise callback is called for each entity found.
88  */
89 void parse_metafont(char *buffer, int length, int count,
90                     void (*callback) (const char *lang, const char *entity,
91                                       int s, int e, void *udata),
92                     void *userdata
93   ) {
94   init
95
96   %% write init;
97   cs = (count) ? metafont_en_metafont_line : metafont_en_metafont_entity;
98   %% write exec;
99
100   // if no newline at EOF; callback contents of last line
101   if (count) { process_last_line(METAFONT_LANG) }
102 }
103
104 #endif
105
106 /*****************************************************************************/