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