1 // golang.rl written by Scott Lawrence <bytbox@gmail.com>
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_GOLANG_PARSER_H
5 #define OHCOUNT_GOLANG_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *GOLANG_LANG = LANG_GOLANG;
12 // the languages entities
13 const char *golang_entities[] = {
14 "space", "comment", "string", "number", "preproc",
15 "keyword", "identifier", "operator", "any"
18 // constants associated with the entities
20 GOLANG_SPACE = 0, GOLANG_COMMENT, GOLANG_STRING, GOLANG_NUMBER, GOLANG_PREPROC,
21 GOLANG_KEYWORD, GOLANG_IDENTIFIER, GOLANG_OPERATOR, GOLANG_ANY
24 /*****************************************************************************/
29 include common "common.rl";
31 # Line counting machine
33 action golang_ccallback {
42 std_internal_newline(GOLANG_LANG)
45 std_newline(GOLANG_LANG)
51 escaped_newline %{ entity = INTERNAL_NL; } %golang_ccallback
55 (nonnewline - ws) @comment
57 golang_block_comment =
59 newline %{ entity = INTERNAL_NL; } %golang_ccallback
63 (nonnewline - ws) @comment
65 golang_comment = golang_line_comment | golang_block_comment;
69 escaped_newline %{ entity = INTERNAL_NL; } %golang_ccallback
79 escaped_newline %{ entity = INTERNAL_NL; } %golang_ccallback
87 golang_string = golang_sq_str | golang_dq_str;
90 spaces ${ entity = GOLANG_SPACE; } => golang_ccallback;
93 newline ${ entity = NEWLINE; } => golang_ccallback;
94 ^space ${ entity = GOLANG_ANY; } => golang_ccallback;
99 action golang_ecallback {
100 callback(GOLANG_LANG, golang_entities[entity], cint(ts), cint(te), userdata);
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;
107 golang_string_entity = sq_str_with_escapes | dq_str_with_escapes;
109 golang_number_entity = float | integer;
111 golang_preprogolang_word =
112 'define' | 'elif' | 'else' | 'endif' | 'error' | 'if' | 'ifdef' |
113 'ifndef' | 'import' | 'include' | 'line' | 'pragma' | 'undef' |
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)*;
121 golang_identifier_entity = (alpha | '_') (alnum | '_')*;
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';
137 golang_operator_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;
152 /************************* Required for every parser *************************/
154 /* Parses a string buffer with C/C++ code.
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.
165 void parse_golang(char *buffer, int length, int count,
166 void (*callback) (const char *lang, const char *entity, int s,
173 cs = (count) ? golang_en_golang_line : golang_en_golang_entity;
176 // if no newline at EOF; callback contents of last line
177 if (count) { process_last_line(GOLANG_LANG) }
180 const char *ORIG_GOLANG_LANG = LANG_GOLANG;
184 /*****************************************************************************/