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