Fixes recursion bug in disambiguate_in().
[ohcount] / src / parsers / objective_j.rl
1 // objective_j.rl written by Francisco tolmasky. francisco<att>280north<dott>com.
2 // Modified from file by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3
4 /************************* Required for every parser *************************/
5 #ifndef OHCOUNT_OBJECTIVE_J_PARSER_H
6 #define OHCOUNT_OBJECTIVE_J_PARSER_H
7
8 #include "../parser_macros.h"
9
10 // the name of the language
11 const char *OBJJ_LANG = LANG_OBJECTIVE_J;
12
13 // the languages entities
14 const char *objj_entities[] = {
15   "space", "comment", "string", "any"
16 };
17
18 // constants associated with the entities
19 enum {
20   OBJJ_SPACE = 0, OBJJ_COMMENT, OBJJ_STRING, OBJJ_ANY,
21 };
22
23 /*****************************************************************************/
24
25 %%{
26   machine objective_j;
27   write data;
28   include common "common.rl";
29
30   # Line counting machine
31
32   action objj_ccallback {
33     switch(entity) {
34     case OBJJ_SPACE:
35       ls
36       break;
37     case OBJJ_ANY:
38       code
39       break;
40     case INTERNAL_NL:
41       std_internal_newline(OBJJ_LANG)
42       break;
43     case NEWLINE:
44       std_newline(OBJJ_LANG)
45     }
46   }
47
48   objj_line_comment =
49     '//' @comment (
50       escaped_newline %{ entity = INTERNAL_NL; } %objj_ccallback
51       |
52       ws
53       |
54       (nonnewline - ws) @comment
55     )*;
56     objj_block_comment =
57     '/*' @comment (
58       newline %{ entity = INTERNAL_NL; } %objj_ccallback
59       |
60       ws
61       |
62       (nonnewline - ws) @comment
63     )* :>> '*/';
64   objj_comment = objj_line_comment | objj_block_comment;
65
66   objj_sq_str = '\'' @code ([^\r\n\f'\\] | '\\' nonnewline)* '\'';
67   objj_dq_str = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
68   objj_string = objj_sq_str | objj_dq_str;
69
70   objj_line := |*
71     spaces        ${ entity = OBJJ_SPACE; } => objj_ccallback;
72     objj_comment;
73     objj_string;
74     newline       ${ entity = NEWLINE;    } => objj_ccallback;
75     ^space        ${ entity = OBJJ_ANY;   } => objj_ccallback;
76   *|;
77
78   # Entity machine
79
80   action objj_ecallback {
81     callback(OBJJ_LANG, objj_entities[entity], cint(ts), cint(te), userdata);
82   }
83
84   objj_line_comment_entity = '//' (escaped_newline | nonnewline)*;
85   objj_block_comment_entity = '/*' any* :>> '*/';
86   objj_comment_entity = objj_line_comment_entity | objj_block_comment_entity;
87
88   objj_entity := |*
89     space+              ${ entity = OBJJ_SPACE;   } => objj_ecallback;
90     objj_comment_entity ${ entity = OBJJ_COMMENT; } => objj_ecallback;
91     # TODO:
92     ^space;
93   *|;
94 }%%
95
96 /************************* Required for every parser *************************/
97
98 /* Parses a string buffer with Objective C code.
99  *
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.
108  */
109 void parse_objective_j(char *buffer, int length, int count,
110                        void (*callback) (const char *lang, const char *entity,
111                                          int s, int e, void *udata),
112                        void *userdata
113   ) {
114   init
115
116   %% write init;
117   cs = (count) ? objective_j_en_objj_line : objective_j_en_objj_entity;
118   %% write exec;
119
120   // if no newline at EOF; callback contents of last line
121   if (count) { process_last_line(OBJJ_LANG) }
122 }
123
124 #endif
125
126 /*****************************************************************************/