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
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *R_LANG = LANG_R;
12 // the languages entities
13 const char *r_entities[] = {
14 "space", "comment", "string", "any",
17 // constants associated with the entities
19 R_SPACE = 0, R_COMMENT, R_STRING, R_ANY
22 /*****************************************************************************/
27 include common "common.rl";
29 # Line counting machine
40 std_internal_newline(R_LANG)
47 r_line_comment = ( '#') @comment nonnewline*;
48 r_comment = r_line_comment;
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;
55 spaces ${ entity = R_SPACE; } => r_ccallback;
58 newline ${ entity = NEWLINE; } => r_ccallback;
59 ^space ${ entity = R_ANY; } => r_ccallback;
65 callback(R_LANG, r_entities[entity], cint(ts), cint(te), userdata);
68 r_line_comment_entity = ('#') nonnewline*;
69 r_comment_entity = r_line_comment_entity;
72 space+ ${ entity = R_SPACE; } => r_ecallback;
73 r_comment_entity ${ entity = R_COMMENT; } => r_ecallback;
79 /************************* Required for every parser *************************/
81 /* Parses a string buffer with R code.
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.
92 void parse_r(char *buffer, int length, int count,
93 void (*callback) (const char *lang, const char *entity, int s,
100 cs = (count) ? r_en_r_line : r_en_r_entity;
103 // if no newline at EOF; callback contents of last line
104 if (count) { process_last_line(R_LANG) }
109 /*****************************************************************************/