1 /************************* Required for every parser *************************/
2 #ifndef OHCOUNT_OCAML_PARSER_H
3 #define OHCOUNT_OCAML_PARSER_H
5 #include "../parser_macros.h"
7 // the name of the language
8 const char *OCAML_LANG = LANG_OCAML;
10 // the languages entities
11 const char *ocaml_entities[] = {
12 "space", "comment", "string", "any"
15 // constants associated with the entities
17 OCAML_SPACE = 0, OCAML_COMMENT, OCAML_STRING, OCAML_ANY
20 /*****************************************************************************/
25 include common "common.rl";
27 # Line counting machine
29 action ocaml_ccallback {
38 std_internal_newline(OCAML_LANG)
41 std_newline(OCAML_LANG)
45 action ocaml_comment_nc_res { nest_count = 0; }
46 action ocaml_comment_nc_inc { nest_count++; }
47 action ocaml_comment_nc_dec { nest_count--; }
49 ocaml_nested_block_comment =
50 '(*' >ocaml_comment_nc_res @comment (
51 newline %{ entity = INTERNAL_NL; } %ocaml_ccallback
55 '(*' @ocaml_comment_nc_inc @comment
57 '*)' @ocaml_comment_nc_dec @comment
59 (nonnewline - ws) @comment
60 )* :>> ('*)' when { nest_count == 0 }) @comment;
62 ocaml_comment = ocaml_nested_block_comment;
63 ocaml_string = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
67 spaces ${ entity = OCAML_SPACE; } => ocaml_ccallback;
70 newline ${ entity = NEWLINE; } => ocaml_ccallback;
71 ^space ${ entity = OCAML_ANY; } => ocaml_ccallback;
76 action ocaml_ecallback {
77 callback(OCAML_LANG, ocaml_entities[entity], cint(ts), cint(te), userdata);
80 ocaml_comment_entity = '(*' >ocaml_comment_nc_res (
81 '(*' @ocaml_comment_nc_inc
83 '*)' @ocaml_comment_nc_dec
86 )* :>> ('*)' when { nest_count == 0 });
89 space+ ${ entity = OCAML_SPACE; } => ocaml_ecallback;
90 ocaml_comment_entity ${ entity = OCAML_COMMENT; } => ocaml_ecallback;
96 /************************* Required for every parser *************************/
98 /* Parses a string buffer with Objective Caml code.
100 * @param *buffer The string to parse.
101 * @param length The length of the string to parse.
102 * @param count Integer flag specifying whether or not to count lines. If yes,
103 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
104 * machine optimized for returning entity positions.
105 * @param *callback Callback function. If count is set, callback is called for
106 * every line of code, comment, or blank with 'lcode', 'lcomment', and
107 * 'lblank' respectively. Otherwise callback is called for each entity found.
109 void parse_ocaml(char *buffer, int length, int count,
110 void (*callback) (const char *lang, const char *entity, int s,
119 cs = (count) ? ocaml_en_ocaml_line : ocaml_en_ocaml_entity;
122 // if no newline at EOF; callback contents of last line
123 if (count) { process_last_line(OCAML_LANG) }
128 /*****************************************************************************/