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