OTWO-1213 Works around lost encoding in Ruby/C binding layer
[ohcount] / src / parsers / scala.rl
1 // scala.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
2
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_SCALA_PARSER_H
5 #define OHCOUNT_SCALA_PARSER_H
6
7 #include "../parser_macros.h"
8
9 // the name of the language
10 const char *SCALA_LANG = LANG_SCALA;
11
12 // the languages entities
13 const char *scala_entities[] = {
14   "space", "comment", "string", "any",
15 };
16
17 // constants associated with the entities
18 enum {
19   SCALA_SPACE = 0, SCALA_COMMENT, SCALA_STRING, SCALA_ANY
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine scala;
26   write data;
27   include common "common.rl";
28
29   # Line counting machine
30
31   action scala_ccallback {
32     switch(entity) {
33     case SCALA_SPACE:
34       ls
35       break;
36     case SCALA_ANY:
37       code
38       break;
39     case INTERNAL_NL:
40       std_internal_newline(SCALA_LANG)
41       break;
42     case NEWLINE:
43       std_newline(SCALA_LANG)
44     }
45   }
46
47   scala_line_comment = '//' @comment nonnewline*;
48   scala_block_comment =
49     '/*' @comment (
50       newline %{ entity = INTERNAL_NL; } %scala_ccallback
51       |
52       ws
53       |
54       (nonnewline - ws) @comment
55     )* :>> '*/';
56   scala_comment = scala_line_comment | scala_block_comment;
57
58   scala_sq_str = '\'' @code ([^\r\n\f'\\] | '\\' nonnewline)* '\'';
59   scala_dq_str = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
60   scala_string = scala_sq_str | scala_dq_str;
61
62   scala_line := |*
63     spaces         ${ entity = SCALA_SPACE; } => scala_ccallback;
64     scala_comment;
65     scala_string;
66     newline        ${ entity = NEWLINE;     } => scala_ccallback;
67     ^space         ${ entity = SCALA_ANY;   } => scala_ccallback;
68   *|;
69
70   # Entity machine
71
72   action scala_ecallback {
73     callback(SCALA_LANG, scala_entities[entity], cint(ts), cint(te), userdata);
74   }
75
76   scala_line_comment_entity = '//' nonnewline*;
77   scala_block_comment_entity = '/*' any* :>> '*/';
78   scala_comment_entity = scala_line_comment_entity | scala_block_comment_entity;
79
80   scala_entity := |*
81     space+               ${ entity = SCALA_SPACE;   } => scala_ecallback;
82     scala_comment_entity ${ entity = SCALA_COMMENT; } => scala_ecallback;
83     # TODO:
84     ^space;
85   *|;
86 }%%
87
88 /************************* Required for every parser *************************/
89
90 /* Parses a string buffer with Scala code.
91  *
92  * @param *buffer The string to parse.
93  * @param length The length of the string to parse.
94  * @param count Integer flag specifying whether or not to count lines. If yes,
95  *   uses the Ragel machine optimized for counting. Otherwise uses the Ragel
96  *   machine optimized for returning entity positions.
97  * @param *callback Callback function. If count is set, callback is called for
98  *   every line of code, comment, or blank with 'lcode', 'lcomment', and
99  *   'lblank' respectively. Otherwise callback is called for each entity found.
100  */
101 void parse_scala(char *buffer, int length, int count,
102                  void (*callback) (const char *lang, const char *entity, int s,
103                                    int e, void *udata),
104                  void *userdata
105   ) {
106   init
107
108   %% write init;
109   cs = (count) ? scala_en_scala_line : scala_en_scala_entity;
110   %% write exec;
111
112   // if no newline at EOF; callback contents of last line
113   if (count) { process_last_line(SCALA_LANG) }
114 }
115
116 #endif
117
118 /*****************************************************************************/