1 // rust.rl written by Sébastien Crozet <developer@crozet.re>
2 // Inpired by golang.rl
4 /************************* Required for every parser *************************/
5 #ifndef OHCOUNT_RUST_PARSER_H
6 #define OHCOUNT_RUST_PARSER_H
8 #include "../parser_macros.h"
10 // the name of the language
11 const char *RUST_LANG = LANG_RUST;
13 // the languages entities
14 const char *rust_entities[] = {
15 "space", "comment", "string", "number",
16 "keyword", "identifier", "operator", "any"
19 // constants associated with the entities
21 RUST_SPACE = 0, RUST_COMMENT, RUST_STRING, RUST_NUMBER,
22 RUST_KEYWORD, RUST_IDENTIFIER, RUST_OPERATOR, RUST_ANY
25 /*****************************************************************************/
30 include common "common.rl";
32 # Line counting machine
34 action rust_ccallback {
43 std_internal_newline(RUST_LANG)
46 std_newline(RUST_LANG)
52 escaped_newline %{ entity = INTERNAL_NL; } %rust_ccallback
56 (nonnewline - ws) @comment
60 newline %{ entity = INTERNAL_NL; } %rust_ccallback
64 (nonnewline - ws) @comment
66 rust_comment = rust_line_comment | rust_block_comment;
70 escaped_newline %{ entity = INTERNAL_NL; } %rust_ccallback
78 rust_string = rust_dq_str;
81 spaces ${ entity = RUST_SPACE; } => rust_ccallback;
84 newline ${ entity = NEWLINE; } => rust_ccallback;
85 ^space ${ entity = RUST_ANY; } => rust_ccallback;
90 action rust_ecallback {
91 callback(RUST_LANG, rust_entities[entity], cint(ts), cint(te), userdata);
94 rust_line_comment_entity = '//' (escaped_newline | nonnewline)*;
95 rust_block_comment_entity = '/*' any* :>> '*/';
96 rust_comment_entity = rust_line_comment_entity | rust_block_comment_entity;
98 rust_string_entity = dq_str_with_escapes;
100 # Up to and including "the number entity" these are almost verbatim from the
101 # "number literals" section of the Rust reference manual
102 rust_int_suffix = [iu] ('8' | '16' | '32' | '64')?;
104 rust_float_suffix_ty = 'f' ('32' | '64');
105 rust_dec_lit = [0-9_]+;
106 rust_exponent = [Ee] [\-+]? rust_dec_lit;
107 rust_float_suffix = (rust_exponent | '.' rust_dec_lit rust_exponent?)?
108 rust_float_suffix_ty?;
110 rust_num_suffix = rust_int_suffix | rust_float_suffix;
112 rust_number_entity = [1-9] [0-9_]* rust_num_suffix?
113 | '0' ( [0-9_]* rust_num_suffix?
114 | 'b' [01_]+ rust_int_suffix?
115 | 'o' [0-7_]+ rust_int_suffix?
116 | 'x' [0-9A-Fa-f_]+ rust_int_suffix?);
118 rust_identifier_entity = (alpha | '_') (alnum | '_')*;
120 rust_keyword_entity =
121 'alignof' | 'as' | 'be' | 'break' | 'const' | 'continue' | 'do' | 'else' |
122 'enum' | 'extern' | 'false' | 'fn' | 'for' | 'if' | 'impl' | 'impl' |
123 'in' | 'let' | 'let' | 'log' | 'log' | 'loop' | 'match' | 'mod' | 'mod' |
124 'mut' | 'offsetof' | 'once' | 'priv' | 'pub' | 'pure' | 'ref' | 'return' |
125 'self' | 'sizeof' | 'static' | 'struct' | 'super' | 'trait' | 'true' |
126 'type' | 'typeof' | 'unsafe' | 'use' | 'while' | 'yield';
128 rust_operator_entity = [+\-/*%<>!=^&|?~:;.,()\[\]{}@];
131 space+ ${ entity = RUST_SPACE; } => rust_ecallback;
132 rust_comment_entity ${ entity = RUST_COMMENT; } => rust_ecallback;
133 rust_string_entity ${ entity = RUST_STRING; } => rust_ecallback;
134 rust_number_entity ${ entity = RUST_NUMBER; } => rust_ecallback;
135 rust_identifier_entity ${ entity = RUST_IDENTIFIER; } => rust_ecallback;
136 rust_keyword_entity ${ entity = RUST_KEYWORD; } => rust_ecallback;
137 rust_operator_entity ${ entity = RUST_OPERATOR; } => rust_ecallback;
138 ^(space | digit) ${ entity = RUST_ANY; } => rust_ecallback;
142 /************************* Required for every parser *************************/
144 /* Parses a string buffer with C/C++ code.
146 * @param *buffer The string to parse.
147 * @param length The length of the string to parse.
148 * @param count Integer flag specifying whether or not to count lines. If yes,
149 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
150 * machine optimized for returning entity positions.
151 * @param *callback Callback function. If count is set, callback is called for
152 * every line of code, comment, or blank with 'lcode', 'lcomment', and
153 * 'lblank' respectively. Otherwise callback is called for each entity found.
155 void parse_rust(char *buffer, int length, int count,
156 void (*callback) (const char *lang, const char *entity, int s,
163 cs = (count) ? rust_en_rust_line : rust_en_rust_entity;
166 // if no newline at EOF; callback contents of last line
167 if (count) { process_last_line(RUST_LANG) }
170 const char *ORIG_RUST_LANG = LANG_RUST;
174 /*****************************************************************************/