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