1 // lua.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_LUA_PARSER_H
5 #define OHCOUNT_LUA_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *LUA_LANG = LANG_LUA;
12 // the languages entities
13 const char *lua_entities[] = {
14 "space", "comment", "string", "number", "keyword",
15 "identifier", "operator", "any"
18 // constants associated with the entities
20 LUA_SPACE = 0, LUA_COMMENT, LUA_STRING, LUA_NUMBER, LUA_KEYWORD,
21 LUA_IDENTIFIER, LUA_OPERATOR, LUA_ANY
24 /*****************************************************************************/
29 include common "common.rl";
31 # Line counting machine
33 action lua_ccallback {
42 std_internal_newline(LUA_LANG)
49 action lua_long_ec_res { equal_count = 0; }
50 action lua_long_ec_inc { equal_count++; }
51 action lua_long_ec_dec { equal_count--; }
54 '--' ('[' >lua_long_ec_res '='* $lua_long_ec_inc '[') @enqueue @comment (
55 newline %{ entity = INTERNAL_NL; } %lua_ccallback
59 (nonnewline - ws) @comment
60 )* :>> (']' '='* $lua_long_ec_dec ']' when { equal_count == 0 }) @commit;
61 lua_line_comment = '--' @comment nonnewline*;
62 lua_comment = lua_long_comment | lua_line_comment;
65 ('[' >lua_long_ec_res '='* $lua_long_ec_inc '[') @enqueue @code (
66 newline %{ entity = INTERNAL_NL; } %lua_ccallback
70 (nonnewline - ws) @code
71 )* :>> (']' '='* $lua_long_ec_dec ']' when { equal_count == 0 }) @commit;
74 newline %{ entity = INTERNAL_NL; } %lua_ccallback
84 newline %{ entity = INTERNAL_NL; } %lua_ccallback
92 lua_string = lua_sq_str | lua_dq_str | lua_long_string;
95 spaces ${ entity = LUA_SPACE; } => lua_ccallback;
98 newline ${ entity = NEWLINE; } => lua_ccallback;
99 ^space ${ entity = LUA_ANY; } => lua_ccallback;
104 action lua_ecallback {
105 callback(LUA_LANG, lua_entities[entity], cint(ts), cint(te), userdata);
108 lua_block_comment_entity =
109 '--[' >lua_long_ec_res '='* $lua_long_ec_inc '[' any*
110 :>> (']' '='* $lua_long_ec_dec ']' when { equal_count == 0 });
111 lua_line_comment_entity = '--' (nonnewline)*;
112 lua_comment_entity = lua_block_comment_entity | lua_line_comment_entity;
114 lua_string_entity = sq_str_with_escapes | dq_str_with_escapes;
116 lua_integer = '-'? (dec_num | hex_num);
117 lua_number_entity = float | lua_integer;
120 'and' | 'break' | 'do' | 'else' | 'elseif' | 'end' | 'false' | 'for' |
121 'function' | 'if' | 'in' | 'local' | 'nil' | 'not' | 'or' | 'repeat' |
122 'return' | 'then' | 'true' | 'until' | 'while';
124 lua_identifier_entity = (alpha | '_') alnum*;
126 lua_operator_entity = '~=' | [+\-*/%^#=<>;:,.{}\[\]()];
129 space+ ${ entity = LUA_SPACE; } => lua_ecallback;
130 lua_comment_entity ${ entity = LUA_COMMENT; } => lua_ecallback;
131 lua_string_entity ${ entity = LUA_STRING; } => lua_ecallback;
132 lua_number_entity ${ entity = LUA_NUMBER; } => lua_ecallback;
133 lua_identifier_entity ${ entity = LUA_IDENTIFIER; } => lua_ecallback;
134 lua_keyword_entity ${ entity = LUA_KEYWORD; } => lua_ecallback;
135 lua_operator_entity ${ entity = LUA_OPERATOR; } => lua_ecallback;
136 ^space ${ entity = LUA_ANY; } => lua_ecallback;
140 /************************* Required for every parser *************************/
142 /* Parses a string buffer with Lua code.
144 * @param *buffer The string to parse.
145 * @param length The length of the string to parse.
146 * @param count Integer flag specifying whether or not to count lines. If yes,
147 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
148 * machine optimized for returning entity positions.
149 * @param *callback Callback function. If count is set, callback is called for
150 * every line of code, comment, or blank with 'lcode', 'lcomment', and
151 * 'lblank' respectively. Otherwise callback is called for each entity found.
153 void parse_lua(char *buffer, int length, int count,
154 void (*callback) (const char *lang, const char *entity, int s,
163 cs = (count) ? lua_en_lua_line : lua_en_lua_entity;
166 // if no newline at EOF; callback contents of last line
167 if (count) { process_last_line(LUA_LANG) }
172 /*****************************************************************************/