OTWO-1213 Works around lost encoding in Ruby/C binding layer
[ohcount] / src / parsers / golang.rl
1 // golang.rl written by Scott Lawrence <bytbox@gmail.com>
2
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_GOLANG_PARSER_H
5 #define OHCOUNT_GOLANG_PARSER_H
6
7 #include "../parser_macros.h"
8
9 // the name of the language
10 const char *GOLANG_LANG = LANG_GOLANG;
11
12 // the languages entities
13 const char *golang_entities[] = {
14   "space", "comment", "string", "number", "preproc",
15   "keyword", "identifier", "operator", "any"
16 };
17
18 // constants associated with the entities
19 enum {
20   GOLANG_SPACE = 0, GOLANG_COMMENT, GOLANG_STRING, GOLANG_NUMBER, GOLANG_PREPROC,
21   GOLANG_KEYWORD, GOLANG_IDENTIFIER, GOLANG_OPERATOR, GOLANG_ANY
22 };
23
24 /*****************************************************************************/
25
26 %%{
27   machine golang;
28   write data;
29   include common "common.rl";
30
31   # Line counting machine
32
33   action golang_ccallback {
34     switch(entity) {
35     case GOLANG_SPACE:
36       ls
37       break;
38     case GOLANG_ANY:
39       code
40       break;
41     case INTERNAL_NL:
42       std_internal_newline(GOLANG_LANG)
43       break;
44     case NEWLINE:
45       std_newline(GOLANG_LANG)
46     }
47   }
48
49   golang_line_comment =
50     '//' @comment (
51       escaped_newline %{ entity = INTERNAL_NL; } %golang_ccallback
52       |
53       ws
54       |
55       (nonnewline - ws) @comment
56     )*;
57   golang_block_comment =
58     '/*' @comment (
59       newline %{ entity = INTERNAL_NL; } %golang_ccallback
60       |
61       ws
62       |
63       (nonnewline - ws) @comment
64     )* :>> '*/';
65   golang_comment = golang_line_comment | golang_block_comment;
66
67   golang_sq_str =
68     '\'' @code (
69       escaped_newline %{ entity = INTERNAL_NL; } %golang_ccallback
70       |
71       ws
72       |
73       [^\t '\\] @code
74       |
75       '\\' nonnewline @code
76     )* '\'';
77   golang_dq_str =
78     '"' @code (
79       escaped_newline %{ entity = INTERNAL_NL; } %golang_ccallback
80       |
81       ws
82       |
83       [^\t "\\] @code
84       |
85       '\\' nonnewline @code
86     )* '"';
87   golang_string = golang_sq_str | golang_dq_str;
88
89   golang_line := |*
90     spaces    ${ entity = GOLANG_SPACE; } => golang_ccallback;
91     golang_comment;
92     golang_string;
93     newline   ${ entity = NEWLINE; } => golang_ccallback;
94     ^space    ${ entity = GOLANG_ANY;   } => golang_ccallback;
95   *|;
96
97   # Entity machine
98
99   action golang_ecallback {
100     callback(GOLANG_LANG, golang_entities[entity], cint(ts), cint(te), userdata);
101   }
102
103   golang_line_comment_entity = '//' (escaped_newline | nonnewline)*;
104   golang_block_comment_entity = '/*' any* :>> '*/';
105   golang_comment_entity = golang_line_comment_entity | golang_block_comment_entity;
106
107   golang_string_entity = sq_str_with_escapes | dq_str_with_escapes;
108
109   golang_number_entity = float | integer;
110
111   golang_preprogolang_word =
112     'define' | 'elif' | 'else' | 'endif' | 'error' | 'if' | 'ifdef' |
113     'ifndef' | 'import' | 'include' | 'line' | 'pragma' | 'undef' |
114     'using' | 'warning';
115   # TODO: find some way of making preproc match the beginning of a line.
116   # Putting a 'when starts_line' conditional throws an assertion error.
117   golang_preprogolang_entity =
118     '#' space* (golang_block_comment_entity space*)?
119       golang_preprogolang_word (escaped_newline | nonnewline)*;
120
121   golang_identifier_entity = (alpha | '_') (alnum | '_')*;
122
123   golang_keyword_entity =
124     'and' | 'and_eq' | 'asm' | 'auto' | 'bitand' | 'bitor' | 'bool' |
125     'break' | 'case' | 'catch' | 'char' | 'class' | 'compl' | 'const' |
126     'const_cast' | 'continue' | 'default' | 'delete' | 'do' | 'double' |
127     'dynamigolang_cast' | 'else' | 'enum' | 'explicit' | 'export' | 'extern' |
128     'false' | 'float' | 'for' | 'friend' | 'goto' | 'if' | 'inline' | 'int' |
129     'long' | 'mutable' | 'namespace' | 'new' | 'not' | 'not_eq' |
130     'operator' | 'or' | 'or_eq' | 'private' | 'protected' | 'public' |
131     'register' | 'reinterpret_cast' | 'return' | 'short' | 'signed' |
132     'sizeof' | 'static' | 'statigolang_cast' | 'struct' | 'switch' |
133     'template' | 'this' | 'throw' | 'true' | 'try' | 'typedef' | 'typeid' |
134     'typename' | 'union' | 'unsigned' | 'using' | 'virtual' | 'void' |
135     'volatile' | 'wchar_t' | 'while' | 'xor' | 'xor_eq';
136
137   golang_operator_entity = [+\-/*%<>!=^&|?~:;.,()\[\]{}];
138
139   golang_entity := |*
140     space+              ${ entity = GOLANG_SPACE;      } => golang_ecallback;
141     golang_comment_entity    ${ entity = GOLANG_COMMENT;    } => golang_ecallback;
142     golang_string_entity     ${ entity = GOLANG_STRING;     } => golang_ecallback;
143     golang_number_entity     ${ entity = GOLANG_NUMBER;     } => golang_ecallback;
144     golang_preprogolang_entity    ${ entity = GOLANG_PREPROC;    } => golang_ecallback;
145     golang_identifier_entity ${ entity = GOLANG_IDENTIFIER; } => golang_ecallback;
146     golang_keyword_entity    ${ entity = GOLANG_KEYWORD;    } => golang_ecallback;
147     golang_operator_entity   ${ entity = GOLANG_OPERATOR;   } => golang_ecallback;
148     ^(space | digit)    ${ entity = GOLANG_ANY;        } => golang_ecallback;
149   *|;
150 }%%
151
152 /************************* Required for every parser *************************/
153
154 /* Parses a string buffer with C/C++ code.
155  *
156  * @param *buffer The string to parse.
157  * @param length The length of the string to parse.
158  * @param count Integer flag specifying whether or not to count lines. If yes,
159  *   uses the Ragel machine optimized for counting. Otherwise uses the Ragel
160  *   machine optimized for returning entity positions.
161  * @param *callback Callback function. If count is set, callback is called for
162  *   every line of code, comment, or blank with 'lcode', 'lcomment', and
163  *   'lblank' respectively. Otherwise callback is called for each entity found.
164  */
165 void parse_golang(char *buffer, int length, int count,
166              void (*callback) (const char *lang, const char *entity, int s,
167                                int e, void *udata),
168              void *userdata
169   ) {
170   init
171
172   %% write init;
173   cs = (count) ? golang_en_golang_line : golang_en_golang_entity;
174   %% write exec;
175
176   // if no newline at EOF; callback contents of last line
177   if (count) { process_last_line(GOLANG_LANG) }
178 }
179
180 const char *ORIG_GOLANG_LANG = LANG_GOLANG;
181
182 #endif
183
184 /*****************************************************************************/