Fixes recursion bug in disambiguate_in().
[ohcount] / src / parsers / objective_c.rl
1 // objective_c.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
2
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_OBJECTIVE_C_PARSER_H
5 #define OHCOUNT_OBJECTIVE_C_PARSER_H
6
7 #include "../parser_macros.h"
8
9 // the name of the language
10 const char *OBJC_LANG = LANG_OBJECTIVE_C;
11
12 // the languages entities
13 const char *objc_entities[] = {
14   "space", "comment", "string", "any"
15 };
16
17 // constants associated with the entities
18 enum {
19   OBJC_SPACE = 0, OBJC_COMMENT, OBJC_STRING, OBJC_ANY,
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine objective_c;
26   write data;
27   include common "common.rl";
28
29   # Line counting machine
30
31   action objc_ccallback {
32     switch(entity) {
33     case OBJC_SPACE:
34       ls
35       break;
36     case OBJC_ANY:
37       code
38       break;
39     case INTERNAL_NL:
40       std_internal_newline(OBJC_LANG)
41       break;
42     case NEWLINE:
43       std_newline(OBJC_LANG)
44     }
45   }
46
47   objc_line_comment =
48     '//' @comment (
49       escaped_newline %{ entity = INTERNAL_NL; } %objc_ccallback
50       |
51       ws
52       |
53       (nonnewline - ws) @comment
54     )*;
55     objc_block_comment =
56     '/*' @comment (
57       newline %{ entity = INTERNAL_NL; } %objc_ccallback
58       |
59       ws
60       |
61       (nonnewline - ws) @comment
62     )* :>> '*/';
63   objc_comment = objc_line_comment | objc_block_comment;
64
65   objc_sq_str = '\'' @code ([^\r\n\f'\\] | '\\' nonnewline)* '\'';
66   objc_dq_str = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
67   objc_string = objc_sq_str | objc_dq_str;
68
69   objc_line := |*
70     spaces        ${ entity = OBJC_SPACE; } => objc_ccallback;
71     objc_comment;
72     objc_string;
73     newline       ${ entity = NEWLINE;    } => objc_ccallback;
74     ^space        ${ entity = OBJC_ANY;   } => objc_ccallback;
75   *|;
76
77   # Entity machine
78
79   action objc_ecallback {
80     callback(OBJC_LANG, objc_entities[entity], cint(ts), cint(te), userdata);
81   }
82
83   objc_line_comment_entity = '//' (escaped_newline | nonnewline)*;
84   objc_block_comment_entity = '/*' any* :>> '*/';
85   objc_comment_entity = objc_line_comment_entity | objc_block_comment_entity;
86
87   objc_entity := |*
88     space+              ${ entity = OBJC_SPACE;   } => objc_ecallback;
89     objc_comment_entity ${ entity = OBJC_COMMENT; } => objc_ecallback;
90     # TODO:
91     ^space;
92   *|;
93 }%%
94
95 /************************* Required for every parser *************************/
96
97 /* Parses a string buffer with Objective C 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_objective_c(char *buffer, int length, int count,
109                        void (*callback) (const char *lang, const char *entity,
110                                          int s, int e, void *udata),
111                        void *userdata
112   ) {
113   init
114
115   %% write init;
116   cs = (count) ? objective_c_en_objc_line : objective_c_en_objc_entity;
117   %% write exec;
118
119   // if no newline at EOF; callback contents of last line
120   if (count) { process_last_line(OBJC_LANG) }
121 }
122
123 #endif
124
125 /*****************************************************************************/