OTWO-1213 Works around lost encoding in Ruby/C binding layer
[ohcount] / src / parsers / haxe.rl
1 // haxe.rl patched by Niel Drummond from actionscript version written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
2
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_HAXE_PARSER_H
5 #define OHCOUNT_HAXE_PARSER_H
6
7 #include "../parser_macros.h"
8
9 // the name of the language
10 const char *HX_LANG = LANG_HAXE;
11
12 // the languages entities
13 const char *hx_entities[] = {
14   "space", "comment", "string", "any",
15 };
16
17 // constants associated with the entities
18 enum {
19   HX_SPACE = 0, HX_COMMENT, HX_STRING, HX_ANY
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine haxe;
26   write data;
27   include common "common.rl";
28
29   # Line counting machine
30
31   action hx_ccallback {
32     switch(entity) {
33     case HX_SPACE:
34       ls
35       break;
36     case HX_ANY:
37       code
38       break;
39     case INTERNAL_NL:
40       std_internal_newline(HX_LANG)
41       break;
42     case NEWLINE:
43       std_newline(HX_LANG)
44     }
45   }
46
47   hx_line_comment =
48     '//' @comment (
49       escaped_newline %{ entity = INTERNAL_NL; } %hx_ccallback
50       |
51       ws
52       |
53       (nonnewline - ws) @comment
54     )*;
55
56                 hx_block_comment =
57     '/*' @comment (
58       newline %{ entity = INTERNAL_NL; } %hx_ccallback
59       |
60       ws
61       |
62       (nonnewline - ws) @comment
63     )* :>> '*/';
64   hx_comment = hx_line_comment | hx_block_comment;
65
66   hx_sq_str = '\'' @code ([^\r\n\f'\\] | '\\' nonnewline)* '\'';
67   hx_dq_str = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
68   hx_string = hx_sq_str | hx_dq_str;
69
70   hx_line := |*
71     spaces      ${ entity = HX_SPACE; } => hx_ccallback;
72     hx_comment;
73     hx_string;
74     newline     ${ entity = NEWLINE;  } => hx_ccallback;
75     ^space      ${ entity = HX_ANY;   } => hx_ccallback;
76   *|;
77
78   # Entity machine
79
80   action hx_ecallback {
81     callback(HX_LANG, hx_entities[entity], cint(ts), cint(te), userdata);
82   }
83   hx_line_comment_entity = '//' nonnewline*;
84   hx_block_comment_entity = '/*' any* :>> '*/';
85   hx_comment_entity = hx_line_comment_entity | hx_block_comment_entity;
86
87   hx_entity := |*
88     space+            ${ entity = HX_SPACE;   } => hx_ecallback;
89     hx_comment_entity ${ entity = HX_COMMENT; } => hx_ecallback;
90     # TODO:
91     ^space;
92   *|;
93 }%%
94
95 /************************* Required for every parser *************************/
96
97 /* Parses a string buffer with Haxe 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_haxe(char *buffer, int length, int count,
109                 void (*callback) (const char *lang, const char *entity, int s,
110                                   int e, void *udata),
111                 void *userdata
112   ) {
113   init
114
115   %% write init;
116   cs = (count) ? haxe_en_hx_line : haxe_en_hx_entity;
117   %% write exec;
118
119   // if no newline at EOF; callback contents of last line
120   if (count) { process_last_line(HX_LANG) }
121 }
122
123 #endif
124
125 /*****************************************************************************/