1 /************************* Required for every parser *************************/
2 #include "ragel_parser_macros.h"
4 // the name of the language
5 const char *LUA_LANG = "lua";
7 // the languages entities
8 const char *lua_entities[] = {
9 "space", "comment", "string", "number",
10 "identifier", "operator", "newline"
13 // constants associated with the entities
15 LUA_SPACE = 0, LUA_COMMENT, LUA_STRING, LUA_NUMBER,
16 LUA_IDENTIFIER, LUA_OPERATOR, LUA_NEWLINE
19 // do not change the following variables
21 // used for newlines inside patterns like strings and comments that can have
23 #define INTERNAL_NL -1
27 char *p, *pe, *eof, *ts, *te;
29 // used for calculating offsets from buffer start for start and end positions
31 #define cint(c) ((int) (c - buffer_start))
33 // state flags for line and comment counting
34 int whole_line_comment;
35 int line_contains_code;
37 // the beginning of a line in the buffer for line and comment counting
40 // state variable for the current entity being matched
43 /*****************************************************************************/
48 include common "common.rl";
63 std_internal_newline(LUA_LANG)
70 action lua_long_ec_res { equal_count = 0; }
71 action lua_long_ec_inc { equal_count++; }
72 action lua_long_ec_dec { equal_count--; }
75 '--' @comment ('[' >lua_long_ec_res '='* $lua_long_ec_inc '[') (
76 newline %{ entity = INTERNAL_NL; } %lua_callback
80 (nonnewline - ws) @comment
81 )* :>> (']' '='* $lua_long_ec_dec ']' when { equal_count == 0 });
82 lua_line_comment = '--' @comment nonnewline*;
83 lua_comment = lua_long_comment | lua_line_comment;
86 ('[' >lua_long_ec_res '='* $lua_long_ec_inc '[') @code (
87 newline %{ entity = INTERNAL_NL; } %lua_callback
91 (nonnewline - ws) @code
92 )* :>> (']' '='* $lua_long_ec_dec ']' when { equal_count == 0 });
95 newline %{ entity = INTERNAL_NL; } %lua_callback
101 '\\' nonnewline @code
105 newline %{ entity = INTERNAL_NL; } %lua_callback
111 '\\' nonnewline @code
113 lua_string = lua_sq_str | lua_dq_str | lua_long_string;
115 lua_integer = '-'? (hex_num | dec_num);
116 lua_number = float | lua_integer;
118 lua_identifier = ([a-zA-Z] | '_') (alnum + '_')*;
120 lua_operator = '~=' | [+\-*/%^#=<>,;:,.{}\[\]()];
123 spaces ${ entity = LUA_SPACE; } => lua_callback;
124 lua_comment ${ entity = LUA_COMMENT; } => lua_callback;
125 lua_string ${ entity = LUA_STRING; } => lua_callback;
126 lua_number ${ entity = LUA_NUMBER; } => lua_callback;
127 lua_identifier ${ entity = LUA_IDENTIFIER; } => lua_callback;
128 lua_operator ${ entity = LUA_OPERATOR; } => lua_callback;
129 newline ${ entity = LUA_NEWLINE; } => lua_callback;
133 /* Parses a string buffer with Lua code.
135 * @param *buffer The string to parse.
136 * @param length The length of the string to parse.
137 * @param count Integer flag specifying whether or not to count lines. If yes,
138 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
139 * machine optimized for returning entity positions.
140 * @param *callback Callback function. If count is set, callback is called for
141 * every line of code, comment, or blank with 'lcode', 'lcomment', and
142 * 'lblank' respectively. Otherwise callback is called for each entity found.
144 void parse_lua(char *buffer, int length, int count,
145 void (*callback) (const char *lang, const char *entity, int start, int end)
148 pe = buffer + length;
151 buffer_start = buffer;
152 whole_line_comment = 0;
153 line_contains_code = 0;
161 %% write exec lua_line;
163 // if no newline at EOF; callback contents of last line
164 process_last_line(LUA_LANG)