OTWO-1213 Works around lost encoding in Ruby/C binding layer
[ohcount] / src / parsers / coffeescript.rl
1 /************************* Required for every parser *************************/
2 #ifndef OHCOUNT_COFFEESCRIPT_PARSER_H
3 #define OHCOUNT_COFFEESCRIPT_PARSER_H
4
5 #include "../parser_macros.h"
6
7 // the name of the language
8 const char *COFFEESCRIPT_LANG = LANG_COFFEESCRIPT;
9
10 // the languages entities
11 const char *coffeescript_entities[] = {
12   "space", "comment", "string", "any"
13 };
14
15 // constants associated with the entities
16 enum {
17   COFFEESCRIPT_SPACE = 0, COFFEESCRIPT_COMMENT, COFFEESCRIPT_STRING, COFFEESCRIPT_ANY
18 };
19
20 /*****************************************************************************/
21
22 #include "javascript.h"
23
24 %%{
25   machine coffeescript;
26   write data;
27   include common "common.rl";
28   #EMBED(javascript)
29
30   # Line counting machine
31
32   action coffeescript_ccallback {
33     switch(entity) {
34     case COFFEESCRIPT_SPACE:
35       ls
36       break;
37     case COFFEESCRIPT_ANY:
38       code
39       break;
40     case INTERNAL_NL:
41       std_internal_newline(COFFEESCRIPT_LANG)
42       break;
43     case NEWLINE:
44       std_newline(COFFEESCRIPT_LANG)
45     }
46   }
47
48   coffeescript_line_comment = ('#') @comment nonnewline*;
49   coffeescript_block_comment =
50     '###' @comment (
51       newline %{ entity = INTERNAL_NL; } %coffeescript_ccallback
52       |
53       ws
54       |
55       (nonnewline - ws) @comment
56     )* :>> '###';
57   coffeescript_comment = coffeescript_line_comment | coffeescript_block_comment;
58
59   coffeescript_sq_str =
60     '\'' @enqueue @code (
61       newline %{ entity = INTERNAL_NL; } %coffeescript_ccallback
62       |
63       ws
64       |
65       [^\r\n\f\t '\\] @code
66       |
67       '\\' nonnewline @code
68     )* '\'' @commit @code;
69   coffeescript_dq_str =
70     '"' @enqueue @code (
71       newline %{ entity = INTERNAL_NL; } %coffeescript_ccallback
72       |
73       ws
74       |
75       [^\r\n\f\t "\\] @code
76       |
77       '\\' nonnewline @code
78     )* '"' @commit @code;
79   coffeescript_sq_here_doc =
80     '\'\'\'' @code (
81       newline %{ entity = INTERNAL_NL; } %coffeescript_ccallback
82       |
83       ws
84       |
85       (nonnewline - ws) @code
86     )* :>> '\'\'\'' @code;
87   coffeescript_dq_here_doc =
88     '"""' @code (
89       newline %{ entity = INTERNAL_NL; } %coffeescript_ccallback
90       |
91       ws
92       |
93       (nonnewline - ws) @code
94     )* :>> '"""' @code;
95   coffeescript_string = (coffeescript_sq_str | coffeescript_dq_str |
96                 coffeescript_sq_here_doc | coffeescript_dq_here_doc) @code;
97
98   coffeescript_js_entry = '`' @code;
99   coffeescript_js_outry = '`' @check_blank_outry @code;
100         coffeescript_js_line := |*
101     coffeescript_js_outry @{ p = ts; fret; };
102     # unmodified JavaScript patterns
103     spaces        ${ entity = JS_SPACE; } => js_ccallback;
104     js_comment;
105     js_string;
106     newline       ${ entity = NEWLINE;    } => js_ccallback;
107     ^space        ${ entity = JS_ANY;   } => js_ccallback;
108   *|;
109
110   coffeescript_line := |*
111                 coffeescript_js_entry @coffeescript_ccallback
112                         @{ saw(JS_LANG); } => { fcall coffeescript_js_line; };
113     spaces          ${ entity = COFFEESCRIPT_SPACE; } => coffeescript_ccallback;
114     coffeescript_comment;
115     coffeescript_string;
116     newline         ${ entity = NEWLINE;      } => coffeescript_ccallback;
117     ^space          ${ entity = COFFEESCRIPT_ANY;   } => coffeescript_ccallback;
118   *|;
119
120   # Entity machine
121
122   action coffeescript_ecallback {
123     callback(COFFEESCRIPT_LANG, coffeescript_entities[entity], cint(ts), cint(te),
124              userdata);
125   }
126
127   coffeescript_line_comment_entity = ('#') nonnewline*;
128   coffeescript_block_comment_entity = '###' any* :>> '###';
129   coffeescript_comment_entity = coffeescript_line_comment_entity |
130     coffeescript_block_comment_entity;
131
132   coffeescript_entity := |*
133     space+                ${ entity = COFFEESCRIPT_SPACE;   } => coffeescript_ecallback;
134     coffeescript_comment_entity ${ entity = COFFEESCRIPT_COMMENT; } => coffeescript_ecallback;
135     # TODO:
136     ^space;
137   *|;
138 }%%
139
140 /************************* Required for every parser *************************/
141
142 /* Parses a string buffer with CoffeeScript code.
143  *
144  * @param *buffer The string to parse.
145  * @param length The length of the string to parse.
146  * @param count Integer flag specifying whether or not to count lines. If yes,
147  *   uses the Ragel machine optimized for counting. Otherwise uses the Ragel
148  *   machine optimized for returning entity positions.
149  * @param *callback Callback function. If count is set, callback is called for
150  *   every line of code, comment, or blank with 'lcode', 'lcomment', and
151  *   'lblank' respectively. Otherwise callback is called for each entity found.
152  */
153 void parse_coffeescript(char *buffer, int length, int count,
154                   void (*callback) (const char *lang, const char *entity, int s,
155                                     int e, void *udata),
156                   void *userdata
157   ) {
158   init
159
160   %% write init;
161   cs = (count) ? coffeescript_en_coffeescript_line : coffeescript_en_coffeescript_entity;
162   %% write exec;
163
164   // if no newline at EOF; callback contents of last line
165   if (count) { process_last_line(COFFEESCRIPT_LANG) }
166 }
167
168 #endif
169
170 /*****************************************************************************/