OTWO-1213 Works around lost encoding in Ruby/C binding layer
[ohcount] / src / parsers / autoconf.rl
1 // autoconf.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
2
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_AUTOCONF_PARSER_H
5 #define OHCOUNT_AUTOCONF_PARSER_H
6
7 #include "../parser_macros.h"
8
9 // the name of the language
10 const char *AC_LANG = LANG_AUTOCONF;
11
12 // the languages entities
13 const char *ac_entities[] = {
14   "space", "comment", "string", "any"
15 };
16
17 // constants associated with the entities
18 enum {
19   AC_SPACE = 0, AC_COMMENT, AC_STRING, AC_ANY
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine autoconf;
26   write data;
27   include common "common.rl";
28
29   # Line counting machine
30
31   action ac_ccallback {
32     switch(entity) {
33     case AC_SPACE:
34       ls
35       break;
36     case AC_ANY:
37       code
38       break;
39     case INTERNAL_NL:
40       std_internal_newline(AC_LANG)
41       break;
42     case NEWLINE:
43       std_newline(AC_LANG)
44     }
45   }
46
47   action ac_str_nc_res { nest_count = 0; }
48   action ac_str_nc_inc { nest_count++; }
49   action ac_str_nc_dec { nest_count--; }
50
51   ac_comment = 'dnl' @comment (ws+ nonnewline+)?;
52
53   ac_string =
54     '[' >ac_str_nc_res @code (
55       newline %{ entity = INTERNAL_NL; } %ac_ccallback
56       |
57       ws
58       |
59       '[' @ac_str_nc_inc @code
60       |
61       ']' @ac_str_nc_dec @code
62       |
63       (nonnewline - ws - [\[\]]) @code
64     )* :>> (']' when { nest_count == 0 });
65
66   ac_line := |*
67     spaces      ${ entity = AC_SPACE; } => ac_ccallback;
68     ac_comment;
69     ac_string;
70     newline     ${ entity = NEWLINE;  } => ac_ccallback;
71     ^space      ${ entity = AC_ANY;   } => ac_ccallback;
72   *|;
73
74   # Entity machine
75
76   action ac_ecallback {
77     callback(AC_LANG, ac_entities[entity], cint(ts), cint(te), userdata);
78   }
79
80   ac_comment_entity = 'dnl' ws+ nonnewline*;
81
82   ac_entity := |*
83     space+            ${ entity = AC_SPACE;   } => ac_ecallback;
84     ac_comment_entity ${ entity = AC_COMMENT; } => ac_ecallback;
85     # TODO:
86     ^space;
87   *|;
88 }%%
89
90 /************************* Required for every parser *************************/
91
92 /* Parses a string buffer with Autoconf code.
93  *
94  * @param *buffer The string to parse.
95  * @param length The length of the string to parse.
96  * @param count Integer flag specifying whether or not to count lines. If yes,
97  *   uses the Ragel machine optimized for counting. Otherwise uses the Ragel
98  *   machine optimized for returning entity positions.
99  * @param *callback Callback function. If count is set, callback is called for
100  *   every line of code, comment, or blank with 'lcode', 'lcomment', and
101  *   'lblank' respectively. Otherwise callback is called for each entity found.
102  */
103 void parse_autoconf(char *buffer, int length, int count,
104                     void (*callback) (const char *lang, const char *entity,
105                                       int s, int e, void *udata),
106                     void *userdata
107   ) {
108   init
109
110   int nest_count = 0;
111
112   %% write init;
113   cs = (count) ? autoconf_en_ac_line : autoconf_en_ac_entity;
114   %% write exec;
115
116   // if no newline at EOF; callback contents of last line
117   if (count) { process_last_line(AC_LANG) }
118 }
119
120 #endif
121
122 /*****************************************************************************/