Merge pull request #41 from blackducksw/ubuntu_14
[ohcount] / src / parsers / groovy.rl
1 // groovy.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
2
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_GROOVY_PARSER_H
5 #define OHCOUNT_GROOVY_PARSER_H
6
7 #include "../parser_macros.h"
8
9 // the name of the language
10 const char *GROOVY_LANG = LANG_GROOVY;
11
12 // the languages entities
13 const char *groovy_entities[] = {
14   "space", "comment", "string", "any"
15 };
16
17 // constants associated with the entities
18 enum {
19   GROOVY_SPACE = 0, GROOVY_COMMENT, GROOVY_STRING, GROOVY_ANY
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine groovy;
26   write data;
27   include common "common.rl";
28
29   # Line counting machine
30
31   action groovy_ccallback {
32     switch(entity) {
33     case GROOVY_SPACE:
34       ls
35       break;
36     case GROOVY_ANY:
37       code
38       break;
39     case INTERNAL_NL:
40       std_internal_newline(GROOVY_LANG)
41       break;
42     case NEWLINE:
43       std_newline(GROOVY_LANG)
44     }
45   }
46
47   groovy_line_comment =
48     '//' @comment (
49       escaped_newline %{ entity = INTERNAL_NL; } %groovy_ccallback
50       |
51       ws
52       |
53       (nonnewline - ws) @comment
54     )*;
55   groovy_block_comment =
56     '/*' @comment (
57       newline %{ entity = INTERNAL_NL; } %groovy_ccallback
58       |
59       ws
60       |
61       (nonnewline - ws) @comment
62     )* :>> '*/';
63   groovy_comment = groovy_line_comment | groovy_block_comment;
64
65   groovy_sq_str =
66     '\'' @code (
67       escaped_newline %{ entity = INTERNAL_NL; } %groovy_ccallback
68       |
69       ws
70       |
71       [^\t '\\] @code
72       |
73       '\\' nonnewline @code
74     )* '\'';
75   groovy_dq_str =
76     '"' @code (
77       escaped_newline %{ entity = INTERNAL_NL; } %groovy_ccallback
78       |
79       ws
80       |
81       [^\t "\\] @code
82       |
83       '\\' nonnewline @code
84     )* '"';
85   groovy_string = groovy_sq_str | groovy_dq_str;
86
87   groovy_line := |*
88     spaces          ${ entity = GROOVY_SPACE; } => groovy_ccallback;
89     groovy_comment;
90     groovy_string;
91     newline         ${ entity = NEWLINE;      } => groovy_ccallback;
92     ^space          ${ entity = GROOVY_ANY;   } => groovy_ccallback;
93   *|;
94
95   # Entity machine
96
97   action groovy_ecallback {
98     callback(GROOVY_LANG, groovy_entities[entity], cint(ts), cint(te),
99              userdata);
100   }
101
102   groovy_line_comment_entity = '//' (escaped_newline | nonnewline)*;
103   groovy_block_comment_entity = '/*' any* :>> '*/';
104   groovy_comment_entity =
105     groovy_line_comment_entity | groovy_block_comment_entity;
106
107   groovy_entity := |*
108     space+                ${ entity = GROOVY_SPACE;   } => groovy_ecallback;
109     groovy_comment_entity ${ entity = GROOVY_COMMENT; } => groovy_ecallback;
110     # TODO:
111     ^space;
112   *|;
113 }%%
114
115 /* Parses a string buffer with Groovy code.
116  *
117  * @param *buffer The string to parse.
118  * @param length The length of the string to parse.
119  * @param count Integer flag specifying whether or not to count lines. If yes,
120  *   uses the Ragel machine optimized for counting. Otherwise uses the Ragel
121  *   machine optimized for returning entity positions.
122  * @param *callback Callback function. If count is set, callback is called for
123  *   every line of code, comment, or blank with 'lcode', 'lcomment', and
124  *   'lblank' respectively. Otherwise callback is called for each entity found.
125  */
126 void parse_groovy(char *buffer, int length, int count,
127                   void (*callback) (const char *lang, const char *entity, int s,
128                                     int e, void *udata),
129                   void *userdata
130   ) {
131   init
132
133   %% write init;
134   cs = (count) ? groovy_en_groovy_line : groovy_en_groovy_entity;
135   %% write exec;
136
137   // if no newline at EOF; callback contents of last line
138   if (count) { process_last_line(GROOVY_LANG) }
139 }
140
141 #endif