1 // lua.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 /************************* Required for every parser *************************/
4 #include "ragel_parser_macros.h"
6 // the name of the language
7 const char *LUA_LANG = "lua";
9 // the languages entities
10 const char *lua_entities[] = {
11 "space", "comment", "string", "number", "keyword",
12 "identifier", "operator", "any"
15 // constants associated with the entities
17 LUA_SPACE = 0, LUA_COMMENT, LUA_STRING, LUA_NUMBER, LUA_KEYWORD,
18 LUA_IDENTIFIER, LUA_OPERATOR, LUA_ANY
21 // do not change the following variables
26 // used for newlines inside patterns like strings and comments that can have
28 #define INTERNAL_NL -2
32 char *p, *pe, *eof, *ts, *te;
34 // used for calculating offsets from buffer start for start and end positions
36 #define cint(c) ((int) (c - buffer_start))
38 // state flags for line and comment counting
39 int whole_line_comment;
40 int line_contains_code;
42 // the beginning of a line in the buffer for line and comment counting
45 // state variable for the current entity being matched
48 /*****************************************************************************/
53 include common "common.rl";
55 # Line counting machine
57 action lua_ccallback {
66 std_internal_newline(LUA_LANG)
73 action lua_long_ec_res { equal_count = 0; }
74 action lua_long_ec_inc { equal_count++; }
75 action lua_long_ec_dec { equal_count--; }
78 '--' @comment ('[' >lua_long_ec_res '='* $lua_long_ec_inc '[') (
79 newline %{ entity = INTERNAL_NL; } %lua_ccallback
83 (nonnewline - ws) @comment
84 )* :>> (']' '='* $lua_long_ec_dec ']' when { equal_count == 0 });
85 lua_line_comment = '--' @comment nonnewline*;
86 lua_comment = lua_long_comment | lua_line_comment;
89 ('[' >lua_long_ec_res '='* $lua_long_ec_inc '[') @code (
90 newline %{ entity = INTERNAL_NL; } %lua_ccallback
94 (nonnewline - ws) @code
95 )* :>> (']' '='* $lua_long_ec_dec ']' when { equal_count == 0 });
98 newline %{ entity = INTERNAL_NL; } %lua_ccallback
104 '\\' nonnewline @code
108 newline %{ entity = INTERNAL_NL; } %lua_ccallback
114 '\\' nonnewline @code
116 lua_string = lua_sq_str | lua_dq_str | lua_long_string;
119 spaces ${ entity = LUA_SPACE; } => lua_ccallback;
122 newline ${ entity = NEWLINE; } => lua_ccallback;
123 ^space ${ entity = LUA_ANY; } => lua_ccallback;
128 action lua_ecallback {
129 callback(LUA_LANG, entity, cint(ts), cint(te));
132 lua_entity := 'TODO:';
135 /* Parses a string buffer with Lua code.
137 * @param *buffer The string to parse.
138 * @param length The length of the string to parse.
139 * @param count Integer flag specifying whether or not to count lines. If yes,
140 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
141 * machine optimized for returning entity positions.
142 * @param *callback Callback function. If count is set, callback is called for
143 * every line of code, comment, or blank with 'lcode', 'lcomment', and
144 * 'lblank' respectively. Otherwise callback is called for each entity found.
146 void parse_lua(char *buffer, int length, int count,
147 void (*callback) (const char *lang, const char *entity, int start, int end)
150 pe = buffer + length;
153 buffer_start = buffer;
154 whole_line_comment = 0;
155 line_contains_code = 0;
162 cs = (count) ? lua_en_lua_line : lua_en_lua_entity;
165 // if no newline at EOF; callback contents of last line
166 if (count) { process_last_line(LUA_LANG) }